X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-java-api-generator%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fjava%2Fapi%2Fgenerator%2FBuilderGeneratorTest.java;h=67aa9a73e9fe6ed7e973d5ff488107075edd7246;hb=adc23d90f8d367517b854bcd7533578c148812e8;hp=79cbe70982680c2fac3110fc5033a422e9fb962d;hpb=95de0ae3f5617dcc910db29c3581d09186e527a6;p=mdsal.git diff --git a/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderGeneratorTest.java b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderGeneratorTest.java index 79cbe70982..67aa9a73e9 100644 --- a/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderGeneratorTest.java +++ b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderGeneratorTest.java @@ -8,6 +8,7 @@ package org.opendaylight.mdsal.binding.java.api.generator; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doCallRealMethod; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; @@ -15,6 +16,7 @@ import static org.mockito.Mockito.spy; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; +import org.eclipse.xtend2.lib.StringConcatenation; import org.junit.Test; import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingGenerator; import org.opendaylight.mdsal.binding.model.api.GeneratedType; @@ -38,7 +40,7 @@ public class BuilderGeneratorTest { public void builderTemplateGenerateHashcodeWithPropertyTest() { final GeneratedType genType = mockGenType("get" + TEST); - assertEquals("/**\n" + assertXtendEquals("/**\n" + " * Default implementation of {@link Object#hashCode()} contract for this interface.\n" + " * Implementations of this interface are encouraged to defer to this method to get consistent" + " hashing\n" @@ -63,7 +65,7 @@ public class BuilderGeneratorTest { @Test public void builderTemplateGenerateHashCodeWithMorePropertiesTest() throws Exception { - assertEquals("/**\n" + assertXtendEquals("/**\n" + " * Default implementation of {@link Object#hashCode()} contract for this interface.\n" + " * Implementations of this interface are encouraged to defer to this method to get consistent" + " hashing\n" @@ -84,72 +86,69 @@ public class BuilderGeneratorTest { @Test public void builderTemplateGenerateHashCodeWithoutPropertyWithAugmentTest() throws Exception { - assertEquals("/**\n" + assertXtendEquals("/**\n" + " * Default implementation of {@link Object#hashCode()} contract for this interface.\n" + " * Implementations of this interface are encouraged to defer to this method to get consistent" + " hashing\n" + " * results across all implementations.\n" + " *\n" - + " * @param implementation type, which has to also implement AugmentationHolder interface\n" - + " * contract.\n" + " * @param obj Object for which to generate hashCode() result.\n" + " * @return Hash code value of data modeled by this interface.\n" + " * @throws NullPointerException if {@code obj} is null\n" + " */\n" - + "static > int bindingHashCode(final @NonNull T$$ obj) {" - + "\n" + + "static int bindingHashCode(final test.@NonNull test obj) {\n" + " final int prime = 31;\n" + " int result = 1;\n" - + " result = prime * result + CodeHelpers.hashAugmentations(obj);\n" + + " for (var augmentation : obj.augmentations().values()) {\n" + + " result += augmentation.hashCode();\n" + + " }\n" + " return result;\n" + "}\n", genHashCode(mockAugment(mockGenType(TEST))).toString()); } @Test public void builderTemplateGenerateHashCodeWithPropertyWithAugmentTest() throws Exception { - assertEquals("/**\n" + assertXtendEquals("/**\n" + " * Default implementation of {@link Object#hashCode()} contract for this interface.\n" + " * Implementations of this interface are encouraged to defer to this method to get consistent" + " hashing\n" + " * results across all implementations.\n" + " *\n" - + " * @param implementation type, which has to also implement AugmentationHolder interface\n" - + " * contract.\n" + " * @param obj Object for which to generate hashCode() result.\n" + " * @return Hash code value of data modeled by this interface.\n" + " * @throws NullPointerException if {@code obj} is null\n" + " */\n" - + "static > int bindingHashCode(final @NonNull T$$ obj) {" - + "\n" + + "static int bindingHashCode(final test.@NonNull test obj) {\n" + " final int prime = 31;\n" + " int result = 1;\n" + " result = prime * result + Objects.hashCode(obj.getTest());\n" - + " result = prime * result + CodeHelpers.hashAugmentations(obj);\n" + + " for (var augmentation : obj.augmentations().values()) {\n" + + " result += augmentation.hashCode();\n" + + " }\n" + " return result;\n" + "}\n", genHashCode(mockAugment(mockGenType("get" + TEST))).toString()); } @Test public void builderTemplateGenerateHashCodeWithMorePropertiesWithAugmentTest() throws Exception { - assertEquals("/**\n" + assertXtendEquals("/**\n" + " * Default implementation of {@link Object#hashCode()} contract for this interface.\n" + " * Implementations of this interface are encouraged to defer to this method to get consistent" + " hashing\n" + " * results across all implementations.\n" + " *\n" - + " * @param implementation type, which has to also implement AugmentationHolder interface\n" - + " * contract.\n" + " * @param obj Object for which to generate hashCode() result.\n" + " * @return Hash code value of data modeled by this interface.\n" + " * @throws NullPointerException if {@code obj} is null\n" + " */\n" - + "static > int bindingHashCode(final @NonNull T$$ obj) {" - + "\n" + + "static int bindingHashCode(final test.@NonNull test obj) {\n" + " final int prime = 31;\n" + " int result = 1;\n" + " result = prime * result + Objects.hashCode(obj.getTest1());\n" + " result = prime * result + Objects.hashCode(obj.getTest2());\n" - + " result = prime * result + CodeHelpers.hashAugmentations(obj);\n" + + " for (var augmentation : obj.augmentations().values()) {\n" + + " result += augmentation.hashCode();\n" + + " }\n" + " return result;\n" + "}\n", genHashCode(mockAugment(mockGenTypeMoreMeth("get" + TEST))).toString()); } @@ -158,7 +157,7 @@ public class BuilderGeneratorTest { public void builderTemplateGenerateToStringWithPropertyTest() { final GeneratedType genType = mockGenType("get" + TEST); - assertEquals("/**\n" + assertXtendEquals("/**\n" + " * Default implementation of {@link Object#toString()} contract for this interface.\n" + " * Implementations of this interface are encouraged to defer to this method to get consistent string" + "\n * representations across all implementations.\n" @@ -176,7 +175,7 @@ public class BuilderGeneratorTest { @Test public void builderTemplateGenerateToStringWithoutAnyPropertyTest() throws Exception { - assertEquals("/**\n" + assertXtendEquals("/**\n" + " * Default implementation of {@link Object#toString()} contract for this interface.\n" + " * Implementations of this interface are encouraged to defer to this method to get consistent string" + "\n * representations across all implementations.\n" @@ -193,7 +192,7 @@ public class BuilderGeneratorTest { @Test public void builderTemplateGenerateToStringWithMorePropertiesTest() throws Exception { - assertEquals("/**\n" + assertXtendEquals("/**\n" + " * Default implementation of {@link Object#toString()} contract for this interface.\n" + " * Implementations of this interface are encouraged to defer to this method to get consistent string" + "\n * representations across all implementations.\n" @@ -212,66 +211,57 @@ public class BuilderGeneratorTest { @Test public void builderTemplateGenerateToStringWithoutPropertyWithAugmentTest() throws Exception { - assertEquals("/**\n" + assertXtendEquals("/**\n" + " * Default implementation of {@link Object#toString()} contract for this interface.\n" + " * Implementations of this interface are encouraged to defer to this method to get consistent string" + "\n * representations across all implementations.\n" + " *\n" - + " * @param implementation type, which has to also implement AugmentationHolder interface\n" - + " * contract.\n" + " * @param obj Object for which to generate toString() result.\n" + " * @return {@link String} value of data modeled by this interface.\n" + " * @throws NullPointerException if {@code obj} is null\n" + " */\n" - + "static > String bindingToString(final @NonNull" - + " T$$ obj) {\n" + + "static String bindingToString(final test.@NonNull test obj) {\n" + " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n" - + " CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n" + + " CodeHelpers.appendAugmentations(helper, \"augmentation\", obj);\n" + " return helper.toString();\n" + "}\n", genToString(mockAugment(mockGenType(TEST))).toString()); } @Test public void builderTemplateGenerateToStringWithPropertyWithAugmentTest() throws Exception { - assertEquals("/**\n" + assertXtendEquals("/**\n" + " * Default implementation of {@link Object#toString()} contract for this interface.\n" + " * Implementations of this interface are encouraged to defer to this method to get consistent string" + "\n * representations across all implementations.\n" + " *\n" - + " * @param implementation type, which has to also implement AugmentationHolder interface\n" - + " * contract.\n" + " * @param obj Object for which to generate toString() result.\n" + " * @return {@link String} value of data modeled by this interface.\n" + " * @throws NullPointerException if {@code obj} is null\n" + " */\n" - + "static > String bindingToString(final @NonNull" - + " T$$ obj) {\n" + + "static String bindingToString(final test.@NonNull test obj) {\n" + " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n" + " CodeHelpers.appendValue(helper, \"test\", obj.gettest());\n" - + " CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n" + + " CodeHelpers.appendAugmentations(helper, \"augmentation\", obj);\n" + " return helper.toString();\n" + "}\n", genToString(mockAugment(mockGenType("get" + TEST))).toString()); } @Test public void builderTemplateGenerateToStringWithMorePropertiesWithAugmentTest() throws Exception { - assertEquals("/**\n" + assertXtendEquals("/**\n" + " * Default implementation of {@link Object#toString()} contract for this interface.\n" + " * Implementations of this interface are encouraged to defer to this method to get consistent string" + "\n * representations across all implementations.\n" + " *\n" - + " * @param implementation type, which has to also implement AugmentationHolder interface\n" - + " * contract.\n" + " * @param obj Object for which to generate toString() result.\n" + " * @return {@link String} value of data modeled by this interface.\n" + " * @throws NullPointerException if {@code obj} is null\n" + " */\n" - + "static > String bindingToString(final @NonNull" - + " T$$ obj) {\n" + + "static String bindingToString(final test.@NonNull test obj) {\n" + " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n" + " CodeHelpers.appendValue(helper, \"test1\", obj.gettest1());\n" + " CodeHelpers.appendValue(helper, \"test2\", obj.gettest2());\n" - + " CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n" + + " CodeHelpers.appendAugmentations(helper, \"augmentation\", obj);\n" + " return helper.toString();\n" + "}\n", genToString(mockAugment(mockGenTypeMoreMeth("get" + TEST))).toString()); } @@ -280,8 +270,11 @@ public class BuilderGeneratorTest { public void builderTemplateGenerateToEqualsComparingOrderTest() { final EffectiveModelContext context = YangParserTestUtils.parseYangResource( "/test-types.yang"); - final List types = new DefaultBindingGenerator().generateTypes(context); - final BuilderTemplate bt = BuilderGenerator.templateForType((GeneratedType) types.get(19)); + final List types = new DefaultBindingGenerator().generateTypes(context); + assertEquals(29, types.size()); + + final BuilderTemplate bt = BuilderGenerator.templateForType( + types.stream().filter(t -> t.getName().equals("Nodes")).findFirst().orElseThrow()); final List sortedProperties = bt.properties.stream() .sorted(ByTypeMemberComparator.getInstance()) @@ -359,10 +352,17 @@ public class BuilderGeneratorTest { final MethodSignature methSign = mock(MethodSignature.class); doReturn(methodeName).when(methSign).getName(); final Type methType = mock(Type.class); + doCallRealMethod().when(methType).getFullyQualifiedName(); doReturn(TYPE_NAME).when(methType).getIdentifier(); doReturn(TEST).when(methType).getName(); doReturn(methType).when(methSign).getReturnType(); doReturn(ValueMechanics.NORMAL).when(methSign).getMechanics(); return methSign; } + + // Xtend's StringConcatenation is using runtime-configured line separator, which can change between runs, notably + // it has a different value on Windows. Make sure we account for that. + private static void assertXtendEquals(final String expected, final String actual) { + assertEquals(expected.replace("\n", StringConcatenation.DEFAULT_LINE_DELIMITER), actual); + } }