|
TestJavac |
|
1 /* 2 * 3 * Copyright (c) 1998-2001, The University of Sheffield. 4 * 5 * This file is part of GATE (see http://gate.ac.uk/), and is free 6 * software, licenced under the GNU Library General Public License, 7 * Version 2, June 1991 (in the distribution as file licence.html, 8 * and also available at http://gate.ac.uk/gate/licence.html). 9 * 10 * Valentin Tablan, 26/Feb/2002 11 * 12 * $Id: TestJavac.java,v 1.8 2002/04/23 10:39:03 valyt Exp $ 13 */ 14 15 package gate.util; 16 17 import java.util.*; 18 import java.io.*; 19 import junit.framework.*; 20 import java.lang.reflect.*; 21 22 import gate.*; 23 import gate.util.*; 24 25 public class TestJavac extends TestCase{ 26 /** Construction */ 27 public TestJavac(String name) { super(name); } 28 29 /** Fixture set up */ 30 public void setUp() { 31 } // setUp 32 33 /** Test suite routine for the test runner */ 34 public static Test suite() { 35 return new TestSuite(TestJavac.class); 36 } // suite 37 38 /** Jdk compiler */ 39 public void testCompiler() throws Exception { 40 Gate.init(); 41 42 String nl = Strings.getNl(); 43 String javaSource = 44 "package foo.bar;" + nl + 45 "public class Outer {" + nl + 46 "//let's make an inner class " + nl + 47 " class Adder{" + nl + 48 " public int inc(int i){" + nl + 49 " return i + 1;" + nl + 50 " }//inc(int)" + nl + 51 " }//class Adder" + nl + 52 " //let's make another inner class" + nl + 53 " class Deccer{" + nl + 54 " public int dec(int i){" + nl + 55 " return i - 1;" + nl + 56 " }//dec(int)" + nl + 57 " }//clas Deccer" + nl + 58 " //some public methods" + nl + 59 " public int inc(int i){" + nl + 60 " return new Adder().inc(i);" + nl + 61 " }" + nl + 62 " public int dec(int i){" + nl + 63 " return new Deccer().dec(i);" + nl + 64 " }" + nl + 65 " }//class Outer" + nl; 66 67 //load the class 68 Map sources = new HashMap(); 69 sources.put("foo.bar.Outer", javaSource); 70 Javac.loadClasses(sources); 71 //try to access the class 72 Class testClass = Gate.getClassLoader().loadClass("foo.bar.Outer"); 73 assertNotNull("Could not find decalred class", testClass); 74 Object testInstance = testClass.newInstance(); 75 assertNotNull("Could not instantiate declared class", testInstance); 76 Method testMethod = testClass.getDeclaredMethod( 77 "inc", 78 new Class[]{int.class}); 79 assertNotNull("Could not find declared method", testMethod); 80 Object result = testMethod.invoke(testInstance, 81 new Object[]{new Integer(1)}); 82 assertEquals("Invalid result", result, new Integer(2)); 83 84 testMethod = testClass.getDeclaredMethod( 85 "dec", 86 new Class[]{int.class}); 87 assertNotNull("Could not find declared method", testMethod); 88 result = testMethod.invoke(testInstance, new Object[]{new Integer(2)}); 89 assertEquals("Invalid result", result, new Integer(1)); 90 } 91 92 public void testCompileError() throws Exception { 93 // disable System.out so that the compiler can't splash its error on screen 94 // PrintStream syserr = System.err; 95 // PrintStream newSyserr = new PrintStream(new ByteArrayOutputStream()); 96 // System.setErr(newSyserr); 97 98 String nl = Strings.getNl(); 99 String javaSource = 100 "package foo.bar;" + nl + 101 "public class X {" + nl + 102 " //some public methods" + nl + 103 " public void foo(){" + nl + 104 " String nullStr = null;" + nl + 105 " nullStr = 123;" + nl + 106 "} " + nl + 107 " " + nl + 108 " " + nl + 109 " }//class Outer" + nl; 110 111 //load the class 112 Map sources = new HashMap(); 113 sources.put("foo.bar.X", javaSource); 114 boolean gotException = false; 115 try{ 116 Javac.loadClasses(sources); 117 }catch(GateException ge){ 118 gotException = true; 119 }finally{ 120 // newSyserr.flush(); 121 // // re-enable System.out 122 // System.setErr(syserr); 123 // newSyserr.close(); 124 } 125 assertTrue("Garbage java code did not raise an exception!", 126 gotException); 127 } 128 129 /** Debug flag */ 130 private static final boolean DEBUG = false; 131 }
|
TestJavac |
|