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 a9f85265964bfa3148e2ac3de7667930ad8bc66c..eec32cca49ee9915cc1d6968fe074c544bb9370b 100644 (file)
@@ -13,8 +13,10 @@ 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;
@@ -141,9 +143,9 @@ public class CompilationTestUtils {
      * @param args
      *            array of argument classes
      */
-    static void assertContainsConstructor(Class<?> clazz, Class<?>... args) {
+    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());
@@ -162,11 +164,13 @@ public class CompilationTestUtils {
      *            method name
      * @param args
      *            array of parameter type classes
+     * @return method with given name, return type and parameter types
      */
-    static void assertContainsMethod(Class<?> clazz, Class<?> returnType, String name, Class<?>... args) {
+    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());
@@ -199,7 +203,7 @@ public class CompilationTestUtils {
     }
 
     /**
-     * Asserts that class containes hashCode, equals and toString methods.
+     * Asserts that class contains hashCode, equals and toString methods.
      *
      * @param clazz
      *            class to test
@@ -210,6 +214,54 @@ public class CompilationTestUtils {
         assertContainsMethod(clazz, String.class, "toString");
     }
 
+    /**
+     * 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>>
@@ -218,9 +270,9 @@ public class CompilationTestUtils {
      * @param clazz
      *            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);
@@ -236,10 +288,8 @@ 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());
         }
     }
 
@@ -361,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<>();