Always generate DataRoot interface
[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         assertTrue(new File(parent, "FooData.java").exists());
41         assertTrue(new File(parent, "FooGr1.java").exists());
42         assertTrue(new File(parent, "Nodes.java").exists());
43         assertTrue(new File(parent, "NodesBuilder.java").exists());
44         assertFilesCount(parent, 6);
45
46         // Test if all sources are generated from module bar
47         parent = new File(sourcesOutputDir, NS_BAR);
48         assertTrue(new File(parent, "BarData.java").exists());
49         assertTrue(new File(parent, "BarGr1.java").exists());
50         assertTrue(new File(parent, "BarGr2.java").exists());
51         assertFilesCount(parent, 4);
52
53         // Test if all sources are generated from module baz
54         parent = new File(sourcesOutputDir, NS_BAZ);
55         assertTrue(new File(parent, "BazData.java").exists());
56         assertTrue(new File(parent, "BazGr1.java").exists());
57         assertFilesCount(parent, 3);
58
59         // Test if sources are compilable
60         testCompilation(sourcesOutputDir, compiledOutputDir);
61
62         ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
63         Class<?> nodesClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.Nodes", true,
64                 loader);
65         Class<?> nodesBuilderClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.NodesBuilder", true,
66                 loader);
67         Class<?> fooGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.FooGr1", true, loader);
68         Class<?> barGr2Class = Class.forName(BASE_PKG + ".urn.opendaylight.bar.rev131008.BarGr2", true, loader);
69         Class<?> barGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.bar.rev131008.BarGr1", true, loader);
70         Class<?> bazGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.baz.rev131008.BazGr1", true, loader);
71
72         // test generated interface from 'container nodes'
73         assertImplementsIfc(nodesClass, fooGr1Class);
74         assertImplementsIfc(nodesClass, barGr2Class);
75
76         // test generated builder for 'container nodes'
77         assertFalse(nodesBuilderClass.isInterface());
78         Constructor<?>[] nodesBuilderConstructors = nodesBuilderClass.getConstructors();
79         assertEquals(6, nodesBuilderConstructors.length);
80
81         // test generation of builder constructors from uses in 'container nodes'
82         Constructor<?> defaultConstructor = null;
83         Constructor<?> usesFooGr1 = null;
84         Constructor<?> usesBarGr2 = null;
85         Constructor<?> usesBarGr1 = null;
86         Constructor<?> usesBazGr1 = null;
87         for (Constructor<?> c : nodesBuilderConstructors) {
88             Class<?>[] params = c.getParameterTypes();
89             if (params.length == 0) {
90                 defaultConstructor = c;
91             } else {
92                 assertEquals(1, params.length);
93                 if (params[0].equals(fooGr1Class)) {
94                     usesFooGr1 = c;
95                 } else if (params[0].equals(barGr2Class)) {
96                     usesBarGr2 = c;
97                 } else if (params[0].equals(barGr1Class)) {
98                     usesBarGr1 = c;
99                 } else if (params[0].equals(bazGr1Class)) {
100                     usesBazGr1 = c;
101                 }
102             }
103         }
104         assertNotNull(defaultConstructor);
105         assertNotNull(usesFooGr1);
106         assertNotNull(usesBarGr2);
107         assertNotNull(usesBarGr1);
108         assertNotNull(usesBazGr1);
109
110         Method fieldsFromMethod = null;
111         for (Method m : nodesBuilderClass.getDeclaredMethods()) {
112             String methodName = m.getName();
113             if ("fieldsFrom".equals(methodName)) {
114                 fieldsFromMethod = m;
115             }
116         }
117         assertNotNull(fieldsFromMethod);
118         assertEquals(1, fieldsFromMethod.getParameterCount());
119
120         cleanUp(sourcesOutputDir, compiledOutputDir);
121     }
122
123 }