e645754f41e80639ac2a0c4d392068a760748b60
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / AugmentToUsesInAugmentCompilationTest.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.assertTrue;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.net.URISyntaxException;
15 import java.net.URL;
16 import java.net.URLClassLoader;
17 import org.junit.Test;
18
19 public class AugmentToUsesInAugmentCompilationTest extends BaseCompilationTest {
20
21     @Test
22     public void testAugmentToUsesInAugment() throws IOException, URISyntaxException {
23         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("augment-uses-to-augment");
24         final File compiledOutputDir = CompilationTestUtils.compilerOutput("augment-uses-to-augment");
25         generateTestSources("/compilation/augment-uses-to-augment", sourcesOutputDir);
26
27         // Test if all sources are generated from 'module foo'
28         File fooParent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
29         CompilationTestUtils.assertFilesCount(fooParent, 5);
30         assertTrue(new File(fooParent, "IgpLinkAttributes.java").exists());
31         assertTrue(new File(fooParent, "Link1.java").exists());
32         assertTrue(new File(fooParent, "Link1Builder.java").exists());
33
34         // Test if all sources are generated from 'module bar'
35         File barParent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
36         CompilationTestUtils.assertFilesCount(barParent, 8);
37         assertTrue(new File(barParent, "BarData.java").exists());
38         assertTrue(new File(barParent, "NetworkTopology.java").exists());
39         assertTrue(new File(barParent, "NetworkTopologyBuilder.java").exists());
40         assertTrue(new File(barParent, "Link.java").exists());
41         assertTrue(new File(barParent, "LinkAttributes.java").exists());
42
43         File networkParent = new File(barParent, "network");
44         CompilationTestUtils.assertFilesCount(networkParent, 1);
45         File topologyParent = new File(networkParent, "topology");
46         CompilationTestUtils.assertFilesCount(topologyParent, 3);
47         assertTrue(new File(topologyParent, "Topology.java").exists());
48         assertTrue(new File(topologyParent, "TopologyBuilder.java").exists());
49         assertTrue(new File(topologyParent, "TopologyKey.java").exists());
50
51         File linkParent = new File(barParent, "link");
52         CompilationTestUtils.assertFilesCount(linkParent, 3);
53         assertTrue(new File(linkParent, "Link.java").exists());
54         assertTrue(new File(linkParent, "LinkBuilder.java").exists());
55         assertTrue(new File(linkParent, "LinkKey.java").exists());
56
57         // Test if all sources are generated from 'module baz'
58         File bazParent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ);
59         CompilationTestUtils.assertFilesCount(bazParent, 5);
60         assertTrue(new File(bazParent, "IgpLinkAttributes1.java").exists());
61         assertTrue(new File(bazParent, "IgpLinkAttributes1Builder.java").exists());
62         assertTrue(new File(bazParent, "LinkAttributes.java").exists());
63
64         // Test if sources are compilable
65         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
66
67         ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
68
69         try {
70             Class<?> link1Class = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.foo.rev131008.Link1",
71                 true, loader);
72             String augmentableNode = CompilationTestUtils.BASE_PKG + ".urn.opendaylight.bar.rev131008.link.Link";
73             CompilationTestUtils.testAugmentation(link1Class, augmentableNode);
74
75             Class<?> igpLinkAttributesClass = Class.forName(CompilationTestUtils.BASE_PKG
76                     + ".urn.opendaylight.foo.rev131008.IgpLinkAttributes", true, loader);
77             CompilationTestUtils.assertImplementsIfc(link1Class, igpLinkAttributesClass);
78         } catch (ClassNotFoundException e) {
79             throw new AssertionError("Class for augment wasn't generated", e);
80         }
81
82         try {
83             Class<?> igpLinkAttributes1Class = Class.forName(CompilationTestUtils.BASE_PKG
84                 + ".urn.opendaylight.baz.rev131008.IgpLinkAttributes1", true, loader);
85             CompilationTestUtils.testAugmentation(igpLinkAttributes1Class, CompilationTestUtils.BASE_PKG
86                 + ".urn.opendaylight.foo.rev131008.igp.link.attributes.IgpLinkAttributes");
87
88             Class<?> linkAttributesClass = Class.forName(CompilationTestUtils.BASE_PKG
89                 + ".urn.opendaylight.baz.rev131008.LinkAttributes", true, loader);
90             CompilationTestUtils.assertImplementsIfc(igpLinkAttributes1Class, linkAttributesClass);
91         } catch (ClassNotFoundException e) {
92             throw new AssertionError("Class for augment wasn't generated", e);
93         }
94
95         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
96     }
97
98 }