1 17 18 package org.apache.geronimo.timer.jdbc; 19 20 import java.sql.Connection ; 21 import java.sql.PreparedStatement ; 22 import java.sql.ResultSet ; 23 import java.util.Collection ; 24 import java.util.Date ; 25 import javax.sql.DataSource ; 26 27 import junit.framework.TestCase; 28 import org.apache.geronimo.timer.Playback; 29 import org.apache.geronimo.timer.WorkInfo; 30 31 37 public class JDBCWorkerPersistenceTestAbstract extends TestCase { 38 39 40 private final String countSQL = "select count(*) from timertasks"; 41 42 private final String serverUniqueId = "TestServerUniqueID"; 43 private final String key = "test:service=Timer"; 44 private final Object userInfo = "test user info"; 45 private Object userId = null; 46 47 private JDBCWorkerPersistence jdbcWorkerPersistence; 48 protected DataSource datasource; 49 protected boolean useSequence; 50 51 52 private WorkInfo workInfo; 53 protected Date time; 54 protected Long period; 55 56 protected void setUp() throws Exception { 57 jdbcWorkerPersistence = new JDBCWorkerPersistence(serverUniqueId, datasource, useSequence); 58 time = new Date (System.currentTimeMillis()); 59 period = new Long (1000); 60 workInfo = new WorkInfo(key, userId, userInfo, time, period, true); 61 } 62 63 64 public void testSaveCancel() throws Exception { 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 { 73 assertEquals(0, countRows()); 74 long now = workInfo.getTime().getTime(); 75 jdbcWorkerPersistence.save(workInfo); 76 assertEquals(1, countRows()); 77 jdbcWorkerPersistence.fixedRateWorkPerformed(workInfo.getId()); 78 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 { 96 time = new Date (System.currentTimeMillis()); 97 period = new Long (1000); 98 WorkInfo workInfo1 = new WorkInfo(key, new Long (1), userInfo, time, period, true); 99 WorkInfo workInfo2 = new WorkInfo(key, new Long (2), userInfo, time, period, true); 100 jdbcWorkerPersistence.save(workInfo1); 101 jdbcWorkerPersistence.save(workInfo2); 102 Collection idsAll = jdbcWorkerPersistence.getIdsByKey(key, null); 103 assertEquals(2, idsAll.size()); 104 Collection ids1 = jdbcWorkerPersistence.getIdsByKey(key, new Long (1)); 105 assertEquals(1, ids1.size()); 106 Collection ids2 = jdbcWorkerPersistence.getIdsByKey(key, new Long (2)); 107 assertEquals(1, ids2.size()); 108 } 109 110 111 112 private void showRows() throws Exception { 113 Connection c = datasource.getConnection(); 114 try { 115 PreparedStatement p = c.prepareStatement("select id, task from timertasks"); 116 try { 117 ResultSet 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 { 134 Connection c = datasource.getConnection(); 135 try { 136 PreparedStatement p = c.prepareStatement(countSQL); 137 try { 138 ResultSet 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 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 getTime() { 168 return time; 169 } 170 171 } 172 173 } 174 | Popular Tags |