1 /* TestClassFactory.java */
2
3 package org.quilt.cl;
4
5 import java.io.ByteArrayOutputStream;
6 import java.io.DataInputStream;
7 import java.io.File;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.net.URL;
11 import java.net.URLClassLoader;
12
13 import junit.framework.TestCase;
14
15 /***
16 *
17 * @author <a href="ddp@apache.org">David Dixon-Peugh</a>
18 * @author <a href="jddixon@users.sourceforge.net">Jim Dixon</a> - mods Jul 2003
19
20 */
21 public class TestClassFactory extends TestCase {
22 private ClassLoader loader = new CFClassLoader();
23 private String name = null;
24 private RunTest tinter = null;
25
26 public class CFClassLoader
27 extends URLClassLoader
28 {
29 public CFClassLoader() {
30 super(new URL[0], TestClassFactory.class.getClassLoader());
31 }
32
33 public Class findClass( String className )
34 throws ClassNotFoundException
35 {
36 String classFile = className.replace('.', File.separatorChar)
37 + ".class";
38 InputStream bytecodeIS = getResourceAsStream( classFile );
39
40 byte bytecode[] = new byte[4096];
41 int len = 0;
42 try {
43 len = bytecodeIS.read(bytecode);
44 } catch (IOException e) {
45 e.printStackTrace();
46 throw new ClassNotFoundException( e.getMessage() );
47 }
48
49 Class RC = defineClass( className,
50 bytecode, 0,
51 len);
52 return RC;
53 }
54
55 /***
56 * This is where we generate bytecode for the
57 * Instrumenting Class Loader to instrument.
58 *
59 * The resourceName looks like "test/data/TestMyStuff.class"
60 * it needs to be converted to "test.data.TestMyStuff"
61 */
62 public InputStream getResourceAsStream( String resName )
63 {
64 return ClassFactory.getInstance().getResourceAsStream( resName );
65 }
66 }
67
68 /***
69 * Constructor for TestClassFactory.
70 * @param arg0
71 */
72 public TestClassFactory(String arg0) {
73 super(arg0);
74 this.name = arg0;
75 }
76
77 public void setUp() {
78 try {
79 Class clazz = loader.loadClass("test.data." + name);
80 tinter = (RunTest) clazz.newInstance();
81 } catch (Exception e) {
82 e.printStackTrace();
83 }
84 }
85
86 public void testDefault()
87 {
88 assertEquals( 2, tinter.runTest( 5 ) );
89 }
90 public void testIfThen() {
91 assertEquals( 3, tinter.runTest( 5 ) );
92 assertEquals( 5, tinter.runTest( -1 ) );
93 }
94
95 public void testSelect() {
96 assertEquals( 2, tinter.runTest( 0 ) );
97 assertEquals( 1, tinter.runTest( 1 ) );
98 assertEquals( 3, tinter.runTest( 2 ) );
99 assertEquals( 5, tinter.runTest( 3 ) );
100 assertEquals( 2, tinter.runTest( 4 ) );
101 }
102
103 public void testWhile() {
104 assertEquals( 0, tinter.runTest( 0 ) );
105 assertEquals( -1, tinter.runTest( -1 ));
106 assertEquals( 0, tinter.runTest( 1 ) );
107 assertEquals( 0, tinter.runTest( 5 ) );
108 }
109
110 public void testNPENoCatch() {
111 try {
112 tinter.runTest( 0 );
113 fail("Expecting Null Pointer Exception.");
114 } catch (NullPointerException npe) { }
115 }
116
117 public void testNPEWithCatch() {
118 int retValue = 0;
119 try {
120 retValue = tinter.runTest( 0 );
121 } catch (NullPointerException npe) {
122 fail ("Unexpected null pointer exception");
123 }
124 assertEquals ( "NPEWithCatch did not handle the exception",
125 3, retValue);
126 }
127 }
This page was automatically generated by Maven