001: package org.objectweb.celtix.pat.internal;
002:
003: import java.util.*;
004:
005: import org.objectweb.celtix.Bus;
006: import org.objectweb.celtix.BusException;
007:
008: public abstract class TestCaseBase {
009: private static boolean initialized;
010:
011: protected String wsdlPath;
012:
013: protected String serviceName;
014:
015: protected String portName;
016:
017: protected String operationName;
018:
019: protected String hostname;
020:
021: protected String hostport;
022:
023: protected int packetSize = 1;
024:
025: protected boolean usingTime;
026:
027: protected int amount = 1;
028:
029: protected String wsdlNameSpace;
030:
031: protected Bus bus;
032:
033: protected List<TestResult> results = new ArrayList<TestResult>();
034:
035: protected boolean usePipe;
036:
037: private int numberOfThreads;
038:
039: private String name;
040:
041: private String[] args;
042:
043: private String faultReason = "no error";
044:
045: public TestCaseBase() {
046: this ("DEFAULT TESTCASE", null);
047: }
048:
049: public TestCaseBase(String cname) {
050: this (cname, null);
051: }
052:
053: public TestCaseBase(String cname, String[] arg) {
054: this .name = cname;
055: this .args = arg;
056: }
057:
058: public abstract void initTestData();
059:
060: public void init() throws Exception {
061: initBus();
062: initTestData();
063: }
064:
065: private void processArgs() {
066: int count = 0;
067: int argc = args.length;
068: while (count < argc) {
069: if ("-WSDL".equals(args[count])) {
070: wsdlPath = args[count + 1];
071: count += 2;
072: } else if ("-Service".equals(args[count])) {
073: serviceName = args[count + 1];
074: count += 2;
075: } else if ("-Pipe".equals(args[count])) {
076: usePipe = true;
077: count++;
078: } else if ("-Port".equals(args[count])) {
079: portName = args[count + 1];
080: count += 2;
081: } else if ("-Operation".equals(args[count])) {
082: operationName = args[count + 1];
083: count += 2;
084: } else if ("-BasedOn".equals(args[count])) {
085: if ("Time".equals(args[count + 1])) {
086: usingTime = true;
087: }
088: count += 2;
089: } else if ("-Amount".equals(args[count])) {
090: amount = Integer.parseInt(args[count + 1]);
091: count += 2;
092: } else if ("-Threads".equals(args[count])) {
093: numberOfThreads = Integer.parseInt(args[count + 1]);
094: count += 2;
095: } else if ("-HostName".equals(args[count])) {
096: hostname = args[count + 1];
097: count += 2;
098: } else if ("-HostPort".equals(args[count])) {
099: hostport = args[count + 1];
100: count += 2;
101: } else if ("-PacketSize".equals(args[count])) {
102: packetSize = Integer.parseInt(args[count + 1]);
103: count += 2;
104: } else {
105: count++;
106: }
107: }
108: }
109:
110: private boolean validate() {
111: if (wsdlNameSpace == null || wsdlNameSpace.trim().equals("")) {
112: System.out.println("WSDL name space is not specified");
113: faultReason = "Missing WSDL name space";
114: return false;
115: }
116: if (serviceName == null || serviceName.trim().equals("")) {
117: System.out.println("Service name is not specified");
118: faultReason = "Missing Service name";
119: return false;
120: }
121: if (portName == null || portName.trim().equals("")) {
122: System.out.println("Port name is not specified");
123: faultReason = "Missing Port name";
124: return false;
125: }
126: if (wsdlPath == null || wsdlPath.trim().equals("")) {
127: System.out.println("WSDL path is not specifed");
128: faultReason = "Missing WSDL path";
129: return false;
130: }
131: return true;
132: }
133:
134: // for the celtix init , here do nothing
135: private void initBus() throws BusException {
136: bus = Bus.init();
137: }
138:
139: public void tearDown() throws BusException {
140: if (bus != null) {
141: System.out.println("Bus is going to shutdown");
142: bus.shutdown(true);
143: System.out.println("Bus shutdown");
144: bus = null;
145: }
146: }
147:
148: protected void setUp() throws Exception {
149:
150: clearTestResults();
151: printTitle();
152: printSetting("Default Setting: ");
153: processArgs();
154: if (!validate()) {
155: System.out.println("Configure Exception!" + faultReason);
156: System.exit(1);
157: }
158: init();
159: printSetting("Runtime Setting: ");
160: }
161:
162: public void initialize() {
163: try {
164: if (!initialized) {
165: setUp();
166: }
167: initialized = true;
168:
169: System.out.println("TestCase " + name
170: + " is warming up the jit. (5 sec/200 iterations)");
171: long endTime = System.currentTimeMillis() + 5000;
172: getPort();
173: int count = 0;
174: while (System.currentTimeMillis() < endTime || count < 200) {
175: count++;
176: doJob();
177: }
178: } catch (Exception e) {
179: e.printStackTrace();
180: }
181: }
182:
183: public abstract void doJob();
184:
185: public abstract void getPort();
186:
187: protected void internalTestRun(String caseName) throws Exception {
188: int numberOfInvocations = 0;
189: long startTime = System.currentTimeMillis();
190: long endTime = startTime + amount * 1000;
191: if (usingTime) {
192: while (System.currentTimeMillis() < endTime) {
193: doJob();
194: numberOfInvocations++;
195: }
196: } else {
197: for (int i = 0; i < amount; i++) {
198: doJob();
199: numberOfInvocations++;
200: }
201: }
202: endTime = System.currentTimeMillis();
203: TestResult testResult = new TestResult(caseName, this );
204: testResult.compute(startTime, endTime, numberOfInvocations);
205: addTestResult(testResult);
206: }
207:
208: public void testRun() throws Exception {
209: if (numberOfThreads == 0) {
210: internalTestRun(name);
211: }
212: List<Thread> threadList = new ArrayList<Thread>();
213: for (int i = 0; i < numberOfThreads; i++) {
214: TestRunner runner = new TestRunner("No." + i
215: + " TestRunner", this );
216: Thread thread = new Thread(runner, "RunnerThread No." + i);
217: thread.start();
218: threadList.add(thread);
219: }
220:
221: for (Iterator iter = threadList.iterator(); iter.hasNext();) {
222: Thread thread = (Thread) iter.next();
223: try {
224: thread.join();
225: } catch (InterruptedException e) {
226: e.printStackTrace();
227: }
228: }
229: }
230:
231: public void run() {
232: try {
233: System.out.println("TestCase " + name + " is running");
234: testRun();
235: tearDown();
236: System.out.println("TestCase " + name + " is finished");
237: } catch (Exception e) {
238: e.printStackTrace();
239: }
240: }
241:
242: protected void clearTestResults() {
243: results.clear();
244: }
245:
246: protected void addTestResult(TestResult result) {
247: results.add(result);
248: }
249:
250: public List getTestResults() {
251: return results;
252: }
253:
254: public abstract void printUsage();
255:
256: public void printSetting(String settingType) {
257: System.out.println(settingType + " [Service] -- > "
258: + serviceName);
259: System.out.println(settingType + " [Port] -- > " + portName);
260: System.out.println(settingType + " [Operation] -- > "
261: + operationName);
262: System.out.println(settingType + " [Threads] -- > "
263: + numberOfThreads);
264: System.out.println(settingType + " [Packet Size] -- > "
265: + packetSize + " packet(s) ");
266: if (usingTime) {
267: System.out.println(settingType + " [Running] --> "
268: + amount + " (secs)");
269: } else {
270: System.out.println(settingType + " [Running] --> "
271: + amount + " (invocations)");
272: }
273: }
274:
275: public void printTitle() {
276: System.out.println(" ---------------------------------");
277: System.out.println(name + " Client (JAVA Version)");
278: System.out.println(" ---------------------------------");
279: }
280:
281: public void setWSDLNameSpace(String nameSpace) {
282: this .wsdlNameSpace = nameSpace;
283: }
284:
285: public void setWSDLPath(String wpath) {
286: this .wsdlPath = wsdlPath;
287: }
288:
289: public void setServiceName(String sname) {
290: this .serviceName = sname;
291: }
292:
293: public void setPortName(String pname) {
294: this .portName = pname;
295: }
296:
297: public void setOperationName(String oname) {
298: this .operationName = oname;
299: }
300:
301: public String getServiceName() {
302: return this .serviceName;
303: }
304:
305: public String getPortName() {
306: return this .portName;
307: }
308:
309: public String getOperationName() {
310: return this .operationName;
311: }
312:
313: public String getName() {
314: return this .name;
315: }
316:
317: public Bus getBus() {
318: return this.bus;
319: }
320:
321: }
|