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;
11 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.BASE_PKG;
12 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.COMPILER_OUTPUT_PATH;
13 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.FS;
14 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.GENERATOR_OUTPUT_PATH;
15 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.NS_TEST;
16 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.assertFilesCount;
17 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.assertImplementsIfc;
18 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.cleanUp;
19 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.getSourceFiles;
20 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.testCompilation;
22 import com.google.common.collect.ImmutableSet;
25 import java.net.URLClassLoader;
26 import java.util.List;
27 import org.junit.Test;
28 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
29 import org.opendaylight.mdsal.binding.model.api.Type;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
34 * Test correct code generation.
37 public class NestedGroupingCompilationTest extends BaseCompilationTest {
40 public void testListGeneration() throws Exception {
41 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "nested-grouping");
42 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
43 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "nested-grouping");
44 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
46 generateTestSources("/compilation/nested-grouping", sourcesOutputDir);
48 // Test if all sources are generated
49 File parent = new File(sourcesOutputDir, NS_TEST);
50 File foo = new File(parent, "Foo.java");
51 File fooBuilder = new File(parent, "FooBuilder.java");
52 File testData = new File(parent, "TestData.java");
53 File fooDir = new File(parent, "foo");
54 assertTrue(foo.exists());
55 assertTrue(fooBuilder.exists());
56 assertTrue(testData.exists());
57 assertTrue(fooDir.exists());
58 assertFilesCount(parent, 4);
60 parent = new File(parent, "foo");
61 File bar = new File(parent, "Bar.java");
62 assertTrue(bar.exists());
63 assertFilesCount(parent, 1);
65 // Test if sources are compilable
66 testCompilation(sourcesOutputDir, compiledOutputDir);
68 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
69 Class<?> fooClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.Foo", true, loader);
70 Class<?> barClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.foo.Bar", true, loader);
72 // Test generated 'foo'
73 assertTrue(fooClass.isInterface());
74 assertImplementsIfc(fooClass, barClass);
76 cleanUp(sourcesOutputDir, compiledOutputDir);
79 private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir) throws Exception {
80 final List<File> sourceFiles = getSourceFiles(resourceDirPath);
81 final SchemaContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
82 final List<Type> types = bindingGenerator.generateTypes(context);
83 final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
84 generator.generateToFile(sourcesOutputDir);