Implemented use of ChildOf interface in generated classes.
[yangtools.git] / code-generator / binding-java-api-generator / src / test / java / org / opendaylight / yangtools / sal / java / api / generator / test / CompilationTestUtils.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 public class CompilationTestUtils {
25     public static final String FS = File.separator;
26     static final String BASE_PKG = "org.opendaylight.yang.gen.v1";
27
28     static final String TEST_PATH = "target" + FS + "test";
29     static final File TEST_DIR = new File(TEST_PATH);
30
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);
35
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
38             + "rev131008";
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";
42
43     /**
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.
47      */
48     static void cleanUp(File... resourceDirs) {
49         for (File resourceDir : resourceDirs) {
50             if (resourceDir.exists()) {
51                 deleteTestDir(resourceDir);
52             }
53         }
54     }
55
56     /**
57      * Test if generated source implements interface.
58      *
59      * @param classToTest
60      *            source to test
61      * @param ifcClass
62      *            expected interface type
63      */
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);
69         }
70     }
71
72     /**
73      * Test if interface generated from augment extends Augmentation interface
74      * with correct generic type.
75      *
76      * @param classToTest
77      *            interface generated from augment
78      * @param genericType
79      *            fully qualified name of expected parameter type
80      */
81     static void testAugmentation(Class<?> classToTest, String genericType) {
82         final String ifcToImplement = "interface org.opendaylight.yangtools.yang.binding.Augmentation";
83         testImplementParameterizedIfc(classToTest, ifcToImplement, genericType);
84     }
85
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())) {
92                     augmentation = pt;
93                 }
94             }
95         }
96         assertNotNull(augmentation);
97
98         java.lang.reflect.Type[] typeArguments = augmentation.getActualTypeArguments();
99         assertEquals(1, typeArguments.length);
100         assertEquals("interface " + genericType, typeArguments[0].toString());
101     }
102
103     /**
104      * Test if source code is compilable.
105      *
106      * @param sourcesOutputDir
107      *            directory containing source files
108      * @param compiledOutputDir
109      *            compiler output directory
110      */
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);
119     }
120
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");
125         } else {
126             assertEquals("Unexpected count of generated files", count, dirContent.length);
127         }
128     }
129
130     /**
131      * Search recursively given directory for *.java files.
132      *
133      * @param directory
134      *            directory to search
135      * @return List of java files found
136      */
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));
144                 } else {
145                     String absPath = file.getAbsolutePath();
146                     if (absPath.endsWith(".java")) {
147                         result.add(file);
148                     }
149                 }
150             }
151         }
152         return result;
153     }
154
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);
163             }
164             sourceFiles.addAll(Arrays.asList(fileArray));
165             return sourceFiles;
166         } else {
167             throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
168         }
169     }
170
171     static void deleteTestDir(File file) {
172         if (file.isDirectory()) {
173             File[] filesToDelete = file.listFiles();
174             if (filesToDelete != null) {
175                 for (File f : filesToDelete) {
176                     deleteTestDir(f);
177                 }
178             }
179         }
180         if (!file.delete()) {
181             throw new RuntimeException("Failed to clean up after test");
182         }
183     }
184
185 }