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 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
27 import org.opendaylight.yangtools.sal.binding.generator.impl.BindingGeneratorImpl;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
30 public abstract class BaseCompilationTest {
31 public static final String FS = File.separator;
32 protected static final String BASE_PKG = "org.opendaylight.yang.gen.v1";
34 protected static final String TEST_PATH = "target" + FS + "test";
35 protected static final File TEST_DIR = new File(TEST_PATH);
37 protected static final String GENERATOR_OUTPUT_PATH = TEST_PATH + FS + "src";
38 protected static final File GENERATOR_OUTPUT_DIR = new File(GENERATOR_OUTPUT_PATH);
39 protected static final String COMPILER_OUTPUT_PATH = TEST_PATH + FS + "bin";
40 protected static final File COMPILER_OUTPUT_DIR = new File(COMPILER_OUTPUT_PATH);
42 protected static final String BASE_PATH = "org" + FS + "opendaylight" + FS + "yang" + FS + "gen" + FS + "v1";
43 protected static final String NS_TEST = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "test" + FS
45 protected static final String NS_FOO = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "foo" + FS + "rev131008";
46 protected static final String NS_BAR = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "bar" + FS + "rev131008";
47 protected static final String NS_BAZ = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "baz" + FS + "rev131008";
49 protected YangParserImpl parser;
50 protected BindingGenerator bindingGenerator;
53 public static void createTestDirs() {
54 if (TEST_DIR.exists()) {
55 deleteTestDir(TEST_DIR);
57 assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
58 assertTrue(COMPILER_OUTPUT_DIR.mkdirs());
63 parser = new YangParserImpl();
64 bindingGenerator = new BindingGeneratorImpl();
68 * Method to clean resources. It is manually called at the end of each test
69 * instead of marking it with @After annotation to prevent removing
70 * generated code if test fails.
72 protected void cleanUp(File... resourceDirs) {
73 for (File resourceDir : resourceDirs) {
74 if (resourceDir.exists()) {
75 deleteTestDir(resourceDir);
80 protected static void testImplementIfc(Class<?> classToTest, Class<?> ifcClass) throws ClassNotFoundException {
81 Class<?>[] interfaces = classToTest.getInterfaces();
82 List<Class<?>> ifcsList = Arrays.asList(interfaces);
83 if (!ifcsList.contains(ifcClass)) {
84 throw new AssertionError(classToTest + " should implement " + ifcClass);
88 protected static void testAugmentation(Class<?> classToTest, String paramClass) {
89 final String augmentationIfc = "interface org.opendaylight.yangtools.yang.binding.Augmentation";
90 ParameterizedType augmentation = null;
91 for (java.lang.reflect.Type ifc : classToTest.getGenericInterfaces()) {
92 if (ifc instanceof ParameterizedType) {
93 ParameterizedType pt = (ParameterizedType) ifc;
94 if (augmentationIfc.equals(pt.getRawType().toString())) {
99 assertNotNull(augmentation);
101 java.lang.reflect.Type[] typeArguments = augmentation.getActualTypeArguments();
102 assertEquals(1, typeArguments.length);
103 assertEquals("interface " + paramClass, typeArguments[0].toString());
107 * Test if source code is compilable.
109 * @param sourcesOutputDir
110 * directory containing source files
111 * @param compiledOutputDir
112 * compiler output directory
114 protected static void testCompilation(File sourcesOutputDir, File compiledOutputDir) {
115 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
116 StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
117 List<File> filesList = getJavaFiles(sourcesOutputDir);
118 Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(filesList);
119 Iterable<String> options = Arrays.asList("-d", compiledOutputDir.getAbsolutePath());
120 boolean compiled = compiler.getTask(null, null, null, options, null, compilationUnits).call();
121 assertTrue(compiled);
125 * Search recursively given directory for *.java files.
128 * directory to search
129 * @return List of java files found
131 private static List<File> getJavaFiles(File directory) {
132 List<File> result = new ArrayList<>();
133 File[] filesToRead = directory.listFiles();
134 if (filesToRead != null) {
135 for (File file : filesToRead) {
136 if (file.isDirectory()) {
137 result.addAll(getJavaFiles(file));
139 String absPath = file.getAbsolutePath();
140 if (absPath.endsWith(".java")) {
149 protected static List<File> getSourceFiles(String path) throws FileNotFoundException {
150 final String resPath = BaseCompilationTest.class.getResource(path).getPath();
151 final File sourcesDir = new File(resPath);
152 if (sourcesDir.exists()) {
153 final List<File> sourceFiles = new ArrayList<>();
154 final File[] fileArray = sourcesDir.listFiles();
155 if (fileArray == null) {
156 throw new IllegalArgumentException("Unable to locate files in " + sourcesDir);
158 sourceFiles.addAll(Arrays.asList(fileArray));
161 throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
165 private static void deleteTestDir(File file) {
166 if (file.isDirectory()) {
167 File[] filesToDelete = file.listFiles();
168 if (filesToDelete != null) {
169 for (File f : filesToDelete) {
174 if (!file.delete()) {
175 throw new RuntimeException("Failed to clean up after test");