1
2
3
4
5
6
7
8 /
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public class DBSchedulerTest extends LedgeTestCase
75 {
76 private FileSystem fs = null;
77
78 private DBScheduler scheduler;
79
80
81
82
83 protected void setUp() throws Exception
84 {
85 super.setUp();
86 fs = FileSystem.getStandardFileSystem("src/test/resources");
87 InputSource source = new InputSource(fs.getInputStream(
88 "config/org.objectledge.logging.LoggingConfigurator.xml"));
89 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
90 Document logConfig = builder.parse(source);
91 LedgeDOMConfigurator configurator = new LedgeDOMConfigurator(fs);
92 configurator.doConfigure(logConfig.getDocumentElement(), LogManager.getLoggerRepository());
93 Logger logger = new Log4JLogger(org.apache.log4j.Logger.getLogger(DBScheduler.class));
94 Configuration config = getConfig("config/org.objectledge.threads.ThreadPool.xml");
95 Context context = new Context();
96 ThreadPool threadPool = new ThreadPool(null, context,config, logger);
97 config = getConfig("config/org.objectledge.scheduler.TransientScheduler.xml");
98 ScheduleFactory[] scheduleFactories = new ScheduleFactory[1];
99 scheduleFactories[0] = new AtScheduleFactory();
100 DataSource dataSource = getDataSource();
101 IdGenerator idGenerator = new IdGenerator(dataSource);
102 JotmTransaction transaction = new JotmTransaction(0, 120, new Context(), logger, null);
103 Database database = new DefaultDatabase(dataSource, idGenerator, transaction);
104
105 Persistence persistence = new DefaultPersistence(database, logger);
106 MutablePicoContainer container = new DefaultPicoContainer();
107 scheduler = new DBScheduler(container, config,
108 logger, threadPool, scheduleFactories, persistence);
109 scheduler.start();
110 }
111
112 public void testCreateJobDescriptor()
113 throws Exception
114 {
115 assertNotNull(scheduler);
116 Schedule schedule = scheduler.createSchedule("at","");
117 scheduler.createJobDescriptor("bar",schedule,"org.objectledge.scheduler.FooJob");
118 try
119 {
120 scheduler.createJobDescriptor("bar",schedule,"org.objectledge.scheduler.FooJob");
121 fail("should throw the exception");
122 }
123 catch(IllegalArgumentException e)
124 {
125
126 }
127 }
128
129
130 public void testDeleteJobDescriptor()
131 throws Exception
132 {
133 AbstractJobDescriptor job = scheduler.getJobDescriptor("foo");
134 assertNotNull(job);
135 scheduler.deleteJobDescriptor(job);
136 }
137
138 public void testAllowsModifications()
139 {
140 assertEquals(true,scheduler.allowsModifications());
141 }
142
143 public void testEnable()
144 throws Exception
145 {
146 Schedule schedule = scheduler.createSchedule("at","");
147 scheduler.createJobDescriptor("bar2",schedule,"org.objectledge.scheduler.FooJob");
148 AbstractJobDescriptor job = scheduler.getJobDescriptor("bar2");
149 assertNotNull(job);
150 assertEquals(false, job.isEnabled());
151 scheduler.enable(job);
152 assertEquals(true, job.isEnabled());
153 }
154
155 public void testGetJobDescriptors()
156 {
157 AbstractJobDescriptor[] jobs = scheduler.getJobDescriptors();
158 assertEquals(2, jobs.length);
159 }
160
161 public void testGetScheduleTypes()
162 {
163 String[] types = scheduler.getScheduleTypes();
164 assertEquals(1, types.length);
165 assertEquals("at", types[0]);
166 }
167
168 public void testCreateSchedule()
169 throws Exception
170 {
171 Schedule schedule = scheduler.createSchedule("at","");
172 assertNotNull(schedule);
173 assertEquals("",schedule.getConfig());
174 assertEquals("at",schedule.getType());
175 assertNull(null,schedule.getNextRunTime(new Date(), new Date()));
176 try
177 {
178 scheduler.createSchedule("foo","");
179 fail("should throw the exception");
180 }
181 catch(InvalidScheduleException e)
182 {
183
184 }
185 }
186
187 public void testGetDateFormat()
188 {
189 assertEquals(new SimpleDateFormat(AbstractScheduler.DATE_FORMAT_DEFAULT),
190 scheduler.getDateFormat());
191 }
192
193 public void testDBJobDescriptor()
194 throws Exception
195 {
196 Schedule schedule = scheduler.createSchedule("at","");
197 scheduler.createJobDescriptor("foo.bar",schedule,"org.objectledge.scheduler.FooJob");
198 DBJobDescriptor job = (DBJobDescriptor)scheduler.getJobDescriptor("foo.bar");
199 assertEquals(false, job.isRunning());
200 job.setSchedule(schedule);
201 job.setJobClassName("foo.bar");
202 job.setAutoClean(false);
203 assertEquals("foo.bar", job.getJobClassName());
204 assertEquals(false, job.getAutoClean());
205 }
206
207 public void testAdditional()
208 {
209 JobNotFoundException e = new JobNotFoundException("foo");
210 assertEquals("foo", e.getMessage());
211 JobNotFoundException ee = new JobNotFoundException("bar", e);
212 assertEquals("bar", ee.getMessage());
213 Exception eee = new JobModificationException("bar", e);
214 assertEquals("bar", eee.getMessage());
215 }
216
217
218
219
220 private Configuration getConfig(String name)
221 throws Exception
222 {
223 return getConfig(fs, name);
224 }
225
226 private DataSource getDataSource() throws Exception
227 {
228 DefaultConfiguration conf = new DefaultConfiguration("config", "", "/");
229 DefaultConfiguration url = new DefaultConfiguration("url", "", "/config");
230 url.setValue("jdbc:hsqldb:.");
231 conf.addChild(url);
232 DefaultConfiguration user = new DefaultConfiguration("user", "", "/config");
233 user.setValue("sa");
234 conf.addChild(user);
235 DataSource ds = new HsqldbDataSource(conf);
236 if(!DatabaseUtils.hasTable(ds, "ledge_id_table"))
237 {
238 DatabaseUtils.runScript(ds, fs.getReader("sql/database/IdGeneratorTables.sql", "UTF-8"));
239 }
240 if(!DatabaseUtils.hasTable(ds, "ledge_scheduler"))
241 {
242 DatabaseUtils.runScript(ds,
243 fs.getReader("sql/scheduler/db/DBSchedulerTables.sql", "UTF-8"));
244 DatabaseUtils.runScript(ds,
245 fs.getReader("sql/scheduler/db/DBSchedulerTest.sql", "UTF-8"));
246 }
247 return ds;
248 }
249
250 }