Skip to content

Commit

Permalink
Add converters for MinguoDate and ThaiBuddhistDate. Add aliases for
Browse files Browse the repository at this point in the history
MinguoEra and ThaiBuddhistEra.
  • Loading branch information
joehni committed Feb 22, 2017
1 parent 4dc8472 commit 51db779
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 0 deletions.
12 changes: 12 additions & 0 deletions xstream/src/java/com/thoughtworks/xstream/XStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,8 @@ protected void setupSecurity() {
types.add(JVM.loadClassForName("java.time.chrono.HijrahDate"));
types.add(JVM.loadClassForName("java.time.chrono.JapaneseDate"));
types.add(JVM.loadClassForName("java.time.chrono.JapaneseEra"));
types.add(JVM.loadClassForName("java.time.chrono.MinguoDate"));
types.add(JVM.loadClassForName("java.time.chrono.ThaiBuddhistDate"));
types.add(JVM.loadClassForName("java.time.chrono.Ser"));
allowTypeHierarchy(JVM.loadClassForName("java.time.chrono.Chronology"));
}
Expand Down Expand Up @@ -788,6 +790,10 @@ protected void setupAliases() {
alias("hijrah-era", JVM.loadClassForName("java.time.chrono.HijrahEra"));
alias("japanese-date", JVM.loadClassForName("java.time.chrono.JapaneseDate"));
alias("japanese-era", JVM.loadClassForName("java.time.chrono.JapaneseEra"));
alias("minguo-date", JVM.loadClassForName("java.time.chrono.MinguoDate"));
alias("minguo-era", JVM.loadClassForName("java.time.chrono.MinguoEra"));
alias("thai-buddhist-date", JVM.loadClassForName("java.time.chrono.ThaiBuddhistDate"));
alias("thai-buddhist-era", JVM.loadClassForName("java.time.chrono.ThaiBuddhistEra"));
alias("chrono-field", JVM.loadClassForName("java.time.temporal.ChronoField"));
alias("chrono-unit", JVM.loadClassForName("java.time.temporal.ChronoUnit"));
alias("iso-field", JVM.loadClassForName("java.time.temporal.IsoFields$Field"));
Expand Down Expand Up @@ -902,6 +908,8 @@ protected void setupConverters() {
PRIORITY_NORMAL, null, null);
registerConverterDynamically("com.thoughtworks.xstream.converters.time.LocalTimeConverter", PRIORITY_NORMAL,
null, null);
registerConverterDynamically("com.thoughtworks.xstream.converters.time.MinguoDateConverter",
PRIORITY_NORMAL, null, null);
registerConverterDynamically("com.thoughtworks.xstream.converters.time.MonthDayConverter", PRIORITY_NORMAL,
null, null);
registerConverterDynamically("com.thoughtworks.xstream.converters.time.OffsetDateTimeConverter",
Expand All @@ -910,6 +918,8 @@ protected void setupConverters() {
PRIORITY_NORMAL, null, null);
registerConverterDynamically("com.thoughtworks.xstream.converters.time.PeriodConverter", PRIORITY_NORMAL,
null, null);
registerConverterDynamically("com.thoughtworks.xstream.converters.time.ThaiBuddhistDateConverter",
PRIORITY_NORMAL, null, null);
registerConverterDynamically("com.thoughtworks.xstream.converters.time.YearConverter", PRIORITY_NORMAL,
null, null);
registerConverterDynamically("com.thoughtworks.xstream.converters.time.YearMonthConverter", PRIORITY_NORMAL,
Expand Down Expand Up @@ -1040,7 +1050,9 @@ protected void setupImmutableTypes() {
addImmutableTypeDynamically("java.time.chrono.JapaneseDate", false);
addImmutableTypeDynamically("java.time.chrono.JapaneseEra", false);
addImmutableTypeDynamically("java.time.chrono.MinguoChronology", false);
addImmutableTypeDynamically("java.time.chrono.MinguoDate", false);
addImmutableTypeDynamically("java.time.chrono.ThaiBuddhistChronology", false);
addImmutableTypeDynamically("java.time.chrono.ThaiBuddhistDate", false);
addImmutableTypeDynamically("java.time.temporal.IsoFields$Field", false);
addImmutableTypeDynamically("java.time.temporal.IsoFields$Unit", false);
addImmutableTypeDynamically("java.time.temporal.JulianFields$Field", false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2017 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
* Created on 22. February 2017 by Joerg Schaible
*/
package com.thoughtworks.xstream.converters.time;

import java.time.chrono.ChronoLocalDate;
import java.time.chrono.MinguoChronology;
import java.time.chrono.MinguoDate;
import java.time.chrono.MinguoEra;
import java.util.Collections;


/**
* Converts a {@link java.time.chrono.MinguoDate} to a string.
*
* @author Jörg Schaible
*/
public class MinguoDateConverter extends AbstractChronoLocalDateConverter<MinguoEra> {

@Override
public boolean canConvert(final Class<?> type) {
return MinguoDate.class == type;
}

@Override
public Object fromString(final String str) {
return parseChronoLocalDate(str, "Minguo", Collections.singleton(MinguoChronology.INSTANCE));
}

@Override
protected ChronoLocalDate chronoLocalDateOf(final MinguoEra era, final int prolepticYear, final int month,
final int dayOfMonth) {
return MinguoDate.of(prolepticYear, month, dayOfMonth);
}

@Override
protected MinguoEra eraOf(final String id) {
return MinguoEra.valueOf(id);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2017 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
* Created on 22. February 2017 by Joerg Schaible
*/
package com.thoughtworks.xstream.converters.time;

import java.time.chrono.ChronoLocalDate;
import java.time.chrono.ThaiBuddhistChronology;
import java.time.chrono.ThaiBuddhistDate;
import java.time.chrono.ThaiBuddhistEra;
import java.util.Collections;


/**
* Converts a {@link java.time.chrono.ThaiBuddhistDate} to a string.
*
* @author J&ouml;rg Schaible
*/
public class ThaiBuddhistDateConverter extends AbstractChronoLocalDateConverter<ThaiBuddhistEra> {

@Override
public boolean canConvert(final Class<?> type) {
return ThaiBuddhistDate.class == type;
}

@Override
public Object fromString(final String str) {
return parseChronoLocalDate(str, "Thai Buddhist", Collections.singleton(ThaiBuddhistChronology.INSTANCE));
}

@Override
protected ChronoLocalDate chronoLocalDateOf(final ThaiBuddhistEra era, final int prolepticYear, final int month,
final int dayOfMonth) {
return ThaiBuddhistDate.of(prolepticYear, month, dayOfMonth);
}

@Override
protected ThaiBuddhistEra eraOf(final String id) {
return ThaiBuddhistEra.valueOf(id);
}

}
112 changes: 112 additions & 0 deletions xstream/src/test/com/thoughtworks/acceptance/Time18TypesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
import java.time.chrono.JapaneseChronology;
import java.time.chrono.JapaneseDate;
import java.time.chrono.JapaneseEra;
import java.time.chrono.MinguoChronology;
import java.time.chrono.MinguoDate;
import java.time.chrono.MinguoEra;
import java.time.chrono.ThaiBuddhistChronology;
import java.time.chrono.ThaiBuddhistDate;
import java.time.chrono.ThaiBuddhistEra;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.IsoFields;
Expand Down Expand Up @@ -845,6 +851,112 @@ public void testJapaneseEraIsImmutable() {
+ "</japanese-era-array>");
}

public void testMinguoDate() {
assertBothWays(MinguoChronology.INSTANCE.date(LocalDate.of(2017, 7, 30)),
"<minguo-date>Minguo ROC 106-07-30</minguo-date>");
}

public void testMinguoDateWithOldFormat() {
assertEquals(MinguoChronology.INSTANCE.date(LocalDate.of(2017, 7, 30)), xstream.fromXML("" //
+ "<java.time.chrono.MinguoDate resolves-to=\"java.time.chrono.Ser\">\n" //
+ " <byte>7</byte>\n" //
+ " <int>106</int>\n" //
+ " <byte>7</byte>\n" //
+ " <byte>30</byte>\n" //
+ "</java.time.chrono.MinguoDate>"));
}

public void testMinguoDateConversionExceptionContainsInvalidValue() {
try {
xstream.fromXML("<minguo-date>Chinese ROC 106-07-30</minguo-date>");
fail("Thrown " + ConversionException.class.getName() + " expected");
} catch (final ConversionException e) {
assertEquals(MinguoDate.class.getName(), e.get("class"));
assertEquals("Chinese ROC 106-07-30", e.get("value"));
}
try {
xstream.fromXML("<minguo-date>Minguo X 106-07-30</minguo-date>");
fail("Thrown " + ConversionException.class.getName() + " expected");
} catch (final ConversionException e) {
assertEquals(MinguoDate.class.getName(), e.get("class"));
assertEquals("Minguo X 106-07-30", e.get("value"));
}
try {
xstream.fromXML("<minguo-date>Minguo ROC 106-13-30</minguo-date>");
fail("Thrown " + ConversionException.class.getName() + " expected");
} catch (final ConversionException e) {
assertEquals(MinguoDate.class.getName(), e.get("class"));
assertEquals("Minguo ROC 106-13-30", e.get("value"));
}
}

public void testMinguoDateIsImmutable() {
final MinguoDate[] array = new MinguoDate[2];
array[0] = array[1] = MinguoChronology.INSTANCE.date(LocalDate.of(2017, 7, 30));
assertBothWays(array, "" //
+ "<minguo-date-array>\n" //
+ " <minguo-date>Minguo ROC 106-07-30</minguo-date>\n" //
+ " <minguo-date>Minguo ROC 106-07-30</minguo-date>\n" //
+ "</minguo-date-array>");
}

public void testMinguoEra() {
assertBothWays(MinguoEra.ROC, "<minguo-era>ROC</minguo-era>");
}

public void testThaiBuddhistDate() {
assertBothWays(ThaiBuddhistChronology.INSTANCE.date(LocalDate.of(2017, 7, 30)),
"<thai-buddhist-date>ThaiBuddhist BE 2560-07-30</thai-buddhist-date>");
}

public void testThaiBuddhistDateWithOldFormat() {
assertEquals(ThaiBuddhistChronology.INSTANCE.date(LocalDate.of(2017, 7, 30)), xstream.fromXML("" //
+ "<java.time.chrono.ThaiBuddhistDate resolves-to=\"java.time.chrono.Ser\">\n" //
+ " <byte>8</byte>\n" //
+ " <int>2560</int>\n" //
+ " <byte>7</byte>\n" //
+ " <byte>30</byte>\n" //
+ "</java.time.chrono.ThaiBuddhistDate>"));
}

public void testThaiBuddhistDateConversionExceptionContainsInvalidValue() {
try {
xstream.fromXML("<thai-buddhist-date>Chinese BE 2560-07-30</thai-buddhist-date>");
fail("Thrown " + ConversionException.class.getName() + " expected");
} catch (final ConversionException e) {
assertEquals(ThaiBuddhistDate.class.getName(), e.get("class"));
assertEquals("Chinese BE 2560-07-30", e.get("value"));
}
try {
xstream.fromXML("<thai-buddhist-date>ThaiBuddhist X 2560-07-30</thai-buddhist-date>");
fail("Thrown " + ConversionException.class.getName() + " expected");
} catch (final ConversionException e) {
assertEquals(ThaiBuddhistDate.class.getName(), e.get("class"));
assertEquals("ThaiBuddhist X 2560-07-30", e.get("value"));
}
try {
xstream.fromXML("<thai-buddhist-date>ThaiBuddhist BE 2560-13-30</thai-buddhist-date>");
fail("Thrown " + ConversionException.class.getName() + " expected");
} catch (final ConversionException e) {
assertEquals(ThaiBuddhistDate.class.getName(), e.get("class"));
assertEquals("ThaiBuddhist BE 2560-13-30", e.get("value"));
}
}

public void testThaiBuddhistDateIsImmutable() {
final ThaiBuddhistDate[] array = new ThaiBuddhistDate[2];
array[0] = array[1] = ThaiBuddhistChronology.INSTANCE.date(LocalDate.of(2017, 7, 30));
assertBothWays(array, "" //
+ "<thai-buddhist-date-array>\n" //
+ " <thai-buddhist-date>ThaiBuddhist BE 2560-07-30</thai-buddhist-date>\n" //
+ " <thai-buddhist-date>ThaiBuddhist BE 2560-07-30</thai-buddhist-date>\n" //
+ "</thai-buddhist-date-array>");
}

public void testThaiBuddhistEra() {
assertBothWays(ThaiBuddhistEra.BE, "<thai-buddhist-era>BE</thai-buddhist-era>");
}

public void testChronoField() {
assertBothWays(ChronoField.ERA, "<chrono-field>ERA</chrono-field>");
}
Expand Down

0 comments on commit 51db779

Please sign in to comment.