X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-java-api-generator%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fjava%2Fapi%2Fgenerator%2Ftest%2FCompilationTestUtils.java;h=fb5569b05e464eb147c7befe17b6e52468931583;hb=50855450c6fac03ca45636ab7d41f7c8dccfb02e;hp=ad6e250319e7b5b457c8a400d2ab0278f728a47c;hpb=8d19626640b8f6cab9ed3e0f9908fa3c416956e5;p=mdsal.git diff --git a/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/test/CompilationTestUtils.java b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/test/CompilationTestUtils.java index ad6e250319..fb5569b05e 100644 --- a/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/test/CompilationTestUtils.java +++ b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/test/CompilationTestUtils.java @@ -7,6 +7,7 @@ */ package org.opendaylight.mdsal.binding.java.api.generator.test; +import static com.google.common.base.Preconditions.checkState; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -25,12 +26,13 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import javax.tools.Diagnostic; import javax.tools.JavaCompiler; import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import javax.tools.ToolProvider; -public class CompilationTestUtils { +public final class CompilationTestUtils { public static final String FS = File.separator; static final String BASE_PKG = "org.opendaylight.yang.gen.v1"; @@ -39,7 +41,7 @@ public class CompilationTestUtils { static final String GENERATOR_OUTPUT_PATH = TEST_PATH + FS + "src"; static final File GENERATOR_OUTPUT_DIR = new File(GENERATOR_OUTPUT_PATH); - static final String COMPILER_OUTPUT_PATH = TEST_PATH + FS + "bin"; + private static final String COMPILER_OUTPUT_PATH = TEST_PATH + FS + "bin"; static final File COMPILER_OUTPUT_DIR = new File(COMPILER_OUTPUT_PATH); static final String AUGMENTATION = "interface org.opendaylight.yangtools.yang.binding.Augmentation"; @@ -50,10 +52,27 @@ public class CompilationTestUtils { static final String NS_BAZ = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "baz" + FS + "rev131008"; static final String NS_BUG5882 = BASE_PATH + FS + "urn" + FS + "yang" + FS + "foo" + FS + "rev160102"; + private CompilationTestUtils() { + + } + + static File compilerOutput(final String name) { + return createDirectory(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + name); + } + + static File generatorOutput(final String name) { + return createDirectory(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + name); + } + + private static File createDirectory(final String path) { + final File ret = new File(path); + checkState(ret.mkdir(), "Failed to create test directory %s", path); + return ret; + } + /** - * Method to clean resources. It is manually called at the end of each test - * instead of marking it with @After annotation to prevent removing - * generated code if test fails. + * Method to clean resources. It is manually called at the end of each test instead of marking it with @After + * annotation to prevent removing generated code if test fails. */ static void cleanUp(final File... resourceDirs) { for (File resourceDir : resourceDirs) { @@ -64,45 +83,35 @@ public class CompilationTestUtils { } /** - * Asserts that class contains field with fiven name and type. + * Asserts that class contains field with given name and type. * - * @param clazz - * class to test - * @param name - * field name - * @param type - * field 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(final Class clazz, final String name, final Class type) { try { - Field f = clazz.getDeclaredField(name); - assertEquals(type, f.getType()); - return f; + Field field = clazz.getDeclaredField(name); + assertEquals(type, field.getType()); + return field; } catch (NoSuchFieldException e) { - throw new AssertionError("Field " + name + " does not exist in class " + clazz.getSimpleName()); + throw new AssertionError("Field " + name + " does not exist in class " + clazz.getSimpleName(), e); } } /** - * 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. + * 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 + * @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(final Class clazz, final String name, final Class returnType, final Object expectedValue, - final Class... constructorArgs) { + static void assertContainsFieldWithValue(final Class clazz, final String name, final Class returnType, + final Object expectedValue, final Class... constructorArgs) { Object[] initargs = null; if (constructorArgs != null && constructorArgs.length > 0) { initargs = new Object[constructorArgs.length]; @@ -116,30 +125,24 @@ public class CompilationTestUtils { /** * 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 + * @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(final Class clazz, final String name, final Class returnType, final Object expectedValue, - final Class[] constructorArgs, final Object... initargs) { - Field f = assertContainsField(clazz, name, returnType); - f.setAccessible(true); + static void assertContainsFieldWithValue(final Class clazz, final String name, final Class returnType, + final Object expectedValue, final Class[] constructorArgs, final Object... initargs) { + Field field = assertContainsField(clazz, name, returnType); + field.setAccessible(true); final Object obj; - if ((f.getModifiers() & Modifier.STATIC) == 0) { + if ((field.getModifiers() & Modifier.STATIC) == 0) { try { - Constructor c = clazz.getDeclaredConstructor(constructorArgs); - obj = c.newInstance(initargs); - } catch (Exception e) { + Constructor cls = clazz.getDeclaredConstructor(constructorArgs); + obj = cls.newInstance(initargs); + } catch (ReflectiveOperationException e) { throw new AssertionError("Failed to instantiate object for " + clazz, e); } } else { @@ -147,7 +150,7 @@ public class CompilationTestUtils { } try { - assertEquals(expectedValue, f.get(obj)); + assertEquals(expectedValue, field.get(obj)); } catch (IllegalArgumentException | IllegalAccessException e) { throw new AssertionError("Failed to get field " + name + " of class " + clazz, e); } @@ -156,75 +159,65 @@ public class CompilationTestUtils { /** * Asserts that class contains constructor with parameter types. * - * @param clazz - * class to test - * @param args - * array of argument classes + * @param clazz class to test + * @param args array of argument classes */ static Constructor assertContainsConstructor(final Class clazz, final Class... args) { try { return clazz.getDeclaredConstructor(args); } catch (NoSuchMethodException e) { throw new AssertionError("Constructor with args " + Arrays.toString(args) + " does not exists in class " - + clazz.getSimpleName()); + + clazz.getSimpleName(), e); } } /** - * Asserts that class contains method with given name, return type and - * parameter types. + * 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 + * @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(final Class clazz, final Class returnType, final String name, final Class... args) { + static Method assertContainsMethod(final Class clazz, final Class returnType, final String name, + final Class... args) { try { - Method m = clazz.getDeclaredMethod(name, args); - assertEquals(returnType, m.getReturnType()); - return m; + Method method = clazz.getDeclaredMethod(name, args); + assertEquals(returnType, method.getReturnType()); + return method; } catch (NoSuchMethodException e) { throw new AssertionError("Method " + name + " with args " + Arrays.toString(args) - + " does not exists in class " + clazz.getSimpleName()); + + " does not exists in class " + clazz.getSimpleName(), e); } } /** * 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 + * @param clazz class to test + * @param returnTypeStr name of method return type + * @param name method name + * @param loader current class loader */ - static void assertContainsMethod(final Class clazz, final String returnTypeStr, final String name, final ClassLoader loader) { + static void assertContainsMethod(final Class clazz, final String returnTypeStr, final String name, + final ClassLoader loader) { Class returnType; try { returnType = Class.forName(returnTypeStr, true, loader); Method method = clazz.getMethod(name); assertEquals(returnType, method.getReturnType()); } catch (ClassNotFoundException e) { - throw new AssertionError("Return type of method '" + name + "' not found"); + throw new AssertionError("Return type of method '" + name + "' not found", e); } catch (NoSuchMethodException e) { - throw new AssertionError("Method " + name + " does not exists in class " + clazz.getSimpleName()); + throw new AssertionError("Method " + name + " does not exists in class " + clazz.getSimpleName(), e); } } /** * Asserts that class contains hashCode, equals and toString methods. * - * @param clazz - * class to test + * @param clazz class to test */ static void assertContainsDefaultMethods(final Class clazz) { assertContainsMethod(clazz, Integer.TYPE, "hashCode"); @@ -235,16 +228,12 @@ public class CompilationTestUtils { /** * Asserts that constructor contains check for illegal argument. * - * @param constructor - * constructor to invoke - * @param errorMsg - * expected error message - * @param args - * constructor arguments - * @throws Exception + * @param constructor constructor to invoke + * @param errorMsg expected error message + * @param args constructor arguments */ - static void assertContainsRestrictionCheck(final Constructor constructor, final String errorMsg, final Object... args) - throws Exception { + static void assertContainsRestrictionCheck(final Constructor constructor, final String errorMsg, + final Object... args) throws ReflectiveOperationException { try { constructor.newInstance(args); fail("constructor invocation should fail"); @@ -258,18 +247,13 @@ public class CompilationTestUtils { /** * 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 + * @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 */ - static void assertContainsRestrictionCheck(final Object obj, final Method method, final String errorMsg, final Object... args) - throws Exception { + static void assertContainsRestrictionCheck(final Object obj, final Method method, final String errorMsg, + final Object... args) throws ReflectiveOperationException { try { method.invoke(obj, args); fail("method invocation should fail"); @@ -283,10 +267,8 @@ public class CompilationTestUtils { /** * Asserts that class implements given interface. * - * @param clazz - * source to test - * @param ifc - * expected interface + * @param clazz source to test + * @param ifc expected interface */ static void assertImplementsIfc(final Class clazz, final Class ifc) { Class[] interfaces = clazz.getInterfaces(); @@ -297,30 +279,24 @@ public class CompilationTestUtils { } /** - * Test if interface generated from augment extends Augmentation interface - * with correct generic type. + * Test if interface generated from augment extends Augmentation interface with correct generic type. * - * @param clazz - * interface generated from augment - * @param genericTypeName - * fully qualified name of expected parameter type + * @param clazz interface generated from augment + * @param genericTypeName fully qualified name of expected parameter type */ static void testAugmentation(final Class clazz, final String genericTypeName) { assertImplementsParameterizedIfc(clazz, AUGMENTATION, genericTypeName); } /** - * Asserts that class implements interface with given name and generic type - * parameter. + * 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 + * @param clazz class to test + * @param ifcName name of interface + * @param genericTypeName name of generic type */ - static void assertImplementsParameterizedIfc(final Class clazz, final String ifcName, final String genericTypeName) { + static void assertImplementsParameterizedIfc(final Class clazz, final String ifcName, + final String genericTypeName) { ParameterizedType ifcType = null; for (java.lang.reflect.Type ifc : clazz.getGenericInterfaces()) { if (ifc instanceof ParameterizedType) { @@ -340,10 +316,8 @@ public class CompilationTestUtils { /** * Test if source code is compilable. * - * @param sourcesOutputDir - * directory containing source files - * @param compiledOutputDir - * compiler output directory + * @param sourcesOutputDir directory containing source files + * @param compiledOutputDir compiler output directory */ static void testCompilation(final File sourcesOutputDir, final File compiledOutputDir) { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); @@ -351,8 +325,12 @@ public class CompilationTestUtils { List filesList = getJavaFiles(sourcesOutputDir); Iterable compilationUnits = fileManager.getJavaFileObjectsFromFiles(filesList); Iterable options = Arrays.asList("-d", compiledOutputDir.getAbsolutePath()); - boolean compiled = compiler.getTask(null, null, null, options, null, compilationUnits).call(); - assertTrue("Compilation failed", compiled); + + List> diags = new ArrayList<>(); + boolean compiled = compiler.getTask(null, null, diags::add, options, null, compilationUnits).call(); + if (!compiled) { + fail("Compilation failed with " + diags); + } } /** @@ -367,16 +345,15 @@ public class CompilationTestUtils { File[] dirContent = dir.listFiles(); if (dirContent == null) { throw new AssertionError("File " + dir + " doesn't exists or it's not a directory"); - } else { - assertEquals("Unexpected count of generated files", count, dirContent.length); } + + assertEquals("Unexpected count of generated files", count, dirContent.length); } /** * Search recursively given directory for *.java files. * - * @param directory - * directory to search + * @param directory directory to search * @return List of java files found */ private static List getJavaFiles(final File directory) { @@ -426,5 +403,4 @@ public class CompilationTestUtils { throw new RuntimeException("Failed to clean up after test"); } } - }