* Minor formatting update
* Use a regexp to discard xml header in tests
This commit is contained in:
Mahmoud Ben Hassine
2020-09-09 16:25:02 +02:00
parent d65203e2d3
commit 65d31bff1d
4 changed files with 12 additions and 29 deletions

View File

@@ -73,6 +73,7 @@ import org.springframework.util.StringUtils;
* @author Robert Kasanicky
* @author Michael Minella
* @author Parikshit Dutta
* @author Mahmoud Ben Hassine
*/
public class StaxEventItemWriter<T> extends AbstractItemStreamItemWriter<T> implements
ResourceAwareItemWriterItemStream<T>, InitializingBean {
@@ -635,7 +636,7 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
XMLEventFactory factory = createXmlEventFactory();
// write start document
if (getStandalone()==null) {
if (getStandalone() == null) {
writer.add(factory.createStartDocument(getEncoding(), getVersion()));
}
else {

View File

@@ -28,6 +28,7 @@ import org.springframework.util.Assert;
*
* @author Michael Minella
* @author Parikshit Dutta
* @author Mahmoud Ben Hassine
* @since 4.0
* @see StaxEventItemWriter
*/
@@ -200,7 +201,7 @@ public class StaxEventItemWriterBuilder<T> {
}
/**
* Standalone document declaration for the output document. Defaults to null.
* Standalone document declaration for the output document. Defaults to {@code null}.
*
* @param standalone Boolean standalone document declaration
* @return the current instance of the builder

View File

@@ -57,6 +57,7 @@ import static org.mockito.Mockito.when;
* Tests for {@link StaxEventItemWriter}.
*
* @author Parikshit Dutta
* @author Mahmoud Ben Hassine
*/
public class StaxEventItemWriterTests {
@@ -1024,13 +1025,7 @@ public class StaxEventItemWriterTests {
private String getOutputFileContent(String encoding, boolean discardHeader) throws IOException {
String value = FileUtils.readFileToString(resource.getFile(), encoding);
if (discardHeader) {
// standalone is omitted if not explicitly set, meaning it will be 'yes'/'no' or no standalone attribute
if (value.contains("standalone")) {
boolean standalone = value.contains("standalone='yes'");
return value.replace("<?xml version='1.0' encoding='" + encoding + "' " +
(standalone ? "standalone='yes'" : "standalone='no'") + "?>", "");
}
return value.replace("<?xml version='1.0' encoding='" + encoding + "'?>", "");
return value.replaceFirst("<\\?xml.*?\\?>", "");
}
return value;
}

View File

@@ -47,6 +47,7 @@ import static org.junit.Assert.assertTrue;
/**
* @author Michael Minella
* @author Parikshit Dutta
* @author Mahmoud Ben Hassine
*/
public class StaxEventItemWriterBuilderTests {
@@ -240,7 +241,7 @@ public class StaxEventItemWriterBuilderTests {
staxEventItemWriter.write(this.items);
staxEventItemWriter.close();
String output = getOutputFileContent(staxEventItemWriter.getEncoding(), false);
String output = getOutputFileContent(staxEventItemWriter.getEncoding());
assertFalse(output.contains("standalone="));
}
@@ -260,7 +261,7 @@ public class StaxEventItemWriterBuilderTests {
staxEventItemWriter.write(this.items);
staxEventItemWriter.close();
String output = getOutputFileContent(staxEventItemWriter.getEncoding(), false);
String output = getOutputFileContent(staxEventItemWriter.getEncoding());
assertTrue(output.contains("standalone='yes'"));
}
@@ -280,31 +281,16 @@ public class StaxEventItemWriterBuilderTests {
staxEventItemWriter.write(this.items);
staxEventItemWriter.close();
String output = getOutputFileContent(staxEventItemWriter.getEncoding(), false);
String output = getOutputFileContent(staxEventItemWriter.getEncoding());
assertTrue(output.contains("standalone='no'"));
}
private String getOutputFileContent(String encoding) throws IOException {
return getOutputFileContent(encoding, true);
}
/**
* @param encoding the encoding
* @param discardHeader the flag to strip XML header
* @return output file content as String
*/
private String getOutputFileContent(String encoding, boolean discardHeader) throws IOException {
String value = FileUtils.readFileToString(resource.getFile(), encoding);
if (discardHeader) {
// standalone is omitted if not explicitly set, meaning it will be 'yes'/'no' or no standalone attribute
if (value.contains("standalone")) {
boolean standalone = value.contains("standalone='yes'");
return value.replace("<?xml version='1.0' encoding='" + encoding + "' " +
(standalone ? "standalone='yes'" : "standalone='no'") + "?>", "");
}
return value.replace("<?xml version='1.0' encoding='" + encoding + "'?>", "");
}
return value;
private String getOutputFileContent(String encoding) throws IOException {
return FileUtils.readFileToString(resource.getFile(), encoding);
}
@XmlRootElement(name="item", namespace="https://www.springframework.org/test")