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