Revert "Bug 3147 - Binding spec v1: auto generated code by YANGTOOLS could" 54/51354/1
authorRobert Varga <rovarga@cisco.com>
Thu, 2 Feb 2017 16:23:37 +0000 (17:23 +0100)
committerRobert Varga <rovarga@cisco.com>
Thu, 2 Feb 2017 16:49:23 +0000 (17:49 +0100)
This reverts commit b88102998caf1d7ed9b9e720cc9fe4bbdc7e11bb.

Change-Id: If322b38d32e759522800af099b42d649e1237cb7
Signed-off-by: Robert Varga <rovarga@cisco.com>
(cherry picked from commit 69299b34a1b1ab2442c23cd145d7a0b3f7f0acd3)

binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend
binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderGeneratorTest.java

index 126c3ed46194c9bd59fc4a785279b1c16f24fa08..300d725f97847938ce89cc39b76ebaf324930264 100644 (file)
@@ -71,10 +71,6 @@ class BuilderTemplate extends BaseTemplate {
     val Set<GeneratedProperty> properties
 
     private static val METHOD_COMPARATOR = new AlphabeticallyTypeMemberComparator<MethodSignature>();
-    private static val APPEND_COMMA = "builder.append(\", \");";
-    private static val APPEND_COMMA_WITH_WS = "\n    "+ APPEND_COMMA + "\n}";
-    private static val APPEND_ENDIF = "    }\n";
-    private static val APPEND_END = "return builder.append(']').toString();";
 
     /**
      * Constructs new instance of this class.
@@ -715,29 +711,33 @@ class BuilderTemplate extends BaseTemplate {
             @Override
             public «String.importedName» toString() {
                 «StringBuilder.importedName» builder = new «StringBuilder.importedName» ("«type.name» [");
-                «FOR property : properties SEPARATOR APPEND_COMMA_WITH_WS AFTER APPEND_ENDIF»
+                boolean first = true;
+
+                «FOR property : properties»
                     if («property.fieldName» != null) {
+                        if (first) {
+                            first = false;
+                        } else {
+                            builder.append(", ");
+                        }
                         builder.append("«property.fieldName»=");
                         «IF property.returnType.name.contains("[")»
                             builder.append(«Arrays.importedName».toString(«property.fieldName»));
                         «ELSE»
                             builder.append(«property.fieldName»);
                         «ENDIF»
+                     }
                 «ENDFOR»
                 «IF augmentField != null»
-                    «IF !properties.empty»
-                «APPEND_COMMA»
-                    «ENDIF»
+                    if (first) {
+                        first = false;
+                    } else {
+                        builder.append(", ");
+                    }
                     builder.append("«augmentField.name»=");
-                    builder.append(«augmentField.name».values());«"\n"»
-                    «APPEND_END»
-                «ELSE»
-                    «IF properties.empty»
-                    «APPEND_END»
-                    «ELSE»
-            «APPEND_END»
-                    «ENDIF»
+                    builder.append(«augmentField.name».values());
                 «ENDIF»
+                return builder.append(']').toString();
             }
         «ENDIF»
     '''
index 2944f36aa0a544a48449a78eae02e902501c4c30..f7a0c4586a86b3f8ef47d1c03036bcd7f2057dba 100644 (file)
 package org.opendaylight.yangtools.sal.java.api.generator;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.spy;
 
-import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
 import org.junit.Test;
-import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
-import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
-import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
 import org.opendaylight.yangtools.sal.binding.model.api.Type;
 
-@SuppressWarnings("deprecation")
 public class BuilderGeneratorTest {
 
-    private static final String PROPERTIES_FIELD_NAME = "properties";
-    private final String firstPartOfGenToStringMethod =
-            "@Override\npublic java.lang.String toString() {\n    java.lang.StringBuilder builder = new java.lang.StringBuilder ("
-                    + "\"test [\");";
-    private final String lastPartOfGenToStringMethode = "\n    return builder.append(']').toString();\n}";
-    private final String argPartOfGenToStringMethode =
-            "\n    builder.append(\"augmentation=\");\n    builder.append(augmentation.values());";
-    private final String comaPartOfAppend = "builder.append(\", \");";
-    private final String TEST = "test";
-
     @Test
     public void basicTest() throws Exception {
         assertEquals("", new BuilderGenerator().generate(mock(Type.class)));
     }
-
-    @Test
-    public void builderTemplateGenerateToStringWithPropertyTest() throws Exception {
-        final GeneratedType genType = mockGenType("get" + this.TEST);
-        final String generateToString = genToString(genType).toString();
-        assertTrue(generateToString.contains(this.firstPartOfGenToStringMethod
-                + "\n    if (_test != null) {\n        builder.append(\"_test=\");\n        builder.append(_test);\n    }"
-                + this.lastPartOfGenToStringMethode));
-    }
-
-    @Test
-    public void builderTemplateGenerateToStringWithoutAnyPropertyTest() throws Exception {
-        final GeneratedType genType = mockGenType(this.TEST);
-        final String generateToString = genToString(genType).toString();
-        assertTrue(generateToString
-                .contains(this.firstPartOfGenToStringMethod + this.lastPartOfGenToStringMethode));
-    }
-
-    @Test
-    public void builderTemplateGenerateToStringWithMorePropertiesTest() throws Exception {
-        final GeneratedType genType = mockGenTypeMoreMeth("get" + this.TEST);
-        final String generateToString = genToString(genType).toString();
-        assertTrue(generateToString.contains(this.firstPartOfGenToStringMethod
-                + "\n    if (_test1 != null) {\n        builder.append(\"_test1=\");\n        builder.append(_test1);"
-                + "\n        " + this.comaPartOfAppend + "\n    }"
-                + "\n    if (_test2 != null) {\n        builder.append(\"_test2=\");\n        builder.append(_test2);\n    }"
-                + this.lastPartOfGenToStringMethode));
-    }
-
-    @Test
-    public void builderTemplateGenerateToStringWithoutPropertyWithAugmentTest() throws Exception {
-        final GeneratedType genType = mockGenType(this.TEST);
-        mockAugment(genType);
-        final String generateToString = genToString(genType).toString();
-        assertTrue(generateToString.contains(this.firstPartOfGenToStringMethod + this.argPartOfGenToStringMethode
-                + this.lastPartOfGenToStringMethode));
-    }
-
-    @Test
-    public void builderTemplateGenerateToStringWithPropertyWithAugmentTest() throws Exception {
-        final GeneratedType genType = mockGenType("get" + this.TEST);
-        mockAugment(genType);
-        final String generateToString = genToString(genType).toString();
-        assertTrue(generateToString.contains(this.firstPartOfGenToStringMethod
-                + "\n    if (_test != null) {\n        builder.append(\"_test=\");\n        builder.append(_test);\n    }"
-                + "\n    " + this.comaPartOfAppend + this.argPartOfGenToStringMethode
-                + this.lastPartOfGenToStringMethode));
-    }
-
-    @Test
-    public void builderTemplateGenerateToStringWithMorePropertiesWithAugmentTest() throws Exception {
-        final GeneratedType genType = mockGenTypeMoreMeth("get" + this.TEST);
-        mockAugment(genType);
-        final String generateToString = genToString(genType).toString();
-        assertTrue(generateToString.contains(this.firstPartOfGenToStringMethod
-                + "\n    if (_test1 != null) {\n        builder.append(\"_test1=\");\n        builder.append(_test1);\n        "
-                + this.comaPartOfAppend + "\n    }"
-                + "\n    if (_test2 != null) {\n        builder.append(\"_test2=\");\n        builder.append(_test2);\n    }"
-                + "\n    " + this.comaPartOfAppend
-                + this.argPartOfGenToStringMethode + this.lastPartOfGenToStringMethode));
-    }
-
-    private void mockAugment(final GeneratedType genType) {
-        final List<Type> impls = new ArrayList<>();
-        final Type impl = mock(Type.class);
-        doReturn("org.opendaylight.yangtools.yang.binding.Augmentable").when(impl).getFullyQualifiedName();
-        impls.add(impl);
-        doReturn(impls).when(genType).getImplements();
-    }
-
-    private GeneratedType mockGenTypeMoreMeth(final String methodeName) {
-        final GeneratedType genType = spy(GeneratedType.class);
-        doReturn(this.TEST).when(genType).getName();
-        doReturn(this.TEST).when(genType).getPackageName();
-
-        final List<MethodSignature> listMethodSign = new ArrayList<>();
-        for (int i = 0; i < 2; i++) {
-            final MethodSignature methSign = mockMethSign(methodeName + (i + 1));
-            listMethodSign.add(methSign);
-        }
-        doReturn(listMethodSign).when(genType).getMethodDefinitions();
-
-        final List<Type> impls = new ArrayList<>();
-        doReturn(impls).when(genType).getImplements();
-        return genType;
-    }
-
-    @SuppressWarnings("unchecked")
-    private CharSequence genToString(final GeneratedType genType)
-            throws NoSuchFieldException, IllegalAccessException {
-        final BuilderTemplate bt = new BuilderTemplate(genType);
-        final Field propertiesField = bt.getClass().getDeclaredField(PROPERTIES_FIELD_NAME);
-        propertiesField.setAccessible(true);
-        return bt.generateToString((Collection<GeneratedProperty>) propertiesField.get(bt));
-    }
-
-    private GeneratedType mockGenType(final String methodeName) {
-        final GeneratedType genType = spy(GeneratedType.class);
-        doReturn(this.TEST).when(genType).getName();
-        doReturn(this.TEST).when(genType).getPackageName();
-
-        final List<MethodSignature> listMethodSign = new ArrayList<>();
-        final MethodSignature methSign = mockMethSign(methodeName);
-        listMethodSign.add(methSign);
-        doReturn(listMethodSign).when(genType).getMethodDefinitions();
-
-        final List<Type> impls = new ArrayList<>();
-        doReturn(impls).when(genType).getImplements();
-        return genType;
-    }
-
-    private MethodSignature mockMethSign(final String methodeName) {
-        final MethodSignature methSign = mock(MethodSignature.class);
-        doReturn(methodeName).when(methSign).getName();
-        final Type methType = mock(Type.class);
-        doReturn(this.TEST).when(methType).getName();
-        doReturn(this.TEST).when(methType).getPackageName();
-        doReturn(methType).when(methSign).getReturnType();
-        return methSign;
-    }
-}
+}
\ No newline at end of file