Fixed incorrect path construction in JUnit tests.
[yangtools.git] / code-generator / binding-java-api-generator / src / test / java / org / opendaylight / yangtools / sal / java / api / generator / test / CompilationTestUtils.java
index c20a52dbb033d4e62766b435d4cb89492651d72b..eec32cca49ee9915cc1d6968fe074c544bb9370b 100644 (file)
@@ -11,9 +11,12 @@ import static org.junit.Assert.*;
 
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
+import java.net.URI;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -54,6 +57,17 @@ public class CompilationTestUtils {
         }
     }
 
+    /**
+     * Asserts that class contains field with fiven name and type.
+     *
+     * @param clazz
+     *            class to test
+     * @param name
+     *            field name
+     * @param type
+     *            field type
+     * @return field with given name if present in class
+     */
     static Field assertContainsField(Class<?> clazz, String name, Class<?> type) {
         try {
             Field f = clazz.getDeclaredField(name);
@@ -64,27 +78,118 @@ public class CompilationTestUtils {
         }
     }
 
-    static void assertContainsConstructor(Class<?> clazz, Class<?>... args) {
+    /**
+     * Asserts that class contains field with given name and value. Method tries
+     * to create new instance of class and get value of field. If class
+     * constructor contains any arguments, class is instantiated with null
+     * values.
+     *
+     * @param clazz
+     *            class to test
+     * @param name
+     *            name of field
+     * @param returnType
+     *            return type of field
+     * @param expectedValue
+     *            expected value of field
+     * @param constructorArgs
+     *            constructor arguments of class to test
+     */
+    static void assertContainsFieldWithValue(Class<?> clazz, String name, Class<?> returnType, Object expectedValue,
+            Class<?>... constructorArgs) {
+        Object[] initargs = null;
+        if (constructorArgs != null && constructorArgs.length > 0) {
+            initargs = new Object[constructorArgs.length];
+            for (int i = 0; i < constructorArgs.length; i++) {
+                initargs[i] = null;
+            }
+        }
+        assertContainsFieldWithValue(clazz, name, returnType, expectedValue, constructorArgs, initargs);
+    }
+
+    /**
+     * Asserts that class contains field with given name, return type and value.
+     *
+     * @param clazz
+     *            class to test
+     * @param name
+     *            name of field
+     * @param returnType
+     *            return type of field
+     * @param expectedValue
+     *            expected value of field
+     * @param constructorArgs
+     *            array of constructor arguments classes
+     * @param initargs
+     *            array of constructor values
+     */
+    static void assertContainsFieldWithValue(Class<?> clazz, String name, Class<?> returnType, Object expectedValue,
+            Class<?>[] constructorArgs, Object... initargs) {
+        Field f = assertContainsField(clazz, name, returnType);
+        try {
+            Constructor<?> c = clazz.getDeclaredConstructor(constructorArgs);
+            Object o = c.newInstance(initargs);
+            assertEquals(expectedValue, f.get(o));
+        } catch (Exception e) {
+            throw new AssertionError("Failed to perform " + name + " field test", e);
+        }
+    }
+
+    /**
+     * Asserts that class contains constructor with parameter types.
+     *
+     * @param clazz
+     *            class to test
+     * @param args
+     *            array of argument classes
+     */
+    static Constructor<?> assertContainsConstructor(Class<?> clazz, Class<?>... args) {
         try {
-            clazz.getDeclaredConstructor(args);
+            return clazz.getDeclaredConstructor(args);
         } catch (NoSuchMethodException e) {
             throw new AssertionError("Constructor with args " + Arrays.toString(args) + " does not exists in class "
                     + clazz.getSimpleName());
         }
     }
 
-    static void assertContainsMethod(Class<?> clazz, Class<?> returnType, String name, Class<?>... args) {
+    /**
+     * Asserts that class contains method with given name, return type and
+     * parameter types.
+     *
+     * @param clazz
+     *            class to test
+     * @param returnType
+     *            method return type
+     * @param name
+     *            method name
+     * @param args
+     *            array of parameter type classes
+     * @return method with given name, return type and parameter types
+     */
+    static Method assertContainsMethod(Class<?> clazz, Class<?> returnType, String name, Class<?>... args) {
         try {
             Method m = clazz.getDeclaredMethod(name, args);
             assertEquals(returnType, m.getReturnType());
+            return m;
         } catch (NoSuchMethodException e) {
             throw new AssertionError("Method " + name + " with args " + Arrays.toString(args)
                     + " does not exists in class " + clazz.getSimpleName());
         }
     }
 
-    static void assertContainsMethod(Class<?> clazz, String returnTypeStr, String name, ClassLoader loader)
-            throws Exception {
+    /**
+     * Asserts that class contains method with given name and return type.
+     *
+     * @param clazz
+     *            class to test
+     * @param returnTypeStr
+     *            name of method return type
+     * @param name
+     *            method name
+     * @param loader
+     *            current class loader
+     */
+    static void assertContainsMethod(Class<?> clazz, String returnTypeStr, String name, ClassLoader loader) {
         Class<?> returnType;
         try {
             returnType = Class.forName(returnTypeStr, true, loader);
@@ -92,14 +197,16 @@ public class CompilationTestUtils {
             assertEquals(returnType, method.getReturnType());
         } catch (ClassNotFoundException e) {
             throw new AssertionError("Return type of method '" + name + "' not found");
+        } catch (NoSuchMethodException e) {
+            throw new AssertionError("Method " + name + " does not exists in class " + clazz.getSimpleName());
         }
     }
 
     /**
-     * Check for presence of hashCode, equals and toString methods.
+     * Asserts that class contains hashCode, equals and toString methods.
      *
      * @param clazz
-     *            class to check
+     *            class to test
      */
     static void assertContainsDefaultMethods(Class<?> clazz) {
         assertContainsMethod(clazz, Integer.TYPE, "hashCode");
@@ -108,16 +215,64 @@ public class CompilationTestUtils {
     }
 
     /**
-     * Check for presence of 'public static
+     * Asserts that constructor contains check for illegal argument.
+     *
+     * @param constructor
+     *            constructor to invoke
+     * @param errorMsg
+     *            expected error message
+     * @param args
+     *            constructor arguments
+     * @throws Exception
+     */
+    static void assertContainsRestrictionCheck(Constructor<?> constructor, String errorMsg, Object... args)
+            throws Exception {
+        try {
+            constructor.newInstance(args);
+            fail("constructor invocation should fail");
+        } catch (InvocationTargetException e) {
+            Throwable cause = e.getCause();
+            assertTrue(cause instanceof IllegalArgumentException);
+            assertEquals(errorMsg, cause.getMessage());
+        }
+    }
+
+    /**
+     * Asserts that method contains check for illegal argument.
+     *
+     * @param obj
+     *            object to test (can be null, if method is static)
+     * @param method
+     *            method to invoke
+     * @param errorMsg
+     *            expected error message
+     * @param args
+     *            constructor arguments
+     * @throws Exception
+     */
+    static void assertContainsRestrictionCheck(Object obj, Method method, String errorMsg, Object... args)
+            throws Exception {
+        try {
+            method.invoke(obj, args);
+            fail("method invocation should fail");
+        } catch (InvocationTargetException e) {
+            Throwable cause = e.getCause();
+            assertTrue(cause instanceof IllegalArgumentException);
+            assertEquals(errorMsg, cause.getMessage());
+        }
+    }
+
+    /**
+     * Asserts that class contains 'public static
      * java.util.List<com.google.common.collect.Range<java.lang.Integer>>
      * getLength()' method.
      *
      * @param clazz
-     *            class to check
+     *            class to test
      */
-    static void assertContainsGetLength(Class<?> clazz) {
+    static void assertContainsGetLengthOrRange(Class<?> clazz, boolean isLength) {
         try {
-            Method m = clazz.getDeclaredMethod("getLength");
+            Method m = clazz.getDeclaredMethod(isLength ? "length" : "range");
             java.lang.reflect.Type returnType = m.getGenericReturnType();
             assertTrue("Return type of getLength method must be ParameterizedType",
                     returnType instanceof ParameterizedType);
@@ -133,26 +288,24 @@ public class CompilationTestUtils {
 
             args = pRange.getActualTypeArguments();
             assertEquals(1, args.length);
-            java.lang.reflect.Type integer = args[0];
-            assertEquals("class java.lang.Integer", integer.toString());
         } catch (NoSuchMethodException e) {
-            throw new AssertionError("Method getLength does not exists in class " + clazz.getSimpleName());
+            throw new AssertionError("Method length()/range() does not exists in class " + clazz.getSimpleName());
         }
     }
 
     /**
-     * Test if generated source implements interface.
+     * Asserts that class implements given interface.
      *
-     * @param classToTest
+     * @param clazz
      *            source to test
-     * @param ifcClass
-     *            expected interface type
+     * @param ifc
+     *            expected interface
      */
-    static void testImplementsIfc(Class<?> classToTest, Class<?> ifcClass) {
-        Class<?>[] interfaces = classToTest.getInterfaces();
+    static void assertImplementsIfc(Class<?> clazz, Class<?> ifc) {
+        Class<?>[] interfaces = clazz.getInterfaces();
         List<Class<?>> ifcsList = Arrays.asList(interfaces);
-        if (!ifcsList.contains(ifcClass)) {
-            throw new AssertionError(classToTest + " should implement " + ifcClass);
+        if (!ifcsList.contains(ifc)) {
+            throw new AssertionError(clazz + " should implement " + ifc);
         }
     }
 
@@ -160,31 +313,42 @@ public class CompilationTestUtils {
      * Test if interface generated from augment extends Augmentation interface
      * with correct generic type.
      *
-     * @param classToTest
+     * @param clazz
      *            interface generated from augment
-     * @param genericType
+     * @param genericTypeName
      *            fully qualified name of expected parameter type
      */
-    static void testAugmentation(Class<?> classToTest, String genericType) {
-        final String ifcToImplement = "interface org.opendaylight.yangtools.yang.binding.Augmentation";
-        testImplementParameterizedIfc(classToTest, ifcToImplement, genericType);
+    static void testAugmentation(Class<?> clazz, String genericTypeName) {
+        final String ifcName = "interface org.opendaylight.yangtools.yang.binding.Augmentation";
+        assertImplementsParameterizedIfc(clazz, ifcName, genericTypeName);
     }
 
-    static void testImplementParameterizedIfc(Class<?> classToTest, String ifcToImplement, String genericType) {
-        ParameterizedType augmentation = null;
-        for (java.lang.reflect.Type ifc : classToTest.getGenericInterfaces()) {
+    /**
+     * Asserts that class implements interface with given name and generic type
+     * parameter.
+     *
+     * @param clazz
+     *            class to test
+     * @param ifcName
+     *            name of interface
+     * @param genericTypeName
+     *            name of generic type
+     */
+    static void assertImplementsParameterizedIfc(Class<?> clazz, String ifcName, String genericTypeName) {
+        ParameterizedType ifcType = null;
+        for (java.lang.reflect.Type ifc : clazz.getGenericInterfaces()) {
             if (ifc instanceof ParameterizedType) {
                 ParameterizedType pt = (ParameterizedType) ifc;
-                if (ifcToImplement.equals(pt.getRawType().toString())) {
-                    augmentation = pt;
+                if (ifcName.equals(pt.getRawType().toString())) {
+                    ifcType = pt;
                 }
             }
         }
-        assertNotNull(augmentation);
+        assertNotNull(ifcType);
 
-        java.lang.reflect.Type[] typeArguments = augmentation.getActualTypeArguments();
+        java.lang.reflect.Type[] typeArguments = ifcType.getActualTypeArguments();
         assertEquals(1, typeArguments.length);
-        assertEquals("interface " + genericType, typeArguments[0].toString());
+        assertEquals("interface " + genericTypeName, typeArguments[0].toString());
     }
 
     /**
@@ -205,7 +369,15 @@ public class CompilationTestUtils {
         assertTrue(compiled);
     }
 
-    static void testFilesCount(File dir, int count) {
+    /**
+     * Asserts that directory contains exactly given count of files.
+     *
+     * @param dir
+     *            directory to test
+     * @param count
+     *            expected count of files in directory
+     */
+    static void assertFilesCount(File dir, int count) {
         File[] dirContent = dir.listFiles();
         if (dirContent == null) {
             throw new AssertionError("File " + dir + " doesn't exists or it's not a directory");
@@ -239,8 +411,8 @@ public class CompilationTestUtils {
         return result;
     }
 
-    static List<File> getSourceFiles(String path) throws FileNotFoundException {
-        final String resPath = BaseCompilationTest.class.getResource(path).getPath();
+    static List<File> getSourceFiles(String path) throws Exception {
+        final URI resPath = BaseCompilationTest.class.getResource(path).toURI();
         final File sourcesDir = new File(resPath);
         if (sourcesDir.exists()) {
             final List<File> sourceFiles = new ArrayList<>();