KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > pat > internal > TestCaseBase


1 package org.objectweb.celtix.pat.internal;
2
3 import java.util.*;
4
5 import org.objectweb.celtix.Bus;
6 import org.objectweb.celtix.BusException;
7
8 public abstract class TestCaseBase {
9     private static boolean initialized;
10     
11     protected String JavaDoc wsdlPath;
12
13     protected String JavaDoc serviceName;
14
15     protected String JavaDoc portName;
16
17     protected String JavaDoc operationName;
18
19     protected String JavaDoc hostname;
20
21     protected String JavaDoc hostport;
22
23     protected int packetSize = 1;
24
25     protected boolean usingTime;
26
27     protected int amount = 1;
28
29     protected String JavaDoc wsdlNameSpace;
30
31     protected Bus bus;
32
33     protected List<TestResult> results = new ArrayList<TestResult>();
34
35     protected boolean usePipe;
36
37     private int numberOfThreads;
38     
39     private String JavaDoc name;
40
41     private String JavaDoc[] args;
42
43     private String JavaDoc faultReason = "no error";
44
45
46     
47     public TestCaseBase() {
48         this("DEFAULT TESTCASE", null);
49     }
50
51     public TestCaseBase(String JavaDoc cname) {
52         this(cname, null);
53     }
54
55     public TestCaseBase(String JavaDoc cname, String JavaDoc[] arg) {
56         this.name = cname;
57         this.args = arg;
58     }
59
60     public abstract void initTestData();
61
62     public void init() throws Exception JavaDoc {
63         initBus();
64         initTestData();
65     }
66
67     private void processArgs() {
68         int count = 0;
69         int argc = args.length;
70         while (count < argc) {
71             if ("-WSDL".equals(args[count])) {
72                 wsdlPath = args[count + 1];
73                 count += 2;
74             } else if ("-Service".equals(args[count])) {
75                 serviceName = args[count + 1];
76                 count += 2;
77             } else if ("-Pipe".equals(args[count])) {
78                 usePipe = true;
79                 count++;
80             } else if ("-Port".equals(args[count])) {
81                 portName = args[count + 1];
82                 count += 2;
83             } else if ("-Operation".equals(args[count])) {
84                 operationName = args[count + 1];
85                 count += 2;
86             } else if ("-BasedOn".equals(args[count])) {
87                 if ("Time".equals(args[count + 1])) {
88                     usingTime = true;
89                 }
90                 count += 2;
91             } else if ("-Amount".equals(args[count])) {
92                 amount = Integer.parseInt(args[count + 1]);
93                 count += 2;
94             } else if ("-Threads".equals(args[count])) {
95                 numberOfThreads = Integer.parseInt(args[count + 1]);
96                 count += 2;
97             } else if ("-HostName".equals(args[count])) {
98                 hostname = args[count + 1];
99                 count += 2;
100             } else if ("-HostPort".equals(args[count])) {
101                 hostport = args[count + 1];
102                 count += 2;
103             } else if ("-PacketSize".equals(args[count])) {
104                 packetSize = Integer.parseInt(args[count + 1]);
105                 count += 2;
106             } else {
107                 count++;
108             }
109         }
110     }
111
112     private boolean validate() {
113         if (wsdlNameSpace == null || wsdlNameSpace.trim().equals("")) {
114             System.out.println("WSDL name space is not specified");
115             faultReason = "Missing WSDL name space";
116             return false;
117         }
118         if (serviceName == null || serviceName.trim().equals("")) {
119             System.out.println("Service name is not specified");
120             faultReason = "Missing Service name";
121             return false;
122         }
123         if (portName == null || portName.trim().equals("")) {
124             System.out.println("Port name is not specified");
125             faultReason = "Missing Port name";
126             return false;
127         }
128         if (wsdlPath == null || wsdlPath.trim().equals("")) {
129             System.out.println("WSDL path is not specifed");
130             faultReason = "Missing WSDL path";
131             return false;
132         }
133         return true;
134     }
135
136     // for the celtix init , here do nothing
137
private void initBus() throws BusException {
138         bus = Bus.init();
139     }
140
141     public void tearDown() throws BusException {
142         if (bus != null) {
143             System.out.println("Bus is going to shutdown");
144             bus.shutdown(true);
145             System.out.println("Bus shutdown");
146             bus = null;
147         }
148     }
149
150     protected void setUp() throws Exception JavaDoc {
151        
152         clearTestResults();
153         printTitle();
154         printSetting("Default Setting: ");
155         processArgs();
156         if (!validate()) {
157             System.out.println("Configure Exception!" + faultReason);
158             System.exit(1);
159         }
160         init();
161         printSetting("Runtime Setting: ");
162     }
163
164     public void initialize() {
165         try {
166             if (!initialized) {
167                 setUp();
168             }
169             initialized = true;
170
171
172             System.out.println("TestCase " + name + " is warming up the jit. (5 sec/200 iterations)");
173             long endTime = System.currentTimeMillis() + 5000;
174             getPort();
175             int count = 0;
176             while (System.currentTimeMillis() < endTime || count < 200) {
177                 count++;
178                 doJob();
179             }
180         } catch (Exception JavaDoc e) {
181             e.printStackTrace();
182         }
183     }
184
185     public abstract void doJob();
186
187     public abstract void getPort();
188
189     protected void internalTestRun(String JavaDoc caseName) throws Exception JavaDoc {
190         int numberOfInvocations = 0;
191         long startTime = System.currentTimeMillis();
192         long endTime = startTime + amount * 1000;
193         if (usingTime) {
194             while (System.currentTimeMillis() < endTime) {
195                 doJob();
196                 numberOfInvocations++;
197             }
198         } else {
199             for (int i = 0; i < amount; i++) {
200                 doJob();
201                 numberOfInvocations++;
202             }
203         }
204         endTime = System.currentTimeMillis();
205         TestResult testResult = new TestResult(caseName, this);
206         testResult.compute(startTime, endTime, numberOfInvocations);
207         addTestResult(testResult);
208     }
209
210     public void testRun() throws Exception JavaDoc {
211         if (numberOfThreads == 0) {
212             internalTestRun(name);
213         }
214         List<Thread JavaDoc> threadList = new ArrayList<Thread JavaDoc>();
215         for (int i = 0; i < numberOfThreads; i++) {
216             TestRunner runner = new TestRunner("No." + i + " TestRunner", this);
217             Thread JavaDoc thread = new Thread JavaDoc(runner, "RunnerThread No." + i);
218             thread.start();
219             threadList.add(thread);
220         }
221
222         for (Iterator iter = threadList.iterator(); iter.hasNext();) {
223             Thread JavaDoc thread = (Thread JavaDoc) iter.next();
224             try {
225                 thread.join();
226             } catch (InterruptedException JavaDoc e) {
227                 e.printStackTrace();
228             }
229         }
230     }
231
232     public void run() {
233         try {
234             System.out.println("TestCase " + name + " is running");
235             testRun();
236             tearDown();
237             System.out.println("TestCase " + name + " is finished");
238         } catch (Exception JavaDoc e) {
239             e.printStackTrace();
240         }
241     }
242
243     protected void clearTestResults() {
244         results.clear();
245     }
246
247     protected void addTestResult(TestResult result) {
248         results.add(result);
249     }
250
251     public List getTestResults() {
252         return results;
253     }
254
255     public abstract void printUsage();
256
257     public void printSetting(String JavaDoc settingType) {
258         System.out.println(settingType + " [Service] -- > " + serviceName);
259         System.out.println(settingType + " [Port] -- > " + portName);
260         System.out.println(settingType + " [Operation] -- > " + operationName);
261         System.out.println(settingType + " [Threads] -- > " + numberOfThreads);
262         System.out.println(settingType + " [Packet Size] -- > " + packetSize
263                            + " packet(s) ");
264         if (usingTime) {
265             System.out.println(settingType + " [Running] --> " + amount + " (secs)");
266         } else {
267             System.out.println(settingType + " [Running] --> " + amount
268                                + " (invocations)");
269         }
270     }
271
272     public void printTitle() {
273         System.out.println(" ---------------------------------");
274         System.out.println(name + " Client (JAVA Version)");
275         System.out.println(" ---------------------------------");
276     }
277
278     public void setWSDLNameSpace(String JavaDoc nameSpace) {
279         this.wsdlNameSpace = nameSpace;
280     }
281
282     public void setWSDLPath(String JavaDoc wpath) {
283         this.wsdlPath = wsdlPath;
284     }
285
286     public void setServiceName(String JavaDoc sname) {
287         this.serviceName = sname;
288     }
289
290     public void setPortName(String JavaDoc pname) {
291         this.portName = pname;
292     }
293
294     public void setOperationName(String JavaDoc oname) {
295         this.operationName = oname;
296     }
297
298     public String JavaDoc getServiceName() {
299         return this.serviceName;
300     }
301
302     public String JavaDoc getPortName() {
303         return this.portName;
304     }
305
306     public String JavaDoc getOperationName() {
307         return this.operationName;
308     }
309
310     public String JavaDoc getName() {
311         return this.name;
312     }
313
314     public Bus getBus() {
315         return this.bus;
316     }
317
318
319 }
320
Popular Tags