Example usage for org.springframework.batch.item ExecutionContext ExecutionContext

List of usage examples for org.springframework.batch.item ExecutionContext ExecutionContext

Introduction

In this page you can find the example usage for org.springframework.batch.item ExecutionContext ExecutionContext.

Prototype

public ExecutionContext() 

Source Link

Document

Default constructor.

Usage

From source file:org.springframework.batch.item.xml.builder.StaxEventItemWriterBuilderTests.java

@Test(expected = ItemStreamException.class)
public void testOverwriteOutput() throws Exception {
    StaxEventItemWriter<Foo> staxEventItemWriter = new StaxEventItemWriterBuilder<Foo>().name("fooWriter")
            .marshaller(marshaller).resource(this.resource).overwriteOutput(false).build();

    staxEventItemWriter.afterPropertiesSet();

    ExecutionContext executionContext = new ExecutionContext();
    staxEventItemWriter.open(executionContext);

    staxEventItemWriter.write(this.items);

    staxEventItemWriter.update(executionContext);
    staxEventItemWriter.close();//  www . ja va 2s . c  o  m

    File output = this.resource.getFile();

    assertTrue(output.exists());

    executionContext = new ExecutionContext();
    staxEventItemWriter.open(executionContext);
}

From source file:org.springframework.batch.item.xml.builder.StaxEventItemWriterBuilderTests.java

@Test
public void testDeleteIfEmpty() throws Exception {
    ExecutionContext executionContext = new ExecutionContext();

    StaxEventItemWriter<Foo> staxEventItemWriter = new StaxEventItemWriterBuilder<Foo>().name("fooWriter")
            .resource(this.resource).marshaller(this.marshaller).shouldDeleteIfEmpty(true).build();

    staxEventItemWriter.afterPropertiesSet();
    staxEventItemWriter.open(executionContext);
    staxEventItemWriter.write(Collections.emptyList());
    staxEventItemWriter.update(executionContext);
    staxEventItemWriter.close();/*  w  w w  .j a  va2s.  c o m*/

    File file = this.resource.getFile();

    assertFalse(file.exists());
}

From source file:org.springframework.batch.item.xml.builder.StaxEventItemWriterBuilderTests.java

@Test
public void testTransactional() {

    StaxEventItemWriter<Foo> staxEventItemWriter = new StaxEventItemWriterBuilder<Foo>().name("fooWriter")
            .resource(this.resource).marshaller(this.marshaller).transactional(true).forceSync(true).build();

    ExecutionContext executionContext = new ExecutionContext();

    staxEventItemWriter.open(executionContext);

    Object writer = ReflectionTestUtils.getField(staxEventItemWriter, "bufferedWriter");

    assertTrue(writer instanceof TransactionAwareBufferedWriter);

    assertTrue((Boolean) ReflectionTestUtils.getField(writer, "forceSync"));
}

From source file:org.springframework.batch.item.xml.builder.StaxEventItemWriterBuilderTests.java

@Test
public void testConfiguration() throws Exception {
    Map<String, String> rootElementAttributes = new HashMap<>();
    rootElementAttributes.put("baz", "quix");

    StaxEventItemWriter<Foo> staxEventItemWriter = new StaxEventItemWriterBuilder<Foo>().name("fooWriter")
            .marshaller(marshaller).encoding("UTF-16").footerCallback(writer -> {
                XMLEventFactory factory = XMLEventFactory.newInstance();
                try {
                    writer.add(factory.createEndElement("ns", "http://www.springframework.org/test", "group"));
                } catch (XMLStreamException e) {
                    throw new RuntimeException(e);
                }//ww  w.j ava2  s  .c  om
            }).headerCallback(writer -> {
                XMLEventFactory factory = XMLEventFactory.newInstance();
                try {
                    writer.add(
                            factory.createStartElement("ns", "http://www.springframework.org/test", "group"));
                } catch (XMLStreamException e) {
                    throw new RuntimeException(e);
                }
            }).resource(this.resource).rootTagName("foobarred").rootElementAttributes(rootElementAttributes)
            .saveState(false).version("1.1").build();

    staxEventItemWriter.afterPropertiesSet();

    ExecutionContext executionContext = new ExecutionContext();
    staxEventItemWriter.open(executionContext);

    staxEventItemWriter.write(this.items);

    staxEventItemWriter.update(executionContext);
    staxEventItemWriter.close();

    assertEquals(FULL_OUTPUT, getOutputFileContent("UTF-16"));
    assertEquals(0, executionContext.size());
}

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

@Before
public void setUp() throws Exception {

    File directory = new File("target/data");
    directory.mkdirs();//from ww w  . j av a2s .  c o  m
    outputFile = File.createTempFile(ClassUtils.getShortName(this.getClass()), ".xml", directory);
    resource = new FileSystemResource(outputFile);

    writer.setResource(resource);

    writer.setMarshaller(getMarshaller());
    writer.setRootTagName("{urn:org.springframework.batch.io.oxm.domain}trades");

    writer.afterPropertiesSet();

    writer.open(new ExecutionContext());

}

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

@Before
public void setUp() throws Exception {
    File directory = new File("build/data");
    directory.mkdirs();/*from   w  w w  .j  a  v  a  2  s.co  m*/
    resource = new FileSystemResource(
            File.createTempFile("StaxEventWriterOutputSourceTests", ".xml", directory));
    writer = createItemWriter();
    executionContext = new ExecutionContext();
    jaxbMarshaller = new Jaxb2Marshaller();
    jaxbMarshaller.setClassesToBeBound(JAXBItem.class);
}

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

@Before
public void setUp() throws Exception {
    resource = new FileSystemResource(File.createTempFile("StaxEventWriterOutputSourceTests", ".xml"));
    writer = createItemWriter();//from  w  w w.  jav  a  2s.c o m
    executionContext = new ExecutionContext();
}