svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / test / java / org / gvsig / fmap / dal / resource / spi / AbstractResourceTest.java @ 40435
History | View | Annotate | Download (3.65 KB)
1 | 40435 | jjdelcerro | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | */
|
||
22 | |||
23 | /*
|
||
24 | * AUTHORS (In addition to CIT):
|
||
25 | * 2009 {} {{Task}}
|
||
26 | */
|
||
27 | package org.gvsig.fmap.dal.resource.spi; |
||
28 | |||
29 | import junit.framework.TestCase; |
||
30 | |||
31 | import org.easymock.MockControl; |
||
32 | import org.gvsig.fmap.dal.resource.ResourceAction; |
||
33 | import org.gvsig.fmap.dal.resource.ResourceParameters; |
||
34 | import org.gvsig.fmap.dal.resource.exception.ResourceNotifyOpenException; |
||
35 | |||
36 | /**
|
||
37 | * Unit tests for the class {@link AbstractResource}
|
||
38 | *
|
||
39 | * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
||
40 | */
|
||
41 | public class AbstractResourceTest extends TestCase { |
||
42 | |||
43 | private AbstractResource resource;
|
||
44 | private MockControl paramsControl;
|
||
45 | private ResourceParameters params;
|
||
46 | |||
47 | protected void setUp() throws Exception { |
||
48 | super.setUp();
|
||
49 | paramsControl = MockControl.createNiceControl(ResourceParameters.class); |
||
50 | params = (ResourceParameters) paramsControl.getMock(); |
||
51 | resource = new TestResource(params);
|
||
52 | } |
||
53 | |||
54 | /**
|
||
55 | * Test method for {@link org.gvsig.fmap.dal.resource.spi.AbstractResource#getLastTimeOpen()}.
|
||
56 | */
|
||
57 | public void testGetLastTimeOpen() throws ResourceNotifyOpenException { |
||
58 | long time = resource.getLastTimeOpen();
|
||
59 | resource.notifyOpen(); |
||
60 | assertTrue("The resource last time open hasn't been updated",
|
||
61 | resource.getLastTimeOpen() > time); |
||
62 | } |
||
63 | |||
64 | /**
|
||
65 | * Test method for {@link org.gvsig.fmap.dal.resource.spi.AbstractResource#getLastTimeUsed()}.
|
||
66 | */
|
||
67 | public void testGetLastTimeUsed() throws Exception { |
||
68 | long time = resource.getLastTimeUsed();
|
||
69 | resource.execute(new ResourceAction() {
|
||
70 | public Object run() throws Exception { |
||
71 | try {
|
||
72 | // Wait for 100 milliseconds
|
||
73 | Thread.sleep(100); |
||
74 | } catch (InterruptedException e) { |
||
75 | // No problem
|
||
76 | } |
||
77 | return null; |
||
78 | } |
||
79 | }); |
||
80 | assertTrue("The resource last time used hasn't been updated",
|
||
81 | resource.getLastTimeUsed() > time); |
||
82 | } |
||
83 | |||
84 | /**
|
||
85 | * Test method for {@link org.gvsig.fmap.dal.resource.spi.AbstractResource#openCount()}.
|
||
86 | */
|
||
87 | public void testOpenCount() throws Exception { |
||
88 | assertEquals(0, resource.openCount());
|
||
89 | resource.notifyOpen(); |
||
90 | assertEquals(1, resource.openCount());
|
||
91 | resource.notifyOpen(); |
||
92 | resource.notifyOpen(); |
||
93 | assertEquals(3, resource.openCount());
|
||
94 | resource.notifyClose(); |
||
95 | assertEquals(2, resource.openCount());
|
||
96 | } |
||
97 | |||
98 | /**
|
||
99 | * Test method for
|
||
100 | * {@link org.gvsig.fmap.dal.resource.spi.AbstractResource#execute(java.lang.Runnable)}
|
||
101 | * .
|
||
102 | */
|
||
103 | public void testExecute() throws Exception { |
||
104 | final MutableBoolean testValue = new MutableBoolean(); |
||
105 | resource.execute(new ResourceAction() {
|
||
106 | public Object run() throws Exception { |
||
107 | testValue.setValue(true);
|
||
108 | return null; |
||
109 | } |
||
110 | }); |
||
111 | assertTrue("Runnable execution not performed", testValue.isValue());
|
||
112 | } |
||
113 | |||
114 | public class MutableBoolean { |
||
115 | private boolean value = false; |
||
116 | |||
117 | public void setValue(boolean value) { |
||
118 | this.value = value;
|
||
119 | } |
||
120 | |||
121 | public boolean isValue() { |
||
122 | return value;
|
||
123 | } |
||
124 | } |
||
125 | } |