public
Created

TestFormatProvider.java

  • Download Gist
TestFormatProvider.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
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);
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.