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.assertTrue;
12 import com.google.common.collect.ImmutableSet;
15 import java.net.URLClassLoader;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
19 import org.opendaylight.mdsal.binding.model.api.Type;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
23 public class AugmentToUsesInAugmentCompilationTest extends BaseCompilationTest {
26 public void testAugmentToUsesInAugment() throws Exception {
27 final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "augment-uses-to-augment");
28 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
29 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "augment-uses-to-augment");
30 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
32 final List<File> sourceFiles = CompilationTestUtils.getSourceFiles("/compilation/augment-uses-to-augment");
33 final SchemaContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
34 final List<Type> types = bindingGenerator.generateTypes(context);
35 final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
36 generator.generateToFile(sourcesOutputDir);
38 // Test if all sources are generated from 'module foo'
39 File fooParent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
40 CompilationTestUtils.assertFilesCount(fooParent, 4);
41 assertTrue(new File(fooParent, "IgpLinkAttributes.java").exists());
42 assertTrue(new File(fooParent, "Link1.java").exists());
43 assertTrue(new File(fooParent, "Link1Builder.java").exists());
45 // Test if all sources are generated from 'module bar'
46 File barParent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
47 CompilationTestUtils.assertFilesCount(barParent, 7);
48 assertTrue(new File(barParent, "BarData.java").exists());
49 assertTrue(new File(barParent, "NetworkTopology.java").exists());
50 assertTrue(new File(barParent, "NetworkTopologyBuilder.java").exists());
51 assertTrue(new File(barParent, "Link.java").exists());
52 assertTrue(new File(barParent, "LinkAttributes.java").exists());
54 File networkParent = new File(barParent, "network");
55 CompilationTestUtils.assertFilesCount(networkParent, 1);
56 File topologyParent = new File(networkParent, "topology");
57 CompilationTestUtils.assertFilesCount(topologyParent, 3);
58 assertTrue(new File(topologyParent, "Topology.java").exists());
59 assertTrue(new File(topologyParent, "TopologyBuilder.java").exists());
60 assertTrue(new File(topologyParent, "TopologyKey.java").exists());
62 File linkParent = new File(barParent, "link");
63 CompilationTestUtils.assertFilesCount(linkParent, 3);
64 assertTrue(new File(linkParent, "Link.java").exists());
65 assertTrue(new File(linkParent, "LinkBuilder.java").exists());
66 assertTrue(new File(linkParent, "LinkKey.java").exists());
68 // Test if all sources are generated from 'module baz'
69 File bazParent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ);
70 CompilationTestUtils.assertFilesCount(bazParent, 4);
71 assertTrue(new File(bazParent, "IgpLinkAttributes1.java").exists());
72 assertTrue(new File(bazParent, "IgpLinkAttributes1Builder.java").exists());
73 assertTrue(new File(bazParent, "LinkAttributes.java").exists());
75 // Test if sources are compilable
76 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
78 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
81 Class<?> link1Class = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.foo.rev131008.Link1", true, loader);
82 String augmentableNode = CompilationTestUtils.BASE_PKG + ".urn.opendaylight.bar.rev131008.link.Link";
83 CompilationTestUtils.testAugmentation(link1Class, augmentableNode);
85 Class<?> igpLinkAttributesClass = Class.forName(CompilationTestUtils.BASE_PKG
86 + ".urn.opendaylight.foo.rev131008.IgpLinkAttributes", true, loader);
87 CompilationTestUtils.assertImplementsIfc(link1Class, igpLinkAttributesClass);
88 } catch (ClassNotFoundException e) {
89 throw new AssertionError("Class for augment wasn't generated");
93 Class<?> igpLinkAttributes1Class = Class.forName(CompilationTestUtils.BASE_PKG
94 + ".urn.opendaylight.baz.rev131008.IgpLinkAttributes1", true, loader);
95 String augmentableNode = CompilationTestUtils.BASE_PKG + ".urn.opendaylight.foo.rev131008.igp.link.attributes.IgpLinkAttributes";
96 CompilationTestUtils.testAugmentation(igpLinkAttributes1Class, augmentableNode);
98 Class<?> linkAttributesClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.baz.rev131008.LinkAttributes",
100 CompilationTestUtils.assertImplementsIfc(igpLinkAttributes1Class, linkAttributesClass);
101 } catch (ClassNotFoundException e) {
102 throw new AssertionError("Class for augment wasn't generated");
105 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);