001: /* TestClassFactory.java */
002:
003: package org.quilt.cl;
004:
005: import java.io.ByteArrayOutputStream;
006: import java.io.DataInputStream;
007: import java.io.File;
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.net.URL;
011: import java.net.URLClassLoader;
012:
013: import junit.framework.TestCase;
014:
015: /**
016: *
017: * @author <a href="ddp@apache.org">David Dixon-Peugh</a>
018: * @author <a href="jddixon@users.sourceforge.net">Jim Dixon</a> - mods Jul 2003
019:
020: */
021: public class TestClassFactory extends TestCase {
022: private ClassLoader loader = new CFClassLoader();
023: private String name = null;
024: private RunTest tinter = null;
025:
026: public class CFClassLoader extends URLClassLoader {
027: public CFClassLoader() {
028: super (new URL[0], TestClassFactory.class.getClassLoader());
029: }
030:
031: public Class findClass(String className)
032: throws ClassNotFoundException {
033: String classFile = className.replace('.',
034: File.separatorChar)
035: + ".class";
036: InputStream bytecodeIS = getResourceAsStream(classFile);
037:
038: byte bytecode[] = new byte[4096];
039: int len = 0;
040: try {
041: len = bytecodeIS.read(bytecode);
042: } catch (IOException e) {
043: e.printStackTrace();
044: throw new ClassNotFoundException(e.getMessage());
045: }
046:
047: Class RC = defineClass(className, bytecode, 0, len);
048: return RC;
049: }
050:
051: /**
052: * This is where we generate bytecode for the
053: * Instrumenting Class Loader to instrument.
054: *
055: * The resourceName looks like "test/data/TestMyStuff.class"
056: * it needs to be converted to "test.data.TestMyStuff"
057: */
058: public InputStream getResourceAsStream(String resName) {
059: return ClassFactory.getInstance().getResourceAsStream(
060: resName);
061: }
062: }
063:
064: /**
065: * Constructor for TestClassFactory.
066: * @param arg0
067: */
068: public TestClassFactory(String arg0) {
069: super (arg0);
070: this .name = arg0;
071: }
072:
073: public void setUp() {
074: try {
075: Class clazz = loader.loadClass("test.data." + name);
076: tinter = (RunTest) clazz.newInstance();
077: } catch (Exception e) {
078: e.printStackTrace();
079: }
080: }
081:
082: public void testDefault() {
083: assertEquals(2, tinter.runTest(5));
084: }
085:
086: public void testIfThen() {
087: assertEquals(3, tinter.runTest(5));
088: assertEquals(5, tinter.runTest(-1));
089: }
090:
091: public void testSelect() {
092: assertEquals(2, tinter.runTest(0));
093: assertEquals(1, tinter.runTest(1));
094: assertEquals(3, tinter.runTest(2));
095: assertEquals(5, tinter.runTest(3));
096: assertEquals(2, tinter.runTest(4));
097: }
098:
099: public void testWhile() {
100: assertEquals(0, tinter.runTest(0));
101: assertEquals(-1, tinter.runTest(-1));
102: assertEquals(0, tinter.runTest(1));
103: assertEquals(0, tinter.runTest(5));
104: }
105:
106: public void testNPENoCatch() {
107: try {
108: tinter.runTest(0);
109: fail("Expecting Null Pointer Exception.");
110: } catch (NullPointerException npe) {
111: }
112: }
113:
114: public void testNPEWithCatch() {
115: int retValue = 0;
116: try {
117: retValue = tinter.runTest(0);
118: } catch (NullPointerException npe) {
119: fail("Unexpected null pointer exception");
120: }
121: assertEquals("NPEWithCatch did not handle the exception", 3,
122: retValue);
123: }
124: }
|