KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > module > TestModuleDeleteThread


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/module/TestModuleDeleteThread.java,v $
3  * Date : $Date: 2005/07/28 15:53:10 $
4  * Version: $Revision: 1.5 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.module;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.main.OpenCms;
36 import org.opencms.report.CmsShellReport;
37 import org.opencms.test.OpenCmsTestCase;
38 import org.opencms.test.OpenCmsTestLogAppender;
39 import org.opencms.test.OpenCmsTestProperties;
40 import org.opencms.workplace.threads.CmsModuleDeleteThread;
41
42 import java.util.ArrayList JavaDoc;
43 import java.util.List JavaDoc;
44
45 import junit.extensions.TestSetup;
46 import junit.framework.Test;
47 import junit.framework.TestSuite;
48
49 /**
50  * Tests the deleting of modules using the module delete thread,
51  * comparing this to the deletion using the module manager alone.<p>
52  *
53  * @author Olaf Watteroth
54  * @author Alexander Kandzior
55  *
56  * @version $Revision: 1.5 $
57  */

58 public class TestModuleDeleteThread extends OpenCmsTestCase {
59
60     /**
61      * Default JUnit constructor.<p>
62      *
63      * @param arg0 JUnit parameters
64      */

65     public TestModuleDeleteThread(String JavaDoc arg0) {
66
67         super(arg0);
68     }
69
70     /**
71      * Test suite for this test class.<p>
72      *
73      * @return the test suite
74      */

75     public static Test suite() {
76
77         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
78
79         TestSuite suite = new TestSuite();
80         suite.setName(TestModuleDeleteThread.class.getName());
81
82         // test to delete a module without resources with a single and with two threads
83
suite.addTest(new TestModuleDeleteThread("testModuleDeleteThread"));
84         // test to delete a module with non-existing resources using the module delete thread
85
suite.addTest(new TestModuleDeleteThread("testModuleResourcesDeleteThread"));
86         // test to delete a module with non-existing resources using the CmsModuleManager - to compare with above
87
suite.addTest(new TestModuleDeleteThread("testModuleResourcesDelete"));
88
89         TestSetup wrapper = new TestSetup(suite) {
90
91             protected void setUp() {
92
93                 setupOpenCms("simpletest", "/sites/default/");
94                 // this test causes issues that are written to the error log channel
95
OpenCmsTestLogAppender.setBreakOnError(false);
96             }
97
98             protected void tearDown() {
99
100                 removeOpenCms();
101             }
102         };
103
104         return wrapper;
105     }
106
107     /**
108      * Test to delete a module without resources with a single and with two threads.<p>
109      *
110      * @throws Exception in case the test fails
111      */

112     public void testModuleDeleteThread() throws Exception JavaDoc {
113
114         echo("Testing to delete a module without resources using the module delete thread.");
115         // get a reference to the CmsObject
116
CmsObject cms = getCmsObject();
117
118         // list for the module - used later
119
List JavaDoc moduleDeleteList;
120
121         // create a new blank module
122
String JavaDoc moduleName = "org.opencms.test.testModuleDeleteThread";
123         CmsModule module1 = new CmsModule(
124             moduleName,
125             "Testing to delete a single module using the module delete thread/1",
126             "ModuleGroup",
127             null,
128             null,
129             new CmsModuleVersion("1.0"),
130             "Olaf Watteroth",
131             "watterot@inf.fu-berlin.de",
132             System.currentTimeMillis(),
133             null,
134             0L,
135             null,
136             null,
137             null,
138             null);
139
140         OpenCms.getModuleManager().addModule(cms, module1);
141         // basic check if the module was created correctly
142
if (!OpenCms.getModuleManager().hasModule(moduleName)) {
143             fail("Module '" + moduleName + "' was not created!");
144         }
145
146         // create two CmsModuleDeleteThread's for testing
147
moduleDeleteList = new ArrayList JavaDoc();
148         moduleDeleteList.add(moduleName);
149         // create a single Thread to delete the module
150
CmsModuleDeleteThread thread1 = new CmsModuleDeleteThread(cms, moduleDeleteList, false, false);
151
152         // start the threads
153
thread1.start();
154         // wait till the thread finish
155
thread1.join();
156
157         while (thread1.isAlive()) {
158             // check if thread1 is still running and wait to finish
159
}
160         // try to get the deleted module
161
CmsModule temp = OpenCms.getModuleManager().getModule(moduleName);
162         // test if the module is null - it should be 'cause it was deleted
163
echo("Test if the module still exists");
164         assertNull(temp);
165
166         CmsModule module2 = new CmsModule(
167             moduleName,
168             "Testing to delete a single module using the module delete thread/2",
169             "ModuleGroup",
170             null,
171             null,
172             new CmsModuleVersion("1.0"),
173             "Olaf Watteroth",
174             "watterot@inf.fu-berlin.de",
175             System.currentTimeMillis(),
176             null,
177             0L,
178             null,
179             null,
180             null,
181             null);
182
183         OpenCms.getModuleManager().addModule(cms, module2);
184
185         // Create two CmsModuleDeleteThread's for testing
186
moduleDeleteList = new ArrayList JavaDoc();
187         moduleDeleteList.add(moduleName);
188         echo("Created a new module again and try to delete it - this time with two threads at once");
189         CmsModuleDeleteThread thread_parallel_1 = new CmsModuleDeleteThread(cms, moduleDeleteList, false, false);
190         CmsModuleDeleteThread thread_parallel_2 = new CmsModuleDeleteThread(cms, moduleDeleteList, false, false);
191
192         // start the threads
193
thread_parallel_1.start();
194         thread_parallel_2.start();
195
196         // wait 'till all threads finish
197
thread_parallel_1.join();
198         thread_parallel_2.join();
199
200         while (thread_parallel_1.isAlive() & thread_parallel_2.isAlive()) {
201             // check if all threads finished
202
Thread.sleep(1000);
203         }
204
205         // try to get the deleted module
206
module1 = OpenCms.getModuleManager().getModule(moduleName);
207         // test if the module is null - it should be 'cause it was deleted
208
echo("Exceptions will be logged - but the module should be deleted correctly");
209         assertNull(module1);
210     }
211
212     /**
213      * Test to delete a module with non-existing resources using the module delete thread.<p>
214      *
215      * @throws Exception in case the test fails
216      */

217     public void testModuleResourcesDeleteThread() throws Exception JavaDoc {
218
219         echo("Test to delete a module with non-existing resources using the module delete thread");
220
221         CmsObject cms = getCmsObject();
222         String JavaDoc moduleName = "org.opencms.test.testModuleResourcesDeleteThread";
223
224         String JavaDoc res1 = "/system/modules/tests/test1/";
225         String JavaDoc res2 = "/system/modules/tests/test2/";
226         String JavaDoc res3 = "/system/modules/tests/test3/";
227         String JavaDoc res4 = "/system/modules/tests/test4/";
228
229         List JavaDoc resources = new ArrayList JavaDoc();
230         resources.add(res1);
231         resources.add(res2);
232         resources.add(res3);
233         resources.add(res4);
234
235         CmsModule module1 = new CmsModule(
236             moduleName,
237             "Test to delete a module with non-existing resources using the module delete thread",
238             "ModuleGroup",
239             null,
240             null,
241             new CmsModuleVersion("1.0"),
242             "Olaf Watteroth",
243             "watterot@inf.fu-berlin.de",
244             System.currentTimeMillis(),
245             null,
246             0L,
247             null,
248             null,
249             resources,
250             null);
251
252         OpenCms.getModuleManager().addModule(cms, module1);
253         module1 = OpenCms.getModuleManager().getModule(moduleName);
254
255         assertEquals(0, module1.getParameters().size());
256         assertEquals(4, module1.getResources().size());
257
258         // Now its new code
259
echo("Module created. Now try to delete it");
260         // Now try to delete this module after it was added
261
// Create a CmsModuleDeleteThread's for testing
262
List JavaDoc module = new ArrayList JavaDoc();
263         module.add(moduleName);
264
265         // Create a single Thread to delete the module
266
CmsModuleDeleteThread thread1 = new CmsModuleDeleteThread(cms, module, false, false);
267
268         // Start the threads
269
thread1.start();
270
271         // Wait till the thread finish
272
thread1.join();
273
274         while (thread1.isAlive()) {
275             // Check if thread1 is still running and wait to finish
276
Thread.sleep(1000);
277         }
278
279         // try to get the deleted module
280
module1 = OpenCms.getModuleManager().getModule(moduleName);
281         // test if the module is null - it should be 'cause it was deleted
282
echo("Test if the module still exists");
283         assertNull(module1);
284     }
285
286     /**
287      * Test to delete a module with non-existing resources using the CmsModuleManager.<p>
288      *
289      * @throws Throwable if something goes wrong
290      */

291     public void testModuleResourcesDelete() throws Throwable JavaDoc {
292
293         echo("Test to delete a module with non-existing resources using the CmsModuleManager");
294
295         CmsObject cms = getCmsObject();
296         String JavaDoc moduleName = "org.opencms.test.testModuleResourcesDelete";
297
298         String JavaDoc res1 = "/system/modules/tests/test1/";
299         String JavaDoc res2 = "/system/modules/tests/test2/";
300         String JavaDoc res3 = "/system/modules/tests/test3/";
301         String JavaDoc res4 = "/system/modules/tests/test4/";
302
303         List JavaDoc resources = new ArrayList JavaDoc();
304         resources.add(res1);
305         resources.add(res2);
306         resources.add(res3);
307         resources.add(res4);
308
309         CmsModule module1 = new CmsModule(
310             moduleName,
311             "Test to delete a module with non-existing resources using the CmsModuleManager",
312             "ModuleGroup",
313             null,
314             null,
315             new CmsModuleVersion("1.0"),
316             "Olaf Watteroth",
317             "watterot@inf.fu-berlin.de",
318             System.currentTimeMillis(),
319             null,
320             0L,
321             null,
322             null,
323             resources,
324             null);
325
326         OpenCms.getModuleManager().addModule(cms, module1);
327         module1 = OpenCms.getModuleManager().getModule(moduleName);
328
329         assertEquals(4, module1.getResources().size());
330
331         echo("Now try to delete it *the normal way*");
332         OpenCms.getModuleManager().deleteModule(cms, moduleName, false, new CmsShellReport(cms.getRequestContext().getLocale()));
333
334         module1 = OpenCms.getModuleManager().getModule(moduleName);
335         // now it should be null
336
echo("Now check if module was deleted");
337         assertNull(module1);
338         echo("Test finished");
339     }
340 }
Popular Tags