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.getSourceFiles;
22 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.testCompilation;
24 import com.google.common.collect.ImmutableSet;
26 import java.lang.reflect.Constructor;
27 import java.lang.reflect.Method;
29 import java.net.URLClassLoader;
30 import java.util.List;
31 import org.junit.Test;
32 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
33 import org.opendaylight.mdsal.binding.model.api.Type;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
37 public class CascadeUsesCompilationTest extends BaseCompilationTest {
40 public void testCascadeUsesCompilation() throws Exception {
41 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("cascade-uses");
42 final File compiledOutputDir = CompilationTestUtils.compilerOutput("cascade-uses");
43 final List<File> sourceFiles = getSourceFiles("/compilation/cascade-uses");
44 final SchemaContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
45 final List<Type> types = bindingGenerator.generateTypes(context);
46 final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
47 generator.generateToFile(sourcesOutputDir);
49 // Test if all sources are generated from module foo
50 File parent = new File(sourcesOutputDir, NS_FOO);
51 assertFilesCount(parent, 5);
52 File fooData = new File(parent, "FooData.java");
53 File foo_gr1 = new File(parent, "FooGr1.java");
54 File nodes = new File(parent, "Nodes.java");
55 File nodesBuilder = new File(parent, "NodesBuilder.java");
56 assertTrue(fooData.exists());
57 assertTrue(foo_gr1.exists());
58 assertTrue(nodes.exists());
59 assertTrue(nodesBuilder.exists());
61 // Test if all sources are generated from module bar
62 parent = new File(sourcesOutputDir, NS_BAR);
63 assertFilesCount(parent, 2);
64 File barGr1 = new File(parent, "BarGr1.java");
65 File barGr2 = new File(parent, "BarGr2.java");
66 assertTrue(barGr1.exists());
67 assertTrue(barGr2.exists());
69 // Test if all sources are generated from module baz
70 parent = new File(sourcesOutputDir, NS_BAZ);
71 assertFilesCount(parent, 1);
72 File bazGr1 = new File(parent, "BazGr1.java");
73 assertTrue(bazGr1.exists());
75 // Test if sources are compilable
76 testCompilation(sourcesOutputDir, compiledOutputDir);
78 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
79 Class<?> nodesClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.Nodes", true,
81 Class<?> nodesBuilderClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.NodesBuilder", true,
83 Class<?> fooGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.FooGr1", true, loader);
84 Class<?> barGr2Class = Class.forName(BASE_PKG + ".urn.opendaylight.bar.rev131008.BarGr2", true, loader);
85 Class<?> barGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.bar.rev131008.BarGr1", true, loader);
86 Class<?> bazGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.baz.rev131008.BazGr1", true, loader);
88 // test generated interface from 'container nodes'
89 assertImplementsIfc(nodesClass, fooGr1Class);
90 assertImplementsIfc(nodesClass, barGr2Class);
92 // test generated builder for 'container nodes'
93 assertFalse(nodesBuilderClass.isInterface());
94 Constructor<?>[] nodesBuilderConstructors = nodesBuilderClass.getConstructors();
95 assertEquals(6, nodesBuilderConstructors.length);
97 // test generation of builder constructors from uses in 'container nodes'
98 Constructor<?> defaultConstructor = null;
99 Constructor<?> usesFooGr1 = null;
100 Constructor<?> usesBarGr2 = null;
101 Constructor<?> usesBarGr1 = null;
102 Constructor<?> usesBazGr1 = null;
103 for (Constructor<?> c : nodesBuilderConstructors) {
104 Class<?>[] params = c.getParameterTypes();
105 if (params.length == 0) {
106 defaultConstructor = c;
108 assertEquals(1, params.length);
109 if (params[0].equals(fooGr1Class)) {
111 } else if (params[0].equals(barGr2Class)) {
113 } else if (params[0].equals(barGr1Class)) {
115 } else if (params[0].equals(bazGr1Class)) {
120 assertNotNull(defaultConstructor);
121 assertNotNull(usesFooGr1);
122 assertNotNull(usesBarGr2);
123 assertNotNull(usesBarGr1);
124 assertNotNull(usesBazGr1);
126 Method fieldsFromMethod = null;
127 for (Method m : nodesBuilderClass.getDeclaredMethods()) {
128 String methodName = m.getName();
129 if ("fieldsFrom".equals(methodName)) {
130 fieldsFromMethod = m;
133 assertNotNull(fieldsFromMethod);
134 assertEquals(1, fieldsFromMethod.getParameterTypes().length);
136 cleanUp(sourcesOutputDir, compiledOutputDir);