001: package test.tck;
002:
003: import gov.nist.javax.sip.address.AddressFactoryImpl;
004:
005: import java.io.File;
006: import java.io.FileInputStream;
007: import java.io.PrintWriter;
008: import java.io.StringWriter;
009: import java.util.Enumeration;
010: import java.util.Properties;
011:
012: import javax.sip.SipFactory;
013: import javax.sip.address.AddressFactory;
014: import javax.sip.header.HeaderFactory;
015: import javax.sip.message.MessageFactory;
016:
017: import junit.framework.TestCase;
018: import junit.framework.TestResult;
019:
020: import org.apache.log4j.Appender;
021: import org.apache.log4j.BasicConfigurator;
022: import org.apache.log4j.ConsoleAppender;
023: import org.apache.log4j.FileAppender;
024: import org.apache.log4j.Logger;
025: import org.apache.log4j.SimpleLayout;
026:
027: public class TestHarness extends TestCase {
028:
029: private static final String PATH_GOV_NIST = "gov.nist";
030:
031: protected static final String IMPLEMENTATION_PATH = "javax.sip.tck.PATH";
032:
033: protected static final String ABORT_ON_FAIL = "javax.sip.tck.ABORT_ON_FAIL";
034:
035: protected static final String LOG_FILE_NAME = "javax.sip.tck.LOG_FILE";
036:
037: protected static final String LOCAL_ADDRESS = "127.0.0.1";
038:
039: protected static final int TI_PORT = 5060;
040:
041: protected static final int RI_PORT = 6050;
042:
043: // Keep these static but initialize from the constructor to allow
044: // changing from the GUI
045: protected static String logFileName = "tcklog.txt";
046:
047: protected static String path = null;
048:
049: protected static PrintWriter printWriter;
050:
051: protected static boolean abortOnFail = true;
052:
053: // this flag is set to false if there is any failure throughout the test
054: // cycle
055: // on either side of the protocol. It helps account for failures that are
056: // not triggered in the
057: // main test thread. It has to be initialized before each run.
058: private static boolean testPassed = true;
059:
060: protected static MessageFactory tiMessageFactory;
061:
062: protected static HeaderFactory tiHeaderFactory;
063:
064: protected static AddressFactory tiAddressFactory;
065:
066: protected static MessageFactory riMessageFactory;
067:
068: protected static HeaderFactory riHeaderFactory;
069:
070: protected static AddressFactory riAddressFactory;
071:
072: protected static int testCounter;
073:
074: protected static SipFactory riFactory;
075:
076: protected static SipFactory tiFactory;
077:
078: protected TestResult testResult;
079:
080: private static Logger logger = Logger.getLogger("test.tck");
081:
082: private static String currentMethodName;
083:
084: private static String currentClassName;
085:
086: protected static Appender console = new ConsoleAppender(
087: new SimpleLayout());
088:
089: static {
090: try {
091: Properties tckProperties = new Properties();
092:
093: tckProperties.load(TestHarness.class.getClassLoader()
094: .getResourceAsStream("tck.properties"));
095: //tckProperties.load(new FileInputStream("tck.properties"));
096: Enumeration props = tckProperties.propertyNames();
097: while (props.hasMoreElements()) {
098: String propname = (String) props.nextElement();
099: System.setProperty(propname, tckProperties
100: .getProperty(propname));
101: }
102:
103: path = System.getProperties().getProperty(
104: IMPLEMENTATION_PATH);
105: String flag = System.getProperties().getProperty(
106: ABORT_ON_FAIL);
107:
108: String lf = System.getProperties().getProperty(
109: LOG_FILE_NAME);
110: if (lf != null)
111: logFileName = lf;
112: abortOnFail = (flag != null && flag
113: .equalsIgnoreCase("true"));
114:
115: // JvB: init log4j
116: //PropertyConfigurator.configure("log4j.properties");
117:
118: BasicConfigurator.configure();
119:
120: // If already created a print writer then just use it.
121: if (lf != null)
122: logger.addAppender(new FileAppender(new SimpleLayout(),
123: logFileName));
124: else
125: logger.addAppender(new FileAppender(new SimpleLayout(),
126: "tckoutput.txt"));
127: } catch (Exception ex) {
128: throw new RuntimeException(ex);
129: }
130:
131: }
132:
133: private static void println(String messageToPrint) {
134: logger.info(messageToPrint);
135: }
136:
137: /**
138: * Default constructor
139: */
140: protected TestHarness() {
141:
142: }
143:
144: protected String getImplementationPath() {
145: return System.getProperties().getProperty(IMPLEMENTATION_PATH,
146: "gov.nist");
147: }
148:
149: public TestHarness(String name) {
150: this (name, false); // default: disable auto-dialog
151: }
152:
153: protected TestHarness(String name, boolean autoDialog) {
154: super (name);
155: this .testResult = new TestResult();
156:
157: getRIFactories(autoDialog);
158: getTIFactories();
159:
160: }
161:
162: private static void logSuccess(String message) {
163: testCounter++;
164: Throwable throwable = new Throwable();
165: StackTraceElement frameset[] = throwable.getStackTrace();
166: StackTraceElement frame = frameset[2];
167: String className = frame.getClassName();
168: //int ind = className.lastIndexOf(".");
169: //if (ind != -1) {
170: // className = className.substring(ind + 1);
171: //}
172:
173: logger.info(className + ":" + frame.getMethodName() + "("
174: + frame.getFileName() + ":" + frame.getLineNumber()
175: + ")" + " : Status = passed ! ");
176: String methodName = frame.getMethodName();
177:
178: if (!(currentMethodName != null
179: && methodName.equals(currentMethodName) && currentClassName
180: .equals(className))) {
181: currentClassName = className;
182: currentMethodName = methodName;
183: System.out.println("\n");
184: System.out
185: .print(currentClassName + ":" + currentMethodName);
186: }
187: }
188:
189: private static void logSuccess() {
190: Throwable throwable = new Throwable();
191: StackTraceElement frameset[] = throwable.getStackTrace();
192: StackTraceElement frame = frameset[2];
193: String className = frame.getClassName();
194: //int ind = className.lastIndexOf(".");
195: //if (ind != -1) {
196: // className = className.substring(ind + 1);
197: //}
198: logger.info(className + ":" + frame.getMethodName()
199: + ": Status = passed ! ");
200: String methodName = frame.getMethodName();
201:
202: if (!(currentMethodName != null
203: && methodName.equals(currentMethodName) && currentClassName
204: .equals(className))) {
205: currentClassName = className;
206: currentMethodName = methodName;
207: System.out.println("\n");
208: System.out
209: .print(currentClassName + ":" + currentMethodName);
210: }
211: }
212:
213: private static void logFailureDetails(String reason) {
214: Throwable throwable = new Throwable();
215: StackTraceElement frameset[] = throwable.getStackTrace();
216: StackTraceElement frame = frameset[2];
217: String className = frame.getClassName();
218: logFailure(className, frame.getMethodName(), reason);
219:
220: }
221:
222: private static void logFailure(String className, String methodName,
223: String reason) {
224: println(" Test in function " + className + ":" + methodName
225: + " failed because of " + reason);
226:
227: StringWriter stringWriter = new StringWriter();
228: new Exception().printStackTrace(new PrintWriter(stringWriter));
229: println(stringWriter.getBuffer().toString());
230:
231: testPassed = false;
232: if (abortOnFail) {
233: new Exception().printStackTrace();
234: System.exit(0);
235: }
236: }
237:
238: private static void logFailure(String reason) {
239: logFailureDetails(reason);
240: }
241:
242: public static void assertTrue(boolean cond) {
243: if (cond) {
244: logSuccess();
245: } else {
246: logFailure("assertTrue failed");
247: }
248: if (!cond) {
249: new Exception().printStackTrace();
250: fail("assertion failure");
251: }
252:
253: TestCase.assertTrue(cond);
254: }
255:
256: public static void assertTrue(String diagnostic, boolean cond) {
257: if (cond) {
258: logSuccess("assertTrue " + diagnostic);
259: } else {
260: logFailure(diagnostic);
261: }
262: if (!cond) {
263: new Exception(diagnostic).printStackTrace();
264: fail(diagnostic + " : Assertion Failure ");
265:
266: }
267:
268: TestCase.assertTrue(diagnostic, cond);
269: }
270:
271: public static void assertEquals(Object me, Object him) {
272: if (me == him) {
273: logSuccess();
274: } else if (me == null && him != null) {
275: logFailure("assertEquals failed");
276:
277: } else if (me != null && him == null) {
278: logFailure("assertEquals failed");
279:
280: } else if (!me.equals(him)) {
281: logFailure("assertEquals failed");
282:
283: }
284: TestCase.assertEquals(me, him);
285: }
286:
287: public static void assertEquals(String me, String him) {
288: if (me == him) {
289: logSuccess();
290: } else if (me == null && him != null) {
291: logFailure("assertEquals failed");
292:
293: } else if (me != null && him == null) {
294: logFailure("assertEquals failed");
295:
296: } else if (!me.equals(him)) {
297: logFailure("assertEquals failed");
298:
299: }
300: TestCase.assertEquals(me, him);
301: }
302:
303: public static void assertEquals(String reason, Object me, Object him) {
304: if (me == him) {
305: logSuccess("assertEquals : " + reason);
306: } else if (me == null && him != null) {
307: logFailure("assertEquals failed");
308: } else if (me != null && him == null) {
309: logFailure("assertEquals failed");
310: } else if (!me.equals(him)) {
311: logFailure(reason);
312: }
313: TestCase.assertEquals(reason, me, him);
314: }
315:
316: public static void assertEquals(String reason, String me, String him) {
317: if (me == him) {
318: logSuccess("assertEquals " + reason);
319: } else if (me == null && him != null) {
320: logFailure("assertEquals failed");
321: } else if (me != null && him == null) {
322: logFailure("assertEquals failed");
323: } else if (!me.equals(him)) {
324: logFailure("assertEquals failed");
325: }
326: TestCase.assertEquals(reason, me, him);
327:
328: }
329:
330: public static void assertNotNull(String reason, Object thing) {
331: if (thing != null) {
332: logSuccess("assertNotNull " + reason);
333: } else {
334: logFailure(reason);
335: }
336: TestCase.assertNotNull(reason, thing);
337: }
338:
339: public static void assertNull(String reason, Object thing) {
340: if (thing == null) {
341: logSuccess("assertNull " + reason);
342: } else {
343: logFailure(reason);
344: }
345: TestCase.assertNull(reason, thing);
346: }
347:
348: public static void assertSame(String diagnostic, Object thing,
349: Object thingie) {
350: if (thing == thingie) {
351: logSuccess("assertSame " + diagnostic);
352: } else {
353: logFailure(diagnostic);
354: }
355: TestCase.assertSame(diagnostic, thing, thingie);
356: }
357:
358: public static void fail(String message) {
359: logFailure(message);
360:
361: TestCase.fail(message);
362: }
363:
364: public static void fail(String message, Exception ex) {
365: logFailure(message);
366: logger.error(message, ex);
367: }
368:
369: public static void fail() {
370: logFailure("Unknown reason for failure. Check logs for more info.");
371: new Exception().printStackTrace();
372: TestCase.fail();
373: }
374:
375: public static void checkImplementsInterface(
376: Class implementationClass, Class jainInterface) {
377:
378: assertTrue(jainInterface.toString() + " is_assignable_from "
379: + implementationClass.toString(), jainInterface
380: .isAssignableFrom(implementationClass));
381: }
382:
383: public static boolean implements Interface(
384: Class implementationClass, Class jainInterface) {
385: return jainInterface.isAssignableFrom(implementationClass);
386: }
387:
388: static void getTIFactories() {
389: try {
390: tiFactory = SipFactory.getInstance();
391:
392: // JvB: need this! but before setting path
393: tiFactory.resetFactory();
394:
395: // if no TI path is specified on the command line, then assume
396: // RI self-test mode
397:
398: //String tiPathName = System.getProperty( IMPLEMENTATION_PATH, PATH_RI_HELPER );
399: String tiPathName = System.getProperty(IMPLEMENTATION_PATH,
400: "gov.nist");
401:
402: // Yes this does access implementation classes but we have to do
403: // things
404: // this way for self test. v1.2 only assumes one instance of
405: // factories per vendor
406: // per jvm.
407: tiFactory.setPathName(tiPathName);
408:
409: tiAddressFactory = tiFactory.createAddressFactory();
410: tiHeaderFactory = tiFactory.createHeaderFactory();
411: tiMessageFactory = tiFactory.createMessageFactory();
412: } catch (Exception ex) {
413: ex.printStackTrace();
414: System.out
415: .println("Cannot get TI factories -- cannot proceed! Bailing");
416: System.exit(0);
417: }
418: // Cannot sensibly proceed so bail out.
419: if (tiAddressFactory == null || tiMessageFactory == null
420: || tiHeaderFactory == null) {
421: System.out
422: .println("Cannot get TI factories -- cannot proceed! Bailing!!");
423: System.exit(0);
424: }
425: }
426:
427: static void getRIFactories(boolean autoDialog) {
428: try {
429: riFactory = SipFactory.getInstance();
430: if (riFactory == null) {
431: throw new TckInternalError("could not get SipFactory");
432: }
433: riFactory.resetFactory();
434:
435: // Testing against the RI.
436: riFactory.setPathName(PATH_GOV_NIST);
437:
438: riAddressFactory = riFactory.createAddressFactory();
439:
440: assertTrue("RI must be gov.nist implementation",
441: riAddressFactory instanceof AddressFactoryImpl);
442:
443: riHeaderFactory = riFactory.createHeaderFactory();
444: riMessageFactory = riFactory.createMessageFactory();
445: } catch (Exception ex) {
446: throw new TckInternalError("Could not get factories");
447: }
448: }
449:
450: public void logTestCompleted() {
451:
452: logger.info(this .getName() + " Completed");
453:
454: }
455:
456: public void logTestCompleted(String info) {
457: logger.info(this .getName() + ":" + info + " Completed");
458:
459: }
460:
461: /**
462: * Returns a properties object containing all RI settings. The result from
463: * this method is passed to the SipFactory when creating the RI Stack
464: */
465: public static Properties getRiProperties(boolean autoDialog) {
466: // TODO collect all system properties
467: // prefixed javax.sip.tck.ri and add them to the local
468: // properties object
469:
470: Properties properties = new Properties();
471:
472: // IP_ADDRESS is deprecated as of jsip 1.2.
473: // Each listening point associated with a stack has its own IP address.
474: properties.setProperty("javax.sip.STACK_NAME", "RiStack");
475: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "0");
476:
477: // properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "32");
478: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
479: "logs/riDebugLog.txt");
480: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
481: "logs/riMessageLog.txt");
482:
483: // JvB: Most TCK tests dont work well with automatic dialog support
484: // enabled
485: // Disable it for the moment
486: properties.setProperty("javax.sip.AUTOMATIC_DIALOG_SUPPORT",
487: autoDialog ? "ON" : "OFF");
488:
489: // JvB: for testing of ACK to non-2xx
490: properties
491: .setProperty(
492: "gov.nist.javax.sip.PASS_INVITE_NON_2XX_ACK_TO_LISTENER",
493: "true");
494: // For testing sending of stateless null keepalive messages.
495: //@see test.tck.msgflow.SipProviderTest.testSendNullRequest
496: properties.setProperty("javax.sip.OUTBOUND_PROXY",
497: LOCAL_ADDRESS + ":" + TI_PORT + "/udp");
498:
499: return properties;
500: }
501:
502: /**
503: * Returns a properties object containing all TI settings. The result from
504: * this method is passed to the SipFactory when creating the TI Stack
505: *
506: *
507: */
508: public static Properties getTiProperties() {
509: // TODO collect all system properties
510: // prefixed javax.sip.tck.ti and add them to the local
511: // properties object
512:
513: Properties properties = new Properties();
514: // IP_ADDRESS is deprecated as of jsip 1.2.
515: // Each listening point associated with a stack has its own IP address.
516: // properties.setProperty("javax.sip.IP_ADDRESS", LOCAL_ADDRESS);
517: properties.setProperty("javax.sip.STACK_NAME", "TiStack");
518:
519: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "0");
520: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
521: "logs/tiDebugLog.txt");
522: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
523: "logs/tiMessageLog.txt");
524: // For testing sending of stateless null keepalive messages.
525: //@see test.tck.msgflow.SipProviderTest.testSendNullRequest
526: properties.setProperty("javax.sip.OUTBOUND_PROXY",
527: LOCAL_ADDRESS + ":" + RI_PORT + "/udp");
528:
529: return properties;
530: }
531:
532: public void setUp() throws Exception {
533: testPassed = true;
534: }
535:
536: public void tearDown() throws Exception {
537: assertTrue("Test failed. See log for details.", testPassed);
538: }
539: }
|