2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.java.api.generator;
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.NS_SVC_BAR;
19 import static org.opendaylight.mdsal.binding.java.api.generator.CompilationTestUtils.NS_SVC_BAZ;
20 import static org.opendaylight.mdsal.binding.java.api.generator.CompilationTestUtils.NS_SVC_FOO;
21 import static org.opendaylight.mdsal.binding.java.api.generator.CompilationTestUtils.assertFilesCount;
22 import static org.opendaylight.mdsal.binding.java.api.generator.CompilationTestUtils.assertImplementsIfc;
23 import static org.opendaylight.mdsal.binding.java.api.generator.CompilationTestUtils.cleanUp;
24 import static org.opendaylight.mdsal.binding.java.api.generator.CompilationTestUtils.testCompilation;
27 import java.lang.reflect.Constructor;
28 import java.lang.reflect.Method;
30 import java.net.URLClassLoader;
31 import org.junit.Test;
33 public class CascadeUsesCompilationTest extends BaseCompilationTest {
36 public void testCascadeUsesCompilation() throws Exception {
37 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("cascade-uses");
38 final File compiledOutputDir = CompilationTestUtils.compilerOutput("cascade-uses");
39 generateTestSources("/compilation/cascade-uses", sourcesOutputDir);
41 // Test if all sources are generated from module foo
42 File parent = new File(sourcesOutputDir, NS_FOO);
43 assertTrue(new File(parent, "FooData.java").exists());
44 assertTrue(new File(parent, "FooGr1.java").exists());
45 assertTrue(new File(parent, "Nodes.java").exists());
46 assertTrue(new File(parent, "NodesBuilder.java").exists());
47 assertFilesCount(parent, 5);
48 File svcParent = new File(sourcesOutputDir, NS_SVC_FOO);
49 assertFilesCount(svcParent, 1);
51 // Test if all sources are generated from module bar
52 parent = new File(sourcesOutputDir, NS_BAR);
53 assertTrue(new File(parent, "BarData.java").exists());
54 assertTrue(new File(parent, "BarGr1.java").exists());
55 assertTrue(new File(parent, "BarGr2.java").exists());
56 assertFilesCount(parent, 3);
57 svcParent = new File(sourcesOutputDir, NS_SVC_BAR);
58 assertFilesCount(svcParent, 1);
60 // Test if all sources are generated from module baz
61 parent = new File(sourcesOutputDir, NS_BAZ);
62 assertTrue(new File(parent, "BazData.java").exists());
63 assertTrue(new File(parent, "BazGr1.java").exists());
64 assertFilesCount(parent, 2);
65 svcParent = new File(sourcesOutputDir, NS_SVC_BAZ);
66 assertFilesCount(svcParent, 1);
68 // Test if sources are compilable
69 testCompilation(sourcesOutputDir, compiledOutputDir);
71 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
72 Class<?> nodesClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.Nodes", true,
74 Class<?> nodesBuilderClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.NodesBuilder", true,
76 Class<?> fooGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.FooGr1", true, loader);
77 Class<?> barGr2Class = Class.forName(BASE_PKG + ".urn.opendaylight.bar.rev131008.BarGr2", true, loader);
78 Class<?> barGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.bar.rev131008.BarGr1", true, loader);
79 Class<?> bazGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.baz.rev131008.BazGr1", true, loader);
81 // test generated interface from 'container nodes'
82 assertImplementsIfc(nodesClass, fooGr1Class);
83 assertImplementsIfc(nodesClass, barGr2Class);
85 // test generated builder for 'container nodes'
86 assertFalse(nodesBuilderClass.isInterface());
87 Constructor<?>[] nodesBuilderConstructors = nodesBuilderClass.getConstructors();
88 assertEquals(6, nodesBuilderConstructors.length);
90 // test generation of builder constructors from uses in 'container nodes'
91 Constructor<?> defaultConstructor = null;
92 Constructor<?> usesFooGr1 = null;
93 Constructor<?> usesBarGr2 = null;
94 Constructor<?> usesBarGr1 = null;
95 Constructor<?> usesBazGr1 = null;
96 for (Constructor<?> c : nodesBuilderConstructors) {
97 Class<?>[] params = c.getParameterTypes();
98 if (params.length == 0) {
99 defaultConstructor = c;
101 assertEquals(1, params.length);
102 if (params[0].equals(fooGr1Class)) {
104 } else if (params[0].equals(barGr2Class)) {
106 } else if (params[0].equals(barGr1Class)) {
108 } else if (params[0].equals(bazGr1Class)) {
113 assertNotNull(defaultConstructor);
114 assertNotNull(usesFooGr1);
115 assertNotNull(usesBarGr2);
116 assertNotNull(usesBarGr1);
117 assertNotNull(usesBazGr1);
119 Method fieldsFromMethod = null;
120 for (Method m : nodesBuilderClass.getDeclaredMethods()) {
121 String methodName = m.getName();
122 if ("fieldsFrom".equals(methodName)) {
123 fieldsFromMethod = m;
126 assertNotNull(fieldsFromMethod);
127 assertEquals(1, fieldsFromMethod.getParameterCount());
129 cleanUp(sourcesOutputDir, compiledOutputDir);