1   /*
2    *
3    *  Copyright (c) 1998-2005, 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.16 2005/01/11 13:51:37 ian Exp $
13   */
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    /** 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  
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        //load the class
67        Map sources = new HashMap();
68        sources.put("foo.bar.Outer", javaSource);
69        Javac.loadClasses(sources);
70        //try to access the class
71        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     //load the class
108     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 //      newSyserr.flush();
119 //      // re-enable System.out
120 //      System.setErr(syserr);
121 //      newSyserr.close();
122     }
123     assertTrue("Garbage java code did not raise an exception!",
124                gotException);
125   }
126 
127 
128   /** Debug flag */
129   private static final boolean DEBUG = false;
130 }