Bug 1672 - Generated javadoc contains symbols of javadoc comment tags 53/10753/6
authorLadislav Borak <lborak@cisco.com>
Wed, 3 Sep 2014 13:16:40 +0000 (15:16 +0200)
committerLadislav Borak <lborak@cisco.com>
Fri, 5 Sep 2014 09:06:40 +0000 (11:06 +0200)
- added extension method into BaseTemplate to replace javadoc comment tags
  in description:
  used &#42;&#47; instaed of */

Change-Id: I754812fac0be45d037fe3a65c60c63c11150b5c8
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/EndodingInJavaDocTest.java [new file with mode: 0644]
code-generator/binding-java-api-generator/src/test/resources/compilation/encoding-javadoc/encoding-javadoc.yang [new file with mode: 0644]

index c258e12092b2188c444203470e6131430e1b4824..26c95d2fa3236738d775797a41816f4c458adaf8 100644 (file)
@@ -157,9 +157,7 @@ abstract class BaseTemplate {
     def protected CharSequence asJavadoc(String comment) {
         if(comment == null) return ''
         var txt = comment
-        if (txt.contains("*/")) {
-            txt = txt.replace("*/", "&#42;&#47;")
-        }
+
         txt = comment.trim
         txt = formatToParagraph(txt)
 
@@ -189,7 +187,7 @@ abstract class BaseTemplate {
     }
 
     def protected String formatDataForJavaDoc(GeneratedType type) {
-        val typeDescription = type.getDescription();
+        val typeDescription = type.getDescription().encodeJavadocSymbols;
 
         return '''
             «IF !typeDescription.nullOrEmpty»
@@ -198,6 +196,13 @@ abstract class BaseTemplate {
         '''.toString
     }
 
+    def encodeJavadocSymbols(String description) {
+        if (!description.nullOrEmpty) {
+            return description.replace("*/", "&#42;&#47;")
+        }
+        return description;
+    }
+
     def asLink(String text) {
         val StringBuilder sb = new StringBuilder()
         var tempText = text
@@ -230,7 +235,7 @@ abstract class BaseTemplate {
         var StringBuilder lineBuilder = new StringBuilder();
         var boolean isFirstElementOnNewLineEmptyChar = false;
 
-        formattedText = formattedText.replace("*/", "&#42;&#47;")
+        formattedText = formattedText.encodeJavadocSymbols
         formattedText = formattedText.replace(NEW_LINE, "")
         formattedText = formattedText.replace("\t", "")
         formattedText = formattedText.replaceAll(" +", " ");
@@ -254,8 +259,9 @@ abstract class BaseTemplate {
                 lineBuilder.setLength(0)
                 sb.append(NEW_LINE)
 
-                if(nextElement.toString == ' ')
+                if(nextElement.toString == ' ') {
                     isFirstElementOnNewLineEmptyChar = !isFirstElementOnNewLineEmptyChar;
+                }
             }
 
             if(isFirstElementOnNewLineEmptyChar) {
diff --git a/code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/EndodingInJavaDocTest.java b/code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/EndodingInJavaDocTest.java
new file mode 100644 (file)
index 0000000..80b073f
--- /dev/null
@@ -0,0 +1,43 @@
+package org.opendaylight.yangtools.sal.java.api.generator.test;
+
+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.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;
+
+/**
+ * Test if generated classes from yang file is compilable, generated javadoc comments contains
+ * symbols as javadoc comment tag, which caused of compilation problem.
+ */
+public class EndodingInJavaDocTest extends BaseCompilationTest {
+
+    @Test
+    public void testAugmentToUsesInAugment() throws Exception {
+        final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "encoding-javadoc");
+        assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
+        final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "encoding-javadoc");
+        assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
+
+        final List<File> sourceFiles = getSourceFiles("/compilation/encoding-javadoc");
+        final SchemaContext context = parser.parseFiles(sourceFiles);
+        final List<Type> types = bindingGenerator.generateTypes(context);
+        final GeneratorJavaFile generator = new GeneratorJavaFile(new HashSet<>(types));
+        generator.generateToFile(sourcesOutputDir);
+
+        // Test if sources are compilable
+        testCompilation(sourcesOutputDir, compiledOutputDir);
+        cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
+}
diff --git a/code-generator/binding-java-api-generator/src/test/resources/compilation/encoding-javadoc/encoding-javadoc.yang b/code-generator/binding-java-api-generator/src/test/resources/compilation/encoding-javadoc/encoding-javadoc.yang
new file mode 100644 (file)
index 0000000..c66cef9
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2013 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
+ */
+module encoding-javadoc {
+    yang-version 1;
+    namespace "urn:opendaylight:encoding-javadoc";
+    prefix "enc";
+
+    revision "2014-04-09" {
+    }
+
+    container cont1 {
+        description "/e.g. 1/*/*";
+        reference "RFC 6020 - http://tools.ietf.org/html/rfc6020";
+
+        list list1 {
+            description "/e.g. 1/*/*";
+            reference "RFC 6020 - http://tools.ietf.org/html/rfc6020";
+
+            key "topology-id";
+            leaf topology-id {
+                description "/e.g. 1/*/*";
+                reference "RFC 6020 - http://tools.ietf.org/html/rfc6020";
+                type int32;
+            }
+            uses link1;
+        }
+    }
+
+    grouping link1 {
+        list link1 {
+            description "/e.g. 1/*/*";
+            reference "RFC 6020 - http://tools.ietf.org/html/rfc6020";
+            key "link-id";
+            uses link-attributes;
+        }
+    }
+
+    grouping link-attributes {
+        leaf link-id {
+            description "/e.g. 1/*/*";
+            reference "RFC 6020 - http://tools.ietf.org/html/rfc6020";
+            type int8;
+        }
+    }
+
+    leaf inclusion-rulez {
+        description "/e.g. 1/*/*";
+        reference "RFC 6020 http://technet.com";
+
+        type string;
+    }
+
+    leaf inclusion-rule {
+        description "/e.g. 1/*/*";
+        default include;
+
+        type enumeration {
+            enum include {
+                description
+                    "/e.g. 1/*/*";
+            }
+            enum exclude {
+                description
+                    "/e.g. 1/*/*";
+            }
+        }
+    }
+}