@Override public void open(ExecutionContext executionContext) { super.open(executionContext); openCalled = true; }
@Test public void testRead() throws Exception { reader.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "input.xml"))); reader.open(new ExecutionContext()); Trade result; List<Trade> results = new ArrayList<>(); while ((result = reader.read()) != null) { results.add(result); } checkResults(results); }
@Test public void testReadNested() throws Exception { reader.setResource(new ClassPathResource(ClassUtils .addResourcePathToPackagePath(getClass(), "input-nested.xml"))); reader.open(new ExecutionContext()); Trade result; List<Trade> results = new ArrayList<>(); while ((result = reader.read()) != null) { results.add(result); } checkResults(results); }
@Override protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception { StaxEventItemReader<Foo> reader = (StaxEventItemReader<Foo>) tested; reader.close(); reader.setResource(new ByteArrayResource("<foos />".getBytes())); reader.afterPropertiesSet(); reader.open(new ExecutionContext()); }
/** * Save restart data and restore from it. */ @Test public void testRestart() throws Exception { source.open(executionContext); source.read(); source.update(executionContext); assertEquals(1, executionContext.getInt(ClassUtils.getShortName(StaxEventItemReader.class) + ".read.count")); List<XMLEvent> expectedAfterRestart = source.read(); source = createNewInputSource(); source.open(executionContext); List<XMLEvent> afterRestart = source.read(); assertEquals(expectedAfterRestart.size(), afterRestart.size()); }
@Test public void testDirectoryResource() throws Exception { FileSystemResource resource = new FileSystemResource("build/data"); resource.getFile().mkdirs(); assertTrue(resource.getFile().isDirectory()); source.setResource(resource); source.afterPropertiesSet(); source.setStrict(false); source.open(executionContext); assertNull(source.read()); }
@Test(expected = ItemStreamException.class) public void testStrictness() throws Exception { source.setResource(new NonExistentResource()); source.setStrict(true); source.afterPropertiesSet(); source.open(executionContext); }
@Test public void testRuntimeFileCreation() throws Exception { source.setResource(new NonExistentResource()); source.afterPropertiesSet(); source.setResource(new ByteArrayResource(xml.getBytes())); source.open(executionContext); source.read(); }
@Before public void setUp() throws Exception { reader.setResource(resource); reader.setFragmentRootElementName("{urn:org.springframework.batch.io.oxm.domain}trade"); reader.setUnmarshaller(getUnmarshaller()); reader.afterPropertiesSet(); reader.open(new ExecutionContext()); }
@Test public void testNonExistentResource() throws Exception { source.setResource(new NonExistentResource()); source.afterPropertiesSet(); source.setStrict(false); source.open(executionContext); assertNull(source.read()); }
@Test public void testFragmentNamespace() throws Exception { source.setResource(new ByteArrayResource(fooXml.getBytes())); source.afterPropertiesSet(); source.open(executionContext); // see asserts in the mock unmarshaller assertNotNull(source.read()); assertNotNull(source.read()); assertNull(source.read()); // there are only two fragments source.close(); }
@Test public void testFragmentMixedNamespace() throws Exception { source.setResource(new ByteArrayResource(mixedXml.getBytes())); source.setFragmentRootElementName("{urn:org.test.bar}" + FRAGMENT_ROOT_ELEMENT); source.afterPropertiesSet(); source.open(executionContext); // see asserts in the mock unmarshaller assertNotNull(source.read()); assertNotNull(source.read()); assertNull(source.read()); // there are only two fragments source.close(); }
@Test public void testMultiFragmentNameSpace() throws Exception { source.setResource(new ByteArrayResource(xmlMultiFragment.getBytes())); source.setFragmentRootElementNames(new String[] {"{urn:org.test.bar}fragmentA", "fragmentB"}); source.afterPropertiesSet(); source.open(executionContext); // see asserts in the mock unmarshaller assertNotNull(source.read()); assertNotNull(source.read()); assertNull(source.read()); // there are only two fragments (one has wrong namespace) source.close(); }
/** * Test restart at end of file. */ @Test public void testRestartAtEndOfFile() throws Exception { source.open(executionContext); assertNotNull(source.read()); assertNotNull(source.read()); assertNull(source.read()); source.update(executionContext); source.close(); assertEquals(3, executionContext.getInt(ClassUtils.getShortName(StaxEventItemReader.class) + ".read.count")); source = createNewInputSource(); source.open(executionContext); assertNull(source.read()); }
@Test public void testItemCountAwareFragmentRestart() throws Exception { StaxEventItemReader<ItemCountAwareFragment> source = createNewItemCountAwareInputSource(); source.afterPropertiesSet(); source.open(executionContext); assertEquals(1, source.read().getItemCount()); source.update(executionContext); source.close(); source = createNewItemCountAwareInputSource(); source.afterPropertiesSet(); source.open(executionContext); assertEquals(2, source.read().getItemCount()); assertNull(source.read()); // there are only two fragments source.close(); }
@Test public void testMultiFragment() throws Exception { source.setResource(new ByteArrayResource(xmlMultiFragment.getBytes())); source.setFragmentRootElementNames(MULTI_FRAGMENT_ROOT_ELEMENTS); source.afterPropertiesSet(); source.open(executionContext); // see asserts in the mock unmarshaller assertNotNull(source.read()); assertNotNull(source.read()); assertNotNull(source.read()); assertNull(source.read()); // there are only three fragments source.close(); }
/** * Regular usage scenario. ItemReader should pass XML fragments to unmarshaller wrapped with StartDocument and * EndDocument events. */ @Test public void testFragmentWrapping() throws Exception { source.afterPropertiesSet(); source.open(executionContext); // see asserts in the mock unmarshaller assertNotNull(source.read()); assertNotNull(source.read()); assertNull(source.read()); // there are only two fragments source.close(); }
@Test public void testMultiFragmentNested() throws Exception { source.setResource(new ByteArrayResource(xmlMultiFragmentNested.getBytes())); source.setFragmentRootElementNames(MULTI_FRAGMENT_ROOT_ELEMENTS); source.afterPropertiesSet(); source.open(executionContext); // see asserts in the mock unmarshaller assertNotNull(source.read()); assertNotNull(source.read()); assertNotNull(source.read()); assertNull(source.read()); // there are only three fragments source.close(); }
@Test(expected = ItemStreamException.class) public void testStrict() throws Exception { Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller(); unmarshaller.setClassesToBeBound(Foo.class); StaxEventItemReader<Foo> reader = new StaxEventItemReaderBuilder<Foo>() .name("fooReader") .resource(this.resource) .addFragmentRootElements("foo") .unmarshaller(unmarshaller) .build(); reader.afterPropertiesSet(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); }
@Test public void testItemCountAwareFragment() throws Exception { StaxEventItemReader<ItemCountAwareFragment> source = createNewItemCountAwareInputSource(); source.afterPropertiesSet(); source.open(executionContext); assertEquals(1, source.read().getItemCount()); assertEquals(2, source.read().getItemCount()); assertNull(source.read()); // there are only two fragments source.close(); }