import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
assertEquals("", new BuilderGenerator().generate(mock(Type.class)));
}
+ @Test
+ public void builderTemplateGenerateHashcodeWithPropertyTest() {
+ final GeneratedType genType = mockGenType("get" + TEST);
+
+ assertEquals("/**\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 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 test.@NonNull test obj) {\n"
+ + " final int prime = 31;\n"
+ + " int result = 1;\n"
+ + " result = prime * result + Objects.hashCode(obj.getTest());\n"
+ + " return result;\n"
+ + "}\n", genHashCode(genType).toString());
+ }
+
+ @Test
+ public void builderTemplateGenerateHashCodeWithoutAnyPropertyTest() throws Exception {
+ assertEquals("", genHashCode(mockGenType(TEST)).toString());
+ }
+
+ @Test
+ public void builderTemplateGenerateHashCodeWithMorePropertiesTest() throws Exception {
+ assertEquals("/**\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 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 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"
+ + " return result;\n"
+ + "}\n", genHashCode(mockGenTypeMoreMeth("get" + TEST)).toString());
+ }
+
+ @Test
+ public void builderTemplateGenerateHashCodeWithoutPropertyWithAugmentTest() throws Exception {
+ assertEquals("/**\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 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 test.@NonNull test obj) {\n"
+ + " final int prime = 31;\n"
+ + " int result = 1;\n"
+ + " result = prime * result + obj.augmentations().hashCode();\n"
+ + " return result;\n"
+ + "}\n", genHashCode(mockAugment(mockGenType(TEST))).toString());
+ }
+
+ @Test
+ public void builderTemplateGenerateHashCodeWithPropertyWithAugmentTest() throws Exception {
+ assertEquals("/**\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 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 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 + obj.augmentations().hashCode();\n"
+ + " return result;\n"
+ + "}\n", genHashCode(mockAugment(mockGenType("get" + TEST))).toString());
+ }
+
+ @Test
+ public void builderTemplateGenerateHashCodeWithMorePropertiesWithAugmentTest() throws Exception {
+ assertEquals("/**\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 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 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 + obj.augmentations().hashCode();\n"
+ + " return result;\n"
+ + "}\n", genHashCode(mockAugment(mockGenTypeMoreMeth("get" + TEST))).toString());
+ }
+
@Test
public void builderTemplateGenerateToStringWithPropertyTest() {
final GeneratedType genType = mockGenType("get" + TEST);
+ " * @return {@link String} value of data modeled by this interface.\n"
+ " * @throws NullPointerException if {@code obj} is null\n"
+ " */\n"
- + "static String bindingToString(final test.test 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"
+ " return helper.toString();\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 test.test obj) {\n"
+ + "static String bindingToString(final test.@NonNull test obj) {\n"
+ " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
+ " return helper.toString();\n"
+ "}\n", genToString(mockGenType(TEST)).toString());
+ " * @return {@link String} value of data modeled by this interface.\n"
+ " * @throws NullPointerException if {@code obj} is null\n"
+ " */\n"
- + "static String bindingToString(final test.test 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"
+ " * Implementations of this interface are encouraged to defer to this method to get consistent string"
+ "\n * representations across all implementations.\n"
+ " *\n"
- + " * @param <T$$> 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 <T$$ extends test.test & AugmentationHolder<test.test>> 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"
+ " return helper.toString();\n"
+ " * Implementations of this interface are encouraged to defer to this method to get consistent string"
+ "\n * representations across all implementations.\n"
+ " *\n"
- + " * @param <T$$> 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 <T$$ extends test.test & AugmentationHolder<test.test>> 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"
+ " * Implementations of this interface are encouraged to defer to this method to get consistent string"
+ "\n * representations across all implementations.\n"
+ " *\n"
- + " * @param <T$$> 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 <T$$ extends test.test & AugmentationHolder<test.test>> 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"
return new InterfaceTemplate(genType).generateBindingToString();
}
+ private static CharSequence genHashCode(final GeneratedType genType) {
+ return new InterfaceTemplate(genType).generateBindingHashCode();
+ }
+
private static GeneratedType mockGenType(final String methodeName) {
final GeneratedType genType = spy(GeneratedType.class);
doReturn(TYPE_NAME).when(genType).getIdentifier();
final MethodSignature methSign = mock(MethodSignature.class);
doReturn(methodeName).when(methSign).getName();
final Type methType = mock(Type.class);
+ when(methType.getFullyQualifiedName()).thenCallRealMethod();
doReturn(TYPE_NAME).when(methType).getIdentifier();
+ doReturn(TEST).when(methType).getName();
doReturn(methType).when(methSign).getReturnType();
doReturn(ValueMechanics.NORMAL).when(methSign).getMechanics();
return methSign;