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