001: /* TestQCLJars.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: * At the moment this is identical to TestQuiltClassLoader except that
012: * test-data.jar is on the classpath and test-data-classes/ is not.
013: *
014: * @author <a href="jddixon@users.sourceforge.net">Jim Dixon</a>
015: */
016: public class TestQCLJars extends TestCase {
017:
018: private URL[] cp = null;
019:
020: private String[] delegating = {
021: // EMPTY -- nothing additional to defaults
022: };
023: private String[] include = {
024: // EMPTY -- nothing being instrumented
025: };
026: private String[] exclude = { "AnonymousClass",
027: "AnonymousClass2Catches", "InnerClass", "NestedTryBlocks",
028: "PrivateClass", "SuperClass", "Wimple" };
029: private QuiltClassLoader qLoader = null;
030:
031: public TestQCLJars(String name) {
032: super (name);
033: }
034:
035: public void setUp() {
036: // TAKE CARE NO TERMINATING SLASH (/) ON THE JAR
037: File sam1 = new File("target/test-data.jar");
038: String fullPath1 = sam1.getAbsolutePath();
039:
040: File sam2 = new File("target/classes");
041: String fullPath2 = sam2.getAbsolutePath() + "/";
042: File sam3 = new File("target/test-classes");
043: String fullPath3 = sam3.getAbsolutePath() + "/";
044: try {
045: // Terminating slash is required. Relative paths don't
046: // work.
047: URL[] samples = { new URL("file://" + fullPath1),
048: new URL("file://" + fullPath2),
049: new URL("file://" + fullPath3) };
050: cp = samples;
051: } catch (MalformedURLException e) {
052: e.printStackTrace();
053: fail("problem creating class path");
054: }
055: qLoader = new QuiltClassLoader(cp, null, // parent
056: delegating, // delegated classes
057: include, // being instrumented
058: exclude); // do NOT instrument
059: }
060:
061: /////////////////////////////////////////////////////////////////
062: // AnonymousClass comes from test-data.jar, as does BasicLoad,
063: // used in testInvokeTestData
064: // //////////////////////////////////////////////////////////////
065: public void testLoader() {
066: Class a1 = null;
067: Class a2 = null;
068: try {
069: a1 = qLoader.loadClass("AnonymousClass");
070: } catch (ClassNotFoundException e) {
071: e.printStackTrace();
072: fail("Error loading AnonymousClass using loadClass");
073: }
074: try {
075: a2 = qLoader.loadClass("AnonymousClass");
076: } catch (ClassNotFoundException e) {
077: fail("Error loading AnonymousClass using loadClass");
078: }
079: assertNotNull("qLoader returned null", a1);
080: assertNotNull("qLoader returned null", a2);
081: assertEquals("second load returned a different class",
082: (Object) a1, (Object) a2);
083: }
084:
085: public void testInvokeTestData() {
086: Class a1 = null;
087: Class a2 = null;
088: try {
089: a1 = qLoader.loadClass("AnonymousClass");
090: } catch (ClassNotFoundException e) {
091: e.printStackTrace();
092: fail("Error loading AnonymousClass using loadClass");
093: }
094: try {
095: a2 = qLoader.loadClass("BasicLoad");
096: } catch (ClassNotFoundException e) {
097: fail("Error loading BasicLoad using loadClass");
098: }
099: RunTest rt = null;
100: // exercise AnonymousClass ////////////////////////
101: try {
102: rt = (RunTest) a1.newInstance();
103: } catch (InstantiationException e) {
104: fail("error instantiating loaded AnonymousClass");
105: } catch (IllegalAccessException e) {
106: fail("error instantiating loaded AnonymousClass");
107: }
108: // AnonymousClass.runTest(x) returns x
109: assertEquals("AnonymousClass isn't working", 47, rt.runTest(47));
110:
111: // exercise BasicLoad ////////////////////////
112: try {
113: rt = (RunTest) a2.newInstance();
114: } catch (InstantiationException e) {
115: fail("error instantiating loaded BasicLoad");
116: } catch (IllegalAccessException e) {
117: fail("error instantiating loaded BasicLoad");
118: }
119: // BasicLoad.runTest(x) returns x*x
120: assertEquals("BasicLoad isn't working", 49, rt.runTest(7));
121: }
122: }
|