KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > timer > jdbc > JDBCWorkerPersistenceTestAbstract


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.timer.jdbc;
19
20 import java.sql.Connection JavaDoc;
21 import java.sql.PreparedStatement JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Date JavaDoc;
25 import javax.sql.DataSource JavaDoc;
26
27 import junit.framework.TestCase;
28 import org.apache.geronimo.timer.Playback;
29 import org.apache.geronimo.timer.WorkInfo;
30
31 /**
32  *
33  *
34  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
35  *
36  * */

37 public class JDBCWorkerPersistenceTestAbstract extends TestCase {
38
39
40     private final String JavaDoc countSQL = "select count(*) from timertasks";
41
42     private final String JavaDoc serverUniqueId = "TestServerUniqueID";
43     private final String JavaDoc key = "test:service=Timer";
44     private final Object JavaDoc userInfo = "test user info";
45     private Object JavaDoc userId = null;
46
47     private JDBCWorkerPersistence jdbcWorkerPersistence;
48     protected DataSource JavaDoc datasource;
49     protected boolean useSequence;
50
51
52     private WorkInfo workInfo;
53     protected Date JavaDoc time;
54     protected Long JavaDoc period;
55
56     protected void setUp() throws Exception JavaDoc {
57         jdbcWorkerPersistence = new JDBCWorkerPersistence(serverUniqueId, datasource, useSequence);
58         time = new Date JavaDoc(System.currentTimeMillis());
59         period = new Long JavaDoc(1000);
60         workInfo = new WorkInfo(key, userId, userInfo, time, period, true);
61     }
62
63
64     public void testSaveCancel() throws Exception JavaDoc {
65         assertEquals(0, countRows());
66         jdbcWorkerPersistence.save(workInfo);
67         assertEquals(1, countRows());
68         jdbcWorkerPersistence.cancel(workInfo.getId());
69         assertEquals(0, countRows());
70     }
71
72     public void testSaveUpdate() throws Exception JavaDoc {
73         assertEquals(0, countRows());
74         long now = workInfo.getTime().getTime();
75         jdbcWorkerPersistence.save(workInfo);
76         assertEquals(1, countRows());
77         jdbcWorkerPersistence.fixedRateWorkPerformed(workInfo.getId());
78 // showRows();
79
PlaybackImpl playback = new PlaybackImpl();
80         jdbcWorkerPersistence.playback(key, playback);
81         assertEquals(now + period.longValue(), playback.getTime().getTime());
82         assertEquals(1, playback.getCount());
83         long before = System.currentTimeMillis();
84         jdbcWorkerPersistence.intervalWorkPerformed(workInfo.getId(), period.longValue());
85         long after = System.currentTimeMillis();
86         playback = new PlaybackImpl();
87         jdbcWorkerPersistence.playback(key, playback);
88         assertTrue(before + period.longValue() <= playback.getTime().getTime());
89         assertTrue(after + period.longValue() >= playback.getTime().getTime());
90         assertEquals(1, playback.getCount());
91         jdbcWorkerPersistence.cancel(workInfo.getId());
92         assertEquals(0, countRows());
93     }
94
95     public void testGetByKey() throws Exception JavaDoc {
96         time = new Date JavaDoc(System.currentTimeMillis());
97         period = new Long JavaDoc(1000);
98         WorkInfo workInfo1 = new WorkInfo(key, new Long JavaDoc(1), userInfo, time, period, true);
99         WorkInfo workInfo2 = new WorkInfo(key, new Long JavaDoc(2), userInfo, time, period, true);
100         jdbcWorkerPersistence.save(workInfo1);
101         jdbcWorkerPersistence.save(workInfo2);
102         Collection JavaDoc idsAll = jdbcWorkerPersistence.getIdsByKey(key, null);
103         assertEquals(2, idsAll.size());
104         Collection JavaDoc ids1 = jdbcWorkerPersistence.getIdsByKey(key, new Long JavaDoc(1));
105         assertEquals(1, ids1.size());
106         Collection JavaDoc ids2 = jdbcWorkerPersistence.getIdsByKey(key, new Long JavaDoc(2));
107         assertEquals(1, ids2.size());
108     }
109
110
111
112     private void showRows() throws Exception JavaDoc {
113         Connection JavaDoc c = datasource.getConnection();
114         try {
115             PreparedStatement JavaDoc p = c.prepareStatement("select id, task from timertasks");
116             try {
117                 ResultSet JavaDoc countRS = p.executeQuery();
118                 try {
119                     while(countRS.next()) {
120                         System.out.println("id: " + countRS.getLong(1) + " task: " + countRS.getString(2));
121                     }
122                 } finally {
123                     countRS.close();
124                 }
125             } finally {
126                 p.close();
127             }
128         } finally {
129             c.close();
130         }
131     }
132
133     private int countRows() throws Exception JavaDoc {
134         Connection JavaDoc c = datasource.getConnection();
135         try {
136             PreparedStatement JavaDoc p = c.prepareStatement(countSQL);
137             try {
138                 ResultSet JavaDoc countRS = p.executeQuery();
139                 try {
140                     countRS.next();
141                     return countRS.getInt(1);
142                 } finally {
143                     countRS.close();
144                 }
145             } finally {
146                 p.close();
147             }
148         } finally {
149             c.close();
150         }
151     }
152
153     private static class PlaybackImpl implements Playback {
154
155         private int count = 0;
156         private Date JavaDoc time;
157
158         public void schedule(WorkInfo workInfo) {
159             count++;
160             this.time = workInfo.getTime();
161         }
162
163         public int getCount() {
164             return count;
165         }
166
167         public Date JavaDoc getTime() {
168             return time;
169         }
170
171     }
172
173 }
174
Popular Tags