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