1 /*
2 * Copyright �, Aegeus Technology Limited.
3 * All rights reserved.
4 */
5 package jsdsi.sexp;
6
7 import java.util.Iterator;
8 import java.util.LinkedList;
9 import java.util.List;
10
11 import jsdsi.Provider;
12 import junit.framework.Test;
13 import junit.framework.TestCase;
14 import junit.framework.TestSuite;
15
16 /***
17 * @author Sean Radford
18 * @version $Revision: 1.2.2.1 $ $Date: 2004/11/16 12:22:53 $
19 */
20 public abstract class ThreadedObjectParsingTest extends TestCase {
21
22 static {
23 Provider.install();
24 }
25
26 private String type;
27 private int NUM_OBJECTS;
28 private int NUM_THREADS;
29 private List byteList;
30 private List runners;
31 private Exception parsingException;
32
33 ThreadedObjectParsingTest(String type, int numOfObjects, int numOfThreads) {
34 super("testThreadParsing");
35 this.type = type;
36 this.NUM_OBJECTS = numOfObjects;
37 this.NUM_THREADS = numOfThreads;
38 }
39
40 /***
41 * @see TestCase#setUp()
42 */
43 protected void setUp() throws Exception {
44 super.setUp();
45 this.byteList = new LinkedList();
46 this.runners = new LinkedList();
47 System.out.println("Setting up test: "+this.type);
48 for (int i = 0; i < NUM_OBJECTS; i++) {
49 Object o = createObject();
50 this.byteList.add(getBytes(o));
51 }
52 System.out.println("Validity creation complete.");
53 }
54
55
56 public static Test suite() {
57 TestSuite s = new TestSuite("ThreadedObjectParsingTest");
58 s.addTest(new ValidityThreadedObjectParsingTestCase(1000, 20));
59 s.addTest(new CertificateThreadedObjectParsingTestCase(100, 20));
60 return s;
61 }
62
63
64 /*** Creates the object to be later parsed (for the test) **/
65 protected abstract Object createObject() throws Exception;
66 /*** Gets the bytes for an object for byteList */
67 protected abstract byte[] getBytes(Object object);
68 /*** Generates an Object from a byte[] - used for the actual parsing test */
69 abstract Object generateObject(byte[] bytes) throws Exception;
70 public void testThreadParsing() throws Exception {
71 for (int i = 0; i < NUM_THREADS; i++) {
72 this.runners.add(new Runner(this, this.byteList));
73 }
74 Iterator it = this.runners.iterator();
75 while (it.hasNext()) {
76 ((Runner) it.next()).start();
77 }
78 boolean done = false;
79 startagain:
80 while (!done) {
81 it = this.runners.iterator();
82 while (it.hasNext()) {
83 Runner runner = (Runner) it.next();
84 if (runner.isAlive()) {
85 Thread.sleep(1000);
86 continue startagain;
87 }
88 }
89 done = true;
90 }
91 if (parsingException != null) {
92 fail(parsingException.toString());
93 }
94 }
95
96
97 class Runner extends Thread {
98 private ThreadedObjectParsingTest test;
99 private List byteList;
100 public Runner(ThreadedObjectParsingTest test, List byteList) {
101 super();
102 this.test = test;
103 this.byteList = byteList;
104 }
105 /***
106 * @see java.lang.Runnable#run()
107 */
108 public void run() {
109 System.out.println("Running test for "+this.test.type);
110 Iterator it = this.byteList.iterator();
111 int index = -1;
112 while (it.hasNext() && this.test.parsingException == null) {
113 index++;
114 byte[] bytes = (byte[]) it.next();
115 try {
116 Object obj = this.test.generateObject(bytes);
117 } catch (Exception e) {
118 this.test.parsingException = e;
119 }
120
121 }
122 System.out.println("Test complete for "+this.test.type);
123 }
124 }
125
126 }
This page was automatically generated by Maven