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;
13 import java.lang.reflect.Constructor;
14 import java.lang.reflect.Method;
16 import java.net.URLClassLoader;
17 import java.util.Arrays;
18 import org.junit.Test;
21 * Previous construction of union constructor
24 * public IpAddress(Arg1 _arg1) {
32 * was incorrect and setting
34 * <code>this._value == null</code>
36 * was replaced with setting _value to correct value, for example:
38 * <code>this._value = arg1.getValue()</code> or
39 * <code>this._value = _arg1.getValue().toString().toCharArray()</code>
42 public class Bug1276Test extends BaseCompilationTest {
45 public void test() throws Exception {
46 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1276");
47 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1276");
48 generateTestSources("/compilation/bug1276", sourcesOutputDir);
50 // Test if sources are compilable
51 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
53 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
54 Class<?> ipAddressClass = Class.forName(CompilationTestUtils.BASE_PKG + ".test.yang.union.rev140715.IpAddress", true, loader);
55 Class<?> ipv4AddressClass = Class.forName(CompilationTestUtils.BASE_PKG + ".test.yang.union.rev140715.Ipv4Address", true, loader);
56 Class<?> hostClass = Class.forName(CompilationTestUtils.BASE_PKG + ".test.yang.union.rev140715.Host", true, loader);
58 Constructor<?> ipAddressConstructor = CompilationTestUtils.assertContainsConstructor(ipAddressClass, ipv4AddressClass);
59 Constructor<?> ipv4addressConstructor = CompilationTestUtils.assertContainsConstructor(ipv4AddressClass, String.class);
60 Constructor<?> hostConstructor = CompilationTestUtils.assertContainsConstructor(hostClass, ipAddressClass);
62 // test IpAddress with Ipv4Address argument
63 Object ipv4address = ipv4addressConstructor.newInstance("192.168.0.1");
64 Object ipAddress = ipAddressConstructor.newInstance(ipv4address);
65 Method getValue = ipAddressClass.getDeclaredMethod("getValue");
66 char[] expected = "192.168.0.1".toCharArray();
67 Object actual = getValue.invoke(ipAddress);
68 assertTrue(actual instanceof char[]);
69 assertTrue(Arrays.equals(expected, (char[]) actual));
71 // test Host with IpAddress argument
72 Object host = hostConstructor.newInstance(ipAddress);
73 getValue = hostClass.getDeclaredMethod("getValue");
74 actual = getValue.invoke(host);
75 assertTrue(actual instanceof char[]);
76 assertTrue(Arrays.equals(expected, (char[]) actual));
78 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);