*/
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;
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";
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";
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) {
}
/**
- * 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];
/**
* 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 {
}
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);
}
/**
* 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");
/**
* 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");
/**
* 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");
/**
* 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();
}
/**
- * 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) {
/**
* 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();
List<File> filesList = getJavaFiles(sourcesOutputDir);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(filesList);
Iterable<String> options = Arrays.asList("-d", compiledOutputDir.getAbsolutePath());
- boolean compiled = compiler.getTask(null, null, null, options, null, compilationUnits).call();
- assertTrue("Compilation failed", compiled);
+
+ List<Diagnostic<?>> diags = new ArrayList<>();
+ boolean compiled = compiler.getTask(null, null, diags::add, options, null, compilationUnits).call();
+ if (!compiled) {
+ fail("Compilation failed with " + diags);
+ }
}
/**
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<File> getJavaFiles(final File directory) {
throw new RuntimeException("Failed to clean up after test");
}
}
-
}