Skip to content

Commit

Permalink
update decoder and build for java 8
Browse files Browse the repository at this point in the history
  • Loading branch information
tcellucci committed Aug 8, 2016
1 parent 0b3cd97 commit 81c29b0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.util.Date;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;

import javax.inject.Singleton;
Expand Down Expand Up @@ -79,6 +81,8 @@ else if (v.equalsIgnoreCase("false") || v.equalsIgnoreCase("no") || v.equalsIgno
decoderRegistry.put(Float.class, Float::valueOf);
decoderRegistry.put(BigInteger.class, BigInteger::new);
decoderRegistry.put(BigDecimal.class, BigDecimal::new);
decoderRegistry.put(AtomicInteger.class, s->new AtomicInteger(Integer.parseInt(s)));
decoderRegistry.put(AtomicLong.class, s->new AtomicLong(Long.parseLong(s)));
decoderRegistry.put(Duration.class, Duration::parse);
decoderRegistry.put(Period.class, Period::parse);
decoderRegistry.put(LocalDateTime.class, LocalDateTime::parse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.BitSet;
import java.util.Currency;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import javax.xml.bind.DatatypeConverter;

Expand All @@ -40,7 +42,7 @@

public class DefaultDecoderTest {
@Test
public void testPrimitives() {
public void testJavaNumbers() {
DefaultDecoder decoder = new DefaultDecoder();

boolean flag = decoder.decode(boolean.class, "true");
Expand All @@ -50,20 +52,39 @@ public void testPrimitives() {

Assert.assertEquals(Byte.valueOf(Byte.MAX_VALUE), decoder.decode(Byte.class, String.valueOf(Byte.MAX_VALUE)));
Assert.assertEquals(Short.valueOf(Short.MAX_VALUE), decoder.decode(Short.class, String.valueOf(Short.MAX_VALUE)));
Assert.assertEquals(Long.valueOf(Long.MAX_VALUE), decoder.decode(Long.class, String.valueOf(Long.MAX_VALUE)));
Assert.assertEquals(Integer.valueOf(Integer.MAX_VALUE), decoder.decode(Integer.class, String.valueOf(Integer.MAX_VALUE)));
Assert.assertEquals(Float.valueOf(Float.MAX_VALUE), decoder.decode(Float.class, String.valueOf(Float.MAX_VALUE)));
Assert.assertEquals(Double.valueOf(Double.MAX_VALUE), decoder.decode(Double.class, String.valueOf(Double.MAX_VALUE)));
Assert.assertEquals(BigInteger.valueOf(Long.MAX_VALUE), decoder.decode(BigInteger.class, String.valueOf(Long.MAX_VALUE)));
Assert.assertEquals(BigDecimal.valueOf(Double.MAX_VALUE), decoder.decode(BigDecimal.class, String.valueOf(Double.MAX_VALUE)));

Assert.assertEquals(new AtomicInteger(Integer.MAX_VALUE).intValue(), decoder.decode(AtomicInteger.class, String.valueOf(Integer.MAX_VALUE)).intValue());
Assert.assertEquals(new AtomicLong(Long.MAX_VALUE).longValue(), decoder.decode(AtomicLong.class, String.valueOf(Long.MAX_VALUE)).longValue());
}

@Test
public void testJavaDateTime() {
DefaultDecoder decoder = new DefaultDecoder();

Assert.assertEquals(Duration.parse("PT20M30S"), decoder.decode(Duration.class, "PT20M30S"));
Assert.assertEquals(Period.of(1, 2, 25), decoder.decode(Period.class, "P1Y2M3W4D"));
Assert.assertEquals(OffsetDateTime.parse("2016-08-03T10:15:30+07:00"), decoder.decode(OffsetDateTime.class, "2016-08-03T10:15:30+07:00"));
Assert.assertEquals(OffsetTime.parse("10:15:30+18:00"), decoder.decode(OffsetTime.class, "10:15:30+18:00"));
Assert.assertEquals(ZonedDateTime.parse("2016-08-03T10:15:30+01:00[Europe/Paris]"), decoder.decode(ZonedDateTime.class, "2016-08-03T10:15:30+01:00[Europe/Paris]"));
Assert.assertEquals(LocalDateTime.parse("2016-08-03T10:15:30"), decoder.decode(LocalDateTime.class, "2016-08-03T10:15:30"));
Assert.assertEquals(LocalDate.parse("2016-08-03"), decoder.decode(LocalDate.class, "2016-08-03"));
Assert.assertEquals(LocalTime.parse("10:15:30"), decoder.decode(LocalTime.class, "10:15:30"));
Assert.assertEquals(Instant.from(OffsetDateTime.parse("2016-08-03T10:15:30+07:00")), decoder.decode(Instant.class, "2016-08-03T10:15:30+07:00"));
Date newDate = new Date();
Assert.assertEquals(newDate, decoder.decode(Date.class, String.valueOf(newDate.getTime())));
}

@Test
public void testJavaMiscellaneous() {
DefaultDecoder decoder = new DefaultDecoder();
Assert.assertEquals(Currency.getInstance("USD"), decoder.decode(Currency.class, "USD"));
Assert.assertEquals(BitSet.valueOf(DatatypeConverter.parseHexBinary("DEADBEEF00DEADBEEF")), decoder.decode(BitSet.class, "DEADBEEF00DEADBEEF"));
Assert.assertEquals("testString", decoder.decode(String.class, "testString"));
}

}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ subprojects {
apply plugin: 'nebula.provided-base'
apply plugin: 'java'

sourceCompatibility = 1.7
targetCompatibility = 1.7
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
jcenter()
Expand Down

0 comments on commit 81c29b0

Please sign in to comment.