gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / test / java / org / gvsig / tools / library / AbstractLibrariesInitializerTest.java @ 661
History | View | Annotate | Download (9.93 KB)
1 |
/* 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 |
package org.gvsig.tools.library; |
23 |
|
24 |
import java.util.LinkedHashSet; |
25 |
import java.util.List; |
26 |
import java.util.Set; |
27 |
|
28 |
import junit.framework.TestCase; |
29 |
|
30 |
/**
|
31 |
* Tests for the {@link AbstractLibrariesInitializer} class.
|
32 |
*
|
33 |
* @author gvSIG Team
|
34 |
* @version $Id$
|
35 |
*/
|
36 |
public class AbstractLibrariesInitializerTest extends TestCase { |
37 |
|
38 |
private Library lib1api = new Lib1API(); |
39 |
private Library lib1impl = new Lib1Impl(); |
40 |
private Library lib1prov1 = new Lib1Prov1(); |
41 |
private Library lib1prov2 = new Lib1Prov2(); |
42 |
private Library lib2api = new Lib2API(); |
43 |
private Library lib2impl = new Lib2Impl(); |
44 |
private Library lib2prov1 = new Lib2Prov1(); |
45 |
private Library lib3api = new Lib3API(); |
46 |
private Library lib3impl = new Lib3Impl(); |
47 |
private Library lib4api = new Lib4API(); |
48 |
private Library lib4impl1 = new Lib4Impl1(); |
49 |
private Library lib4impl2 = new Lib4Impl2(); |
50 |
|
51 |
protected void setUp() throws Exception { |
52 |
super.setUp();
|
53 |
} |
54 |
|
55 |
public void testInitialize() { |
56 |
// Test with 3 different libs orders just in case
|
57 |
testInitializer(new TestLibrariesInitializer1());
|
58 |
testInitializer(new TestLibrariesInitializer2());
|
59 |
testInitializer(new TestLibrariesInitializer3());
|
60 |
} |
61 |
|
62 |
private void testInitializer(AbstractLibrariesInitializer init) { |
63 |
// This is an ordered set
|
64 |
List libs = init.getLibraries();
|
65 |
|
66 |
checkOrder(libs); |
67 |
} |
68 |
|
69 |
private void checkOrder(List libs) { |
70 |
int ilib1api = libs.indexOf(lib1api);
|
71 |
int ilib1impl = libs.indexOf(lib1impl);
|
72 |
int ilib1prov1 = libs.indexOf(lib1prov1);
|
73 |
int ilib1prov2 = libs.indexOf(lib1prov2);
|
74 |
int ilib2api = libs.indexOf(lib2api);
|
75 |
int ilib2impl = libs.indexOf(lib2impl);
|
76 |
int ilib2prov1 = libs.indexOf(lib2prov1);
|
77 |
int ilib3api = libs.indexOf(lib3api);
|
78 |
int ilib3impl = libs.indexOf(lib3impl);
|
79 |
int ilib4api = libs.indexOf(lib4api);
|
80 |
int ilib4impl1 = libs.indexOf(lib4impl1);
|
81 |
int ilib4impl2 = libs.indexOf(lib4impl2);
|
82 |
|
83 |
// Check all needed libraries have been included into the list
|
84 |
assertTrue("Lib1API is not included in the list", ilib1api > -1); |
85 |
assertTrue("Lib1Impl is not included in the list", ilib1impl > -1); |
86 |
assertTrue("Lib1Prov1 is not included in the list", ilib1prov1 > -1); |
87 |
assertTrue("Lib1Prov2 is not included in the list", ilib1prov2 > -1); |
88 |
assertTrue("Lib2API is not included in the list", ilib2api > -1); |
89 |
assertTrue("Lib2Impl is not included in the list", ilib2impl > -1); |
90 |
assertTrue("Lib2Prov1 is not included in the list", ilib2prov1 > -1); |
91 |
assertTrue("Lib3API is not included in the list", ilib3api > -1); |
92 |
assertTrue("Lib3Impl is not included in the list", ilib3impl > -1); |
93 |
assertTrue("Lib4API is not included in the list", ilib4api > -1); |
94 |
|
95 |
// Check implementation priorities
|
96 |
assertEquals( |
97 |
"Lib4Impl1 has less priority than Lib4Impl2, it must not be initialized",
|
98 |
ilib4impl1, -1);
|
99 |
assertTrue( |
100 |
"Lib4Impl2 has more priority than Lib4Impl1, it must have been initialized",
|
101 |
ilib4impl2 > -1);
|
102 |
|
103 |
// Check api-impl-service(provider) relationships:
|
104 |
// first API, next Impl and then services (providers)
|
105 |
assertTrue("Lib1API must be initialized before Lib1Impl",
|
106 |
ilib1api < ilib1impl); |
107 |
assertTrue("Lib1Impl must be initialized before Lib1Prov1",
|
108 |
ilib1impl < ilib1prov1); |
109 |
assertTrue("Lib1Impl must be initialized before Lib1Prov2",
|
110 |
ilib1impl < ilib1prov2); |
111 |
|
112 |
assertTrue("Lib2API must be initialized before Lib2Impl",
|
113 |
ilib2api < ilib2impl); |
114 |
assertTrue("Lib2Impl must be initialized before Lib2Prov1",
|
115 |
ilib2impl < ilib2prov1); |
116 |
|
117 |
assertTrue("Lib3API must be initialized before Lib3Impl",
|
118 |
ilib3api < ilib3impl); |
119 |
|
120 |
assertTrue("Lib4API must be initialized before Lib4Impl",
|
121 |
ilib4api < ilib4impl2); |
122 |
|
123 |
// Check dependencies between libraries
|
124 |
assertTrue( |
125 |
"Lib2Impl dependends on Lib1API, must be initialized after it",
|
126 |
ilib1api < ilib2impl); |
127 |
assertTrue( |
128 |
"Lib2Impl dependends on Lib1API, must be initialized after Lib1Impl",
|
129 |
ilib1impl < ilib2impl); |
130 |
assertTrue( |
131 |
"Lib2Impl dependends on Lib1API, must be initialized after Lib1Prov1",
|
132 |
ilib1prov1 < ilib2impl); |
133 |
assertTrue( |
134 |
"Lib2Impl dependends on Lib1API, must be initialized after Lib1Prov2",
|
135 |
ilib1prov2 < ilib2impl); |
136 |
|
137 |
assertTrue( |
138 |
"Lib2Prov1 dependends on Lib3API, must be initialized afterwards",
|
139 |
ilib3api < ilib2prov1); |
140 |
assertTrue( |
141 |
"Lib2Prov1 dependends on Lib3Impl, must be initialized after it",
|
142 |
ilib3impl < ilib2prov1); |
143 |
|
144 |
assertTrue( |
145 |
"Lib4API dependends on Lib2Prov1, must be initialized after it",
|
146 |
ilib4api > ilib2prov1); |
147 |
} |
148 |
|
149 |
private class TestLibrariesInitializer1 extends |
150 |
AbstractLibrariesInitializer { |
151 |
|
152 |
protected Set findLibraries(Class libraryClass, ClassLoader classLoader) { |
153 |
Set libs = new LinkedHashSet(); |
154 |
|
155 |
libs.add(lib4impl1); |
156 |
libs.add(lib2api); |
157 |
libs.add(lib2impl); |
158 |
libs.add(lib2prov1); |
159 |
libs.add(lib1api); |
160 |
libs.add(lib1impl); |
161 |
libs.add(lib4impl2); |
162 |
libs.add(lib1prov1); |
163 |
libs.add(lib1prov2); |
164 |
libs.add(lib3api); |
165 |
libs.add(lib3impl); |
166 |
libs.add(lib4api); |
167 |
|
168 |
return libs;
|
169 |
} |
170 |
|
171 |
} |
172 |
|
173 |
private class TestLibrariesInitializer2 extends |
174 |
AbstractLibrariesInitializer { |
175 |
|
176 |
protected Set findLibraries(Class libraryClass, ClassLoader classLoader) { |
177 |
Set libs = new LinkedHashSet(); |
178 |
|
179 |
libs.add(lib2api); |
180 |
libs.add(lib4impl1); |
181 |
libs.add(lib2prov1); |
182 |
libs.add(lib1impl); |
183 |
libs.add(lib4impl2); |
184 |
libs.add(lib3impl); |
185 |
libs.add(lib2impl); |
186 |
libs.add(lib1api); |
187 |
libs.add(lib1prov2); |
188 |
libs.add(lib3api); |
189 |
libs.add(lib1prov1); |
190 |
libs.add(lib4api); |
191 |
|
192 |
return libs;
|
193 |
} |
194 |
|
195 |
} |
196 |
|
197 |
private class TestLibrariesInitializer3 extends |
198 |
AbstractLibrariesInitializer { |
199 |
|
200 |
protected Set findLibraries(Class libraryClass, ClassLoader classLoader) { |
201 |
Set libs = new LinkedHashSet(); |
202 |
|
203 |
libs.add(lib4api); |
204 |
libs.add(lib2prov1); |
205 |
libs.add(lib2api); |
206 |
libs.add(lib4impl2); |
207 |
libs.add(lib3api); |
208 |
libs.add(lib1api); |
209 |
libs.add(lib4impl1); |
210 |
libs.add(lib2impl); |
211 |
libs.add(lib1prov2); |
212 |
libs.add(lib3impl); |
213 |
libs.add(lib1impl); |
214 |
libs.add(lib1prov1); |
215 |
|
216 |
return libs;
|
217 |
} |
218 |
|
219 |
} |
220 |
|
221 |
private class DummyLibrary extends AbstractLibrary { |
222 |
|
223 |
protected void doInitialize() throws LibraryException { |
224 |
// Nothing to do
|
225 |
} |
226 |
|
227 |
protected void doPostInitialize() throws LibraryException { |
228 |
// Nothing to do
|
229 |
} |
230 |
} |
231 |
|
232 |
private class Lib1API extends DummyLibrary { |
233 |
|
234 |
public void doRegistration() { |
235 |
registerAsAPI(Lib1API.class); |
236 |
} |
237 |
} |
238 |
|
239 |
private class Lib1Impl extends DummyLibrary { |
240 |
|
241 |
public void doRegistration() { |
242 |
registerAsImplementationOf(Lib1API.class); |
243 |
} |
244 |
} |
245 |
|
246 |
private class Lib1Prov1 extends DummyLibrary { |
247 |
|
248 |
public void doRegistration() { |
249 |
registerAsServiceOf(Lib1API.class); |
250 |
} |
251 |
} |
252 |
|
253 |
private class Lib1Prov2 extends DummyLibrary { |
254 |
|
255 |
public void doRegistration() { |
256 |
registerAsServiceOf(Lib1API.class); |
257 |
} |
258 |
} |
259 |
|
260 |
private class Lib2API extends DummyLibrary { |
261 |
|
262 |
public void doRegistration() { |
263 |
registerAsAPI(Lib2API.class); |
264 |
} |
265 |
} |
266 |
|
267 |
private class Lib2Impl extends DummyLibrary { |
268 |
|
269 |
public void doRegistration() { |
270 |
registerAsImplementationOf(Lib2API.class); |
271 |
require(Lib1API.class); |
272 |
} |
273 |
} |
274 |
|
275 |
private class Lib2Prov1 extends DummyLibrary { |
276 |
|
277 |
public void doRegistration() { |
278 |
registerAsServiceOf(Lib2API.class); |
279 |
require(Lib3API.class); |
280 |
} |
281 |
} |
282 |
|
283 |
private class Lib3API extends DummyLibrary { |
284 |
|
285 |
public void doRegistration() { |
286 |
registerAsAPI(Lib3API.class); |
287 |
} |
288 |
} |
289 |
|
290 |
private class Lib3Impl extends DummyLibrary { |
291 |
|
292 |
public void doRegistration() { |
293 |
registerAsImplementationOf(Lib3API.class); |
294 |
} |
295 |
} |
296 |
|
297 |
private class Lib4API extends DummyLibrary { |
298 |
|
299 |
public void doRegistration() { |
300 |
registerAsAPI(Lib4API.class); |
301 |
require(Lib2Prov1.class); |
302 |
} |
303 |
} |
304 |
|
305 |
private class Lib4Impl1 extends DummyLibrary { |
306 |
|
307 |
public void doRegistration() { |
308 |
registerAsImplementationOf(Lib4API.class); |
309 |
} |
310 |
} |
311 |
|
312 |
private class Lib4Impl2 extends DummyLibrary { |
313 |
|
314 |
public void doRegistration() { |
315 |
registerAsImplementationOf(Lib4API.class, 10);
|
316 |
} |
317 |
} |
318 |
} |