Merge branch 'blueprint' from controller
[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 java.io.File;
11 import java.lang.reflect.Constructor;
12 import java.net.URL;
13 import java.net.URLClassLoader;
14 import org.junit.Test;
15
16 /**
17  * Previous construction of union constructor
18  *
19  * <code>
20  * public IpAddress(Arg1 _arg1) {
21  *     super();
22  *     this._arg1 = _arg1;
23  *     this._arg2 = null;
24  *     this._value = null;
25  * }
26  * </code>
27  *
28  * was incorrect and setting
29  *
30  * <code>this._value == null</code>
31  *
32  * was replaced with setting _value to correct value, for example:
33  *
34  * <code>this._value = arg1.getValue()</code> or
35  * <code>this._value = _arg1.getValue().toString().toCharArray()</code>
36  *
37  */
38 public class Bug1276Test extends BaseCompilationTest {
39
40     @Test
41     public void test() throws Exception {
42         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1276");
43         final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1276");
44         generateTestSources("/compilation/bug1276", sourcesOutputDir);
45
46         // Test if sources are compilable
47         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
48
49         ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
50         Class<?> ipAddressClass = Class.forName(CompilationTestUtils.BASE_PKG + ".test.yang.union.rev140715.IpAddress", true, loader);
51         Class<?> ipv4AddressClass = Class.forName(CompilationTestUtils.BASE_PKG + ".test.yang.union.rev140715.Ipv4Address", true, loader);
52         Class<?> hostClass = Class.forName(CompilationTestUtils.BASE_PKG + ".test.yang.union.rev140715.Host", true, loader);
53
54         Constructor<?> ipAddressConstructor = CompilationTestUtils.assertContainsConstructor(ipAddressClass, ipv4AddressClass);
55         Constructor<?> ipv4addressConstructor = CompilationTestUtils.assertContainsConstructor(ipv4AddressClass, String.class);
56         Constructor<?> hostConstructor = CompilationTestUtils.assertContainsConstructor(hostClass, ipAddressClass);
57
58         // test IpAddress with Ipv4Address argument
59         Object ipv4address = ipv4addressConstructor.newInstance("192.168.0.1");
60         Object ipAddress = ipAddressConstructor.newInstance(ipv4address);
61
62         // test Host with IpAddress argument
63         Object host = hostConstructor.newInstance(ipAddress);
64
65         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
66     }
67 }