1
1
package de .zalando .typemapper .postgres ;
2
2
3
3
import static org .hamcrest .CoreMatchers .is ;
4
-
4
+ import static org . cthul . matchers . CthulMatchers .*;
5
5
import static org .junit .Assert .assertThat ;
6
6
7
7
import static de .zalando .typemapper .postgres .PgArray .ARRAY ;
12
12
import java .util .Arrays ;
13
13
import java .util .Collection ;
14
14
import java .util .Date ;
15
+ import java .util .regex .Pattern ;
15
16
16
17
import org .junit .Test ;
17
18
@@ -26,16 +27,27 @@ public class PgSerializerTest {
26
27
// Fields
27
28
private Object objectToSerialize ;
28
29
private String expectedString ;
30
+ private Pattern expectedPattern ;
29
31
30
32
/*
31
33
* Constructor. The JUnit test runner will instantiate this class once for
32
34
* every element in the Collection returned by the method annotated with
33
35
*
34
36
* @Parameters.
35
37
*/
36
- public PgSerializerTest (final Object objectToSerialize , final String expectedString ) {
38
+ public PgSerializerTest (final Object objectToSerialize , final Object expected ) {
37
39
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
+ }
39
51
}
40
52
41
53
/*
@@ -49,10 +61,10 @@ public PgSerializerTest(final Object objectToSerialize, final String expectedStr
49
61
public static Collection <Object []> generateData () throws SQLException {
50
62
return Arrays .asList (
51
63
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" ) },
54
66
{1 , "1" },
55
- {Integer . valueOf ( 69 ) , "69" },
67
+ {69 , "69" },
56
68
{true , "t" },
57
69
{new int [] {1 , 2 , 3 , 4 }, "{1,2,3,4}" },
58
70
{new Integer [] {null , 2 , 3 , 4 }, "{NULL,2,3,4}" },
@@ -78,12 +90,15 @@ public static Collection<Object[]> generateData() throws SQLException {
78
90
79
91
/**
80
92
* Test how SerializationUtils.toPgString() method works.
81
- *
82
- * @throws SerializationError
83
93
*/
84
94
@ Test
85
95
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
+ }
87
102
}
88
103
89
104
}
0 commit comments