package com.morgan.design.properties.conversion;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.joda.time.Period;
import org.junit.Before;
import org.junit.Test;

public class DefaultPropertyConversionServiceUnitTest {

	private PropertyConversionService conversionService;

	@Before
	public void setUp() throws Exception {
		this.conversionService = new DefaultPropertyConversionService();
	}

	@Test
	public void shouldConvertPeriodForPropertyForField() throws NoSuchFieldException, SecurityException {
		assertThat((Period) convertPropertyForField("period", "24:00:00"), is(new Period(24, 0, 0, 0)));
		assertThat((Period) convertPropertyForField("period", "48:00:00"), is(new Period(48, 0, 0, 0)));
		assertThat((Period) convertPropertyForField("period", "72:00:00"), is(new Period(72, 0, 0, 0)));
	}

	@Test
	public void shouldConvertLocalDateTimeForPropertyForField() throws NoSuchFieldException, SecurityException {
		LocalDateTime actual = (LocalDateTime) convertPropertyForField("localDateTime", "2006-05-27 16:03:34.0");
		assertThat(actual, is(new LocalDateTime(2006, 5, 27, 16, 3, 34)));

		actual = (LocalDateTime) convertPropertyForField("localDateTime", "2007-8-1 3:2:4.0");
		assertThat(actual, is(new LocalDateTime(2007, 8, 1, 3, 2, 4)));
	}

	@Test
	public void shouldConvertLocalDateForPropertyForField() throws NoSuchFieldException, SecurityException {
		final LocalDate date = (LocalDate) convertPropertyForField("localDate", "2007-08-02");
		assertThat(date, is(new LocalDate(2007, 8, 2)));
	}

	@Test
	public void shouldConvertLocalTimeForPropertyForField() throws NoSuchFieldException, SecurityException {
		assertThat((LocalTime) convertPropertyForField("localTime", "09:30:51"), is(new LocalTime(9, 30, 51)));
		assertThat((LocalTime) convertPropertyForField("localTime", "23:18:41"), is(new LocalTime(23, 18, 41)));
	}

	@Test
	public void shouldConvertBooleanValue() throws NoSuchFieldException, SecurityException {
		assertThat((Boolean) convertPropertyForField("booleanValue", "true"), is(true));
	}

	static class TestObject {
		Period period = new Period();
		LocalTime localTime = new LocalTime();
		LocalDate localDate = new LocalDate();
		LocalDateTime localDateTime = new LocalDateTime();
		boolean booleanValue;
	}

	private Object convertPropertyForField(final String fieldName, final Object value) throws NoSuchFieldException, SecurityException {
		return this.conversionService.convertPropertyForField(TestObject.class.getDeclaredField(fieldName), value);
	}
}

@@ -127,16 +127,108 @@
 tThat;%0A%0A
+import java.text.ParseException;%0Aimport java.text.SimpleDateFormat;%0Aimport java.util.Date;%0A%0A
 import o
@@ -369,248 +369,474 @@
 nit.
-Before;%0Aimport org.junit.
+Test;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.test.context.ContextConfiguration;%0Aimport org.springframework.test.context.junit4.AbstractJUnit4SpringContext
 Test
+s
 ;%0A%0A
-public class DefaultPropertyConversionServiceUnitTest %7B%0A%0A%09private PropertyConversionService conversionService;%0A%0A%09@Befo
+@ContextConfiguration(locations = %7B%22classpath:/spring/spring-reloadablePropertyPostProcessorIntTest.xml%22%7D)%0Apublic class DefaultPropertyConversionServiceUnitTest extends AbstractJUnit4SpringContextTests %7B%0A%0A%09@Autowi
 re
+d
 %0A%09p
-ublic void setUp() throws Exception %7B%0A%09%09this.conversionService = new DefaultPropertyC
+rivate PropertyConversionService c
 onve
@@ -851,14 +851,9 @@
 vice
-();%0A%09%7D
+;
 %0A%0A%09@
@@ -2486,16 +2486,288 @@
 ));%0A%09%7D%0A%0A
+%09@Test%0A%09public void shouldConvertDateValue() throws NoSuchFieldException, SecurityException, ParseException %7B%0A%09%09Date expected = new SimpleDateFormat(%22dd-MM-yyyy%22).parse(%229-4-2017%22);%0A%09%09assertThat((Date) convertPropertyForField(%22dateValue%22, %229-4-2017%22), is(expected));%0A%09%7D%0A%09%0A
 %09static 
@@ -2952,16 +2952,47 @@
 Time();%0A
+%09%09Date dateValue = new Date();%0A
 %09%09boolea

add test case for date conversion.