2 * Copyright (c) 2016 Intel corporation 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.mdsal.binding.java.api.generator.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.Method;
17 import java.net.URLClassLoader;
18 import org.junit.Test;
21 * Union constructor with indentityref. Previously identityref was ignored so that there is no constructor for
24 public class UnionWithIdentityrefTest extends BaseCompilationTest {
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);
32 // Test if sources are compilable
33 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
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);
43 // test UnionType with IdentOne argument
44 Constructor<?> unionTypeIdentBaseConstructor = CompilationTestUtils.assertContainsConstructor(unionTypeClass,
46 Object unionType = unionTypeIdentBaseConstructor.newInstance(identOneClass);
48 Method getUint8 = unionTypeClass.getDeclaredMethod("getUint8");
49 Object actualUint8 = getUint8.invoke(unionType);
50 assertNull(actualUint8);
52 Method getIdentityref = unionTypeClass.getDeclaredMethod("getIdentityref");
53 Object actualIdentityref = getIdentityref.invoke(unionType);
54 assertEquals(identOneClass, actualIdentityref);
56 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);