|
| 1 | +package works.weave.socks.shipping.entities; |
| 2 | + |
| 3 | +import com.openpojo.reflection.PojoClass; |
| 4 | +import com.openpojo.reflection.PojoClassFilter; |
| 5 | +import com.openpojo.reflection.filters.FilterClassName; |
| 6 | +import com.openpojo.reflection.impl.PojoClassFactory; |
| 7 | +import com.openpojo.validation.Validator; |
| 8 | +import com.openpojo.validation.ValidatorBuilder; |
| 9 | +import com.openpojo.validation.affirm.Affirm; |
| 10 | +import com.openpojo.validation.rule.impl.*; |
| 11 | +import com.openpojo.validation.test.impl.GetterTester; |
| 12 | +import com.openpojo.validation.test.impl.SetterTester; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import static org.hamcrest.CoreMatchers.*; |
| 18 | +import static org.junit.Assert.assertThat; |
| 19 | + |
| 20 | +public class UnitPojo { |
| 21 | + // Configured for expectation, so we know when a class gets added or removed. |
| 22 | + private static final int EXPECTED_CLASS_COUNT = 1; |
| 23 | + |
| 24 | + // The package to test |
| 25 | + private static final String POJO_PACKAGE = "works.weave.socks.shipping.entities"; |
| 26 | + |
| 27 | + private final PojoClassFilter filter = new FilterClassName("^((?!Unit).)*$"); |
| 28 | + |
| 29 | + @Test |
| 30 | + public void ensureExpectedPojoCount() { |
| 31 | + List<PojoClass> pojoClasses = PojoClassFactory.getPojoClasses(POJO_PACKAGE, filter); |
| 32 | + Affirm.affirmEquals("Classes added / removed?", EXPECTED_CLASS_COUNT, pojoClasses.size()); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testPojoStructureAndBehavior() { |
| 37 | + Validator validator = ValidatorBuilder.create() |
| 38 | + // Add Rules to validate structure for POJO_PACKAGE |
| 39 | + // See com.openpojo.validation.rule.impl for more ... |
| 40 | + .with(new GetterMustExistRule()) |
| 41 | + .with(new SetterMustExistRule()) |
| 42 | + // Add Testers to validate behaviour for POJO_PACKAGE |
| 43 | + // See com.openpojo.validation.test.impl for more ... |
| 44 | + .with(new SetterTester()) |
| 45 | + .with(new GetterTester()) |
| 46 | + // Static fields must be final |
| 47 | + .with(new NoStaticExceptFinalRule()) |
| 48 | + // Don't shadow parent's field names. |
| 49 | + .with(new NoFieldShadowingRule()) |
| 50 | + // What about public fields, use one of the following rules |
| 51 | + // allow them only if they are static and final. |
| 52 | + .with(new NoPublicFieldsExceptStaticFinalRule()) |
| 53 | + .build(); |
| 54 | + |
| 55 | + validator.validate(POJO_PACKAGE, filter); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testEquals() throws Exception { |
| 60 | + assertThat(new Shipment("id", "name"), is(equalTo(new Shipment("id", "name")))); |
| 61 | + assertThat(new Shipment("id", "name"), is(equalTo(new Shipment("id", "another")))); |
| 62 | + assertThat(new Shipment("id", "name"), is(not(equalTo(new Shipment("another", "name"))))); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testHashcode() throws Exception { |
| 67 | + assertThat(new Shipment("id", "name").hashCode(), is(equalTo(new Shipment("id", "name").hashCode()))); |
| 68 | + assertThat(new Shipment("id", "name").hashCode(), is(equalTo(new Shipment("id", "another").hashCode()))); |
| 69 | + assertThat(new Shipment("id", "name").hashCode(), is(not(equalTo(new Shipment("aa", "name").hashCode())))); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testString() throws Exception { |
| 74 | + assertThat(new Shipment("id").toString(), is(notNullValue())); |
| 75 | + |
| 76 | + } |
| 77 | +} |
0 commit comments