Remove @Deprecated ClassLoaderUtils as it's now in yangtools.util
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / yangtools / sal / java / api / generator / stmt / parser / retest / NestedGroupingCompilationTest.java
1 /*
2  * Copyright (c) 2013 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.yangtools.sal.java.api.generator.stmt.parser.retest;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.BASE_PKG;
12 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.COMPILER_OUTPUT_PATH;
13 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.FS;
14 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.GENERATOR_OUTPUT_PATH;
15 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.NS_TEST;
16 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.assertFilesCount;
17 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.assertImplementsIfc;
18 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.cleanUp;
19 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.getSourceFiles;
20 import static org.opendaylight.yangtools.sal.java.api.generator.stmt.parser.retest.CompilationTestUtils.testCompilation;
21 import com.google.common.collect.ImmutableSet;
22 import java.io.File;
23 import java.net.URL;
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;
30
31 /**
32  * Test correct code generation.
33  *
34  */
35 public class NestedGroupingCompilationTest extends BaseCompilationTest {
36
37     @Test
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());
43
44         generateTestSources("/compilation/nested-grouping", sourcesOutputDir);
45
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);
57
58         parent = new File(parent, "foo");
59         File bar = new File(parent, "Bar.java");
60         assertTrue(bar.exists());
61         assertFilesCount(parent, 1);
62
63         // Test if sources are compilable
64         testCompilation(sourcesOutputDir, compiledOutputDir);
65
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);
69
70         // Test generated 'foo'
71         assertTrue(fooClass.isInterface());
72         assertImplementsIfc(fooClass, barClass);
73
74         cleanUp(sourcesOutputDir, compiledOutputDir);
75     }
76
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);
83     }
84
85 }