2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.sal.java.api.generator.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
16 import java.io.FileNotFoundException;
17 import java.lang.reflect.Constructor;
18 import java.lang.reflect.Field;
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.lang.reflect.Modifier;
22 import java.lang.reflect.ParameterizedType;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27 import javax.tools.JavaCompiler;
28 import javax.tools.JavaFileObject;
29 import javax.tools.StandardJavaFileManager;
30 import javax.tools.ToolProvider;;
32 public class CompilationTestUtils {
33 public static final String FS = File.separator;
34 static final String BASE_PKG = "org.opendaylight.yang.gen.v1";
36 static final String TEST_PATH = "target" + FS + "test";
37 static final File TEST_DIR = new File(TEST_PATH);
39 static final String GENERATOR_OUTPUT_PATH = TEST_PATH + FS + "src";
40 static final File GENERATOR_OUTPUT_DIR = new File(GENERATOR_OUTPUT_PATH);
41 static final String COMPILER_OUTPUT_PATH = TEST_PATH + FS + "bin";
42 static final File COMPILER_OUTPUT_DIR = new File(COMPILER_OUTPUT_PATH);
44 static final String AUGMENTATION = "interface org.opendaylight.yangtools.yang.binding.Augmentation";
45 static final String BASE_PATH = "org" + FS + "opendaylight" + FS + "yang" + FS + "gen" + FS + "v1";
46 static final String NS_TEST = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "test" + FS + "rev131008";
47 static final String NS_FOO = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "foo" + FS + "rev131008";
48 static final String NS_BAR = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "bar" + FS + "rev131008";
49 static final String NS_BAZ = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "baz" + FS + "rev131008";
50 static final String NS_BUG5882 = BASE_PATH + FS + "urn" + FS + "yang" + FS + "foo" + FS + "rev160102";
53 * Method to clean resources. It is manually called at the end of each test
54 * instead of marking it with @After annotation to prevent removing
55 * generated code if test fails.
57 static void cleanUp(final File... resourceDirs) {
58 for (File resourceDir : resourceDirs) {
59 if (resourceDir.exists()) {
60 deleteTestDir(resourceDir);
66 * Asserts that class contains field with fiven name and type.
74 * @return field with given name if present in class
76 static Field assertContainsField(final Class<?> clazz, final String name, final Class<?> type) {
78 Field f = clazz.getDeclaredField(name);
79 assertEquals(type, f.getType());
81 } catch (NoSuchFieldException e) {
82 throw new AssertionError("Field " + name + " does not exist in class " + clazz.getSimpleName());
87 * Asserts that class contains field with given name and value. Method tries
88 * to create new instance of class and get value of field. If class
89 * constructor contains any arguments, class is instantiated with null
97 * return type of field
98 * @param expectedValue
99 * expected value of field
100 * @param constructorArgs
101 * constructor arguments of class to test
103 static void assertContainsFieldWithValue(final Class<?> clazz, final String name, final Class<?> returnType, final Object expectedValue,
104 final Class<?>... constructorArgs) {
105 Object[] initargs = null;
106 if (constructorArgs != null && constructorArgs.length > 0) {
107 initargs = new Object[constructorArgs.length];
108 for (int i = 0; i < constructorArgs.length; i++) {
112 assertContainsFieldWithValue(clazz, name, returnType, expectedValue, constructorArgs, initargs);
116 * Asserts that class contains field with given name, return type and value.
123 * return type of field
124 * @param expectedValue
125 * expected value of field
126 * @param constructorArgs
127 * array of constructor arguments classes
129 * array of constructor values
131 static void assertContainsFieldWithValue(final Class<?> clazz, final String name, final Class<?> returnType, final Object expectedValue,
132 final Class<?>[] constructorArgs, final Object... initargs) {
133 Field f = assertContainsField(clazz, name, returnType);
134 f.setAccessible(true);
137 if ((f.getModifiers() & Modifier.STATIC) == 0) {
139 Constructor<?> c = clazz.getDeclaredConstructor(constructorArgs);
140 obj = c.newInstance(initargs);
141 } catch (Exception e) {
142 throw new AssertionError("Failed to instantiate object for " + clazz, e);
149 assertEquals(expectedValue, f.get(obj));
150 } catch (IllegalArgumentException | IllegalAccessException e) {
151 throw new AssertionError("Failed to get field " + name + " of class " + clazz, e);
156 * Asserts that class contains constructor with parameter types.
161 * array of argument classes
163 static Constructor<?> assertContainsConstructor(final Class<?> clazz, final Class<?>... args) {
165 return clazz.getDeclaredConstructor(args);
166 } catch (NoSuchMethodException e) {
167 throw new AssertionError("Constructor with args " + Arrays.toString(args) + " does not exists in class "
168 + clazz.getSimpleName());
173 * Asserts that class contains method with given name, return type and
183 * array of parameter type classes
184 * @return method with given name, return type and parameter types
186 static Method assertContainsMethod(final Class<?> clazz, final Class<?> returnType, final String name, final Class<?>... args) {
188 Method m = clazz.getDeclaredMethod(name, args);
189 assertEquals(returnType, m.getReturnType());
191 } catch (NoSuchMethodException e) {
192 throw new AssertionError("Method " + name + " with args " + Arrays.toString(args)
193 + " does not exists in class " + clazz.getSimpleName());
198 * Asserts that class contains method with given name and return type.
202 * @param returnTypeStr
203 * name of method return type
207 * current class loader
209 static void assertContainsMethod(final Class<?> clazz, final String returnTypeStr, final String name, final ClassLoader loader) {
212 returnType = Class.forName(returnTypeStr, true, loader);
213 Method method = clazz.getMethod(name);
214 assertEquals(returnType, method.getReturnType());
215 } catch (ClassNotFoundException e) {
216 throw new AssertionError("Return type of method '" + name + "' not found");
217 } catch (NoSuchMethodException e) {
218 throw new AssertionError("Method " + name + " does not exists in class " + clazz.getSimpleName());
223 * Asserts that class contains hashCode, equals and toString methods.
228 static void assertContainsDefaultMethods(final Class<?> clazz) {
229 assertContainsMethod(clazz, Integer.TYPE, "hashCode");
230 assertContainsMethod(clazz, Boolean.TYPE, "equals", Object.class);
231 assertContainsMethod(clazz, String.class, "toString");
235 * Asserts that constructor contains check for illegal argument.
238 * constructor to invoke
240 * expected error message
242 * constructor arguments
245 static void assertContainsRestrictionCheck(final Constructor<?> constructor, final String errorMsg, final Object... args)
248 constructor.newInstance(args);
249 fail("constructor invocation should fail");
250 } catch (InvocationTargetException e) {
251 Throwable cause = e.getCause();
252 assertTrue(cause instanceof IllegalArgumentException);
253 assertEquals(errorMsg, cause.getMessage());
258 * Asserts that method contains check for illegal argument.
261 * object to test (can be null, if method is static)
265 * expected error message
267 * constructor arguments
270 static void assertContainsRestrictionCheck(final Object obj, final Method method, final String errorMsg, final Object... args)
273 method.invoke(obj, args);
274 fail("method invocation should fail");
275 } catch (InvocationTargetException e) {
276 Throwable cause = e.getCause();
277 assertTrue(cause instanceof IllegalArgumentException);
278 assertEquals(errorMsg, cause.getMessage());
283 * Asserts that class implements given interface.
290 static void assertImplementsIfc(final Class<?> clazz, final Class<?> ifc) {
291 Class<?>[] interfaces = clazz.getInterfaces();
292 List<Class<?>> ifcsList = Arrays.asList(interfaces);
293 if (!ifcsList.contains(ifc)) {
294 throw new AssertionError(clazz + " should implement " + ifc);
299 * Test if interface generated from augment extends Augmentation interface
300 * with correct generic type.
303 * interface generated from augment
304 * @param genericTypeName
305 * fully qualified name of expected parameter type
307 static void testAugmentation(final Class<?> clazz, final String genericTypeName) {
308 assertImplementsParameterizedIfc(clazz, AUGMENTATION, genericTypeName);
312 * Asserts that class implements interface with given name and generic type
319 * @param genericTypeName
320 * name of generic type
322 static void assertImplementsParameterizedIfc(final Class<?> clazz, final String ifcName, final String genericTypeName) {
323 ParameterizedType ifcType = null;
324 for (java.lang.reflect.Type ifc : clazz.getGenericInterfaces()) {
325 if (ifc instanceof ParameterizedType) {
326 ParameterizedType pt = (ParameterizedType) ifc;
327 if (ifcName.equals(pt.getRawType().toString())) {
332 assertNotNull(ifcType);
334 java.lang.reflect.Type[] typeArguments = ifcType.getActualTypeArguments();
335 assertEquals(1, typeArguments.length);
336 assertEquals("interface " + genericTypeName, typeArguments[0].toString());
340 * Test if source code is compilable.
342 * @param sourcesOutputDir
343 * directory containing source files
344 * @param compiledOutputDir
345 * compiler output directory
347 static void testCompilation(final File sourcesOutputDir, final File compiledOutputDir) {
348 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
349 StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
350 List<File> filesList = getJavaFiles(sourcesOutputDir);
351 Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(filesList);
352 Iterable<String> options = Arrays.asList("-d", compiledOutputDir.getAbsolutePath());
353 boolean compiled = compiler.getTask(null, null, null, options, null, compilationUnits).call();
354 assertTrue("Compilation failed", compiled);
358 * Asserts that directory contains exactly given count of files.
363 * expected count of files in directory
365 static void assertFilesCount(final File dir, final int count) {
366 File[] dirContent = dir.listFiles();
367 if (dirContent == null) {
368 throw new AssertionError("File " + dir + " doesn't exists or it's not a directory");
370 assertEquals("Unexpected count of generated files", count, dirContent.length);
375 * Search recursively given directory for *.java files.
378 * directory to search
379 * @return List of java files found
381 private static List<File> getJavaFiles(final File directory) {
382 List<File> result = new ArrayList<>();
383 File[] filesToRead = directory.listFiles();
384 if (filesToRead != null) {
385 for (File file : filesToRead) {
386 if (file.isDirectory()) {
387 result.addAll(getJavaFiles(file));
389 String absPath = file.getAbsolutePath();
390 if (absPath.endsWith(".java")) {
399 static List<File> getSourceFiles(final String path) throws Exception {
400 final URI resPath = BaseCompilationTest.class.getResource(path).toURI();
401 final File sourcesDir = new File(resPath);
402 if (sourcesDir.exists()) {
403 final List<File> sourceFiles = new ArrayList<>();
404 final File[] fileArray = sourcesDir.listFiles();
405 if (fileArray == null) {
406 throw new IllegalArgumentException("Unable to locate files in " + sourcesDir);
408 sourceFiles.addAll(Arrays.asList(fileArray));
411 throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
415 static void deleteTestDir(final File file) {
416 if (file.isDirectory()) {
417 File[] filesToDelete = file.listFiles();
418 if (filesToDelete != null) {
419 for (File f : filesToDelete) {
424 if (!file.delete()) {
425 throw new RuntimeException("Failed to clean up after test");