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.*;
13 import java.io.FileNotFoundException;
14 import java.lang.reflect.ParameterizedType;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
19 import javax.tools.JavaCompiler;
20 import javax.tools.JavaFileObject;
21 import javax.tools.StandardJavaFileManager;
22 import javax.tools.ToolProvider;
24 public class CompilationTestUtils {
25 public static final String FS = File.separator;
26 static final String BASE_PKG = "org.opendaylight.yang.gen.v1";
28 static final String TEST_PATH = "target" + FS + "test";
29 static final File TEST_DIR = new File(TEST_PATH);
31 static final String GENERATOR_OUTPUT_PATH = TEST_PATH + FS + "src";
32 static final File GENERATOR_OUTPUT_DIR = new File(GENERATOR_OUTPUT_PATH);
33 static final String COMPILER_OUTPUT_PATH = TEST_PATH + FS + "bin";
34 static final File COMPILER_OUTPUT_DIR = new File(COMPILER_OUTPUT_PATH);
36 static final String BASE_PATH = "org" + FS + "opendaylight" + FS + "yang" + FS + "gen" + FS + "v1";
37 static final String NS_TEST = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "test" + FS
39 static final String NS_FOO = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "foo" + FS + "rev131008";
40 static final String NS_BAR = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "bar" + FS + "rev131008";
41 static final String NS_BAZ = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "baz" + FS + "rev131008";
44 * Method to clean resources. It is manually called at the end of each test
45 * instead of marking it with @After annotation to prevent removing
46 * generated code if test fails.
48 static void cleanUp(File... resourceDirs) {
49 for (File resourceDir : resourceDirs) {
50 if (resourceDir.exists()) {
51 deleteTestDir(resourceDir);
57 * Test if generated source implements interface.
62 * expected interface type
64 static void testImplementsIfc(Class<?> classToTest, Class<?> ifcClass) {
65 Class<?>[] interfaces = classToTest.getInterfaces();
66 List<Class<?>> ifcsList = Arrays.asList(interfaces);
67 if (!ifcsList.contains(ifcClass)) {
68 throw new AssertionError(classToTest + " should implement " + ifcClass);
73 * Test if interface generated from augment extends Augmentation interface
74 * with correct generic type.
77 * interface generated from augment
79 * fully qualified name of expected parameter type
81 static void testAugmentation(Class<?> classToTest, String genericType) {
82 final String ifcToImplement = "interface org.opendaylight.yangtools.yang.binding.Augmentation";
83 testImplementParameterizedIfc(classToTest, ifcToImplement, genericType);
86 static void testImplementParameterizedIfc(Class<?> classToTest, String ifcToImplement, String genericType) {
87 ParameterizedType augmentation = null;
88 for (java.lang.reflect.Type ifc : classToTest.getGenericInterfaces()) {
89 if (ifc instanceof ParameterizedType) {
90 ParameterizedType pt = (ParameterizedType) ifc;
91 if (ifcToImplement.equals(pt.getRawType().toString())) {
96 assertNotNull(augmentation);
98 java.lang.reflect.Type[] typeArguments = augmentation.getActualTypeArguments();
99 assertEquals(1, typeArguments.length);
100 assertEquals("interface " + genericType, typeArguments[0].toString());
104 * Test if source code is compilable.
106 * @param sourcesOutputDir
107 * directory containing source files
108 * @param compiledOutputDir
109 * compiler output directory
111 static void testCompilation(File sourcesOutputDir, File compiledOutputDir) {
112 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
113 StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
114 List<File> filesList = getJavaFiles(sourcesOutputDir);
115 Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(filesList);
116 Iterable<String> options = Arrays.asList("-d", compiledOutputDir.getAbsolutePath());
117 boolean compiled = compiler.getTask(null, null, null, options, null, compilationUnits).call();
118 assertTrue(compiled);
121 static void testFilesCount(File dir, int count) {
122 File[] dirContent = dir.listFiles();
123 if (dirContent == null) {
124 throw new AssertionError("File " + dir + " doesn't exists or it's not a directory");
126 assertEquals("Unexpected count of generated files", count, dirContent.length);
131 * Search recursively given directory for *.java files.
134 * directory to search
135 * @return List of java files found
137 private static List<File> getJavaFiles(File directory) {
138 List<File> result = new ArrayList<>();
139 File[] filesToRead = directory.listFiles();
140 if (filesToRead != null) {
141 for (File file : filesToRead) {
142 if (file.isDirectory()) {
143 result.addAll(getJavaFiles(file));
145 String absPath = file.getAbsolutePath();
146 if (absPath.endsWith(".java")) {
155 static List<File> getSourceFiles(String path) throws FileNotFoundException {
156 final String resPath = BaseCompilationTest.class.getResource(path).getPath();
157 final File sourcesDir = new File(resPath);
158 if (sourcesDir.exists()) {
159 final List<File> sourceFiles = new ArrayList<>();
160 final File[] fileArray = sourcesDir.listFiles();
161 if (fileArray == null) {
162 throw new IllegalArgumentException("Unable to locate files in " + sourcesDir);
164 sourceFiles.addAll(Arrays.asList(fileArray));
167 throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
171 static void deleteTestDir(File file) {
172 if (file.isDirectory()) {
173 File[] filesToDelete = file.listFiles();
174 if (filesToDelete != null) {
175 for (File f : filesToDelete) {
180 if (!file.delete()) {
181 throw new RuntimeException("Failed to clean up after test");