1
14
15 package gate.util;
16
17 import java.lang.reflect.Method;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import junit.framework.*;
22
23 import gate.Gate;
24
25 public class TestJavac extends TestCase{
26
27 public TestJavac(String name) { super(name); }
28
29
30 public void setUp() {
31 }
33
34 public static Test suite() {
35 return new TestSuite(TestJavac.class);
36 }
38
39 public void testCompiler() throws Exception {
40
41 String nl = Strings.getNl();
42 String javaSource =
43 "package foo.bar;" + nl +
44 "public class Outer {" + nl +
45 "//let's make an inner class " + nl +
46 " class Adder{" + nl +
47 " public int inc(int i){" + nl +
48 " return i + 1;" + nl +
49 " }//inc(int)" + nl +
50 " }//class Adder" + nl +
51 " //let's make another inner class" + nl +
52 " class Deccer{" + nl +
53 " public int dec(int i){" + nl +
54 " return i - 1;" + nl +
55 " }//dec(int)" + nl +
56 " }//clas Deccer" + nl +
57 " //some public methods" + nl +
58 " public int inc(int i){" + nl +
59 " return new Adder().inc(i);" + nl +
60 " }" + nl +
61 " public int dec(int i){" + nl +
62 " return new Deccer().dec(i);" + nl +
63 " }" + nl +
64 " }//class Outer" + nl;
65
66 Map sources = new HashMap();
68 sources.put("foo.bar.Outer", javaSource);
69 Javac.loadClasses(sources);
70 Class testClass = Gate.getClassLoader().loadClass("foo.bar.Outer");
72 assertNotNull("Could not find decalred class", testClass);
73 Object testInstance = testClass.newInstance();
74 assertNotNull("Could not instantiate declared class", testInstance);
75 Method testMethod = testClass.getDeclaredMethod(
76 "inc",
77 new Class[]{int.class});
78 assertNotNull("Could not find declared method", testMethod);
79 Object result = testMethod.invoke(testInstance,
80 new Object[]{new Integer(1)});
81 assertEquals("Invalid result", result, new Integer(2));
82
83 testMethod = testClass.getDeclaredMethod(
84 "dec",
85 new Class[]{int.class});
86 assertNotNull("Could not find declared method", testMethod);
87 result = testMethod.invoke(testInstance, new Object[]{new Integer(2)});
88 assertEquals("Invalid result", result, new Integer(1));
89 }
90
91 public void testCompileError() throws Exception {
92 System.err.println("Testing for a compile error:");
93 String nl = Strings.getNl();
94 String javaSource =
95 "package foo.bar;" + nl +
96 "public class X {" + nl +
97 " //some public methods" + nl +
98 " public void foo(){" + nl +
99 " String nullStr = null;" + nl +
100 "// This should cause a compile error:" + nl +
101 " nullStr = 123;" + nl +
102 "} " + nl +
103 " " + nl +
104 " " + nl +
105 " }//class Outer" + nl;
106
107 Map sources = new HashMap();
109 sources.put("foo.bar.X", javaSource);
110 boolean gotException = false;
111 try {
112 Javac.loadClasses(sources);
113 }
114 catch (GateException ge) {
115 gotException = true;
116 }
117 finally {
118 }
123 assertTrue("Garbage java code did not raise an exception!",
124 gotException);
125 }
126
127
128
129 private static final boolean DEBUG = false;
130 }