-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change DateTimeFormatter (closes #13)
- Loading branch information
Showing
3 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
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
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
97 changes: 97 additions & 0 deletions
97
src/test/java/de/rwth/idsg/ocpp/jaxb/JodaDateTimeConverterTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package de.rwth.idsg.ocpp.jaxb; | ||
|
||
import org.joda.time.DateTime; | ||
import org.joda.time.DateTimeZone; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.TimeZone; | ||
import java.util.stream.Stream; | ||
|
||
public class JodaDateTimeConverterTest { | ||
|
||
private final JodaDateTimeConverter converter = new JodaDateTimeConverter(); | ||
|
||
static { | ||
TimeZone.setDefault(TimeZone.getTimeZone("UTC")); | ||
DateTimeZone.setDefault(DateTimeZone.forID("UTC")); | ||
} | ||
|
||
// ------------------------------------------------------------------------- | ||
// Marshal | ||
// ------------------------------------------------------------------------- | ||
|
||
@Test | ||
public void testMarshallNullInput() throws Exception { | ||
String val = converter.marshal(null); | ||
Assertions.assertNull(val); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideValidInput") | ||
public void testMarshallEmptyInput(String val, String expected) throws Exception { | ||
DateTime input = converter.unmarshal(val); | ||
String output = converter.marshal(input); | ||
Assertions.assertEquals(expected, output); | ||
} | ||
|
||
// ------------------------------------------------------------------------- | ||
// Unmarshal | ||
// ------------------------------------------------------------------------- | ||
|
||
@Test | ||
public void testUnmarshallNullInput() throws Exception { | ||
converter.unmarshal(null); | ||
} | ||
|
||
@Test | ||
public void testUnmarshallEmptyInput() throws Exception { | ||
converter.unmarshal(""); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideValidInput") | ||
public void testUnmarshalValid(String val) throws Exception { | ||
converter.unmarshal(val); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideInvalidInput") | ||
public void testUnmarshalInvalid(String val) { | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> { | ||
converter.unmarshal(val); | ||
}); | ||
} | ||
|
||
/** | ||
* First argument is used for marshaling only. | ||
* Both arguments are used for unmarshaling: We use the second as the expected output of formatting. | ||
*/ | ||
private static Stream<Arguments> provideValidInput() { | ||
return Stream.of( | ||
Arguments.of("2022-06-30T01:20:52", "2022-06-30T01:20:52.000Z"), | ||
Arguments.of("2022-06-30T01:20:52+02:00", "2022-06-29T23:20:52.000Z"), | ||
Arguments.of("2022-06-30T01:20:52Z", "2022-06-30T01:20:52.000Z"), | ||
Arguments.of("2022-06-30T01:20:52+00:00", "2022-06-30T01:20:52.000Z"), | ||
Arguments.of("2022-06-30T01:20:52.126", "2022-06-30T01:20:52.126Z"), | ||
Arguments.of("2022-06-30T01:20:52.126+05:00", "2022-06-29T20:20:52.126Z"), | ||
Arguments.of("2018-11-13T20:20:39+00:00", "2018-11-13T20:20:39.000Z"), | ||
Arguments.of("-2022-06-30T01:20:52", "-2022-06-30T01:20:52.000Z") | ||
); | ||
} | ||
|
||
private static Stream<Arguments> provideInvalidInput() { | ||
return Stream.of( | ||
Arguments.of("-1"), | ||
Arguments.of("10000"), // https://github.com/steve-community/steve/issues/1292 | ||
Arguments.of("text"), | ||
Arguments.of("2022-06-30"), // no time | ||
Arguments.of("2022-06-30T01:20"), // seconds are required | ||
Arguments.of("2022-06-30T25:20:34"), // hour out of range | ||
Arguments.of("22-06-30T25:20:34") // year not YYYY-format | ||
); | ||
} | ||
} |