/** * Test that setting a person's age correctly sets their birth date and records that this is * inexact * * @throws ParseException * @throws Exception */ @Test public void shouldSetInexactBirthdateFromAge() throws ParseException { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Person p = new Person(); // Test that default values are correct assertNull(p.getAge()); assertFalse(p.getBirthdateEstimated()); // Test standard case and ensure estimated field is set to true p.setBirthdateFromAge(10, df.parse("2008-05-20")); assertEquals(p.getBirthdate(), df.parse("1998-01-01")); assertTrue(p.getBirthdateEstimated()); // Test boundary cases p.setBirthdateFromAge(52, df.parse("2002-01-01")); assertEquals(p.getBirthdate(), df.parse("1950-01-01")); p.setBirthdateFromAge(35, df.parse("2004-12-31")); assertEquals(p.getBirthdate(), df.parse("1969-01-01")); p.setBirthdateFromAge(0, df.parse("2008-05-20")); assertEquals(p.getBirthdate(), df.parse("2008-01-01")); }
@PropertySetter("age") public static void setAge(Person person, Integer age) throws ResourceDoesNotSupportOperationException { if (person.getBirthdate() == null && age != null) { person.setBirthdateFromAge(age, new Date()); person.setBirthdateEstimated(true); } }
Double ageRemainder = BigDecimal.valueOf(age).subtract(BigDecimal.valueOf(Math.floor(age))).doubleValue(); if (ageRemainder.equals(Double.valueOf(0))) person.setBirthdateFromAge(age.intValue(), new Date()); //default to usual behavior from core else { //a decimal was entered Calendar c = Calendar.getInstance();
final Integer expectedAge = 42; Person person = new Person(); person.setBirthdateFromAge(expectedAge, new Date()); final Date expectedBirthDate = person.getBirthdate();