Cleanup mdsal-binding-java-api-generator compilation tests
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / test / Bug1276Test.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.java.api.generator.test;
9
10 import static org.junit.Assert.assertTrue;
11
12 import java.io.File;
13 import java.lang.reflect.Constructor;
14 import java.lang.reflect.Method;
15 import java.net.URL;
16 import java.net.URLClassLoader;
17 import java.util.Arrays;
18 import org.junit.Test;
19
20 /**
21  * Previous construction of union constructor
22  *
23  * <code>
24  * public IpAddress(Arg1 _arg1) {
25  *     super();
26  *     this._arg1 = _arg1;
27  *     this._arg2 = null;
28  *     this._value = null;
29  * }
30  * </code>
31  *
32  * was incorrect and setting
33  *
34  * <code>this._value == null</code>
35  *
36  * was replaced with setting _value to correct value, for example:
37  *
38  * <code>this._value = arg1.getValue()</code> or
39  * <code>this._value = _arg1.getValue().toString().toCharArray()</code>
40  *
41  */
42 public class Bug1276Test extends BaseCompilationTest {
43
44     @Test
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);
49
50         // Test if sources are compilable
51         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
52
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);
57
58         Constructor<?> ipAddressConstructor = CompilationTestUtils.assertContainsConstructor(ipAddressClass, ipv4AddressClass);
59         Constructor<?> ipv4addressConstructor = CompilationTestUtils.assertContainsConstructor(ipv4AddressClass, String.class);
60         Constructor<?> hostConstructor = CompilationTestUtils.assertContainsConstructor(hostClass, ipAddressClass);
61
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));
70
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));
77
78         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
79     }
80 }