Bug 2248 - mvn clean install is failing due to checkstyle 53/12953/13
authorLadislav Borak <lborak@cisco.com>
Thu, 11 Dec 2014 12:07:59 +0000 (13:07 +0100)
committerRobert Varga <nite@hq.sk>
Thu, 12 Mar 2015 09:37:15 +0000 (09:37 +0000)
- added check in BaseTemplate.imports() if imported type has same package
  as generated type

Change-Id: Ifefb4be88c145aa222541cbb901aa9cab63b510d
Signed-off-by: Ladislav Borak <lborak@cisco.com>
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BaseTemplate.xtend
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/UnionTypedefUnusedImportTest.java [new file with mode: 0644]
code-generator/binding-java-api-generator/src/test/resources/compilation/union-typedef/union-typedef-test.yang [new file with mode: 0644]

index a9788cdef433ec811ee5185dbdc86fc5f364b6bd..63ca2b6c03dcd7f5fd5f430500dbd536e8ec9961 100644 (file)
@@ -66,7 +66,7 @@ abstract class BaseTemplate {
     protected def imports() ''' 
         «IF !importMap.empty»
             «FOR entry : importMap.entrySet»
-                «IF entry.value != fullyQualifiedName»
+                «IF !hasSamePackage(entry.value)»
                     import «entry.value».«entry.key»;
                 «ENDIF»
             «ENDFOR»
@@ -74,6 +74,17 @@ abstract class BaseTemplate {
 
     '''
 
+    /**
+     * Checks if packages of generated type and imported type is the same
+     *
+     * @param importedTypePackageNam
+     * the package name of imported type
+     * @return true if the packages are the same false otherwise
+     */
+    final private def boolean hasSamePackage(String importedTypePackageName) {
+        return type.packageName.equals(importedTypePackageName);
+    }
+
     protected abstract def CharSequence body();
 
     // Helper patterns
diff --git a/code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/UnionTypedefUnusedImportTest.java b/code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/UnionTypedefUnusedImportTest.java
new file mode 100644 (file)
index 0000000..39f3607
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2014 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.yangtools.sal.java.api.generator.test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.COMPILER_OUTPUT_PATH;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.FS;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.GENERATOR_OUTPUT_PATH;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.cleanUp;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.getSourceFiles;
+import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.testCompilation;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.HashSet;
+import java.util.List;
+import org.junit.Test;
+import org.opendaylight.yangtools.sal.binding.model.api.Type;
+import org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public class UnionTypedefUnusedImportTest extends BaseCompilationTest {
+
+    @Test
+    public void testUnionTypedefUnusedImport() throws Exception {
+        final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "union-typedef");
+        assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
+        final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "union-typedef");
+        assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
+
+        final List<File> sourceFiles = getSourceFiles("/compilation/union-typedef");
+        final SchemaContext context = parser.parseFiles(sourceFiles);
+        final List<Type> types = bindingGenerator.generateTypes(context);
+        final GeneratorJavaFile generator = new GeneratorJavaFile(new HashSet<>(types));
+        generator.generateToFile(sourcesOutputDir);
+        final boolean isUsedImport = containsImport("org.opendaylight.yang.gen.v1.org.opendaylight.yangtools.union.typedef.rev130208.TypedefUnion");
+        assertFalse(String.format("Class shouldn't contain import for this type '%s'", types.get(1).getName()),
+                isUsedImport);
+
+        testCompilation(sourcesOutputDir, compiledOutputDir);
+        cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
+    private String readFile(String path, Charset encoding) throws IOException {
+        byte[] encoded = Files.readAllBytes(Paths.get(path));
+        return new String(encoded, encoding);
+    }
+
+    private boolean containsImport(final String fullImport) throws URISyntaxException, IOException {
+        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";
+        final String fileContent = readFile(filePath, StandardCharsets.UTF_8);
+
+        if (fileContent.contains(fullImport)) {
+            return true;
+        }
+        return false;
+    }
+}
diff --git a/code-generator/binding-java-api-generator/src/test/resources/compilation/union-typedef/union-typedef-test.yang b/code-generator/binding-java-api-generator/src/test/resources/compilation/union-typedef/union-typedef-test.yang
new file mode 100644 (file)
index 0000000..b271f07
--- /dev/null
@@ -0,0 +1,19 @@
+module union-typedef-test {
+    yang-version 1;
+    namespace "org:opendaylight:yangtools:union:typedef";
+    prefix "tp";
+
+    description
+        "Test unused import";
+
+    revision "2014-11-24" {
+        reference "WILL BE DEFINED LATER";
+    }
+
+    typedef typedef-union {
+        type union {
+            type int32;
+            type string;
+        }
+    }
+}
\ No newline at end of file