Skip to content

Commit e30d572

Browse files
committed
Merge pull request #12 from zalando/issue11
Fix issue 11: Tests work in arbitrary timezones
2 parents ec240de + 470749b commit e30d572

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@
144144
<version>${spring.version}</version>
145145
<scope>test</scope>
146146
</dependency>
147+
<dependency>
148+
<groupId>org.cthul</groupId>
149+
<artifactId>cthul-matchers</artifactId>
150+
<version>1.1.0</version>
151+
<scope>test</scope>
152+
</dependency>
147153
<dependency>
148154
<groupId>com.jolbox</groupId>
149155
<artifactId>bonecp</artifactId>

src/test/java/de/zalando/typemapper/core/fieldMapper/DateFieldMapperTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNotNull;
55
import static org.junit.Assert.assertTrue;
6+
import static org.junit.Assert.fail;
67

78
import java.sql.Date;
89

@@ -19,10 +20,17 @@
1920
@RunWith(Theories.class)
2021
public class DateFieldMapperTest {
2122

23+
private static final int MS_PER_HOUR = 1000*60*60;
24+
private static final int MS_PER_MINUTE = 1000*60;
25+
private static final int rawOffset = java.util.TimeZone.getDefault().getRawOffset();
26+
private static final int offsetHours = rawOffset / MS_PER_HOUR;
27+
private static final int offsetMinutes = (rawOffset % MS_PER_HOUR) / MS_PER_MINUTE;
28+
private static final String sign = offsetHours > 0 ? "+" : "-";
29+
2230
@DataPoints
2331
public static final String[][] DATES_RESULTS = {
24-
{"2011-11-11 11:11:11", "2011-11-11 11:11:11.000+0100"},
25-
{"2011-11-11", "2011-11-11 00:00:00.000+0100"},
32+
{"2011-11-11 11:11:11", String.format("2011-11-11 11:11:11.000%s%02d%02d", sign, offsetHours, offsetMinutes)},
33+
{"2011-11-11", String.format("2011-11-11 00:00:00.000%s%02d%02d", sign, offsetHours, offsetMinutes)},
2634
{"2011-11-11 11:11:11 +02:00", "2011-11-11 10:11:11.000+0100"},
2735
{"2011-11-11 11:11:11 -02:00", "2011-11-11 14:11:11.000+0100"},
2836
{"2011-11-11 11:11:11.123 -02:00", "2011-11-11 14:11:11.123+0100"}
@@ -39,6 +47,13 @@ public void parseAble(final String[] string, final Class<?> clazz) throws Except
3947
assertNotNull(result);
4048
assertTrue(clazz.isAssignableFrom(result.getClass()));
4149

42-
assertEquals(string[1], new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ", Locale.GERMANY).format(result));
50+
java.util.Date shouldBe = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ", Locale.GERMANY).parse(string[1]);
51+
if (result instanceof java.sql.Date) {
52+
assertEquals(((Date) result).getTime(), shouldBe.getTime());
53+
} else if (result instanceof java.sql.Timestamp) {
54+
assertEquals(((java.sql.Timestamp) result).getTime(), shouldBe.getTime());
55+
} else {
56+
fail(String.format("Unknown type for result: %s (%s)", result, result.getClass()));
57+
}
4358
}
4459
}

src/test/java/de/zalando/typemapper/postgres/PgSerializerTest.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package de.zalando.typemapper.postgres;
22

33
import static org.hamcrest.CoreMatchers.is;
4-
4+
import static org.cthul.matchers.CthulMatchers.*;
55
import static org.junit.Assert.assertThat;
66

77
import static de.zalando.typemapper.postgres.PgArray.ARRAY;
@@ -12,6 +12,7 @@
1212
import java.util.Arrays;
1313
import java.util.Collection;
1414
import java.util.Date;
15+
import java.util.regex.Pattern;
1516

1617
import org.junit.Test;
1718

@@ -26,16 +27,27 @@ public class PgSerializerTest {
2627
// Fields
2728
private Object objectToSerialize;
2829
private String expectedString;
30+
private Pattern expectedPattern;
2931

3032
/*
3133
* Constructor. The JUnit test runner will instantiate this class once for
3234
* every element in the Collection returned by the method annotated with
3335
*
3436
* @Parameters.
3537
*/
36-
public PgSerializerTest(final Object objectToSerialize, final String expectedString) {
38+
public PgSerializerTest(final Object objectToSerialize, final Object expected) {
3739
this.objectToSerialize = objectToSerialize;
38-
this.expectedString = expectedString;
40+
if (expected instanceof String) {
41+
this.expectedString = (String) expected;
42+
this.expectedPattern = null;
43+
} else if (expected instanceof Pattern) {
44+
this.expectedString = null;
45+
this.expectedPattern = (Pattern) expected;
46+
} else {
47+
throw new IllegalArgumentException(String.format(
48+
"Expected either a String or a Pattern, got: %s (%s)",
49+
expected, expected == null ? null : expected.getClass()));
50+
}
3951
}
4052

4153
/*
@@ -49,10 +61,10 @@ public PgSerializerTest(final Object objectToSerialize, final String expectedStr
4961
public static Collection<Object[]> generateData() throws SQLException {
5062
return Arrays.asList(
5163
new Object[][] {
52-
{new Date(112, 11, 1, 6, 6, 6), "2012-12-01 06:06:06.000000 +01:00:00"},
53-
{new Date(112, 9, 1, 6, 6, 6), "2012-10-01 06:06:06.000000 +02:00:00"},
64+
{new Date(112, 11, 1, 6, 6, 6), Pattern.compile("2012-12-01 06:06:06.000000 [+-]?\\d{2}:\\d{2}:00")},
65+
{new Date(112, 9, 1, 6, 6, 6), Pattern.compile("2012-10-01 06:06:06.000000 [+-]?\\d{2}:\\d{2}:00")},
5466
{1, "1"},
55-
{Integer.valueOf(69), "69"},
67+
{69, "69"},
5668
{true, "t"},
5769
{new int[] {1, 2, 3, 4}, "{1,2,3,4}"},
5870
{new Integer[] {null, 2, 3, 4}, "{NULL,2,3,4}"},
@@ -78,12 +90,15 @@ public static Collection<Object[]> generateData() throws SQLException {
7890

7991
/**
8092
* Test how SerializationUtils.toPgString() method works.
81-
*
82-
* @throws SerializationError
8393
*/
8494
@Test
8595
public void serializationTest() {
86-
assertThat(PgTypeHelper.toPgString(this.objectToSerialize), is(this.expectedString));
96+
String result = PgTypeHelper.toPgString(this.objectToSerialize);
97+
if (this.expectedString != null) {
98+
assertThat(result, is(this.expectedString));
99+
} else {
100+
assertThat(result, matchesPattern(this.expectedPattern));
101+
}
87102
}
88103

89104
}

0 commit comments

Comments
 (0)