|
TestJdk |
|
1 /* 2 * TestJdk.java 3 * 4 * Copyright (c) 1998-2001, The University of Sheffield. 5 * 6 * This file is part of GATE (see http://gate.ac.uk/), and is free 7 * software, licenced under the GNU Library General Public License, 8 * Version 2, June 1991 (in the distribution as file licence.html, 9 * and also available at http://gate.ac.uk/gate/licence.html). 10 * 11 * Hamish Cunningham, 16/Mar/00 12 * 13 * $Id: TestJdk.java,v 1.28 2001/11/14 11:31:15 hamish Exp $ 14 */ 15 16 package gate.util; 17 18 import java.util.*; 19 import java.io.*; 20 import junit.framework.*; 21 import java.net.*; 22 23 import gate.*; 24 25 /** Tests for the Jdk class and for GateClassLoader. The testReloading method 26 * reads a class from a .jar that is reached via a URL. This is called 27 * TestJdk.jar; to build it, do "make TestJdk.jar" in the build directory 28 * (the source for the class lives there, under "testpkg"). 29 */ 30 public class TestJdk extends TestCase 31 { 32 /** Debug flag */ 33 private static final boolean DEBUG = false; 34 35 /** Instance of the Jdk class */ 36 private Jdk jdk; 37 38 /** Construction */ 39 public TestJdk(String name) { super(name); } 40 41 /** Fixture set up */ 42 public void setUp() { 43 jdk = new Jdk(); 44 } // setUp 45 46 /** Jdk tool directory finder */ 47 public void testFinder() throws Exception { 48 String toolsDir = jdk.getToolsHome().getPath(); 49 assertTrue(true); 50 // assertTrue( 51 // "Tools dir was found to be: " + toolsDir, 52 // toolsDir.startsWith("w:\\jdk\\jdk1") || 53 // toolsDir.startsWith("W:\\jdk\\jdk1") || 54 // toolsDir.startsWith("W:\\JBuilder") || 55 // toolsDir.startsWith("w:\\JBuilder") || 56 // toolsDir.startsWith("H:\\JBuilder") || 57 // toolsDir.startsWith("D:\\apps\\JBuilder4\\jdk1.3\\jre\\..\\bin") || 58 // toolsDir.startsWith("W:\\apps\\JBuilder4\\jdk1.3\\jre\\..\\bin") || 59 // //toolsDir.startsWith("") || 60 // toolsDir.startsWith("h:\\JBuilder") || 61 // toolsDir.startsWith("/usr/local/") || 62 // toolsDir.startsWith("/usr/java") || 63 // toolsDir.startsWith("/usr/j2se/jre/../bin") || 64 // toolsDir.startsWith("/usr/j2se/jre/bin") || 65 // toolsDir.startsWith("/opt/") 66 // ); 67 } // testFinder() 68 69 /** Jdk compiler */ 70 public void testCompiler() throws GateException { 71 String nl = Strings.getNl(); 72 String javaSource = 73 "package gate.util;" + nl + 74 "import java.io.*;" + nl + 75 "public class X {" + nl + 76 " public X() { /*Out.println(\"X construcing\");*/ } " + nl + 77 " public static void main(String[] args)" + nl + 78 " { Out.println(\"Hello from X\"); }" + nl + 79 " public static String getSomething() { return \"something\"; }" + nl + 80 "} " + nl 81 ; 82 Gate.init(); 83 byte[] classBytes = jdk.compile(javaSource, "gate/util/X.java"); 84 assertTrue( 85 "no bytes returned from compiler", 86 classBytes != null && classBytes.length > 0 87 ); 88 89 /* if you want to write it to disk... 90 FileOutputStream outFile = 91 new FileOutputStream("z:\\gate\\classes\\gate\\util\\X.class"); 92 outFile.write(classBytes); 93 outFile.close(); 94 */ 95 96 // try and instantiate one 97 Class theXClass = jdk.defineClass("gate/util/X", classBytes); 98 Object theXObject = jdk.instantiateClass(theXClass); 99 assertTrue("couldn't instantiate the X class", theXObject != null); 100 assertTrue( 101 "X instantiated wrongly, name = " + theXObject.getClass().getName(), 102 theXObject.getClass().getName().equals("gate.util.X") 103 ); 104 105 } // testCompiler() 106 107 /** Jdk compiler test 2. Does nothing if it can't find the 108 * gate class files in the usual places. 109 */ 110 public void testCompiler2() throws GateException { 111 byte[] thisClassBytes = null; 112 String thisClassSource = null; 113 114 // try and get the bytes from the usual place on NT 115 try { 116 File sf = new File("z:\\gate\\src\\gate\\util\\X.java"); 117 File bf = new File("z:\\gate\\classes\\gate\\util\\X.class"); 118 thisClassBytes = Files.getByteArray(bf); 119 thisClassSource = Files.getString(sf); 120 } catch(IOException e) { 121 } 122 123 // try and get them from the usual Solaris place 124 if(thisClassBytes == null || thisClassBytes.length == 0) 125 try { 126 File sf = new File("/share/nlp/projects/gate/webpages/gate.ac.uk/gate"+ 127 "/src/gate/util/TestJdk.java" 128 ); 129 File bf = new File("/share/nlp/projects/gate/webpages/gate.ac.uk/gate/" 130 +"classes/gate/util/TestJdk.class" 131 ); 132 thisClassBytes = Files.getByteArray(bf); 133 thisClassSource = Files.getString(sf); 134 } catch(IOException e) { 135 136 // we couldn't find the bytes; in an ideal world we'd get it 137 // from gate.jar.... 138 return; 139 } 140 141 // compile the source 142 Jdk jdk = new Jdk(); 143 byte[] compiledBytes = 144 jdk.compile(thisClassSource, "gate/util/TestJdk.java"); 145 146 // testing the binary to see if it is the same as the one on 147 // disk doesn't work accross platforms as various strings to 148 // do with source, libraries and so on get embedded. the 149 // best test would be to do a javap and check compatibility, 150 // but life is finite, so: 151 if(true) return; 152 153 assertTrue( 154 "compiled binary doesn't equal on-disk binary", 155 compiledBytes.equals(thisClassBytes) 156 ); 157 158 } // testCompiler2() 159 160 161 /** Test reloading of classes. */ 162 public void testReloading() throws Exception { 163 164 GateClassLoader loader = Gate.getClassLoader(); 165 loader.addURL(Gate.getUrl("tests/TestJdk.jar")); 166 167 Class dummyClass1 = loader.loadClass("testpkg.Dummy"); 168 assertTrue("dummy1 is null", dummyClass1 != null); 169 Object dummyObject1 = dummyClass1.newInstance(); 170 assertTrue("dummy1 object is null", dummyObject1 != null); 171 172 Class dummyClass2 = loader.reloadClass("testpkg.Dummy"); 173 assertTrue("dummy2 is null", dummyClass2 != null); 174 Object dummyObject2 = dummyClass2.newInstance(); 175 assertTrue("dummy2 object is null", dummyObject2 != null); 176 assertTrue("dummy1 and dummy2 are the same", dummyClass1 != dummyClass2); 177 178 Class dummyClass3 = loader.reloadClass("testpkg.Dummy"); 179 assertTrue("dummy3 is null", dummyClass2 != null); 180 Object dummyObject3 = dummyClass3.newInstance(); 181 assertTrue("dummy3 object is null", dummyObject3 != null); 182 assertTrue("dummy2 and dummy3 are the same", dummyClass2 != dummyClass3); 183 184 } // testReloading 185 186 187 /** Test suite routine for the test runner */ 188 public static Test suite() { 189 return new TestSuite(TestJdk.class); 190 } // suite 191 192 public static void main(String[] args){ 193 try { 194 TestJdk testJdk = new TestJdk(""); 195 testJdk.setUp(); 196 testJdk.testCompiler(); 197 testJdk.testCompiler2(); 198 testJdk.testFinder(); 199 testJdk.testReloading(); 200 } catch (Exception e) {e.printStackTrace(Err.getPrintWriter());} 201 } 202 203 } // class TestJdk 204
|
TestJdk |
|