Skip to content

Commit 65d31bf

Browse files
committed
Polish d65203e
* Minor formatting update * Use a regexp to discard xml header in tests
1 parent d65203e commit 65d31bf

File tree

4 files changed

+12
-29
lines changed

4 files changed

+12
-29
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
* @author Robert Kasanicky
7474
* @author Michael Minella
7575
* @author Parikshit Dutta
76+
* @author Mahmoud Ben Hassine
7677
*/
7778
public class StaxEventItemWriter<T> extends AbstractItemStreamItemWriter<T> implements
7879
ResourceAwareItemWriterItemStream<T>, InitializingBean {
@@ -635,7 +636,7 @@ protected void startDocument(XMLEventWriter writer) throws XMLStreamException {
635636
XMLEventFactory factory = createXmlEventFactory();
636637

637638
// write start document
638-
if (getStandalone()==null) {
639+
if (getStandalone() == null) {
639640
writer.add(factory.createStartDocument(getEncoding(), getVersion()));
640641
}
641642
else {

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/builder/StaxEventItemWriterBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*
2929
* @author Michael Minella
3030
* @author Parikshit Dutta
31+
* @author Mahmoud Ben Hassine
3132
* @since 4.0
3233
* @see StaxEventItemWriter
3334
*/
@@ -200,7 +201,7 @@ public StaxEventItemWriterBuilder<T> version(String version) {
200201
}
201202

202203
/**
203-
* Standalone document declaration for the output document. Defaults to null.
204+
* Standalone document declaration for the output document. Defaults to {@code null}.
204205
*
205206
* @param standalone Boolean standalone document declaration
206207
* @return the current instance of the builder

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemWriterTests.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* Tests for {@link StaxEventItemWriter}.
5858
*
5959
* @author Parikshit Dutta
60+
* @author Mahmoud Ben Hassine
6061
*/
6162
public class StaxEventItemWriterTests {
6263

@@ -1024,13 +1025,7 @@ private String getOutputFileContent(String encoding) throws IOException {
10241025
private String getOutputFileContent(String encoding, boolean discardHeader) throws IOException {
10251026
String value = FileUtils.readFileToString(resource.getFile(), encoding);
10261027
if (discardHeader) {
1027-
// standalone is omitted if not explicitly set, meaning it will be 'yes'/'no' or no standalone attribute
1028-
if (value.contains("standalone")) {
1029-
boolean standalone = value.contains("standalone='yes'");
1030-
return value.replace("<?xml version='1.0' encoding='" + encoding + "' " +
1031-
(standalone ? "standalone='yes'" : "standalone='no'") + "?>", "");
1032-
}
1033-
return value.replace("<?xml version='1.0' encoding='" + encoding + "'?>", "");
1028+
return value.replaceFirst("<\\?xml.*?\\?>", "");
10341029
}
10351030
return value;
10361031
}

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/builder/StaxEventItemWriterBuilderTests.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
/**
4848
* @author Michael Minella
4949
* @author Parikshit Dutta
50+
* @author Mahmoud Ben Hassine
5051
*/
5152
public class StaxEventItemWriterBuilderTests {
5253

@@ -240,7 +241,7 @@ public void testStandaloneDeclarationInHeaderWhenNotSet() throws Exception {
240241
staxEventItemWriter.write(this.items);
241242
staxEventItemWriter.close();
242243

243-
String output = getOutputFileContent(staxEventItemWriter.getEncoding(), false);
244+
String output = getOutputFileContent(staxEventItemWriter.getEncoding());
244245
assertFalse(output.contains("standalone="));
245246
}
246247

@@ -260,7 +261,7 @@ public void testStandaloneDeclarationInHeaderWhenSetToTrue() throws Exception {
260261
staxEventItemWriter.write(this.items);
261262
staxEventItemWriter.close();
262263

263-
String output = getOutputFileContent(staxEventItemWriter.getEncoding(), false);
264+
String output = getOutputFileContent(staxEventItemWriter.getEncoding());
264265
assertTrue(output.contains("standalone='yes'"));
265266
}
266267

@@ -280,31 +281,16 @@ public void testStandaloneDeclarationInHeaderWhenSetToFalse() throws Exception {
280281
staxEventItemWriter.write(this.items);
281282
staxEventItemWriter.close();
282283

283-
String output = getOutputFileContent(staxEventItemWriter.getEncoding(), false);
284+
String output = getOutputFileContent(staxEventItemWriter.getEncoding());
284285
assertTrue(output.contains("standalone='no'"));
285286
}
286287

287-
private String getOutputFileContent(String encoding) throws IOException {
288-
return getOutputFileContent(encoding, true);
289-
}
290-
291288
/**
292289
* @param encoding the encoding
293-
* @param discardHeader the flag to strip XML header
294290
* @return output file content as String
295291
*/
296-
private String getOutputFileContent(String encoding, boolean discardHeader) throws IOException {
297-
String value = FileUtils.readFileToString(resource.getFile(), encoding);
298-
if (discardHeader) {
299-
// standalone is omitted if not explicitly set, meaning it will be 'yes'/'no' or no standalone attribute
300-
if (value.contains("standalone")) {
301-
boolean standalone = value.contains("standalone='yes'");
302-
return value.replace("<?xml version='1.0' encoding='" + encoding + "' " +
303-
(standalone ? "standalone='yes'" : "standalone='no'") + "?>", "");
304-
}
305-
return value.replace("<?xml version='1.0' encoding='" + encoding + "'?>", "");
306-
}
307-
return value;
292+
private String getOutputFileContent(String encoding) throws IOException {
293+
return FileUtils.readFileToString(resource.getFile(), encoding);
308294
}
309295

310296
@XmlRootElement(name="item", namespace="https://www.springframework.org/test")

0 commit comments

Comments
 (0)