1 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 49 public class TestCmsSchedulerInSystem extends OpenCmsTestCase { 50 51 56 public TestCmsSchedulerInSystem(String arg0) { 57 super(arg0); 58 } 59 60 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 92 public void testDefaultConfiguration() throws Exception { 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 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 OpenCms.getScheduleManager().scheduleJob(getCmsObject(), jobInfo); 108 109 int seconds = 0; 110 do { 111 try { 112 Thread.sleep(1000); 113 } catch (InterruptedException 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 |