Author: chatellier Date: 2009-03-11 14:19:34 +0000 (Wed, 11 Mar 2009) New Revision: 1938 Modified: isis-fish/trunk/src/test/java/fr/ifremer/isisfish/util/CompileHelperTest.java Log: Move test to junit 4 (improve test) Modified: isis-fish/trunk/src/test/java/fr/ifremer/isisfish/util/CompileHelperTest.java =================================================================== --- isis-fish/trunk/src/test/java/fr/ifremer/isisfish/util/CompileHelperTest.java 2009-03-11 11:29:21 UTC (rev 1937) +++ isis-fish/trunk/src/test/java/fr/ifremer/isisfish/util/CompileHelperTest.java 2009-03-11 14:19:34 UTC (rev 1938) @@ -1,35 +1,41 @@ -/* - * *##% Copyright (C) 2006 - 2009 Code Lutin, Cédric Pineau, Benjamin Poussin - * - * This program is free software; you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation; either version 2 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 59 Temple - * Place - Suite 330, Boston, MA 02111-1307, USA. ##% - */ +/* *##% + * Copyright (C) 2009 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%*/ package fr.ifremer.isisfish.util; -import junit.framework.TestCase; -import org.codelutin.util.FileUtil; - import java.io.File; +import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.util.FileUtil; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import fr.ifremer.TestUtils; + /** - * ClassUtilTest. + * CompileHelperTest. * * Created: 12 janv. 2006 16:20:33 * @@ -40,12 +46,68 @@ * Last update: $Date$ * by : $Author$ */ -public class CompileHelperTest extends TestCase { +public class CompileHelperTest { - public void testCompile() throws Exception { + /** Class logger */ + private static Log log = LogFactory.getLog(CompileHelperTest.class); + + /** + * Called once before all class tests. + * @throws Exception + */ + @BeforeClass + public static void init() throws Exception { + TestUtils.init(); + } + + /** + * Return a Java code content in Java 6. + * + * @param name Class name + * + * @return java code + */ + protected String getFirstClassContent(String name) { + StringBuffer content = new StringBuffer(); + content.append("public class " + name + " {"); + content.append(" /** Java 6 code */"); + content.append(" public boolean isEmpty(String s) {"); + content.append(" return s.isEmpty();"); + content.append(" }"); + content.append("}"); + return content.toString(); + } + + /** + * Return a Java code content in Java 6 that extends a class. + * + * @param name + * @param extendsName + * @return java code + */ + protected String getSecondClassContent(String name, String extendsName) { + StringBuffer content = new StringBuffer(); + content.append("public class " + name + " extends " + extendsName + " {"); + content.append(" @Override"); + content.append(" public boolean isEmpty(String s) {"); + content.append(" return false;"); + content.append(" }"); + content.append("}"); + return content.toString(); + } + + /** + * Test une compilation de classe et une + * instantiation de la classe compilée. + * + * @throws IOException + * @throws ClassNotFoundException + */ + @Test + public void testCompile() throws IOException, ClassNotFoundException { File f = File.createTempFile("testCompile", ".java"); String filename = FileUtil.basename(f, ".java"); - String code = "public class " + filename + " {}"; + String code = getFirstClassContent(filename); FileUtil.writeString(f, code); List<File> classpath = new ArrayList<File>(); @@ -56,49 +118,64 @@ // essai de chargement de la classe URL[] cp = new URL[] { f.getParentFile().toURI().toURL() }; - //URL[] cp = new URL[] { f.getParentFile().toURL() }; ClassLoader cl = new URLClassLoader(cp); - Class c = cl.loadClass(filename); - System.out.println("class name: " + c.getName()); - assertEquals(filename, c.getName()); + Class<?> c = cl.loadClass(filename); + log.info("class name: " + c.getName()); + Assert.assertEquals(filename, c.getName()); + + // delete + File fclass = new File(f.getAbsolutePath().replaceAll("java$", "class")); + fclass.delete(); + f.delete(); } - public void testCompileDepend() throws Exception { + /** + * Test de compiler deux classes dont l'un etend l'autre. + * Puis instancie les deux. + * @throws IOException + * @throws ClassNotFoundException + */ + @Test + public void testCompileDepend() throws IOException, ClassNotFoundException { + File fA = File.createTempFile("testCompileA", ".java"); String filenameA = FileUtil.basename(fA, ".java"); - String codeA = "public class " + filenameA + " {}"; + String codeA = getFirstClassContent(filenameA); FileUtil.writeString(fA, codeA); File fB = File.createTempFile("testCompileB", ".java"); String filenameB = FileUtil.basename(fB, ".java"); - String codeB = "public class " + filenameB + " extends " + filenameA - + " {}"; + String codeB = getSecondClassContent(filenameB, filenameA); FileUtil.writeString(fB, codeB); File dest = new File(fB.getParentFile(), "testCompile"); List<File> classpath = new ArrayList<File>(); classpath.add(fB.getParentFile()); - CompileHelper.compile(classpath, Collections.singletonList(fB), dest, - null); + CompileHelper.compile(classpath, Collections.singletonList(fB), + dest, null); - { - // essai de chargement de la classe - URL[] cp = new URL[] { dest.toURI().toURL() }; - //URL[] cp = new URL[] { dest.toURL() }; - ClassLoader cl = new URLClassLoader(cp); - Class c = cl.loadClass(filenameB); - System.out.println("class name: " + c.getName()); - assertEquals(filenameB, c.getName()); - } - { - // essai de chargement de la classe - URL[] cp = new URL[] { dest.toURI().toURL() }; - //URL[] cp = new URL[] { dest.toURL() }; - ClassLoader cl = new URLClassLoader(cp); - Class c = cl.loadClass(filenameA); - System.out.println("class name: " + c.getName()); - assertEquals(filenameA, c.getName()); - } + // essai de chargement de la classe + URL[] cp = new URL[] { dest.toURI().toURL() }; + ClassLoader cl = new URLClassLoader(cp); + Class<?> c = cl.loadClass(filenameB); + log.info("class name: " + c.getName()); + Assert.assertEquals(filenameB, c.getName()); + + // essai de chargement de la classe + cp = new URL[] { dest.toURI().toURL() }; + //URL[] cp = new URL[] { dest.toURL() }; + cl = new URLClassLoader(cp); + c = cl.loadClass(filenameA); + log.info("class name: " + c.getName()); + Assert.assertEquals(filenameA, c.getName()); + + // delete + File fclassA = new File(fA.getAbsolutePath().replaceAll("java$", "class")); + fclassA.delete(); + File fclassB = new File(fB.getAbsolutePath().replaceAll("java$", "class")); + fclassB.delete(); + fA.delete(); + fB.delete(); } }