/*
* Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.mdsal.binding.java.api.generator.test;
import static org.junit.Assert.assertTrue;
import com.google.common.collect.ImmutableSet;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
import org.opendaylight.mdsal.binding.model.api.Type;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
/**
* Previous construction of union constructor
*
*
* public IpAddress(Arg1 _arg1) {
* super();
* this._arg1 = _arg1;
* this._arg2 = null;
* this._value = null;
* }
*
*
* was incorrect and setting
*
* this._value == null
*
* was replaced with setting _value to correct value, for example:
*
* this._value = arg1.getValue()
or
* this._value = _arg1.getValue().toString().toCharArray()
*
*/
public class Bug1276Test extends BaseCompilationTest {
@Test
public void test() throws Exception {
final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1276");
final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1276");
generateTestSources("/compilation/bug1276", sourcesOutputDir);
// Test if sources are compilable
CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
Class> ipAddressClass = Class.forName(CompilationTestUtils.BASE_PKG + ".test.yang.union.rev140715.IpAddress", true, loader);
Class> ipv4AddressClass = Class.forName(CompilationTestUtils.BASE_PKG + ".test.yang.union.rev140715.Ipv4Address", true, loader);
Class> hostClass = Class.forName(CompilationTestUtils.BASE_PKG + ".test.yang.union.rev140715.Host", true, loader);
Constructor> ipAddressConstructor = CompilationTestUtils.assertContainsConstructor(ipAddressClass, ipv4AddressClass);
Constructor> ipv4addressConstructor = CompilationTestUtils.assertContainsConstructor(ipv4AddressClass, String.class);
Constructor> hostConstructor = CompilationTestUtils.assertContainsConstructor(hostClass, ipAddressClass);
// test IpAddress with Ipv4Address argument
Object ipv4address = ipv4addressConstructor.newInstance("192.168.0.1");
Object ipAddress = ipAddressConstructor.newInstance(ipv4address);
Method getValue = ipAddressClass.getDeclaredMethod("getValue");
char[] expected = "192.168.0.1".toCharArray();
Object actual = getValue.invoke(ipAddress);
assertTrue(actual instanceof char[]);
assertTrue(Arrays.equals(expected, (char[]) actual));
// test Host with IpAddress argument
Object host = hostConstructor.newInstance(ipAddress);
getValue = hostClass.getDeclaredMethod("getValue");
actual = getValue.invoke(host);
assertTrue(actual instanceof char[]);
assertTrue(Arrays.equals(expected, (char[]) actual));
CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
}
private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir)
throws IOException, URISyntaxException {
final List sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath);
final SchemaContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
final List types = bindingGenerator.generateTypes(context);
final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
generator.generateToFile(sourcesOutputDir);
}
}