1
1
/*
2
- * Copyright 2012 the original author or authors.
2
+ * Copyright 2012-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
import static org .hamcrest .CoreMatchers .*;
19
19
import static org .junit .Assert .*;
20
20
21
+ import java .io .IOException ;
21
22
import java .io .StringWriter ;
22
23
import java .util .Arrays ;
23
24
import java .util .List ;
33
34
import javax .xml .bind .annotation .XmlRootElement ;
34
35
import javax .xml .bind .annotation .adapters .XmlJavaTypeAdapter ;
35
36
36
- import org .junit . After ;
37
+ import org .custommonkey . xmlunit . Diff ;
37
38
import org .junit .Before ;
38
39
import org .junit .Test ;
39
40
import org .springframework .core .io .ClassPathResource ;
@@ -57,11 +58,11 @@ public class SpringDataJaxbUnitTests {
57
58
Unmarshaller unmarshaller ;
58
59
59
60
Sort sort = new Sort (Direction .ASC , "firstname" , "lastname" );
60
- Pageable reference = new PageRequest (2 , 15 , sort );
61
+ Pageable pageable = new PageRequest (2 , 15 , sort );
61
62
Resource resource = new ClassPathResource ("pageable.xml" , this .getClass ());
62
63
Resource schemaFile = new ClassPathResource ("spring-data-jaxb.xsd" , this .getClass ());
63
64
64
- Scanner scanner ;
65
+ String reference = readFile ( resource ) ;
65
66
66
67
@ Before
67
68
public void setUp () throws Exception {
@@ -72,29 +73,19 @@ public void setUp() throws Exception {
72
73
marshaller .setProperty (Marshaller .JAXB_FORMATTED_OUTPUT , true );
73
74
74
75
unmarshaller = context .createUnmarshaller ();
75
-
76
- scanner = new Scanner (resource .getFile ());
77
- }
78
-
79
- @ After
80
- public void tearDown () {
81
- scanner .close ();
82
76
}
83
77
84
78
@ Test
85
79
public void usesCustomTypeAdapterForPageRequests () throws Exception {
86
80
87
81
StringWriter writer = new StringWriter ();
88
82
Wrapper wrapper = new Wrapper ();
89
- wrapper .pageable = reference ;
83
+ wrapper .pageable = pageable ;
90
84
wrapper .sort = sort ;
91
85
wrapper .pageableWithoutSort = new PageRequest (10 , 20 );
92
86
marshaller .marshal (wrapper , writer );
93
87
94
- for (String line : writer .toString ().split ("\n " )) {
95
- assertThat (scanner .hasNextLine (), is (true ));
96
- assertThat (line , is (scanner .nextLine ()));
97
- }
88
+ assertThat (new Diff (reference , writer .toString ()).similar (), is (true ));
98
89
}
99
90
100
91
@ Test
@@ -103,7 +94,7 @@ public void readsPageRequest() throws Exception {
103
94
Object result = unmarshaller .unmarshal (resource .getFile ());
104
95
105
96
assertThat (result , is (instanceOf (Wrapper .class )));
106
- assertThat (((Wrapper ) result ).pageable , is (reference ));
97
+ assertThat (((Wrapper ) result ).pageable , is (pageable ));
107
98
assertThat (((Wrapper ) result ).sort , is (sort ));
108
99
}
109
100
@@ -119,35 +110,49 @@ public void writesPlainPage() throws Exception {
119
110
marshaller .marshal (wrapper , new StringWriter ());
120
111
}
121
112
113
+ private static String readFile (Resource resource ) {
114
+
115
+ try {
116
+
117
+ Scanner scanner = new Scanner (resource .getInputStream ());
118
+ StringBuilder builder = new StringBuilder ();
119
+
120
+ while (scanner .hasNextLine ()) {
121
+ builder .append (scanner .nextLine ()).append ("\n " );
122
+ }
123
+
124
+ scanner .close ();
125
+
126
+ return builder .toString ();
127
+
128
+ } catch (IOException o_O ) {
129
+ throw new RuntimeException (o_O );
130
+ }
131
+ }
132
+
122
133
@ XmlRootElement
123
134
@ XmlAccessorType (XmlAccessType .FIELD )
124
135
static class Wrapper {
125
136
126
- @ XmlElement (name = "page-request" , namespace = SpringDataJaxb .NAMESPACE )
127
- Pageable pageable ;
137
+ @ XmlElement (name = "page-request" , namespace = SpringDataJaxb .NAMESPACE ) Pageable pageable ;
128
138
129
- @ XmlElement (name = "page-request-without-sort" , namespace = SpringDataJaxb .NAMESPACE )
130
- Pageable pageableWithoutSort ;
139
+ @ XmlElement (name = "page-request-without-sort" , namespace = SpringDataJaxb .NAMESPACE ) Pageable pageableWithoutSort ;
131
140
132
- @ XmlElement (name = "sort" , namespace = SpringDataJaxb .NAMESPACE )
133
- Sort sort ;
141
+ @ XmlElement (name = "sort" , namespace = SpringDataJaxb .NAMESPACE ) Sort sort ;
134
142
}
135
143
136
144
@ XmlRootElement (name = "wrapper" , namespace = SpringDataJaxb .NAMESPACE )
137
145
static class PageWrapper {
138
146
139
147
Page <Content > page ;
140
148
141
- @ XmlElement (name = "page-with-links" )
142
- @ XmlJavaTypeAdapter (LinkedPageAdapter .class )
143
- Page <Content > pageWithLinks ;
149
+ @ XmlElement (name = "page-with-links" ) @ XmlJavaTypeAdapter (LinkedPageAdapter .class ) Page <Content > pageWithLinks ;
144
150
}
145
151
146
152
@ XmlRootElement
147
153
static class Content {
148
154
149
- @ XmlAttribute
150
- String name ;
155
+ @ XmlAttribute String name ;
151
156
}
152
157
153
158
static class LinkedPageAdapter extends PageAdapter {
0 commit comments