1 package org.xmlBlaster.test.classtest;
2
3 import java.util.logging.Logger;
4 import java.util.logging.Level;
5 import org.xmlBlaster.util.Timestamp;
6 import org.xmlBlaster.util.XmlBlasterException;
7
8 import junit.framework.*;
9
10 /**
11 * Test Timestamp class (creating unique timestamps in JVM scope).
12 * <p />
13 * All methods starting with 'test' and without arguments are invoked automatically
14 * <p />
15 * Invoke: java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.classtest.TimestampTest
16 * @see org.xmlBlaster.util.Timestamp
17 */
18 public class TimestampTest extends TestCase {
19 private String ME = "TimestampTest";
20 private static Logger log = Logger.getLogger(TimestampTest.class.getName());
21 private boolean event = false;
22 private int counter = 0;
23
24 public TimestampTest(String name) {
25 super(name);
26
27 }
28
29 /**
30 * Test basics
31 */
32 public void testTimestamp() {
33 Timestamp ts1 = new Timestamp();
34 Timestamp ts2 = new Timestamp();
35 assertFalse("Same timestamp", ts1.equals(ts2));
36 assertTrue("timestamp descending", ts2.compareTo(ts1) == 1);
37 assertEquals("string parse", ts2.toString(), Timestamp.valueOf(ts2.toString()).toString());
38 System.out.println("***TimestampTest: testTimestamp [SUCCESS]");
39 }
40
41 /**
42 * Test that timstamps are unique and ascending
43 */
44 public void testUnique() {
45 long last = 0L;
46 int n = 10000;
47 for(int i=0; i<n; i++) {
48 Timestamp ts = new Timestamp();
49 assertTrue("Timestamp not ascending or unique last="+last+" curr="+ts.getTimestamp(), ts.getTimestamp() > last);
50 last = ts.getTimestamp();
51 }
52 System.out.println("***TimestampTest: testUnique [SUCCESS]");
53 }
54
55 int iThread;
56 /**
57 * Test that timstamps are unique and ascending
58 */
59 public void testSync() {
60 final int n = 20;
61 final int m = 10000;
62 final java.util.Set set = java.util.Collections.synchronizedSet(new java.util.HashSet());
63 Thread[] threadArr = new Thread[n];
64 for(iThread=0; iThread<n; iThread++) {
65 threadArr[iThread] = new Thread() {
66 public void run() {
67 super.setName(""+iThread);
68 long last = 0L;
69 for(int j=0; j<m; j++) {
70 Timestamp ts = new Timestamp();
71 set.add(ts.getTimestampLong());
72 assertTrue("Timestamp not ascending or unique", ts.getTimestamp() > last);
73 last = ts.getTimestamp();
74 }
75 System.out.println("Thread #" + super.getName() + " done");
76 }
77 };
78 threadArr[iThread].start();
79 System.out.println("Started #" + iThread);
80 }
81 try { Thread.sleep(2000L); } catch( InterruptedException i) {}
82 for(int i=0; i<n; i++) {
83 try {
84 threadArr[i].join();
85 }
86 catch (InterruptedException e) {
87 }
88 }
89 assertEquals("Missing timestamps", n*m, set.size());
90 System.out.println("***TimestampTest: testSync [SUCCESS]");
91 }
92 }
syntax highlighted by Code2HTML, v. 0.9.1