2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.java.api.generator.test;
10 import static org.junit.Assert.assertTrue;
12 import com.google.common.collect.ImmutableSet;
14 import java.io.IOException;
15 import java.lang.reflect.Constructor;
16 import java.lang.reflect.Method;
17 import java.net.URISyntaxException;
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;
29 * Previous construction of union constructor
32 * public IpAddress(Arg1 _arg1) {
40 * was incorrect and setting
42 * <code>this._value == null</code>
44 * was replaced with setting _value to correct value, for example:
46 * <code>this._value = arg1.getValue()</code> or
47 * <code>this._value = _arg1.getValue().toString().toCharArray()</code>
50 public class Bug1276Test extends BaseCompilationTest {
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);
58 // Test if sources are compilable
59 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
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);
66 Constructor<?> ipAddressConstructor = CompilationTestUtils.assertContainsConstructor(ipAddressClass, ipv4AddressClass);
67 Constructor<?> ipv4addressConstructor = CompilationTestUtils.assertContainsConstructor(ipv4AddressClass, String.class);
68 Constructor<?> hostConstructor = CompilationTestUtils.assertContainsConstructor(hostClass, ipAddressClass);
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));
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));
86 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
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);