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.test;
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.test.CompilationTestUtils.BASE_PKG;
15 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.NS_BAR;
16 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.NS_BAZ;
17 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.NS_FOO;
18 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.assertFilesCount;
19 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.assertImplementsIfc;
20 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.cleanUp;
21 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.testCompilation;
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.Method;
27 import java.net.URLClassLoader;
28 import org.junit.Test;
30 public class CascadeUsesCompilationTest extends BaseCompilationTest {
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);
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());
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());
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());
64 // Test if sources are compilable
65 testCompilation(sourcesOutputDir, compiledOutputDir);
67 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
68 Class<?> nodesClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.Nodes", true,
70 Class<?> nodesBuilderClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.NodesBuilder", true,
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);
77 // test generated interface from 'container nodes'
78 assertImplementsIfc(nodesClass, fooGr1Class);
79 assertImplementsIfc(nodesClass, barGr2Class);
81 // test generated builder for 'container nodes'
82 assertFalse(nodesBuilderClass.isInterface());
83 Constructor<?>[] nodesBuilderConstructors = nodesBuilderClass.getConstructors();
84 assertEquals(6, nodesBuilderConstructors.length);
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;
97 assertEquals(1, params.length);
98 if (params[0].equals(fooGr1Class)) {
100 } else if (params[0].equals(barGr2Class)) {
102 } else if (params[0].equals(barGr1Class)) {
104 } else if (params[0].equals(bazGr1Class)) {
109 assertNotNull(defaultConstructor);
110 assertNotNull(usesFooGr1);
111 assertNotNull(usesBarGr2);
112 assertNotNull(usesBarGr1);
113 assertNotNull(usesBazGr1);
115 Method fieldsFromMethod = null;
116 for (Method m : nodesBuilderClass.getDeclaredMethods()) {
117 String methodName = m.getName();
118 if ("fieldsFrom".equals(methodName)) {
119 fieldsFromMethod = m;
122 assertNotNull(fieldsFromMethod);
123 assertEquals(1, fieldsFromMethod.getParameterCount());
125 cleanUp(sourcesOutputDir, compiledOutputDir);