KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > management > systemstate > StateStorageTest


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: StateStorageTest.java 15:35:28 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.management.systemstate;
23
24 import java.io.File JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.commons.io.FileUtils;
28
29 import junit.framework.TestCase;
30
31 /**
32  * Class used to test the StateStorage class
33  *
34  * @author ddesjardins - eBMWebsourcing
35  * @author alouis - eBMWebsourcing
36  */

37 public class StateStorageTest extends TestCase {
38
39     /**
40      * Base directory of the project
41      */

42     private String JavaDoc baseDir;
43
44     private StateStorage stateStorage;
45
46     private ComponentState c = new ComponentState("c", "iURL", "zURL", "iS",
47             "lS");;
48
49     private ServiceAssemblyState sa = new ServiceAssemblyState("sa", "iURL",
50             "zURL", "lS");;
51
52     private SharedLibraryState sl = new SharedLibraryState("sl", "iURL", "zURL");
53
54     private File JavaDoc stateFile;
55
56     /**
57      * Defines the path used to store the states
58      */

59     protected void setUp() throws Exception JavaDoc {
60         super.setUp();
61         baseDir = this.getClass().getResource(".").toString();
62         baseDir = baseDir.substring(0, baseDir.indexOf("target"));
63         baseDir = baseDir.substring(baseDir.indexOf(":") + 1);
64
65         System.setProperty("petals.home", new File JavaDoc(baseDir + "target")
66                 .getAbsolutePath());
67         new File JavaDoc(baseDir + "target" + File.separator + "lib").mkdir();
68         new File JavaDoc(baseDir + "target" + File.separator + "lib" + File.separator
69                 + "petals-1.0-SNAPSHOT.jar").mkdir();
70         new File JavaDoc(baseDir + "target" + File.separator + "conf").mkdir();
71
72         stateFile = new File JavaDoc(baseDir + "target" + File.separator
73                 + "systemstate.xml");
74         stateFile.createNewFile();
75         File JavaDoc xsdFile = new File JavaDoc(baseDir.replace("container", "assembly")
76                 + "src" + File.separator + StateStorage.SCHEMA_FILE_PATH);
77
78         stateStorage = new StateStorage();
79         stateStorage.file = stateFile;
80         stateStorage.schemaFile = xsdFile;
81     }
82
83     public void tearDown() {
84         System.clearProperty("petals.home");
85         stateFile.delete();
86         stateFile = null;
87     }
88
89     /*
90      * Test method for
91      * 'org.objectweb.petals.jbi.management.systemstate.StateStorage.init()'
92      */

93     public void testInit() {
94         // init must failed if file is null
95
stateStorage.file = null;
96
97         boolean exception = true;
98         try {
99             stateStorage.init();
100             exception = false;
101         } catch (Exception JavaDoc e) {
102             // nothing
103
}
104         assertTrue(exception);
105     }
106
107     /*
108      * Test method for
109      * 'org.objectweb.petals.jbi.management.systemstate.StateStorage.init()'
110      */

111     public void testInit2() {
112         // init must failed if file is incorrect - wrong element
113
stateStorage.file = new File JavaDoc(baseDir + File.separator + "target"
114                 + File.separator + "test-classes" + File.separator
115                 + "systemstate" + File.separator + "wrongSystemState.xml");
116         assert (stateStorage.file.exists());
117
118         boolean exception = true;
119         try {
120             stateStorage.init();
121             exception = false;
122         } catch (Exception JavaDoc e) {
123             // nothing
124
}
125         assertTrue(exception);
126
127         stateStorage.file = new File JavaDoc(baseDir + File.separator + "target"
128                 + File.separator + "test-classes" + File.separator
129                 + "systemstate" + File.separator + "wrongSystemState.xml");
130         assert (stateStorage.file.exists());
131     }
132
133     /*
134      * Test method for
135      * 'org.objectweb.petals.jbi.management.systemstate.StateStorage.init()'
136      */

137     public void testInit3() {
138         // init must failed if file is incorrect - wrong attribute
139
stateStorage.file = new File JavaDoc(baseDir + File.separator + "target"
140                 + File.separator + "test-classes" + File.separator
141                 + "systemstate" + File.separator + "wrongSystemState.xml");
142         assert (stateStorage.file.exists());
143
144         boolean exception = true;
145         try {
146             stateStorage.init();
147             exception = false;
148         } catch (Exception JavaDoc e) {
149             // nothing
150
}
151         assertTrue(exception);
152     }
153
154     /*
155      * Test method for
156      * 'org.objectweb.petals.jbi.management.systemstate.StateStorage.init()'
157      */

158     public void testInit4() throws Exception JavaDoc {
159         // init must success if file is correct
160
stateStorage.file = new File JavaDoc(baseDir + "src" + File.separator
161                 + "test-data" + File.separator + "systemstate" + File.separator
162                 + "goodSystemState.xml");
163         assertTrue(stateStorage.file.exists());
164         stateStorage.init();
165     }
166
167     /*
168      * Test method for
169      * 'org.objectweb.petals.jbi.management.systemstate.StateStorage.createComponentStateHolder(String,
170      * String, String, String, String)'
171      */

172     public void testCreateAndRetreiveComponent() throws Exception JavaDoc {
173         stateStorage.init();
174
175         ComponentState cc = stateStorage.createComponentStateHolder(
176                 c.getName(), c.getInstallURL(), c.getArchiveURL(), c
177                         .getInstallState(), c.getLifecycleState());
178
179         // test the result of the call
180
assertTrue(c.equals(cc));
181
182         // test that the element was saved in the file
183
// by reloading the systemState file.
184
stateStorage.document = null;
185         stateStorage.file = stateFile;
186
187         stateStorage.init();
188         List JavaDoc<ComponentState> l = stateStorage.retrieveAllComponentStates();
189         assertTrue(l != null);
190         assertTrue(l.size() == 1);
191         assertTrue(c.equals(l.get(0)));
192     }
193
194     /*
195      * Test method for
196      * 'org.objectweb.petals.jbi.management.systemstate.StateStorage.createServiceAssemblyStateHolder(String,
197      * String, String, String)'
198      */

199     public void testCreateAndRetreiveServiceAssembly() throws Exception JavaDoc {
200         stateStorage.init();
201
202         ServiceAssemblyState cc = stateStorage
203                 .createServiceAssemblyStateHolder(sa.getName(), sa
204                         .getInstallURL(), sa.getArchiveURL(), sa
205                         .getLifecycleState());
206
207         // test the result of the call
208
assertTrue(sa.equals(cc));
209
210         // test that the element was saved in the file
211
// by reloading the systemState file.
212
// Thread.sleep(1000);
213
stateStorage.document = null;
214         stateStorage.file = stateFile;
215
216         stateStorage.loadStateStorageDocument();
217         List JavaDoc<ServiceAssemblyState> l = stateStorage
218                 .retrieveAllServiceAssemblyStates();
219         assertTrue(l != null);
220         assertTrue(l.size() == 1);
221         assertTrue(sa.equals(l.get(0)));
222     }
223
224     /*
225      * Test method for
226      * 'org.objectweb.petals.jbi.management.systemstate.StateStorage.createSharedLibraryStateHolder(String,
227      * String, String)'
228      */

229     public void testCreateAndRetreiveSharedLibrary() throws Exception JavaDoc {
230         stateStorage.init();
231
232         SharedLibraryState cc = stateStorage.createSharedLibraryStateHolder(sl
233                 .getName(), sl.getInstallURL(), sl.getArchiveURL());
234
235         // test the result of the call
236
assertTrue(sl.equals(cc));
237
238         // test that the element was saved in the file
239
// by reloading the systemState file.
240
// Thread.sleep(1000);
241
stateStorage.document = null;
242         stateStorage.file = stateFile;
243
244         stateStorage.init();
245         List JavaDoc<SharedLibraryState> l = stateStorage
246                 .retrieveAllSharedLibraryStates();
247         assertTrue(l != null);
248         assertTrue(l.size() == 1);
249         assertTrue(sl.equals(l.get(0)));
250     }
251
252     /*
253      * Test method for
254      * 'org.objectweb.petals.jbi.management.systemstate.StateStorage.updateComponentInstallationState(String,
255      * String)'
256      */

257     public void testUpdateComponentInstallationState() throws Exception JavaDoc {
258         new File JavaDoc(baseDir + "target" + File.separator
259                 + "updatecompoinst.xml").delete();
260         stateStorage.file = new File JavaDoc(baseDir + "target" + File.separator
261                 + "updatecompoinst.xml");
262         
263         stateStorage.init();
264
265         stateStorage.createComponentStateHolder(c.getName(), c.getInstallURL(),
266                 c.getArchiveURL(), c.getInstallState(), c.getLifecycleState());
267
268         stateStorage.updateComponentInstallationState(c.getName(),
269                 "uninstalled");
270
271         // test that the state is modified
272
String JavaDoc xmlRef = FileUtils.readFileToString(new File JavaDoc(baseDir + "src"
273                 + File.separator + "test-data" + File.separator + "systemstate"
274                 + File.separator + "updatecompoinst.xml"), "UTF-8");
275         String JavaDoc xmlComp = FileUtils.readFileToString(new File JavaDoc(baseDir + "target"
276                 + File.separator + "updatecompoinst.xml"), "UTF-8");
277         assertEquals(xmlComp, xmlRef);
278     }
279
280     /*
281      * Test method for
282      * 'org.objectweb.petals.jbi.management.systemstate.StateStorage.updateComponentLifeCycleState(String,
283      * String)'
284      */

285     public void testUpdateComponentLifeCycleState() throws Exception JavaDoc {
286         new File JavaDoc(baseDir + "target" + File.separator
287                 + "updatecompolife.xml").delete();
288         stateStorage.file = new File JavaDoc(baseDir + "target" + File.separator
289                 + "updatecompolife.xml");
290         
291         stateStorage.init();
292
293         stateStorage.createComponentStateHolder(c.getName(), c.getInstallURL(),
294                 c.getArchiveURL(), c.getInstallState(), c.getLifecycleState());
295
296         stateStorage.updateComponentLifeCycleState(c.getName(), "stopped");
297
298         // test that the state is modified
299
String JavaDoc xmlRef = FileUtils.readFileToString(new File JavaDoc(baseDir + "src"
300                 + File.separator + "test-data" + File.separator + "systemstate"
301                 + File.separator + "updatecompolife.xml"), "UTF-8");
302         String JavaDoc xmlComp = FileUtils.readFileToString(new File JavaDoc(baseDir + "target"
303                 + File.separator + "updatecompolife.xml"), "UTF-8");
304         assertEquals(xmlComp, xmlRef);
305     }
306
307     /*
308      * Test method for
309      * 'org.objectweb.petals.jbi.management.systemstate.StateStorage.updateServiceAssemblyState(String,
310      * String)'
311      */

312     public void testUpdateServiceAssemblyState() throws Exception JavaDoc {
313         new File JavaDoc(baseDir + "target" + File.separator
314                 + "updatesa.xml").delete();
315         stateStorage.file = new File JavaDoc(baseDir + "target" + File.separator
316                 + "updatesa.xml");
317         
318         stateStorage.init();
319
320         stateStorage.createServiceAssemblyStateHolder(c.getName(), c
321                 .getInstallURL(), c.getArchiveURL(), c.getLifecycleState());
322
323         stateStorage.updateServiceAssemblyState(c.getName(), "stopped");
324
325         // test that the state is modified
326
String JavaDoc xmlRef = FileUtils.readFileToString(new File JavaDoc(baseDir + "src"
327                 + File.separator + "test-data" + File.separator + "systemstate"
328                 + File.separator + "updatesa.xml"), "UTF-8");
329         String JavaDoc xmlComp = FileUtils.readFileToString(new File JavaDoc(baseDir + "target"
330                 + File.separator + "updatesa.xml"), "UTF-8");
331         assertEquals(xmlComp, xmlRef);
332     }
333
334     /*
335      * Test method for
336      * 'org.objectweb.petals.jbi.management.systemstate.StateStorage.deleteComponentStateHolder(String)'
337      */

338     public void testDeleteComponentStateHolder() throws Exception JavaDoc {
339         new File JavaDoc(baseDir + "target" + File.separator
340                 + "deletecompo.xml").delete();
341         stateStorage.file = new File JavaDoc(baseDir + "target" + File.separator
342                 + "deletecompo.xml");
343         
344         stateStorage.init();
345
346         stateStorage.createComponentStateHolder(c.getName(), c.getInstallURL(),
347                 c.getArchiveURL(), c.getInstallState(), c.getLifecycleState());
348
349         stateStorage.deleteComponentStateHolder(c.getName());
350
351         // test that the state is modified
352
String JavaDoc xmlRef = FileUtils.readFileToString(new File JavaDoc(baseDir + "src"
353                 + File.separator + "test-data" + File.separator + "systemstate"
354                 + File.separator + "deletecompo.xml"), "UTF-8");
355         String JavaDoc xmlComp = FileUtils.readFileToString(new File JavaDoc(baseDir + "target"
356                 + File.separator + "deletecompo.xml"), "UTF-8");
357         assertEquals(xmlComp, xmlRef);
358     }
359
360     /*
361      * Test method for
362      * 'org.objectweb.petals.jbi.management.systemstate.StateStorage.deleteServiceAssemblyStateHolder(String)'
363      */

364     public void testDeleteServiceAssemblyStateHolder() throws Exception JavaDoc {
365         new File JavaDoc(baseDir + "target" + File.separator
366                 + "deletesa.xml").delete();
367         stateStorage.file = new File JavaDoc(baseDir + "target" + File.separator
368                 + "deletesa.xml");
369         
370         stateStorage.init();
371
372         stateStorage.createServiceAssemblyStateHolder(c.getName(), c
373                 .getInstallURL(), c.getArchiveURL(), c.getLifecycleState());
374
375         stateStorage.deleteServiceAssemblyStateHolder(c.getName());
376
377         // test that the state is modified
378
String JavaDoc xmlRef = FileUtils.readFileToString(new File JavaDoc(baseDir + "src"
379                 + File.separator + "test-data" + File.separator + "systemstate"
380                 + File.separator + "deletesa.xml"), "UTF-8");
381         String JavaDoc xmlComp = FileUtils.readFileToString(new File JavaDoc(baseDir + "target"
382                 + File.separator + "deletesa.xml"), "UTF-8");
383         assertEquals(xmlComp, xmlRef);
384     }
385
386     /*
387      * Test method for
388      * 'org.objectweb.petals.jbi.management.systemstate.StateStorage.deleteSharedLibraryStateHolder(String)'
389      */

390     public void testDeleteSharedLibraryStateHolder() throws Exception JavaDoc {
391         new File JavaDoc(baseDir + "target" + File.separator
392                 + "deletesl.xml").delete();
393         stateStorage.file = new File JavaDoc(baseDir + "target" + File.separator
394                 + "deletesl.xml");
395         
396         stateStorage.init();
397
398         stateStorage.createSharedLibraryStateHolder(c.getName(), c
399                 .getInstallURL(), c.getArchiveURL());
400
401         stateStorage.deleteSharedLibraryStateHolder(c.getName());
402
403         // test that the state is deleted
404
String JavaDoc xmlRef = FileUtils.readFileToString(new File JavaDoc(baseDir + "src"
405                 + File.separator + "test-data" + File.separator + "systemstate"
406                 + File.separator + "deletesl.xml"), "UTF-8");
407         String JavaDoc xmlComp = FileUtils.readFileToString(new File JavaDoc(baseDir + "target"
408                 + File.separator + "deletesl.xml"), "UTF-8");
409         assertEquals(xmlComp, xmlRef);
410     }
411
412 }
413
Popular Tags