KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > scheduler > TestCmsSchedulerInSystem


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/scheduler/TestCmsSchedulerInSystem.java,v $
3  * Date : $Date: 2005/06/27 23:22:16 $
4  * Version: $Revision: 1.9 $
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.scheduler;
33
34 import org.opencms.main.CmsContextInfo;
35 import org.opencms.main.OpenCms;
36 import org.opencms.test.OpenCmsTestCase;
37 import org.opencms.test.OpenCmsTestProperties;
38
39 import junit.extensions.TestSetup;
40 import junit.framework.Test;
41 import junit.framework.TestSuite;
42
43 /**
44  * Unit test for the OpenCms scheduler in a running system.<p>
45  *
46  * @author Alexander Kandzior
47  * @version $Revision: 1.9 $
48  */

49 public class TestCmsSchedulerInSystem extends OpenCmsTestCase {
50   
51     /**
52      * Default JUnit constructor.<p>
53      *
54      * @param arg0 JUnit parameters
55      */

56     public TestCmsSchedulerInSystem(String JavaDoc arg0) {
57         super(arg0);
58     }
59     
60     /**
61      * Test suite for this test class.<p>
62      *
63      * @return the test suite
64      */

65     public static Test suite() {
66         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
67         
68         TestSuite suite = new TestSuite();
69         suite.setName(TestCmsSchedulerInSystem.class.getName());
70                 
71         suite.addTest(new TestCmsSchedulerInSystem("testDefaultConfiguration"));
72                
73         TestSetup wrapper = new TestSetup(suite) {
74             
75             protected void setUp() {
76                 setupOpenCms("simpletest", "/sites/default/");
77             }
78             
79             protected void tearDown() {
80                 removeOpenCms();
81             }
82         };
83         
84         return wrapper;
85     }
86     
87     /**
88      * Test case for the initCmsObject methods.<p>
89      *
90      * @throws Exception if the test fails
91      */

92     public void testDefaultConfiguration() throws Exception JavaDoc {
93         
94         System.out.println("Trying to run a persistent OpenCms job 5x with the OpenCms system scheduler.");
95         TestScheduledJob.m_runCount = 0;
96                 
97         // generate job description
98
CmsScheduledJobInfo jobInfo = new CmsScheduledJobInfo();
99         CmsContextInfo contextInfo = new CmsContextInfo(OpenCms.getDefaultUsers().getUserAdmin());
100         jobInfo.setContextInfo(contextInfo);
101         jobInfo.setJobName("Test job");
102         jobInfo.setClassName(TestScheduledJob.class.getName());
103         jobInfo.setReuseInstance(true);
104         jobInfo.setCronExpression("0/2 * * * * ?");
105         
106         // add the job to the manager
107
OpenCms.getScheduleManager().scheduleJob(getCmsObject(), jobInfo);
108         
109         int seconds = 0;
110         do {
111             try {
112                 Thread.sleep(1000);
113             } catch (InterruptedException JavaDoc e) {
114                 fail("Something caused the waiting test thread to interrupt!");
115             }
116             seconds++;
117         } while ((seconds < TestCmsScheduler.SECONDS_TO_WAIT) && (TestScheduledJob.m_runCount < 5));
118
119
120         if (TestScheduledJob.m_runCount == 5) {
121             System.out.println("Test job was correctly run 5 times in OpenCms scheduler.");
122         } else {
123             fail("Test class not run after " + TestCmsScheduler.SECONDS_TO_WAIT + " seconds.");
124         }
125         
126         if (TestScheduledJob.m_instanceCountCopy == 5) {
127             System.out.println("Instance counter was correctly incremented 5 times.");
128         } else {
129             fail("Instance counter was not incremented!");
130         }
131     }
132 }
Popular Tags