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;
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;
19 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
22 * Union constructor with indentityref. Previously identityref was ignored so that there is no constructor for
25 public class UnionWithIdentityrefTest extends BaseCompilationTest {
28 public void test() throws Exception {
29 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("union-with-identityref");
30 final File compiledOutputDir = CompilationTestUtils.compilerOutput("union-with-identityref");
31 generateTestSources("/compilation/union-with-identityref", sourcesOutputDir);
33 // Test if sources are compilable
34 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
36 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
37 Class<?> identBaseClass = Class.forName(CompilationTestUtils.BASE_PKG
38 + ".urn.opendaylight.yang.union.test.rev160509.IdentBase", true, loader);
39 Class<?> identOneClass = Class.forName(CompilationTestUtils.BASE_PKG
40 + ".urn.opendaylight.yang.union.test.rev160509.IdentOne", true, loader);
41 Class<?> unionTypeClass = Class.forName(CompilationTestUtils.BASE_PKG
42 + ".urn.opendaylight.yang.union.test.rev160509.UnionType", true, loader);
44 Object identOneValue = identOneClass.getDeclaredField(BindingMapping.VALUE_STATIC_FIELD_NAME).get(null);
46 // test UnionType with IdentOne argument
47 Constructor<?> unionTypeIdentBaseConstructor = CompilationTestUtils.assertContainsConstructor(unionTypeClass,
49 Object unionType = unionTypeIdentBaseConstructor.newInstance(identOneValue);
51 Method getUint8 = unionTypeClass.getDeclaredMethod("getUint8");
52 Object actualUint8 = getUint8.invoke(unionType);
53 assertNull(actualUint8);
55 Method getIdentityref = unionTypeClass.getDeclaredMethod("getIdentityref");
56 Object actualIdentityref = getIdentityref.invoke(unionType);
57 assertEquals(identOneValue, actualIdentityref);
59 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);