001: /* Copyright 2002 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal;
007:
008: import junit.framework.*;
009: import java.sql.*;
010: import java.util.HashSet;
011: import java.util.Set;
012:
013: /**
014: * Tests the sequence generator.
015: * @author: Dan Ellentuck
016: */
017: public class SequenceGeneratorTester extends TestCase {
018: private int numTestCounters;
019: private String[] testCounterNames;
020: private ReferenceSequenceGenerator generator;
021:
022: protected class Tester implements Runnable {
023: protected ReferenceSequenceGenerator generator = null;
024: protected int numTests = 0;
025: protected String[] counterValues = null;
026:
027: protected Tester(ReferenceSequenceGenerator gen, int tests) {
028: super ();
029: generator = gen;
030: numTests = tests;
031: counterValues = new String[numTests];
032: }
033:
034: public void run() {
035: for (int i = 0; i < numTests; i++) {
036: String ctrValue = null;
037: for (int j = 0; ctrValue == null && j < 10; j++) {
038: try {
039: ctrValue = generator
040: .getNext(testCounterNames[0]);
041: } catch (Exception e) {
042: print("SequenceGeneratorTester$Tester: Caught Exception: "
043: + e.getMessage());
044: }
045: }
046: counterValues[i] = ctrValue;
047: }
048: }
049: }
050:
051: /**
052: * EntityLockTester constructor comment.
053: */
054: public SequenceGeneratorTester(String name) {
055: super (name);
056: }
057:
058: /**
059: * @return org.jasig.portal.concurrency.locking.IEntityLockStore
060: */
061: private ReferenceSequenceGenerator getGenerator() {
062: if (generator == null) {
063: generator = new ReferenceSequenceGenerator();
064: }
065: return generator;
066: }
067:
068: /**
069: * Starts the application.
070: * @param args an array of command-line arguments
071: */
072: public static void main(java.lang.String[] args) throws Exception {
073: String[] mainArgs = { "org.jasig.portal.SequenceGeneratorTester" };
074: print("START TESTING SEQUENCE GENERATOR");
075: printBlankLine();
076: junit.swingui.TestRunner.main(mainArgs);
077: printBlankLine();
078: print("END TESTING SEQUENCE GENERATOR");
079:
080: }
081:
082: /**
083: * @param msg java.lang.String
084: */
085: private static void print(String msg) {
086: java.sql.Timestamp ts = new java.sql.Timestamp(System
087: .currentTimeMillis());
088: System.out.println(ts + " : " + msg);
089: }
090:
091: /**
092: */
093: private static void printBlankLine() {
094: System.out.println("");
095: }
096:
097: /**
098: */
099: protected void setUp() {
100: Connection conn = null;
101: Statement stmt = null;
102:
103: try {
104: String sql;
105: int idx;
106: numTestCounters = 5;
107: testCounterNames = new String[numTestCounters];
108:
109: print("Creating test counters.");
110:
111: for (idx = 0; idx < numTestCounters; idx++) {
112: testCounterNames[idx] = "TEST_COUNTER_" + idx;
113: }
114:
115: conn = RDBMServices.getConnection();
116: stmt = conn.createStatement();
117:
118: // Delete any left over counters that could interfere.
119: for (idx = 0; idx < testCounterNames.length; idx++) {
120: sql = "DELETE FROM UP_SEQUENCE WHERE SEQUENCE_NAME = "
121: + "'" + testCounterNames[idx] + "'";
122: stmt.executeUpdate(sql);
123: }
124: // create some test counters:
125: for (idx = 0; idx < numTestCounters; idx++) {
126: sql = "INSERT INTO UP_SEQUENCE (SEQUENCE_NAME, SEQUENCE_VALUE) "
127: + "VALUES ("
128: + "'"
129: + testCounterNames[idx]
130: + "', 0)";
131: stmt.executeUpdate(sql);
132: }
133: } catch (Exception ex) {
134: print("SequenceGeneratorTester.setUp(): " + ex.getMessage());
135: } finally {
136: if (stmt != null) {
137: try {
138: stmt.close();
139: } catch (SQLException sqle) {
140: }
141: }
142: if (conn != null) {
143: RDBMServices.releaseConnection(conn);
144: }
145: }
146: }
147:
148: /**
149: * @return junit.framework.Test
150: */
151: public static junit.framework.Test suite() {
152: TestSuite suite = new TestSuite();
153:
154: suite.addTest(new SequenceGeneratorTester(
155: "testGetUniqueSequenceNumbers"));
156: suite.addTest(new SequenceGeneratorTester(
157: "testCreateNewCounters"));
158: suite.addTest(new SequenceGeneratorTester(
159: "testSetCounterValues"));
160: suite.addTest(new SequenceGeneratorTester(
161: "testConcurrentAccess"));
162:
163: // Add more tests here.
164: // NB: Order of tests is not guaranteed.
165:
166: return suite;
167: }
168:
169: /**
170: */
171: protected void tearDown() {
172: Connection conn = null;
173: Statement stmt = null;
174: try {
175: // delete the test counters:
176: print("Deleting test counters.");
177: String sql;
178: int idx;
179: conn = RDBMServices.getConnection();
180: stmt = conn.createStatement();
181: for (idx = 0; idx < testCounterNames.length; idx++) {
182: sql = "DELETE FROM UP_SEQUENCE WHERE SEQUENCE_NAME = "
183: + "'" + testCounterNames[idx] + "'";
184: stmt.executeUpdate(sql);
185: }
186: } catch (Exception ex) {
187: print("SequenceGeneratorTester.tearDown(): "
188: + ex.getMessage());
189: } finally {
190: if (stmt != null) {
191: try {
192: stmt.close();
193: } catch (SQLException sqle) {
194: }
195: }
196: if (conn != null) {
197: RDBMServices.releaseConnection(conn);
198: }
199: }
200: }
201:
202: /**
203: */
204: public void testConcurrentAccess() throws Exception {
205: ReferenceSequenceGenerator gen = new ReferenceSequenceGenerator();
206:
207: int numTests = 50;
208: int numThreads = 5;
209: String msg = null;
210:
211: print("Setting up testing Threads.");
212: Tester[] testers = new Tester[numThreads];
213: for (int i = 0; i < numThreads; i++) {
214: testers[i] = new Tester(gen, numTests);
215: Thread thread = new Thread(testers[i]);
216: thread.start();
217: }
218:
219: long millis = numTests * numThreads * 100;
220: print("Now sleeping for " + (millis / 1000) + " seconds.");
221: Thread.sleep(millis);
222:
223: msg = "Checking counter values for uniqueness.";
224: print(msg);
225:
226: String testValue;
227: Set testValues = new HashSet();
228: boolean testResult = false;
229:
230: for (int testerIdx = 0; testerIdx < numThreads; testerIdx++) {
231: for (int valueIdx = 0; valueIdx < numTests; valueIdx++) {
232: testValue = testers[testerIdx].counterValues[valueIdx];
233: assertNotNull(msg, testValue);
234: assertTrue(msg, !testValues.contains(testValue));
235: testValues.add(testValue);
236: }
237: }
238:
239: }
240:
241: /**
242: */
243: public void testCreateNewCounters() throws Exception {
244: String msg = null;
245: int numNewCounters = 5;
246: int idx;
247: int counterValue;
248:
249: String[] counterNames = new String[numNewCounters];
250: for (idx = 0; idx < numNewCounters; idx++) {
251: counterNames[idx] = "NEW_CTR_" + idx;
252: }
253:
254: print("Creating new counter(s).");
255: for (idx = 0; idx < numNewCounters; idx++) {
256: getGenerator().createCounter(counterNames[idx]);
257: }
258:
259: msg = "Getting sequence value from new counter(s).";
260: print(msg);
261: for (idx = 0; idx < numNewCounters; idx++) {
262: counterValue = getGenerator().getNextInt(counterNames[idx]);
263: assertEquals(msg, 1, counterValue);
264: }
265:
266: print("Deleting new counter(s).");
267: Connection conn = null;
268: Statement stmt = null;
269: try {
270: String sql;
271: conn = RDBMServices.getConnection();
272: stmt = conn.createStatement();
273: for (idx = 0; idx < counterNames.length; idx++) {
274: sql = "DELETE FROM UP_SEQUENCE WHERE SEQUENCE_NAME = "
275: + "'" + counterNames[idx] + "'";
276: stmt.executeUpdate(sql);
277: }
278: } catch (Exception ex) {
279: print("SequenceGeneratorTester.testCreateNewCounters(): "
280: + ex.getMessage());
281: } finally {
282: if (stmt != null) {
283: try {
284: stmt.close();
285: } catch (SQLException sqle) {
286: }
287: }
288: if (conn != null) {
289: RDBMServices.releaseConnection(conn);
290: }
291: }
292:
293: }
294:
295: /**
296: */
297: public void testGetUniqueSequenceNumbers() throws Exception {
298: String msg = null;
299: int numTestValues = 10;
300: int idx;
301: String[][] testValues = new String[numTestCounters][numTestValues];
302:
303: print("Getting sequence values.");
304: for (idx = 0; idx < numTestCounters; idx++) {
305: for (int i = 0; i < numTestValues; i++) {
306: testValues[idx][i] = getGenerator().getNext(
307: testCounterNames[idx]);
308: }
309:
310: }
311:
312: msg = "Testing sequence values for uniqueness.";
313: print(msg);
314: boolean assertionValue;
315: for (idx = 0; idx < numTestCounters; idx++) {
316: for (int i = 1; i < numTestValues; i++) {
317: assertionValue = testValues[idx][i - 1]
318: .equals(testValues[idx][i]);
319: assertTrue(msg, !assertionValue);
320: }
321:
322: }
323: }
324:
325: /**
326: */
327: public void testSetCounterValues() throws Exception {
328: int idx, testValue, nextCounterValue;
329:
330: print("Setting sequence values.");
331: for (idx = 0; idx < numTestCounters; idx++) {
332: testValue = idx * 999;
333: getGenerator().setCounter(testCounterNames[idx], testValue);
334: nextCounterValue = getGenerator().getNextInt(
335: testCounterNames[idx]);
336: nextCounterValue--;
337: assertEquals(testValue, nextCounterValue);
338: }
339:
340: }
341: }
|