2 * Copyright (c) 2014 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.assertFalse;
11 import static org.junit.Assert.assertTrue;
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.cleanUp;
16 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.getSourceFiles;
17 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.testCompilation;
20 import java.io.IOException;
21 import java.net.URISyntaxException;
22 import java.nio.charset.Charset;
23 import java.nio.charset.StandardCharsets;
24 import java.nio.file.Files;
25 import java.nio.file.Paths;
26 import java.util.HashSet;
27 import java.util.List;
28 import org.junit.Test;
29 import org.opendaylight.yangtools.sal.binding.model.api.Type;
30 import org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 public class UnionTypedefUnusedImportTest extends BaseCompilationTest {
36 public void testUnionTypedefUnusedImport() throws Exception {
37 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "union-typedef");
38 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
39 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "union-typedef");
40 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
42 final List<File> sourceFiles = getSourceFiles("/compilation/union-typedef");
43 final SchemaContext context = parser.parseFiles(sourceFiles);
44 final List<Type> types = bindingGenerator.generateTypes(context);
45 final GeneratorJavaFile generator = new GeneratorJavaFile(new HashSet<>(types));
46 generator.generateToFile(sourcesOutputDir);
47 final boolean isUsedImport = containsImport("org.opendaylight.yang.gen.v1.org.opendaylight.yangtools.union.typedef.rev130208.TypedefUnion");
48 assertFalse(String.format("Class shouldn't contain import for this type '%s'", types.get(1).getName()),
51 testCompilation(sourcesOutputDir, compiledOutputDir);
52 cleanUp(sourcesOutputDir, compiledOutputDir);
55 private String readFile(String path, Charset encoding) throws IOException {
56 byte[] encoded = Files.readAllBytes(Paths.get(path));
57 return new String(encoded, encoding);
60 private boolean containsImport(final String fullImport) throws URISyntaxException, IOException {
61 final String filePath = GENERATOR_OUTPUT_PATH + FS + "union-typedef" + FS + "org" + FS + "opendaylight" + FS + "yang" + FS + "gen" + FS + "v1" + FS + "org" + FS + "opendaylight" + FS + "yangtools" + FS + "union" + FS + "typedef" + FS + "rev141124" + FS + "TypedefUnionBuilder.java";
62 final String fileContent = readFile(filePath, StandardCharsets.UTF_8);
64 if (fileContent.contains(fullImport)) {