001 /*
002 *
003 * Copyright (c) 1995-2010, The University of Sheffield. See the file
004 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
005 *
006 * This file is part of GATE (see http://gate.ac.uk/), and is free
007 * software, licenced under the GNU Library General Public License,
008 * Version 2, June 1991 (in the distribution as file licence.html,
009 * and also available at http://gate.ac.uk/gate/licence.html).
010 *
011 * Valentin Tablan, 26/Feb/2002
012 *
013 * $Id: TestJavac.java 12006 2009-12-01 17:24:28Z thomas_heitz $
014 */
015
016 package gate.util;
017
018 import java.lang.reflect.Method;
019 import java.util.HashMap;
020 import java.util.Map;
021
022 import junit.framework.*;
023
024 import gate.Gate;
025
026 public class TestJavac extends TestCase{
027 /** Construction */
028 public TestJavac(String name) { super(name); }
029
030 /** Fixture set up */
031 public void setUp() {
032 } // setUp
033
034 /** Test suite routine for the test runner */
035 public static Test suite() {
036 return new TestSuite(TestJavac.class);
037 } // suite
038
039 /** Jdk compiler */
040 public void testCompiler() throws Exception {
041
042 String nl = Strings.getNl();
043 String javaSource =
044 "package foo.bar;" + nl +
045 "public class Outer {" + nl +
046 "//let's make an inner class " + nl +
047 " class Adder{" + nl +
048 " public int inc(int i){" + nl +
049 " return i + 1;" + nl +
050 " }//inc(int)" + nl +
051 " }//class Adder" + nl +
052 " //let's make another inner class" + nl +
053 " class Deccer{" + nl +
054 " public int dec(int i){" + nl +
055 " return i - 1;" + nl +
056 " }//dec(int)" + nl +
057 " }//clas Deccer" + nl +
058 " //some public methods" + nl +
059 " public int inc(int i){" + nl +
060 " return new Adder().inc(i);" + nl +
061 " }" + nl +
062 " public int dec(int i){" + nl +
063 " return new Deccer().dec(i);" + nl +
064 " }" + nl +
065 " }//class Outer" + nl;
066
067 //load the class
068 Map sources = new HashMap();
069 sources.put("foo.bar.Outer", javaSource);
070 Javac.loadClasses(sources);
071 //try to access the class
072 Class testClass = Gate.getClassLoader().loadClass("foo.bar.Outer");
073 assertNotNull("Could not find decalred class", testClass);
074 Object testInstance = testClass.newInstance();
075 assertNotNull("Could not instantiate declared class", testInstance);
076 Method testMethod = testClass.getDeclaredMethod(
077 "inc",
078 new Class[]{int.class});
079 assertNotNull("Could not find declared method", testMethod);
080 Object result = testMethod.invoke(testInstance,
081 new Object[]{new Integer(1)});
082 assertEquals("Invalid result", result, new Integer(2));
083
084 testMethod = testClass.getDeclaredMethod(
085 "dec",
086 new Class[]{int.class});
087 assertNotNull("Could not find declared method", testMethod);
088 result = testMethod.invoke(testInstance, new Object[]{new Integer(2)});
089 assertEquals("Invalid result", result, new Integer(1));
090 }
091
092 public void testCompileError() throws Exception {
093 System.err.println("Testing for a compile error:");
094 String nl = Strings.getNl();
095 String javaSource =
096 "package foo.bar;" + nl +
097 "public class X {" + nl +
098 " //some public methods" + nl +
099 " public void foo(){" + nl +
100 " String nullStr = null;" + nl +
101 "// This should cause a compile error:" + nl +
102 " nullStr = 123;" + nl +
103 "} " + nl +
104 " " + nl +
105 " " + nl +
106 " }//class Outer" + nl;
107
108 //load the class
109 Map sources = new HashMap();
110 sources.put("foo.bar.X", javaSource);
111 boolean gotException = false;
112 try {
113 Javac.loadClasses(sources);
114 }
115 catch (GateException ge) {
116 gotException = true;
117 }
118 finally {
119 // newSyserr.flush();
120 // // re-enable System.out
121 // System.setErr(syserr);
122 // newSyserr.close();
123 }
124 assertTrue("Garbage java code did not raise an exception!",
125 gotException);
126 }
127
128
129 /** Debug flag */
130 private static final boolean DEBUG = false;
131 }
|