From: Jakub Toth Date: Fri, 23 Dec 2016 21:23:59 +0000 (+0100) Subject: Bug 3147 - Binding spec v1: auto generated code by YANGTOOLS could X-Git-Tag: release/boron-sr3~12 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=mdsal.git;a=commitdiff_plain;h=3c156c1ca8c7bb91a2579811c6b0cbe555997a91 Bug 3147 - Binding spec v1: auto generated code by YANGTOOLS could be more efficient *fix generating of commas in toString() for more effecient *tests Change-Id: I5a1ac0ad8068655168824d89a9acb1f203a64639 Signed-off-by: Jakub Toth --- diff --git a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend index 300d725f97..126c3ed461 100644 --- a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend +++ b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend @@ -71,6 +71,10 @@ class BuilderTemplate extends BaseTemplate { val Set properties private static val METHOD_COMPARATOR = new AlphabeticallyTypeMemberComparator(); + 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. @@ -711,33 +715,29 @@ class BuilderTemplate extends BaseTemplate { @Override public «String.importedName» toString() { «StringBuilder.importedName» builder = new «StringBuilder.importedName» ("«type.name» ["); - boolean first = true; - - «FOR property : properties» + «FOR property : properties SEPARATOR APPEND_COMMA_WITH_WS AFTER APPEND_ENDIF» 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 (first) { - first = false; - } else { - builder.append(", "); - } + «IF !properties.empty» + «APPEND_COMMA» + «ENDIF» builder.append("«augmentField.name»="); - builder.append(«augmentField.name».values()); + builder.append(«augmentField.name».values());«"\n"» + «APPEND_END» + «ELSE» + «IF properties.empty» + «APPEND_END» + «ELSE» + «APPEND_END» + «ENDIF» «ENDIF» - return builder.append(']').toString(); } «ENDIF» ''' diff --git a/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderGeneratorTest.java b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderGeneratorTest.java new file mode 100644 index 0000000000..2944f36aa0 --- /dev/null +++ b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderGeneratorTest.java @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2016 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; + +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 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 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 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) 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 listMethodSign = new ArrayList<>(); + final MethodSignature methSign = mockMethSign(methodeName); + listMethodSign.add(methSign); + doReturn(listMethodSign).when(genType).getMethodDefinitions(); + + final List 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; + } +}