From 81c29b0ffe987b9aa94699821829223234baa8c4 Mon Sep 17 00:00:00 2001 From: Thomas A Cellucci Date: Mon, 8 Aug 2016 14:38:38 -0700 Subject: [PATCH] update decoder and build for java 8 --- .../com/netflix/archaius/DefaultDecoder.java | 4 +++ .../netflix/archaius/DefaultDecoderTest.java | 25 +++++++++++++++++-- build.gradle | 4 +-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/archaius2-core/src/main/java/com/netflix/archaius/DefaultDecoder.java b/archaius2-core/src/main/java/com/netflix/archaius/DefaultDecoder.java index defdb7836..d7ba5ffda 100644 --- a/archaius2-core/src/main/java/com/netflix/archaius/DefaultDecoder.java +++ b/archaius2-core/src/main/java/com/netflix/archaius/DefaultDecoder.java @@ -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; @@ -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); diff --git a/archaius2-core/src/test/java/com/netflix/archaius/DefaultDecoderTest.java b/archaius2-core/src/test/java/com/netflix/archaius/DefaultDecoderTest.java index a0de15bb5..0cc150873 100644 --- a/archaius2-core/src/test/java/com/netflix/archaius/DefaultDecoderTest.java +++ b/archaius2-core/src/test/java/com/netflix/archaius/DefaultDecoderTest.java @@ -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; @@ -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"); @@ -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")); } + } diff --git a/build.gradle b/build.gradle index 14aeee477..c8dc31e57 100644 --- a/build.gradle +++ b/build.gradle @@ -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()