2 * Copyright (c) 2013 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;
24 import java.net.URLClassLoader;
25 import java.util.HashSet;
26 import java.util.List;
27 import org.junit.Test;
28 import org.opendaylight.yangtools.sal.binding.model.api.Type;
29 import org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 * Test correct code generation.
36 public class NestedGroupingCompilationTest extends BaseCompilationTest {
39 public void testListGeneration() throws Exception {
40 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "nested-grouping");
41 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
42 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "nested-grouping");
43 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
45 generateTestSources("/compilation/nested-grouping", sourcesOutputDir);
47 // Test if all sources are generated
48 File parent = new File(sourcesOutputDir, NS_TEST);
49 File foo = new File(parent, "Foo.java");
50 File fooBuilder = new File(parent, "FooBuilder.java");
51 File testData = new File(parent, "TestData.java");
52 File fooDir = new File(parent, "foo");
53 assertTrue(foo.exists());
54 assertTrue(fooBuilder.exists());
55 assertTrue(testData.exists());
56 assertTrue(fooDir.exists());
57 assertFilesCount(parent, 4);
59 parent = new File(parent, "foo");
60 File bar = new File(parent, "Bar.java");
61 assertTrue(bar.exists());
62 assertFilesCount(parent, 1);
64 // Test if sources are compilable
65 testCompilation(sourcesOutputDir, compiledOutputDir);
67 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
68 Class<?> fooClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.Foo", true, loader);
69 Class<?> barClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.foo.Bar", true, loader);
71 // Test generated 'foo'
72 assertTrue(fooClass.isInterface());
73 assertImplementsIfc(fooClass, barClass);
75 cleanUp(sourcesOutputDir, compiledOutputDir);
78 private void generateTestSources(String resourceDirPath, File sourcesOutputDir) throws Exception {
79 final List<File> sourceFiles = getSourceFiles(resourceDirPath);
80 final SchemaContext context = parser.parseFiles(sourceFiles);
81 final List<Type> types = bindingGenerator.generateTypes(context);
82 final GeneratorJavaFile generator = new GeneratorJavaFile(new HashSet<>(types));
83 generator.generateToFile(sourcesOutputDir);