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.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
14 import com.google.common.collect.ImmutableSet;
16 import java.lang.reflect.Constructor;
17 import java.lang.reflect.Method;
19 import java.net.URLClassLoader;
20 import java.util.List;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28 * union constructor with indentityref
29 * previously identityref was ignored so that there is no constructor
33 public class UnionWithIdentityrefTest extends BaseCompilationTest {
36 public void test() throws Exception {
37 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("union-with-identityref");
38 final File compiledOutputDir = CompilationTestUtils.compilerOutput("union-with-identityref");
39 generateTestSources("/compilation/union-with-identityref", sourcesOutputDir);
41 // Test if sources are compilable
42 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
44 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
45 Class<?> identBaseClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.yang.union.test.rev160509.IdentBase", true, loader);
46 Class<?> identOneClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.yang.union.test.rev160509.IdentOne", true, loader);
47 Class<?> unionTypeClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.yang.union.test.rev160509.UnionType", true, loader);
49 // test UnionType with IdentOne argument
50 Constructor<?> unionTypeIdentBaseConstructor = CompilationTestUtils.assertContainsConstructor(unionTypeClass, Class.class);
51 Object unionType = unionTypeIdentBaseConstructor.newInstance(identOneClass);
53 Method getUint8 = unionTypeClass.getDeclaredMethod("getUint8");
54 Object actualUint8 = getUint8.invoke(unionType);
55 assertNull(actualUint8);
57 Method getIdentityref = unionTypeClass.getDeclaredMethod("getIdentityref");
58 Object actualIdentityref = getIdentityref.invoke(unionType);
59 assertEquals(identOneClass, actualIdentityref);
61 Method getValue = unionTypeClass.getDeclaredMethod("getValue");
62 Object actualValue = getValue.invoke(unionType);
63 assertArrayEquals(identOneClass.toString().toCharArray(), (char[])actualValue);
65 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
68 private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir) throws Exception {
69 final List<File> sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath);
70 final SchemaContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
71 final List<Type> types = bindingGenerator.generateTypes(context);
72 final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
73 generator.generateToFile(sourcesOutputDir);