f7b94396417a3cc91996a343011eab3c9c19db36
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / UnionWithIdentityrefTest.java
1 /*
2  * Copyright (c) 2016 Intel corporation 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.mdsal.binding.java.api.generator;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12
13 import java.io.File;
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.Method;
16 import java.net.URL;
17 import java.net.URLClassLoader;
18 import org.junit.Test;
19
20 /**
21  * Union constructor with indentityref. Previously identityref was ignored so that there is no constructor for
22  * identityref.
23  */
24 public class UnionWithIdentityrefTest extends BaseCompilationTest {
25
26     @Test
27     public void test() throws Exception {
28         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("union-with-identityref");
29         final File compiledOutputDir = CompilationTestUtils.compilerOutput("union-with-identityref");
30         generateTestSources("/compilation/union-with-identityref", sourcesOutputDir);
31
32         // Test if sources are compilable
33         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
34
35         ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
36         Class<?> identBaseClass = Class.forName(CompilationTestUtils.BASE_PKG
37             + ".urn.opendaylight.yang.union.test.rev160509.IdentBase", true, loader);
38         Class<?> identOneClass = Class.forName(CompilationTestUtils.BASE_PKG
39             + ".urn.opendaylight.yang.union.test.rev160509.IdentOne", true, loader);
40         Class<?> unionTypeClass = Class.forName(CompilationTestUtils.BASE_PKG
41             + ".urn.opendaylight.yang.union.test.rev160509.UnionType", true, loader);
42
43         // test UnionType with IdentOne argument
44         Constructor<?> unionTypeIdentBaseConstructor = CompilationTestUtils.assertContainsConstructor(unionTypeClass,
45             Class.class);
46         Object unionType = unionTypeIdentBaseConstructor.newInstance(identOneClass);
47
48         Method getUint8 = unionTypeClass.getDeclaredMethod("getUint8");
49         Object actualUint8 = getUint8.invoke(unionType);
50         assertNull(actualUint8);
51
52         Method getIdentityref = unionTypeClass.getDeclaredMethod("getIdentityref");
53         Object actualIdentityref = getIdentityref.invoke(unionType);
54         assertEquals(identOneClass, actualIdentityref);
55
56         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
57     }
58 }