Example usage for javax.xml.stream XMLEventFactory createEndElement

List of usage examples for javax.xml.stream XMLEventFactory createEndElement

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventFactory createEndElement.

Prototype

public abstract EndElement createEndElement(String prefix, String namespaceUri, String localName);

Source Link

Document

Create a new EndElement

Usage

From source file:org.springframework.batch.item.xml.TransactionalStaxEventItemWriterTests.java

/**
 * Item is written to the output file only after flush.
 *//*from  w  w w . j  a v a2  s  .c o  m*/
@Test
public void testWriteWithHeaderAfterRollback() throws Exception {
    writer.setHeaderCallback(new StaxWriterCallback() {

        @Override
        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<Void>() {
            @Override
            public Void doInTransaction(TransactionStatus status) {
                try {
                    writer.write(items);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                throw new RuntimeException("Planned");
            }
        });
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        // expected
    }
    writer.close();
    writer.open(executionContext);
    new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus status) {
            try {
                writer.write(items);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return null;
        }
    });
    writer.close();
    String content = outputFileContent();
    assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, ("<header/>")));
    assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, TEST_STRING));
}

From source file:org.springframework.batch.item.xml.TransactionalStaxEventItemWriterTests.java

/**
 * Item is written to the output file only after flush.
 *///from  www . ja v a2  s.c o m
@Test
public void testWriteWithHeaderAfterFlushAndRollback() throws Exception {
    writer.setHeaderCallback(new StaxWriterCallback() {

        @Override
        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<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus status) {
            try {
                writer.write(items);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return null;
        }
    });
    writer.update(executionContext);
    writer.close();
    writer.open(executionContext);
    try {
        new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
            @Override
            public Void doInTransaction(TransactionStatus status) {
                try {
                    writer.write(items);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                throw new RuntimeException("Planned");
            }
        });
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        // expected
    }
    writer.close();
    String content = outputFileContent();
    assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, ("<header/>")));
    assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, TEST_STRING));
}