001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.timer.jdbc;
017:
018: import java.sql.Connection;
019: import java.sql.PreparedStatement;
020: import java.sql.ResultSet;
021: import java.util.Collection;
022: import java.util.Date;
023: import javax.sql.DataSource;
024:
025: import junit.framework.TestCase;
026: import org.apache.geronimo.timer.Playback;
027: import org.apache.geronimo.timer.WorkInfo;
028:
029: /**
030: *
031: *
032: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
033: *
034: * */
035: public class JDBCWorkerPersistenceTestAbstract extends TestCase {
036:
037: private final String countSQL = "select count(*) from timertasks";
038:
039: private final String serverUniqueId = "TestServerUniqueID";
040: private final String key = "test:service=Timer";
041: private final Object userInfo = "test user info";
042: private Object userId = null;
043:
044: private JDBCWorkerPersistence jdbcWorkerPersistence;
045: protected DataSource datasource;
046: protected boolean useSequence;
047:
048: private WorkInfo workInfo;
049: protected Date time;
050: protected Long period;
051:
052: protected void setUp() throws Exception {
053: jdbcWorkerPersistence = new JDBCWorkerPersistence(
054: serverUniqueId, datasource, useSequence);
055: time = new Date(System.currentTimeMillis());
056: period = new Long(1000);
057: workInfo = new WorkInfo(key, userId, userInfo, time, period,
058: true);
059: }
060:
061: public void testSaveCancel() throws Exception {
062: assertEquals(0, countRows());
063: jdbcWorkerPersistence.save(workInfo);
064: assertEquals(1, countRows());
065: jdbcWorkerPersistence.cancel(workInfo.getId());
066: assertEquals(0, countRows());
067: }
068:
069: public void testSaveUpdate() throws Exception {
070: assertEquals(0, countRows());
071: long now = workInfo.getTime().getTime();
072: jdbcWorkerPersistence.save(workInfo);
073: assertEquals(1, countRows());
074: jdbcWorkerPersistence.fixedRateWorkPerformed(workInfo.getId());
075: // showRows();
076: PlaybackImpl playback = new PlaybackImpl();
077: jdbcWorkerPersistence.playback(key, playback);
078: assertEquals(now + period.longValue(), playback.getTime()
079: .getTime());
080: assertEquals(1, playback.getCount());
081: long before = System.currentTimeMillis();
082: jdbcWorkerPersistence.intervalWorkPerformed(workInfo.getId(),
083: period.longValue());
084: long after = System.currentTimeMillis();
085: playback = new PlaybackImpl();
086: jdbcWorkerPersistence.playback(key, playback);
087: assertTrue(before + period.longValue() <= playback.getTime()
088: .getTime());
089: assertTrue(after + period.longValue() >= playback.getTime()
090: .getTime());
091: assertEquals(1, playback.getCount());
092: jdbcWorkerPersistence.cancel(workInfo.getId());
093: assertEquals(0, countRows());
094: }
095:
096: public void testGetByKey() throws Exception {
097: time = new Date(System.currentTimeMillis());
098: period = new Long(1000);
099: WorkInfo workInfo1 = new WorkInfo(key, new Long(1), userInfo,
100: time, period, true);
101: WorkInfo workInfo2 = new WorkInfo(key, new Long(2), userInfo,
102: time, period, true);
103: jdbcWorkerPersistence.save(workInfo1);
104: jdbcWorkerPersistence.save(workInfo2);
105: Collection idsAll = jdbcWorkerPersistence
106: .getIdsByKey(key, null);
107: assertEquals(2, idsAll.size());
108: Collection ids1 = jdbcWorkerPersistence.getIdsByKey(key,
109: new Long(1));
110: assertEquals(1, ids1.size());
111: Collection ids2 = jdbcWorkerPersistence.getIdsByKey(key,
112: new Long(2));
113: assertEquals(1, ids2.size());
114: }
115:
116: private void showRows() throws Exception {
117: Connection c = datasource.getConnection();
118: try {
119: PreparedStatement p = c
120: .prepareStatement("select id, task from timertasks");
121: try {
122: ResultSet countRS = p.executeQuery();
123: try {
124: while (countRS.next()) {
125: System.out.println("id: " + countRS.getLong(1)
126: + " task: " + countRS.getString(2));
127: }
128: } finally {
129: countRS.close();
130: }
131: } finally {
132: p.close();
133: }
134: } finally {
135: c.close();
136: }
137: }
138:
139: private int countRows() throws Exception {
140: Connection c = datasource.getConnection();
141: try {
142: PreparedStatement p = c.prepareStatement(countSQL);
143: try {
144: ResultSet countRS = p.executeQuery();
145: try {
146: countRS.next();
147: return countRS.getInt(1);
148: } finally {
149: countRS.close();
150: }
151: } finally {
152: p.close();
153: }
154: } finally {
155: c.close();
156: }
157: }
158:
159: private static class PlaybackImpl implements Playback {
160:
161: private int count = 0;
162: private Date time;
163:
164: public void schedule(WorkInfo workInfo) {
165: count++;
166: this .time = workInfo.getTime();
167: }
168:
169: public int getCount() {
170: return count;
171: }
172:
173: public Date getTime() {
174: return time;
175: }
176:
177: }
178:
179: }
|