001: /* TestSamples.java */
002: package org.quilt.cl;
003:
004: import java.io.*;
005: import java.lang.Class;
006: import java.lang.reflect.*;
007: import java.net.*;
008: import junit.framework.*;
009:
010: /**
011: * Checks to make sure that the samples in test-data will load
012: * and execute correctly. Some of them wouldn't ;-)
013: *
014: * @author <a href="jddixon@users.sourceforge.net">Jim Dixon</a>
015: */
016: public class TestSamples extends TestCase {
017:
018: public TestSamples(String name) {
019: super (name);
020: }
021:
022: public void setUp() {
023: }
024:
025: private RunTest loadAsRunTest(String name) {
026: Class clazz = null;
027: try {
028: clazz = Class.forName(name);
029: } catch (ClassNotFoundException e) {
030: e.printStackTrace();
031: fail("exception loading " + name + " using loadClass");
032: }
033: RunTest rt = null;
034: try {
035: rt = (RunTest) clazz.newInstance();
036: } catch (InstantiationException e) {
037: fail("InstantiationException instantiating loaded class "
038: + name);
039: } catch (IllegalAccessException e) {
040: fail("IllegalAccessException instantiating loaded class "
041: + name);
042: } catch (ClassCastException e) {
043: fail("ClassCastException instantiating loaded class "
044: + name);
045: }
046: return rt;
047: }
048:
049: public void testInvokeTestData() {
050: RunTest rt = loadAsRunTest("AnonymousClass");
051: // AnonymousClass.runTest(x) returns x
052: assertEquals("AnonymousClass isn't working", 47, rt.runTest(47));
053:
054: rt = loadAsRunTest("BasicLoad");
055: // BasicLoad.runTest(x) returns x*x
056: assertEquals("BasicLoad isn't working", 49, rt.runTest(7));
057:
058: rt = loadAsRunTest("ComplicatedConstructor");
059: assertEquals("ComplicatedConstructor isn't working", 61, rt
060: .runTest(3));
061: // XXX causes ClassCastException when instantiating
062: rt = loadAsRunTest("ExceptionLoad");
063: // ExceptionLoad.runTest(x) also returns x*x
064: assertEquals("ExceptionLoad isn't working", 121, rt.runTest(11));
065: }
066:
067: public void testInvokeTestData2() {
068: RunTest rt = loadAsRunTest("Finally");
069: // Finally.runTest(x) returns -1
070: assertEquals("Finally isn't working", -1, rt.runTest(11));
071: assertEquals("Finally isn't working", -1, rt.runTest(1));
072:
073: rt = loadAsRunTest("Finally2Catches");
074: // what Finally.runTest(x) returns is a bit complicated ...
075: assertEquals("Finally2Catches isn't working", 3600, rt
076: .runTest(11));
077:
078: rt = loadAsRunTest("InnerClass");
079: // InnerClass.runTest(x) also returns x*x
080: assertEquals("InnerClass isn't working", 9, rt.runTest(3));
081:
082: rt = loadAsRunTest("Looper");
083: assertEquals("Looper isn't working", 127008000, rt.runTest(5));
084:
085: rt = loadAsRunTest("NestedTryBlocks");
086: assertEquals("NestedTryBlocks isn't working", 22, rt.runTest(7));
087:
088: rt = loadAsRunTest("OddSwitches");
089: // we like to play
090: assertEquals(91, rt.runTest(1001));
091: assertEquals(31, rt.runTest(3));
092: assertEquals(9, rt.runTest(9));
093: assertEquals(101, rt.runTest(1005));
094: assertEquals(-41, rt.runTest(-1));
095: assertEquals(-3, rt.runTest(-51));
096: assertEquals(7, rt.runTest(-2));
097: }
098:
099: public void testInvokeTestData3() {
100: RunTest rt;
101: try {
102: Class clazz = Class.forName("PrivateClass");
103: rt = (RunTest) clazz.newInstance();
104: fail("Expected IllegalAccessException");
105: } catch (Exception e) {
106: ; // ignore it
107: }
108:
109: rt = loadAsRunTest("StaticInit");
110: assertEquals("StaticInit isn't working", 10, rt.runTest(7));
111:
112: rt = loadAsRunTest("SuperClass");
113: // returns 3*x
114: assertEquals("SuperClass isn't working", 21, rt.runTest(7));
115:
116: rt = loadAsRunTest("SwitchLoad");
117: assertEquals("SwitchLoad isn't working", 42, rt.runTest(7));
118:
119: }
120: }
|