Remove @Deprecated ClassLoaderUtils as it's now in yangtools.util
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / yangtools / sal / java / api / generator / stmt / parser / retest / CascadeUsesCompilationTest.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.stmt.parser.retest;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.BASE_PKG;
15 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.COMPILER_OUTPUT_PATH;
16 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.FS;
17 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.GENERATOR_OUTPUT_PATH;
18 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.NS_BAR;
19 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.NS_BAZ;
20 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.NS_FOO;
21 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.assertFilesCount;
22 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.assertImplementsIfc;
23 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.cleanUp;
24 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.getSourceFiles;
25 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.testCompilation;
26 import com.google.common.collect.ImmutableSet;
27 import java.io.File;
28 import java.lang.reflect.Constructor;
29 import java.lang.reflect.Method;
30 import java.net.URL;
31 import java.net.URLClassLoader;
32 import java.util.List;
33 import org.junit.Test;
34 import org.opendaylight.yangtools.sal.binding.model.api.Type;
35 import org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37
38 public class CascadeUsesCompilationTest extends BaseCompilationTest {
39
40     @Test
41     public void testCascadeUsesCompilation() throws Exception {
42         final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "cascade-uses");
43         assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
44         final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "cascade-uses");
45         assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
46
47         final List<File> sourceFiles = getSourceFiles("/compilation/cascade-uses");
48         final SchemaContext context = RetestUtils.parseYangSources(sourceFiles);
49         final List<Type> types = bindingGenerator.generateTypes(context);
50         final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
51         generator.generateToFile(sourcesOutputDir);
52
53         // Test if all sources are generated from module foo
54         File parent = new File(sourcesOutputDir, NS_FOO);
55         assertFilesCount(parent, 5);
56         File fooData = new File(parent, "FooData.java");
57         File foo_gr1 = new File(parent, "FooGr1.java");
58         File nodes = new File(parent, "Nodes.java");
59         File nodesBuilder = new File(parent, "NodesBuilder.java");
60         assertTrue(fooData.exists());
61         assertTrue(foo_gr1.exists());
62         assertTrue(nodes.exists());
63         assertTrue(nodesBuilder.exists());
64
65         // Test if all sources are generated from module bar
66         parent = new File(sourcesOutputDir, NS_BAR);
67         assertFilesCount(parent, 2);
68         File barGr1 = new File(parent, "BarGr1.java");
69         File barGr2 = new File(parent, "BarGr2.java");
70         assertTrue(barGr1.exists());
71         assertTrue(barGr2.exists());
72
73         // Test if all sources are generated from module baz
74         parent = new File(sourcesOutputDir, NS_BAZ);
75         assertFilesCount(parent, 1);
76         File bazGr1 = new File(parent, "BazGr1.java");
77         assertTrue(bazGr1.exists());
78
79         // Test if sources are compilable
80         testCompilation(sourcesOutputDir, compiledOutputDir);
81
82         ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
83         Class<?> nodesClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.Nodes", true,
84                 loader);
85         Class<?> nodesBuilderClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.NodesBuilder", true,
86                 loader);
87         Class<?> fooGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.FooGr1", true, loader);
88         Class<?> barGr2Class = Class.forName(BASE_PKG + ".urn.opendaylight.bar.rev131008.BarGr2", true, loader);
89         Class<?> barGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.bar.rev131008.BarGr1", true, loader);
90         Class<?> bazGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.baz.rev131008.BazGr1", true, loader);
91
92         // test generated interface from 'container nodes'
93         assertImplementsIfc(nodesClass, fooGr1Class);
94         assertImplementsIfc(nodesClass, barGr2Class);
95
96         // test generated builder for 'container nodes'
97         assertFalse(nodesBuilderClass.isInterface());
98         Constructor<?>[] nodesBuilderConstructors = nodesBuilderClass.getConstructors();
99         assertEquals(6, nodesBuilderConstructors.length);
100
101         // test generation of builder constructors from uses in 'container nodes'
102         Constructor<?> defaultConstructor = null;
103         Constructor<?> usesFooGr1 = null;
104         Constructor<?> usesBarGr2 = null;
105         Constructor<?> usesBarGr1 = null;
106         Constructor<?> usesBazGr1 = null;
107         for (Constructor<?> c : nodesBuilderConstructors) {
108             Class<?>[] params = c.getParameterTypes();
109             if (params.length == 0) {
110                 defaultConstructor = c;
111             } else {
112                 assertEquals(1, params.length);
113                 if (params[0].equals(fooGr1Class)) {
114                     usesFooGr1 = c;
115                 } else if (params[0].equals(barGr2Class)) {
116                     usesBarGr2 = c;
117                 } else if (params[0].equals(barGr1Class)) {
118                     usesBarGr1 = c;
119                 } else if (params[0].equals(bazGr1Class)) {
120                     usesBazGr1 = c;
121                 }
122             }
123         }
124         assertNotNull(defaultConstructor);
125         assertNotNull(usesFooGr1);
126         assertNotNull(usesBarGr2);
127         assertNotNull(usesBarGr1);
128         assertNotNull(usesBazGr1);
129
130         Method fieldsFromMethod = null;
131         for (Method m : nodesBuilderClass.getDeclaredMethods()) {
132             String methodName = m.getName();
133             if ("fieldsFrom".equals(methodName)) {
134                 fieldsFromMethod = m;
135             }
136         }
137         assertNotNull(fieldsFromMethod);
138         assertEquals(1, fieldsFromMethod.getParameterTypes().length);
139
140         cleanUp(sourcesOutputDir, compiledOutputDir);
141     }
142
143 }