1 package org.pojomatic; 2 3 import static org.testng.Assert.*; 4 5 import org.testng.annotations.Test; 6 import org.pojomatic.annotations.Property; 7 import org.pojomatic.annotations.SkipArrayCheck; 8 import org.pojomatic.diff.NoDifferences; 9 import org.pojomatic.internal.PojomatorFactory; 10 11 public class PojomaticTest { 12 public static class Bean { 13 @Property public final int x; 14 public Bean(int x) { this.x = x; } 15 public Bean() { x = 0; } 16 } 17 18 private static Pojomator<Bean> BEAN_POJOMATOR = PojomatorFactory.makePojomator(Bean.class); 19 private static Bean BEAN = new Bean(1); 20 21 @Test 22 public void testPojomator() { 23 Pojomator<Bean> pojomator = Pojomatic.pojomator(Bean.class); 24 assertEquals(pojomator.doToString(BEAN), BEAN_POJOMATOR.doToString(BEAN)); 25 } 26 27 @Test 28 public void testToString() { 29 assertEquals(Pojomatic.toString(BEAN), BEAN_POJOMATOR.doToString(BEAN)); 30 } 31 32 @Test 33 public void testDiffNoDifferences() { 34 assertEquals(Pojomatic.diff(BEAN, BEAN), NoDifferences.getInstance()); 35 } 36 37 @Test(expectedExceptions=NullPointerException.class) 38 public void testDiffBothNull() { 39 Pojomatic.diff(null, null); 40 } 41 42 @Test(expectedExceptions=NullPointerException.class) 43 public void testDiffNullFirst() { 44 Pojomatic.diff(null, BEAN); 45 } 46 47 @Test(expectedExceptions=NullPointerException.class) 48 public void testDiffNullSecond() { 49 Pojomatic.diff(BEAN, null); 50 } 51 52 @Test 53 public void testHashCode() { 54 assertEquals(Pojomatic.hashCode(BEAN), BEAN_POJOMATOR.doHashCode(BEAN)); 55 } 56 57 @Test 58 public void testEquals() { 59 assertTrue(Pojomatic.equals(new Bean(3), new Bean(3))); 60 assertFalse(Pojomatic.equals(new Bean(3), new Bean(4))); 61 } 62 63 @Test 64 public void testCompatibleForEquality() { 65 class BeanSubClass extends Bean{} 66 67 class BeanWithExtraData extends Bean { 68 @Property public int getY() { return 0; } 69 } 70 assertTrue(Pojomatic.areCompatibleForEquals(Bean.class, BeanSubClass.class)); 71 assertFalse(Pojomatic.areCompatibleForEquals(Bean.class, BeanWithExtraData.class)); 72 assertFalse(Pojomatic.areCompatibleForEquals(BeanWithExtraData.class, Bean.class)); 73 } 74 75 @Test 76 public void testSkipArrayCheck() { 77 class Box { 78 @Property 79 @SkipArrayCheck 80 Object o; 81 82 Box(Object o) { this.o = o; } 83 } 84 assertFalse(Pojomatic.equals(new Box(new String[] { "x" }), new Box(new String[] { "x" }))); 85 assertNotEquals(Pojomatic.hashCode(new Box(new String[] { "x" })), Pojomatic.hashCode(new Box(new String[] { "x" }))); 86 87 String[] array = new String[] { "y" }; 88 assertTrue(Pojomatic.equals(new Box(array), new Box(array))); 89 assertEquals(Pojomatic.hashCode(new Box(array)), Pojomatic.hashCode(new Box(array))); 90 } 91 92 @Test 93 public void testNoSkipArrayCheck() { 94 class Box { 95 @Property 96 Object o; 97 98 Box(Object o) { this.o = o; } 99 } 100 assertTrue(Pojomatic.equals(new Box(new String[] { "x" }), new Box(new String[] { "x" }))); 101 assertEquals(Pojomatic.hashCode(new Box(new String[] { "x" })), Pojomatic.hashCode(new Box(new String[] { "x" }))); 102 } 103 104 @Test 105 public void testSkipArrayCheckIgnoredForArrayType() { 106 class Box { 107 @Property 108 @SkipArrayCheck 109 Object[] os; 110 111 Box(Object o) { this.os = new Object[] { o }; } 112 } 113 assertTrue(Pojomatic.equals(new Box(new String[] { "x" }), new Box(new String[] { "x" }))); 114 assertEquals(Pojomatic.hashCode(new Box(new String[] { "x" })), Pojomatic.hashCode(new Box(new String[] { "x" }))); 115 } 116 117 118 }