*/
package org.opendaylight.mdsal.binding.java.api.generator.test;
-import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import com.google.common.collect.ImmutableSet;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
-import java.util.List;
import org.junit.Test;
-import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
-import org.opendaylight.mdsal.binding.model.api.Type;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
/**
- * union constructor with indentityref
- * previously identityref was ignored so that there is no constructor
- * for indentityref
- *
+ * Union constructor with indentityref. Previously identityref was ignored so that there is no constructor for
+ * identityref.
*/
public class UnionWithIdentityrefTest extends BaseCompilationTest {
@Test
public void test() throws Exception {
- final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "union-with-identityref");
- assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
- final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "union-with-identityref");
- assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
-
+ final File sourcesOutputDir = CompilationTestUtils.generatorOutput("union-with-identityref");
+ final File compiledOutputDir = CompilationTestUtils.compilerOutput("union-with-identityref");
generateTestSources("/compilation/union-with-identityref", sourcesOutputDir);
// Test if sources are compilable
CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
- Class<?> identBaseClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.yang.union.test.rev160509.IdentBase", true, loader);
- Class<?> identOneClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.yang.union.test.rev160509.IdentOne", true, loader);
- Class<?> unionTypeClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.yang.union.test.rev160509.UnionType", true, loader);
+ Class<?> identBaseClass = Class.forName(CompilationTestUtils.BASE_PKG
+ + ".urn.opendaylight.yang.union.test.rev160509.IdentBase", true, loader);
+ Class<?> identOneClass = Class.forName(CompilationTestUtils.BASE_PKG
+ + ".urn.opendaylight.yang.union.test.rev160509.IdentOne", true, loader);
+ Class<?> unionTypeClass = Class.forName(CompilationTestUtils.BASE_PKG
+ + ".urn.opendaylight.yang.union.test.rev160509.UnionType", true, loader);
// test UnionType with IdentOne argument
- Constructor<?> unionTypeIdentBaseConstructor = CompilationTestUtils.assertContainsConstructor(unionTypeClass, Class.class);
+ Constructor<?> unionTypeIdentBaseConstructor = CompilationTestUtils.assertContainsConstructor(unionTypeClass,
+ Class.class);
Object unionType = unionTypeIdentBaseConstructor.newInstance(identOneClass);
Method getUint8 = unionTypeClass.getDeclaredMethod("getUint8");
Object actualIdentityref = getIdentityref.invoke(unionType);
assertEquals(identOneClass, actualIdentityref);
- Method getValue = unionTypeClass.getDeclaredMethod("getValue");
- Object actualValue = getValue.invoke(unionType);
- assertArrayEquals(identOneClass.toString().toCharArray(), (char[])actualValue);
-
CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
}
-
- private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir) throws Exception {
- final List<File> sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath);
- final SchemaContext context = YangParserTestUtils.parseYangSources(sourceFiles);
- final List<Type> types = bindingGenerator.generateTypes(context);
- final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
- generator.generateToFile(sourcesOutputDir);
- }
}