2 * Copyright (c) 2016 Cisco Systems, Inc. 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.assertTrue;
12 import com.google.common.collect.ImmutableSet;
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.Method;
17 import java.net.URLClassLoader;
18 import java.util.Arrays;
19 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
22 import org.opendaylight.mdsal.binding.model.api.Type;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
27 * Previous construction of union constructor
30 * public IpAddress(Arg1 _arg1) {
38 * was incorrect and setting
40 * <code>this._value == null</code>
42 * was replaced with setting _value to correct value, for example:
44 * <code>this._value = arg1.getValue()</code> or
45 * <code>this._value = _arg1.getValue().toString().toCharArray()</code>
48 public class Bug1276Test extends BaseCompilationTest {
51 public void test() throws Exception {
52 final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "bug1276");
53 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
54 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "bug1276");
55 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
57 generateTestSources("/compilation/bug1276", sourcesOutputDir);
59 // Test if sources are compilable
60 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
62 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
63 Class<?> ipAddressClass = Class.forName(CompilationTestUtils.BASE_PKG + ".test.yang.union.rev140715.IpAddress", true, loader);
64 Class<?> ipv4AddressClass = Class.forName(CompilationTestUtils.BASE_PKG + ".test.yang.union.rev140715.Ipv4Address", true, loader);
65 Class<?> hostClass = Class.forName(CompilationTestUtils.BASE_PKG + ".test.yang.union.rev140715.Host", true, loader);
67 Constructor<?> ipAddressConstructor = CompilationTestUtils.assertContainsConstructor(ipAddressClass, ipv4AddressClass);
68 Constructor<?> ipv4addressConstructor = CompilationTestUtils.assertContainsConstructor(ipv4AddressClass, String.class);
69 Constructor<?> hostConstructor = CompilationTestUtils.assertContainsConstructor(hostClass, ipAddressClass);
71 // test IpAddress with Ipv4Address argument
72 Object ipv4address = ipv4addressConstructor.newInstance("192.168.0.1");
73 Object ipAddress = ipAddressConstructor.newInstance(ipv4address);
74 Method getValue = ipAddressClass.getDeclaredMethod("getValue");
75 char[] expected = "192.168.0.1".toCharArray();
76 Object actual = getValue.invoke(ipAddress);
77 assertTrue(actual instanceof char[]);
78 assertTrue(Arrays.equals(expected, (char[]) actual));
80 // test Host with IpAddress argument
81 Object host = hostConstructor.newInstance(ipAddress);
82 getValue = hostClass.getDeclaredMethod("getValue");
83 actual = getValue.invoke(host);
84 assertTrue(actual instanceof char[]);
85 assertTrue(Arrays.equals(expected, (char[]) actual));
87 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
90 private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir) throws Exception {
91 final List<File> sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath);
92 final SchemaContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
93 final List<Type> types = bindingGenerator.generateTypes(context);
94 final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
95 generator.generateToFile(sourcesOutputDir);