2 * Copyright (c) 2022 PANTHEON.tech s.r.o. 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;
13 import java.lang.reflect.Constructor;
14 import java.lang.reflect.Method;
16 import java.net.URLClassLoader;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.binding.contract.Naming;
20 public class UnionWithMultipleIdentityrefsTest extends BaseCompilationTest {
22 public void test() throws Exception {
23 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("union-with-multiple-identityrefs");
24 final File compiledOutputDir = CompilationTestUtils.compilerOutput("union-with-multiple-identityrefs");
25 generateTestSources("/compilation/union-with-multiple-identityrefs", sourcesOutputDir);
27 // Test if sources are compilable
28 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
30 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
31 Class<?> identOneClass = Class.forName(CompilationTestUtils.BASE_PKG
32 + ".urn.opendaylight.yang.union.test.rev220428.IdentOne", true, loader);
33 Class<?> identTwoClass = Class.forName(CompilationTestUtils.BASE_PKG
34 + ".urn.opendaylight.yang.union.test.rev220428.IdentTwo", true, loader);
35 Class<?> unionTypeClass = Class.forName(CompilationTestUtils.BASE_PKG
36 + ".urn.opendaylight.yang.union.test.rev220428.UnionType", true, loader);
38 Object identOneValue = identOneClass.getDeclaredField(Naming.VALUE_STATIC_FIELD_NAME).get(null);
39 Object identTwoValue = identTwoClass.getDeclaredField(Naming.VALUE_STATIC_FIELD_NAME).get(null);
41 Constructor<?> unionTypeIdentOneConstructor = CompilationTestUtils.assertContainsConstructor(unionTypeClass,
43 Constructor<?> unionTypeIdentTwoConstructor = CompilationTestUtils.assertContainsConstructor(unionTypeClass,
45 Object unionTypeOne = unionTypeIdentOneConstructor.newInstance(identOneValue);
46 Object unionTypeTwo = unionTypeIdentTwoConstructor.newInstance(identTwoValue);
48 Method getIdentityOne = unionTypeClass.getDeclaredMethod("getIdentOne");
49 Object actualIdentityOne = getIdentityOne.invoke(unionTypeOne);
50 assertEquals(identOneValue, actualIdentityOne);
52 Method getIdentityTwo = unionTypeClass.getDeclaredMethod("getIdentTwo");
53 Object actualIdentityTwo = getIdentityTwo.invoke(unionTypeTwo);
54 assertEquals(identTwoValue, actualIdentityTwo);
56 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);