cf649baf3344f6834e7b1f1effcfed2689f2ef35
[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 com.google.common.collect.ImmutableSet;
13 import java.io.File;
14 import java.io.IOException;
15 import java.lang.reflect.Constructor;
16 import java.lang.reflect.Method;
17 import java.net.URISyntaxException;
18 import java.net.URL;
19 import java.net.URLClassLoader;
20 import java.util.Arrays;
21 import java.util.List;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
24 import org.opendaylight.mdsal.binding.model.api.Type;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
27
28 /**
29  * Previous construction of union constructor
30  *
31  * <code>
32  * public IpAddress(Arg1 _arg1) {
33  *     super();
34  *     this._arg1 = _arg1;
35  *     this._arg2 = null;
36  *     this._value = null;
37  * }
38  * </code>
39  *
40  * was incorrect and setting
41  *
42  * <code>this._value == null</code>
43  *
44  * was replaced with setting _value to correct value, for example:
45  *
46  * <code>this._value = arg1.getValue()</code> or
47  * <code>this._value = _arg1.getValue().toString().toCharArray()</code>
48  *
49  */
50 public class Bug1276Test extends BaseCompilationTest {
51
52     @Test
53     public void test() throws Exception {
54         final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1276");
55         final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1276");
56         generateTestSources("/compilation/bug1276", sourcesOutputDir);
57
58         // Test if sources are compilable
59         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
60
61         ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
62         Class<?> ipAddressClass = Class.forName(CompilationTestUtils.BASE_PKG + ".test.yang.union.rev140715.IpAddress", true, loader);
63         Class<?> ipv4AddressClass = Class.forName(CompilationTestUtils.BASE_PKG + ".test.yang.union.rev140715.Ipv4Address", true, loader);
64         Class<?> hostClass = Class.forName(CompilationTestUtils.BASE_PKG + ".test.yang.union.rev140715.Host", true, loader);
65
66         Constructor<?> ipAddressConstructor = CompilationTestUtils.assertContainsConstructor(ipAddressClass, ipv4AddressClass);
67         Constructor<?> ipv4addressConstructor = CompilationTestUtils.assertContainsConstructor(ipv4AddressClass, String.class);
68         Constructor<?> hostConstructor = CompilationTestUtils.assertContainsConstructor(hostClass, ipAddressClass);
69
70         // test IpAddress with Ipv4Address argument
71         Object ipv4address = ipv4addressConstructor.newInstance("192.168.0.1");
72         Object ipAddress = ipAddressConstructor.newInstance(ipv4address);
73         Method getValue = ipAddressClass.getDeclaredMethod("getValue");
74         char[] expected = "192.168.0.1".toCharArray();
75         Object actual = getValue.invoke(ipAddress);
76         assertTrue(actual instanceof char[]);
77         assertTrue(Arrays.equals(expected, (char[]) actual));
78
79         // test Host with IpAddress argument
80         Object host = hostConstructor.newInstance(ipAddress);
81         getValue = hostClass.getDeclaredMethod("getValue");
82         actual = getValue.invoke(host);
83         assertTrue(actual instanceof char[]);
84         assertTrue(Arrays.equals(expected, (char[]) actual));
85
86         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
87     }
88
89     private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir)
90             throws IOException, URISyntaxException {
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);
96     }
97
98 }