2 * Copyright (c) 2013 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.yangtools.sal.java.api.generator.test;
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;
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.Method;
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;
33 * Previous construction of union constructor
36 * public IpAddress(Arg1 _arg1) {
44 * was incorrect and setting
46 * <code>this._value == null</code>
48 * was replaced with setting _value to correct value, for example:
50 * <code>this._value = arg1.getValue()</code> or
51 * <code>this._value = _arg1.getValue().toString().toCharArray()</code>
54 public class Bug1276Test extends BaseCompilationTest {
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());
63 generateTestSources("/compilation/bug1276", sourcesOutputDir);
65 // Test if sources are compilable
66 testCompilation(sourcesOutputDir, compiledOutputDir);
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);
73 Constructor<?> ipAddressConstructor = assertContainsConstructor(ipAddressClass, ipv4AddressClass);
74 Constructor<?> ipv4addressConstructor = assertContainsConstructor(ipv4AddressClass, String.class);
75 Constructor<?> hostConstructor = assertContainsConstructor(hostClass, ipAddressClass);
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));
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));
93 cleanUp(sourcesOutputDir, compiledOutputDir);
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);