d34d6e63a0b1199ce238cedce9e669b92417bcdf
[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.lang.reflect.Constructor;
15 import java.lang.reflect.Method;
16 import java.net.URL;
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;
25
26 /**
27  * Previous construction of union constructor
28  *
29  * <code>
30  * public IpAddress(Arg1 _arg1) {
31  *     super();
32  *     this._arg1 = _arg1;
33  *     this._arg2 = null;
34  *     this._value = null;
35  * }
36  * </code>
37  *
38  * was incorrect and setting
39  *
40  * <code>this._value == null</code>
41  *
42  * was replaced with setting _value to correct value, for example:
43  *
44  * <code>this._value = arg1.getValue()</code> or
45  * <code>this._value = _arg1.getValue().toString().toCharArray()</code>
46  *
47  */
48 public class Bug1276Test extends BaseCompilationTest {
49
50     @Test
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());
56
57         generateTestSources("/compilation/bug1276", sourcesOutputDir);
58
59         // Test if sources are compilable
60         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
61
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);
66
67         Constructor<?> ipAddressConstructor = CompilationTestUtils.assertContainsConstructor(ipAddressClass, ipv4AddressClass);
68         Constructor<?> ipv4addressConstructor = CompilationTestUtils.assertContainsConstructor(ipv4AddressClass, String.class);
69         Constructor<?> hostConstructor = CompilationTestUtils.assertContainsConstructor(hostClass, ipAddressClass);
70
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));
79
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));
86
87         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
88     }
89
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);
96     }
97
98 }