IN PROGRESS - BATCH-826: Create callback for header and footer reading in xml and flat files

changed header and footer items to callbacks in StaxEventItemWriter
This commit is contained in:
robokaso
2008-09-11 09:49:42 +00:00
parent 4b5cbef820
commit 13e5796b85
4 changed files with 125 additions and 43 deletions

View File

@@ -10,11 +10,11 @@ import static org.junit.Assert.fail;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Result;
@@ -95,7 +95,6 @@ public class StaxEventItemWriterTests {
// check the output is concatenation of 'before restart' and 'after
// restart' writes.
String outputFile = outputFileContent();
System.out.println(outputFile);
assertEquals(2, StringUtils.countOccurrencesOf(outputFile, TEST_STRING));
assertTrue(outputFile.contains("<root>" + TEST_STRING + TEST_STRING + "</root>"));
}
@@ -105,19 +104,26 @@ public class StaxEventItemWriterTests {
*/
@Test
public void testWriteWithHeader() throws Exception {
final Object header1 = new Object();
final Object header2 = new Object();
writer.setHeaderItems(new ArrayList<Object>() {
{
add(header1);
add(header2);
writer.setHeaderCallback(new StaxWriterCallback(){
public void write(XMLEventWriter writer) throws IOException {
XMLEventFactory factory = XMLEventFactory.newInstance();
try {
writer.add(factory.createStartElement("", "", "header"));
writer.add(factory.createEndElement("", "", "header"));
}
catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
});
writer.open(executionContext);
writer.write(items);
String content = outputFileContent();
assertTrue("Wrong content: " + content, content.contains(("<!--" + header1 + "-->")));
assertTrue("Wrong content: " + content, content.contains(("<!--" + header2 + "-->")));
assertTrue("Wrong content: " + content, content.contains(("<header></header>")));
assertTrue("Wrong content: " + content, content.contains(TEST_STRING));
}
@@ -144,12 +150,45 @@ public class StaxEventItemWriterTests {
*/
@Test
public void testOpenAndClose() throws Exception {
writer.setHeaderCallback(new StaxWriterCallback(){
public void write(XMLEventWriter writer) throws IOException {
XMLEventFactory factory = XMLEventFactory.newInstance();
try {
writer.add(factory.createStartElement("", "", "header"));
writer.add(factory.createEndElement("", "", "header"));
}
catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
});
writer.setFooterCallback(new StaxWriterCallback() {
public void write(XMLEventWriter writer) throws IOException {
XMLEventFactory factory = XMLEventFactory.newInstance();
try {
writer.add(factory.createStartElement("", "", "footer"));
writer.add(factory.createEndElement("", "", "footer"));
}
catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
});
writer.setRootTagName("testroot");
writer.setRootElementAttributes(Collections.<String, String> singletonMap("attribute", "value"));
writer.open(executionContext);
writer.close(null);
String content = outputFileContent();
assertTrue(content.contains("<testroot attribute=\"value\">"));
assertTrue(content.contains("<header></header>"));
assertTrue(content.contains("<footer></footer>"));
assertTrue(content.endsWith("</testroot>"));
}

View File

@@ -10,6 +10,7 @@ import java.util.Collections;
import java.util.List;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Result;
@@ -92,8 +93,21 @@ public class TransactionalStaxEventItemWriterTests {
*/
@Test
public void testWriteWithHeaderAfterRollback() throws Exception {
Object header = new Object();
writer.setHeaderItems(Collections.singletonList(header));
writer.setHeaderCallback(new StaxWriterCallback(){
public void write(XMLEventWriter writer) throws IOException {
XMLEventFactory factory = XMLEventFactory.newInstance();
try {
writer.add(factory.createStartElement("", "", "header"));
writer.add(factory.createEndElement("", "", "header"));
}
catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
});
writer.open(executionContext);
try {
new TransactionTemplate(transactionManager).execute(new TransactionCallback() {
@@ -127,7 +141,7 @@ public class TransactionalStaxEventItemWriterTests {
});
writer.close(executionContext);
String content = outputFileContent();
assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, ("<!--" + header + "-->")));
assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, ("<header></header>")));
assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, TEST_STRING));
}
@@ -136,8 +150,21 @@ public class TransactionalStaxEventItemWriterTests {
*/
@Test
public void testWriteWithHeaderAfterFlushAndRollback() throws Exception {
Object header = new Object();
writer.setHeaderItems(Collections.singletonList(header));
writer.setHeaderCallback(new StaxWriterCallback(){
public void write(XMLEventWriter writer) throws IOException {
XMLEventFactory factory = XMLEventFactory.newInstance();
try {
writer.add(factory.createStartElement("", "", "header"));
writer.add(factory.createEndElement("", "", "header"));
}
catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
});
writer.open(executionContext);
new TransactionTemplate(transactionManager).execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
@@ -172,7 +199,7 @@ public class TransactionalStaxEventItemWriterTests {
}
writer.close(executionContext);
String content = outputFileContent();
assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, ("<!--" + header + "-->")));
assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, ("<header></header>")));
assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, TEST_STRING));
}