/** * Read input from start to end. */ @Test public void testRead() throws Exception { tested.open(ctx); assertEquals("1", tested.read()); assertEquals("2", tested.read()); assertEquals("3", tested.read()); assertEquals("4", tested.read()); assertEquals("5", tested.read()); assertEquals("6", tested.read()); assertEquals("7", tested.read()); assertEquals("8", tested.read()); assertEquals(null, tested.read()); tested.close(); }
private void assertValueAndResource(Resource expectedResource, String expectedValue) throws Exception { Foo foo = tested.read(); assertEquals(expectedValue, foo.value); assertEquals(expectedResource, foo.resource); }
/** * Restore from saved state. */ @Test public void testRestart() throws Exception { tested.open(ctx); assertEquals("1", tested.read()); assertEquals("2", tested.read()); assertEquals("3", tested.read()); assertEquals("4", tested.read()); tested.update(ctx); assertEquals("5", tested.read()); assertEquals("6", tested.read()); tested.close(); tested.open(ctx); assertEquals("5", tested.read()); assertEquals("6", tested.read()); assertEquals("7", tested.read()); assertEquals("8", tested.read()); assertEquals(null, tested.read()); }
@Test public void testRestartWhenStateNotSaved() throws Exception { tested.setSaveState(false); tested.open(ctx); assertEquals("1", tested.read()); tested.update(ctx); assertEquals("2", tested.read()); assertEquals("3", tested.read()); tested.close(); tested.open(ctx); assertEquals("1", tested.read()); }
assertEquals("1", tested.read()); assertEquals("2", tested.read()); assertEquals("3", tested.read()); assertEquals("2", tested.read()); assertEquals("3", tested.read()); assertEquals("4", tested.read()); assertEquals("2", tested.read()); assertEquals("3", tested.read()); assertEquals("4", tested.read()); assertEquals("5", tested.read()); assertEquals("6", tested.read()); assertEquals("7", tested.read()); assertEquals("6", tested.read()); assertEquals("7", tested.read()); assertEquals("8", tested.read()); assertEquals(null, tested.read());
@Test public void testMiddleResourceThrowsException() throws Exception { Resource badResource = new AbstractResource() { @Override public InputStream getInputStream() throws IOException { throw new RuntimeException(); } @Override public String getDescription() { return null; } }; tested.setResources(new Resource[] { r1, badResource, r3, r4, r5 }); tested.open(ctx); assertEquals("1", tested.read()); assertEquals("2", tested.read()); assertEquals("3", tested.read()); try { assertEquals("4", tested.read()); fail(); } catch (ItemStreamException ex) { // a try/catch was used to ensure the exception was thrown when reading // the 4th item, rather than on open } }
@Test public void testBadIOInput() throws Exception { Resource badResource = new AbstractResource() { @Override public boolean exists() { // Looks good ... return true; } @Override public InputStream getInputStream() throws IOException { // ... but fails during read throw new RuntimeException(); } @Override public String getDescription() { return null; } }; tested.setResources(new Resource[] { badResource, r2, r3, r4, r5 }); tested.open(ctx); try { assertEquals("1", tested.read()); fail(); } catch (ItemStreamException ex) { // expected } // Now check the next read gets the next resource assertEquals("4", tested.read()); }
@Test public void testGetCurrentResource() throws Exception { tested.open(ctx); assertEquals("1", tested.read()); assertSame(r1, tested.getCurrentResource()); assertEquals("2", tested.read()); assertSame(r1, tested.getCurrentResource()); assertEquals("3", tested.read()); assertSame(r1, tested.getCurrentResource()); assertEquals("4", tested.read()); assertSame(r2, tested.getCurrentResource()); assertEquals("5", tested.read()); assertSame(r2, tested.getCurrentResource()); assertEquals("6", tested.read()); assertSame(r4, tested.getCurrentResource()); assertEquals("7", tested.read()); assertSame(r5, tested.getCurrentResource()); assertEquals("8", tested.read()); assertSame(r5, tested.getCurrentResource()); assertEquals(null, tested.read()); assertSame(null, tested.getCurrentResource()); tested.close(); }
/** * Missing resource is OK. */ @Test public void testNonExistentResources() throws Exception { tested.setResources(new Resource[] { new FileSystemResource("no/such/file.txt") }); itemReader.setStrict(false); tested.open(new ExecutionContext()); assertNull(tested.read()); tested.close(); }
/** * Directory resource behaves as if it was empty. */ @Test public void testDirectoryResources() throws Exception { FileSystemResource resource = new FileSystemResource("build/data"); resource.getFile().mkdirs(); assertTrue(resource.getFile().isDirectory()); tested.setResources(new Resource[] { resource }); itemReader.setStrict(false); tested.open(new ExecutionContext()); assertNull(tested.read()); tested.close(); }
/** * Empty resource list is OK. */ @Test public void testNoResourcesFound() throws Exception { tested.setResources(new Resource[] {}); tested.open(new ExecutionContext()); assertNull(tested.read()); tested.close(); }
/** * E.g. when using the reader in the processing phase reading might not have been attempted at all before the job * crashed (BATCH-1798). */ @Test public void testRestartAfterFailureWithoutRead() throws Exception { // save reader state without calling read tested.open(ctx); tested.update(ctx); tested.close(); // restart should work OK tested.open(ctx); assertEquals("1", tested.read()); }
@Test public void testFirstResourceThrowsExceptionOnRead() throws Exception { Resource badResource = new AbstractResource() { @Override public InputStream getInputStream() throws IOException { throw new RuntimeException(); } @Override public String getDescription() { return null; } }; tested.setResources(new Resource[] { badResource, r2, r3, r4, r5 }); tested.open(ctx); try { assertEquals("1", tested.read()); fail(); } catch (ItemStreamException ex) { // a try/catch was used to ensure the exception was thrown when reading // the 1st item, rather than on open } }
/** * Test {@link org.springframework.batch.item.ItemStream} lifecycle symmetry */ @Test public void testNonExistentResourcesItemStreamLifecycle() throws Exception { ItemStreamReaderImpl delegate = new ItemStreamReaderImpl(); tested.setDelegate(delegate); tested.setResources(new Resource[] { }); itemReader.setStrict(false); tested.open(new ExecutionContext()); assertNull(tested.read()); assertFalse(delegate.openCalled); assertFalse(delegate.closeCalled); assertFalse(delegate.updateCalled); tested.close(); }
/** * Read input from start to end. */ @Test public void testRead() throws Exception { tested.open(ctx); assertValueAndResource(r1, "1"); assertValueAndResource(r1, "2"); assertValueAndResource(r1, "3"); assertValueAndResource(r2, "4"); assertValueAndResource(r2, "5"); assertValueAndResource(r4, "6"); assertValueAndResource(r5, "7"); assertValueAndResource(r5, "8"); assertEquals(null, tested.read()); tested.close(); }