da586db213e8aa502b1d6ed16d4bea84d2cb8bb5
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / CascadeUsesCompilationTest.java
1 /*
2  * Copyright (c) 2016 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.mdsal.binding.java.api.generator;
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.mdsal.binding.java.api.generator.CompilationTestUtils.BASE_PKG;
15 import static org.opendaylight.mdsal.binding.java.api.generator.CompilationTestUtils.NS_BAR;
16 import static org.opendaylight.mdsal.binding.java.api.generator.CompilationTestUtils.NS_BAZ;
17 import static org.opendaylight.mdsal.binding.java.api.generator.CompilationTestUtils.NS_FOO;
18 import static org.opendaylight.mdsal.binding.java.api.generator.CompilationTestUtils.assertFilesCount;
19 import static org.opendaylight.mdsal.binding.java.api.generator.CompilationTestUtils.assertImplementsIfc;
20 import static org.opendaylight.mdsal.binding.java.api.generator.CompilationTestUtils.cleanUp;
21 import static org.opendaylight.mdsal.binding.java.api.generator.CompilationTestUtils.testCompilation;
22
23 import java.io.File;
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.Method;
26 import java.net.URL;
27 import java.net.URLClassLoader;
28 import org.junit.Test;
29
30 public class CascadeUsesCompilationTest extends BaseCompilationTest {
31
32     @Test
33     public void testCascadeUsesCompilation() throws Exception {
34         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("cascade-uses");
35         final File compiledOutputDir = CompilationTestUtils.compilerOutput("cascade-uses");
36         generateTestSources("/compilation/cascade-uses", sourcesOutputDir);
37
38         // Test if all sources are generated from module foo
39         File parent = new File(sourcesOutputDir, NS_FOO);
40         assertFilesCount(parent, 6);
41         final File fooData = new File(parent, "FooData.java");
42         final File fooGr1 = new File(parent, "FooGr1.java");
43         final File nodes = new File(parent, "Nodes.java");
44         final File nodesBuilder = new File(parent, "NodesBuilder.java");
45         assertTrue(fooData.exists());
46         assertTrue(fooGr1.exists());
47         assertTrue(nodes.exists());
48         assertTrue(nodesBuilder.exists());
49
50         // Test if all sources are generated from module bar
51         parent = new File(sourcesOutputDir, NS_BAR);
52         assertFilesCount(parent, 3);
53         File barGr1 = new File(parent, "BarGr1.java");
54         File barGr2 = new File(parent, "BarGr2.java");
55         assertTrue(barGr1.exists());
56         assertTrue(barGr2.exists());
57
58         // Test if all sources are generated from module baz
59         parent = new File(sourcesOutputDir, NS_BAZ);
60         assertFilesCount(parent, 2);
61         File bazGr1 = new File(parent, "BazGr1.java");
62         assertTrue(bazGr1.exists());
63
64         // Test if sources are compilable
65         testCompilation(sourcesOutputDir, compiledOutputDir);
66
67         ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
68         Class<?> nodesClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.Nodes", true,
69                 loader);
70         Class<?> nodesBuilderClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.NodesBuilder", true,
71                 loader);
72         Class<?> fooGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.FooGr1", true, loader);
73         Class<?> barGr2Class = Class.forName(BASE_PKG + ".urn.opendaylight.bar.rev131008.BarGr2", true, loader);
74         Class<?> barGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.bar.rev131008.BarGr1", true, loader);
75         Class<?> bazGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.baz.rev131008.BazGr1", true, loader);
76
77         // test generated interface from 'container nodes'
78         assertImplementsIfc(nodesClass, fooGr1Class);
79         assertImplementsIfc(nodesClass, barGr2Class);
80
81         // test generated builder for 'container nodes'
82         assertFalse(nodesBuilderClass.isInterface());
83         Constructor<?>[] nodesBuilderConstructors = nodesBuilderClass.getConstructors();
84         assertEquals(6, nodesBuilderConstructors.length);
85
86         // test generation of builder constructors from uses in 'container nodes'
87         Constructor<?> defaultConstructor = null;
88         Constructor<?> usesFooGr1 = null;
89         Constructor<?> usesBarGr2 = null;
90         Constructor<?> usesBarGr1 = null;
91         Constructor<?> usesBazGr1 = null;
92         for (Constructor<?> c : nodesBuilderConstructors) {
93             Class<?>[] params = c.getParameterTypes();
94             if (params.length == 0) {
95                 defaultConstructor = c;
96             } else {
97                 assertEquals(1, params.length);
98                 if (params[0].equals(fooGr1Class)) {
99                     usesFooGr1 = c;
100                 } else if (params[0].equals(barGr2Class)) {
101                     usesBarGr2 = c;
102                 } else if (params[0].equals(barGr1Class)) {
103                     usesBarGr1 = c;
104                 } else if (params[0].equals(bazGr1Class)) {
105                     usesBazGr1 = c;
106                 }
107             }
108         }
109         assertNotNull(defaultConstructor);
110         assertNotNull(usesFooGr1);
111         assertNotNull(usesBarGr2);
112         assertNotNull(usesBarGr1);
113         assertNotNull(usesBazGr1);
114
115         Method fieldsFromMethod = null;
116         for (Method m : nodesBuilderClass.getDeclaredMethods()) {
117             String methodName = m.getName();
118             if ("fieldsFrom".equals(methodName)) {
119                 fieldsFromMethod = m;
120             }
121         }
122         assertNotNull(fieldsFromMethod);
123         assertEquals(1, fieldsFromMethod.getParameterCount());
124
125         cleanUp(sourcesOutputDir, compiledOutputDir);
126     }
127
128 }