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.assertFalse;
11 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.FS;
12 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.GENERATOR_OUTPUT_PATH;
13 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.cleanUp;
14 import static org.opendaylight.mdsal.binding.java.api.generator.test.CompilationTestUtils.testCompilation;
17 import java.io.IOException;
18 import java.net.URISyntaxException;
19 import java.nio.charset.Charset;
20 import java.nio.charset.StandardCharsets;
21 import java.nio.file.Files;
22 import java.nio.file.Paths;
23 import java.util.List;
24 import org.junit.Test;
25 import org.opendaylight.mdsal.binding.model.api.Type;
27 public class UnionTypedefUnusedImportTest extends BaseCompilationTest {
30 public void testUnionTypedefUnusedImport() throws Exception {
31 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("union-typedef");
32 final List<Type> types = generateTestSources("/compilation/union-typedef", sourcesOutputDir);
34 final boolean isUsedImport = containsImport(
35 "org.opendaylight.yang.gen.v1.org.opendaylight.yangtools.union.typedef.rev130208.TypedefUnion");
36 assertFalse(String.format("Class shouldn't contain import for this type '%s'", types.get(1).getName()),
39 final File compiledOutputDir = CompilationTestUtils.compilerOutput("union-typedef");
40 testCompilation(sourcesOutputDir, compiledOutputDir);
41 cleanUp(sourcesOutputDir, compiledOutputDir);
44 private static String readFile(final String path, final Charset encoding) throws IOException {
45 byte[] encoded = Files.readAllBytes(Paths.get(path));
46 return new String(encoded, encoding);
49 private static boolean containsImport(final String fullImport) throws URISyntaxException, IOException {
50 final String filePath = GENERATOR_OUTPUT_PATH + FS + "union-typedef" + FS + "org" + FS + "opendaylight" + FS
51 + "yang" + FS + "gen" + FS + "v1" + FS + "org" + FS + "opendaylight" + FS + "yangtools" + FS + "union"
52 + FS + "typedef" + FS + "rev141124" + FS + "TypedefUnionBuilder.java";
53 final String fileContent = readFile(filePath, StandardCharsets.UTF_8);
55 if (fileContent.contains(fullImport)) {