Skip to content

Commit 3982e80

Browse files
committed
DATACMNS-478 - Allow build to be run on Java 8.
Introduced XmlUnit to make sure we can test XML rendering Java 6, 7 and 8.
1 parent 275bb8e commit 3982e80

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@
160160
<scope>test</scope>
161161
</dependency>
162162

163+
<dependency>
164+
<groupId>xmlunit</groupId>
165+
<artifactId>xmlunit</artifactId>
166+
<version>1.3</version>
167+
<scope>test</scope>
168+
</dependency>
169+
163170
<!-- Groovy -->
164171
<dependency>
165172
<groupId>org.codehaus.groovy</groupId>

src/test/java/org/springframework/data/domain/jaxb/SpringDataJaxbUnitTests.java

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 the original author or authors.
2+
* Copyright 2012-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
2020

21+
import java.io.IOException;
2122
import java.io.StringWriter;
2223
import java.util.Arrays;
2324
import java.util.List;
@@ -33,7 +34,7 @@
3334
import javax.xml.bind.annotation.XmlRootElement;
3435
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
3536

36-
import org.junit.After;
37+
import org.custommonkey.xmlunit.Diff;
3738
import org.junit.Before;
3839
import org.junit.Test;
3940
import org.springframework.core.io.ClassPathResource;
@@ -57,11 +58,11 @@ public class SpringDataJaxbUnitTests {
5758
Unmarshaller unmarshaller;
5859

5960
Sort sort = new Sort(Direction.ASC, "firstname", "lastname");
60-
Pageable reference = new PageRequest(2, 15, sort);
61+
Pageable pageable = new PageRequest(2, 15, sort);
6162
Resource resource = new ClassPathResource("pageable.xml", this.getClass());
6263
Resource schemaFile = new ClassPathResource("spring-data-jaxb.xsd", this.getClass());
6364

64-
Scanner scanner;
65+
String reference = readFile(resource);
6566

6667
@Before
6768
public void setUp() throws Exception {
@@ -72,29 +73,19 @@ public void setUp() throws Exception {
7273
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
7374

7475
unmarshaller = context.createUnmarshaller();
75-
76-
scanner = new Scanner(resource.getFile());
77-
}
78-
79-
@After
80-
public void tearDown() {
81-
scanner.close();
8276
}
8377

8478
@Test
8579
public void usesCustomTypeAdapterForPageRequests() throws Exception {
8680

8781
StringWriter writer = new StringWriter();
8882
Wrapper wrapper = new Wrapper();
89-
wrapper.pageable = reference;
83+
wrapper.pageable = pageable;
9084
wrapper.sort = sort;
9185
wrapper.pageableWithoutSort = new PageRequest(10, 20);
9286
marshaller.marshal(wrapper, writer);
9387

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));
9889
}
9990

10091
@Test
@@ -103,7 +94,7 @@ public void readsPageRequest() throws Exception {
10394
Object result = unmarshaller.unmarshal(resource.getFile());
10495

10596
assertThat(result, is(instanceOf(Wrapper.class)));
106-
assertThat(((Wrapper) result).pageable, is(reference));
97+
assertThat(((Wrapper) result).pageable, is(pageable));
10798
assertThat(((Wrapper) result).sort, is(sort));
10899
}
109100

@@ -119,35 +110,49 @@ public void writesPlainPage() throws Exception {
119110
marshaller.marshal(wrapper, new StringWriter());
120111
}
121112

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+
122133
@XmlRootElement
123134
@XmlAccessorType(XmlAccessType.FIELD)
124135
static class Wrapper {
125136

126-
@XmlElement(name = "page-request", namespace = SpringDataJaxb.NAMESPACE)
127-
Pageable pageable;
137+
@XmlElement(name = "page-request", namespace = SpringDataJaxb.NAMESPACE) Pageable pageable;
128138

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;
131140

132-
@XmlElement(name = "sort", namespace = SpringDataJaxb.NAMESPACE)
133-
Sort sort;
141+
@XmlElement(name = "sort", namespace = SpringDataJaxb.NAMESPACE) Sort sort;
134142
}
135143

136144
@XmlRootElement(name = "wrapper", namespace = SpringDataJaxb.NAMESPACE)
137145
static class PageWrapper {
138146

139147
Page<Content> page;
140148

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;
144150
}
145151

146152
@XmlRootElement
147153
static class Content {
148154

149-
@XmlAttribute
150-
String name;
155+
@XmlAttribute String name;
151156
}
152157

153158
static class LinkedPageAdapter extends PageAdapter {

0 commit comments

Comments
 (0)