Created
April 28, 2014 22:12
-
-
Save Falci/11385525 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.falci.planoB.converter; | |
import java.util.Calendar; | |
import java.util.Date; | |
import org.springframework.core.convert.converter.Converter; | |
/** | |
* @author Falci | |
* | |
*/ | |
public class ConverterLongToDate implements Converter<Long, Date> { | |
@Override | |
public Date convert(Long timestamp) { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTimeInMillis(timestamp); | |
return calendar.getTime(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
*/ | |
package me.falci.planoB; | |
import static org.junit.Assert.assertEquals; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.GregorianCalendar; | |
import me.falci.planoB.converter.ConverterLongToDate; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.springframework.core.convert.ConverterNotFoundException; | |
import org.springframework.core.convert.support.DefaultConversionService; | |
/** | |
* @author Falci | |
* | |
*/ | |
public class ConverterTest { | |
private DefaultConversionService conversionService; | |
@Before | |
public void init() { | |
conversionService = new DefaultConversionService(); | |
} | |
@Test | |
public void testConversaoSimples() { | |
assertEquals(new Long(123L), conversionService.convert("123", Long.class)); | |
assertEquals(new Double(12.34), conversionService.convert("12.34", Double.class)); | |
assertEquals(new Long(123L), conversionService.convert(123D, Long.class)); | |
} | |
@Test(expected=ConverterNotFoundException.class) | |
public void testConversorNaoRegistrado() { | |
conversionService.convert(1398654000000L, Date.class); | |
} | |
@Test | |
public void testConversaoDeLongParaData() { | |
conversionService.addConverter(new ConverterLongToDate()); | |
Date time = new GregorianCalendar(2014, Calendar.APRIL, 28).getTime(); | |
Long timeAsLong = time.getTime(); | |
assertEquals(time, conversionService.convert(timeAsLong, Date.class)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment