Skip to content

Instantly share code, notes, and snippets.

@aVolpe
Created September 14, 2013 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aVolpe/6558316 to your computer and use it in GitHub Desktop.
Save aVolpe/6558316 to your computer and use it in GitHub Desktop.
TestFormatProvider.java
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import py.una.med.base.test.base.BaseTest;
import py.una.med.base.test.configuration.BaseTestConfiguration;
import py.una.med.base.util.FormatProvider;
/**
*
* @author Arturo Volpe
* @since 2.2
* @version 1.0 Sep 11, 2013
*
*/
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class TestFormatProvider extends BaseTest {
private static final String EMPTY_STRING = "";
private static final String SHORT_DATE = "01-01-13";
private static final String LONG_DATE = "01-01-2013";
private static final String TIME = "06:30";
private static final String DATE_TIME = "01-01-2013 06:30";
private static final String DATE_SHORT_TIME = "01-01-13 06:30";
@Configuration
static class ContextConfiguration extends BaseTestConfiguration {
@Bean(name = FormatProvider.FORMAT_PROVIDER_NAME)
public FormatProvider formatProvider() {
return new FormatProvider();
}
}
@Autowired
private FormatProvider fp;
private Calendar calendar;
private Date date;
/**
*
*/
@Before
public void setUp() {
calendar = GregorianCalendar.getInstance();
calendar.set(Calendar.YEAR, 2013);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 10);
date = calendar.getTime();
}
@Test
public void testShortDate() throws ParseException {
assertEquals(SHORT_DATE, fp.asShortDate(date));
assertTrue(DateUtils.isSameDay(date, fp.parseShortDate(SHORT_DATE)));
assertEquals(EMPTY_STRING, fp.asShortDate((Date) null));
assertEquals(null, fp.parseShortDate(EMPTY_STRING));
assertEquals(null, fp.parseShortDate((String) null));
assertEquals(SHORT_DATE, fp.asShortDate(fp.parseShortDate(SHORT_DATE)));
}
@Test(expected = ParseException.class)
public void testWrongFormatShortDate() throws ParseException {
fp.parseShortDate(LONG_DATE);
}
@Test(expected = ParseException.class)
public void testLenientFormatShortDate() throws ParseException {
fp.parseShortDate("31-31-31");
}
@Test
public void testLongDate() throws ParseException {
assertEquals(LONG_DATE, fp.asDate(date));
assertTrue(DateUtils.isSameDay(date, fp.parseDate(LONG_DATE)));
assertEquals(EMPTY_STRING, fp.asDate(null));
assertEquals(null, fp.parseDate(EMPTY_STRING));
assertEquals(null, fp.parseDate(null));
assertEquals(LONG_DATE, fp.asDate(fp.parseDate(LONG_DATE)));
}
@Test(expected = ParseException.class)
public void testWrongFormatLongDate() throws ParseException {
fp.parseDate(SHORT_DATE);
}
@Test(expected = ParseException.class)
public void testLenientFormatLongDate() throws ParseException {
fp.parseShortDate("31-31-2031");
}
@Test
public void testTime() throws ParseException {
assertEquals(TIME, fp.asTime(date));
assertTrue(hasSameHourAndMinute(date, fp.parseTime(TIME)));
assertEquals(EMPTY_STRING, fp.asTime(null));
assertEquals(null, fp.parseTime(EMPTY_STRING));
assertEquals(null, fp.parseTime(null));
assertEquals(TIME, fp.asTime(fp.parseTime(TIME)));
}
@Test(expected = ParseException.class)
public void testWrongFormatTime() throws ParseException {
fp.parseTime(SHORT_DATE);
}
@Test(expected = ParseException.class)
public void testLenientFormatTime() throws ParseException {
fp.parseTime("99:99");
}
@Test
public void testLongDateTime() throws ParseException {
assertEquals(DATE_TIME, fp.asDateTime(date));
assertTrue(hasSameHourMinuteAndDate(date, fp.parseDateTime(DATE_TIME)));
assertEquals(EMPTY_STRING, fp.asDateTime(null));
assertEquals(null, fp.parseDateTime(EMPTY_STRING));
assertEquals(null, fp.parseDateTime(null));
assertEquals(DATE_TIME, fp.asDateTime(fp.parseDateTime(DATE_TIME)));
}
@Test(expected = ParseException.class)
public void testWrongFormatDateTime() throws ParseException {
fp.parseDateTime(SHORT_DATE);
}
@Test(expected = ParseException.class)
public void testLenientFormatDateTime() throws ParseException {
fp.parseDateTime("31-12-2031 99:99");
}
@Test
public void testShortDateTime() throws ParseException {
assertEquals(DATE_SHORT_TIME, fp.asShortDateTime(date));
assertTrue(hasSameHourMinuteAndDate(date,
fp.parseShortDateTime(DATE_SHORT_TIME)));
assertEquals(EMPTY_STRING, fp.asShortDateTime(null));
assertEquals(null, fp.parseShortDateTime(EMPTY_STRING));
assertEquals(null, fp.parseShortDateTime(null));
assertEquals(DATE_SHORT_TIME,
fp.asShortDateTime(fp.parseShortDateTime(DATE_SHORT_TIME)));
}
@Test(expected = ParseException.class)
public void testWrongFormatShortDateTime() throws ParseException {
fp.parseShortDateTime(SHORT_DATE);
}
@Test(expected = ParseException.class)
public void testLenientFormatShortDateTime() throws ParseException {
fp.parseShortDateTime("31-12-31 24:40");
}
private boolean hasSameHourMinuteAndDate(Date one, Date two) {
return DateUtils.isSameDay(one, two) && hasSameHourAndMinute(one, two);
}
/**
* @param date2
* @param date
* @return
*/
private boolean hasSameHourAndMinute(Date date2, Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
Calendar c2 = Calendar.getInstance();
c2.setTime(date2);
return c.get(Calendar.HOUR_OF_DAY) == c2.get(Calendar.HOUR_OF_DAY)
&& c.get(Calendar.MINUTE) == c2.get(Calendar.MINUTE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment