package bytecode; import static org.junit.Assert.*; import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; import org.junit.BeforeClass; import org.junit.Test; import org.objectweb.asm.Opcodes; import de.dhbwstuttgart.core.JavaTXCompiler; public class OpTest { private static String path; private static File fileToTest; private static JavaTXCompiler compiler; private static ClassLoader loader; private static Class classToTest; private static String pathToClassFile; private static Object instanceOfClass; @BeforeClass public static void setUpBeforeClass() throws Exception { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Op.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); compiler.generateBytecode(); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("Op"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); } @Test public void testClassname() { assertEquals("Op", classToTest.getName()); } @Test public void testClassModifiers() { assertEquals(Opcodes.ACC_PUBLIC, classToTest.getModifiers()); } // @Test // public void testNumberOfMethods() { // int numOfMeth = classToTest.getDeclaredMethods().length; // assertEquals(5, numOfMeth); // } // // @Test // public void testAddString() throws NoSuchMethodException, SecurityException, IllegalAccessException, // IllegalArgumentException, InvocationTargetException, InstantiationException { // Method addString = classToTest.getDeclaredMethod("addString", String.class,String.class); // String result = (String) addString.invoke(instanceOfClass, "Byte","Code"); // assertEquals("ByteCode", result); // } // // @Test // public void testAddInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, // IllegalArgumentException, InvocationTargetException, InstantiationException { // Method addInt = classToTest.getDeclaredMethod("addInt", Integer.class,Integer.class); // Integer result = (Integer) addInt.invoke(instanceOfClass, 7,3); // assertEquals(10, result); // } // // @Test // public void testAddLong() throws NoSuchMethodException, SecurityException, IllegalAccessException, // IllegalArgumentException, InvocationTargetException, InstantiationException { // Method addLong = classToTest.getDeclaredMethod("addLong", Long.class,Long.class); // Long result = (Long) addLong.invoke(instanceOfClass, 7L,3L); // assertEquals(10L, result); // } // // @Test // public void testAddFloat() throws NoSuchMethodException, SecurityException, IllegalAccessException, // IllegalArgumentException, InvocationTargetException, InstantiationException { // Method addFloat = classToTest.getDeclaredMethod("addFloat", Float.class,Float.class); // Float result = (Float) addFloat.invoke(instanceOfClass, 7f,3f); // assertEquals(10f, result); // } // // @Test // public void testAddDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, // IllegalArgumentException, InvocationTargetException, InstantiationException { // Method addDouble = classToTest.getDeclaredMethod("addDouble", Double.class,Double.class); // Double result = (Double) addDouble.invoke(instanceOfClass, 7.0,3.0); // assertEquals(10.0, result); // } // @Test // public void testAddIntLong() throws NoSuchMethodException, SecurityException, IllegalAccessException, // IllegalArgumentException, InvocationTargetException, InstantiationException { // Method add = classToTest.getDeclaredMethod("add", Integer.class,Long.class); // Long result = (Long) add.invoke(instanceOfClass, 7,3L); // assertEquals(10L, result); // } // @Test // public void testAddDLong() throws NoSuchMethodException, SecurityException, IllegalAccessException, // IllegalArgumentException, InvocationTargetException, InstantiationException { // Method add = classToTest.getDeclaredMethod("add", Double.class,Long.class); // Double result = (Double) add.invoke(instanceOfClass, 7d,3L); // assertEquals(10d, result); // } // @Test // public void testAddIntShort() throws NoSuchMethodException, SecurityException, IllegalAccessException, // IllegalArgumentException, InvocationTargetException, InstantiationException { // Method add = classToTest.getDeclaredMethod("add", Integer.class,Short.class); // Short s = 3; // Integer result = (Integer) add.invoke(instanceOfClass, 7,s); // assertEquals(10, result); // } // @Test // public void testAddIntByte() throws NoSuchMethodException, SecurityException, IllegalAccessException, // IllegalArgumentException, InvocationTargetException, InstantiationException { // Method add = classToTest.getDeclaredMethod("add", Integer.class,Byte.class); // Byte b = 3; // Integer result = (Integer) add.invoke(instanceOfClass, 7,b); // assertEquals(10, result); // } // @Test // public void testAddDFloat() throws NoSuchMethodException, SecurityException, IllegalAccessException, // IllegalArgumentException, InvocationTargetException, InstantiationException { // Method add = classToTest.getDeclaredMethod("add", Float.class,Double.class); // Double result = (Double) add.invoke(instanceOfClass, 7f,3d); // assertEquals(10d, result); // } // @Test // public void testAddIntD() throws NoSuchMethodException, SecurityException, IllegalAccessException, // IllegalArgumentException, InvocationTargetException, InstantiationException { // Method add = classToTest.getDeclaredMethod("add", Integer.class,Double.class); // Double result = (Double) add.invoke(instanceOfClass, 7,3d); // assertEquals(10d, result); // } // @Test // public void testAddShortD() throws NoSuchMethodException, SecurityException, IllegalAccessException, // IllegalArgumentException, InvocationTargetException, InstantiationException { // Method add = classToTest.getDeclaredMethod("add", Short.class,Double.class); // Short s = 7; // Double result = (Double) add.invoke(instanceOfClass, s,3d); // assertEquals(10d, result); // } // @Test // public void testAddByteD() throws NoSuchMethodException, SecurityException, IllegalAccessException, // IllegalArgumentException, InvocationTargetException, InstantiationException { // Method add = classToTest.getDeclaredMethod("add", Byte.class,Double.class); // Byte b = 7; // Double result = (Double) add.invoke(instanceOfClass, b,3d); // assertEquals(10d, result); // } @Test public void testMulInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException { Method mulInt = classToTest.getDeclaredMethod("mulInt", Integer.class,Integer.class); Integer result = (Integer) mulInt.invoke(instanceOfClass, 7,3); assertEquals(21, result); } @Test public void testMulLong() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException { Method mulLong = classToTest.getDeclaredMethod("mulLong", Long.class,Long.class); Long result = (Long) mulLong.invoke(instanceOfClass, 7L,3L); assertEquals(21L, result); } @Test public void testMulFloat() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException { Method mulFloat = classToTest.getDeclaredMethod("mulFloat", Float.class,Float.class); Float result = (Float) mulFloat.invoke(instanceOfClass, 7f,3f); assertEquals(21f, result); } @Test public void testMulDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException { Method mulDouble = classToTest.getDeclaredMethod("mulDouble", Double.class,Double.class); Double result = (Double) mulDouble.invoke(instanceOfClass, 7.0,3.0); assertEquals(21.0, result); } }