Added constructors to builder classes based on implemented interfaces from uses nodes.
[yangtools.git] / code-generator / binding-java-api-generator / src / test / java / org / opendaylight / yangtools / sal / java / api / generator / test / BaseCompilationTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.sal.java.api.generator.test;
9
10 import static org.junit.Assert.*;
11
12 import java.io.File;
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;
18
19 import javax.tools.JavaCompiler;
20 import javax.tools.JavaFileObject;
21 import javax.tools.StandardJavaFileManager;
22 import javax.tools.ToolProvider;
23
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;
29
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";
33
34     protected static final String TEST_PATH = "target" + FS + "test";
35     protected static final File TEST_DIR = new File(TEST_PATH);
36
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);
41
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
44             + "rev131008";
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";
48
49     protected YangParserImpl parser;
50     protected BindingGenerator bindingGenerator;
51
52     @BeforeClass
53     public static void createTestDirs() {
54         if (TEST_DIR.exists()) {
55             deleteTestDir(TEST_DIR);
56         }
57         assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
58         assertTrue(COMPILER_OUTPUT_DIR.mkdirs());
59     }
60
61     @Before
62     public void init() {
63         parser = new YangParserImpl();
64         bindingGenerator = new BindingGeneratorImpl();
65     }
66
67     /**
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.
71      */
72     protected void cleanUp(File... resourceDirs) {
73         for (File resourceDir : resourceDirs) {
74             if (resourceDir.exists()) {
75                 deleteTestDir(resourceDir);
76             }
77         }
78     }
79
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);
85         }
86     }
87
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())) {
95                     augmentation = pt;
96                 }
97             }
98         }
99         assertNotNull(augmentation);
100
101         java.lang.reflect.Type[] typeArguments = augmentation.getActualTypeArguments();
102         assertEquals(1, typeArguments.length);
103         assertEquals("interface " + paramClass, typeArguments[0].toString());
104     }
105
106     /**
107      * Test if source code is compilable.
108      * 
109      * @param sourcesOutputDir
110      *            directory containing source files
111      * @param compiledOutputDir
112      *            compiler output directory
113      */
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);
122     }
123
124     /**
125      * Search recursively given directory for *.java files.
126      * 
127      * @param directory
128      *            directory to search
129      * @return List of java files found
130      */
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));
138                 } else {
139                     String absPath = file.getAbsolutePath();
140                     if (absPath.endsWith(".java")) {
141                         result.add(file);
142                     }
143                 }
144             }
145         }
146         return result;
147     }
148
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);
157             }
158             sourceFiles.addAll(Arrays.asList(fileArray));
159             return sourceFiles;
160         } else {
161             throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
162         }
163     }
164
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) {
170                     deleteTestDir(f);
171                 }
172             }
173         }
174         if (!file.delete()) {
175             throw new RuntimeException("Failed to clean up after test");
176         }
177     }
178
179 }