@Test public void testUpdateOnBoundary() { monitor.resetOffset(); ExecutionContext executionContext = new ExecutionContext(); monitor.update(executionContext); assertEquals(0, executionContext.size()); executionContext.put(ChunkMonitor.class.getName() + ".OFFSET", 3); monitor.update(executionContext); assertEquals(0, executionContext.size()); }
@Test public void testUpdateVanilla() { monitor.incrementOffset(); ExecutionContext executionContext = new ExecutionContext(); monitor.update(executionContext); assertEquals(1, executionContext.size()); }
@Test public void testSetSaveState() throws Exception { reader.read(); ExecutionContext context = new ExecutionContext(); reader.update(context); assertEquals(1, context.size()); }
@Test public void testUpdateWithMax() throws Exception { ExecutionContext context = new ExecutionContext(); context.putInt("foo.read.count.max", 1); reader.open(context); reader.update(context); assertEquals(2, context.size()); }
@Test public void testUpdate() throws Exception { reader.read(); ExecutionContext context = new ExecutionContext(); reader.update(context); assertEquals(1, context.size()); assertEquals(1, context.getInt("foo.read.count")); }
@Test public void testUpdateWithNoStream() throws Exception { monitor = new ChunkMonitor(); monitor.setItemReader(new ItemReader<String>() { @Override public String read() throws Exception, UnexpectedInputException, ParseException { return "" + (count++); } }); monitor.setChunkSize(CHUNK_SIZE); monitor.incrementOffset(); ExecutionContext executionContext = new ExecutionContext(); monitor.update(executionContext); assertEquals(0, executionContext.size()); } }
@Test public void testSaveStateDisabled() throws Exception { this.ldifReader = new LdifReaderBuilder().saveState(false).resource(context.getResource("classpath:/test.ldif")) .build(); ExecutionContext executionContext = new ExecutionContext(); firstRead(executionContext); this.ldifReader.update(executionContext); assertEquals("ExecutionContext should have been empty", 0, executionContext.size()); }
@Test public void testConfigurationNoSaveState() throws Exception { Map<String, Object> parameters = new HashMap<>(); parameters.put("value", 2); HibernateCursorItemReader<Foo> reader = new HibernateCursorItemReaderBuilder<Foo>() .name("fooReader") .sessionFactory(this.sessionFactory) .queryString("from Foo foo where foo.id > :value") .parameterValues(parameters) .saveState(false) .build(); reader.afterPropertiesSet(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); int i = 0; while(reader.read() != null) { i++; } reader.update(executionContext); reader.close(); assertEquals(3, i); assertEquals(0, executionContext.size()); }
@Test public void testConfigurationNoSaveState() throws Exception { Map<String, Object> parameters = new HashMap<>(); parameters.put("value", 2); HibernatePagingItemReader<Foo> reader = new HibernatePagingItemReaderBuilder<Foo>() .name("fooReader") .sessionFactory(this.sessionFactory) .queryString("from Foo foo where foo.id > :value") .parameterValues(parameters) .saveState(false) .build(); reader.afterPropertiesSet(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); int i = 0; while(reader.read() != null) { i++; } reader.update(executionContext); reader.close(); assertEquals(3, i); assertEquals(0, executionContext.size()); }
@Test public void testConfigurationNoSaveState() throws Exception { Map<String, Object> parameters = new HashMap<>(); parameters.put("value", 2); JpaPagingItemReader<Foo> reader = new JpaPagingItemReaderBuilder<Foo>() .name("fooReader") .entityManagerFactory(this.entityManagerFactory) .queryString("select f from Foo f where f.id > :value") .parameterValues(parameters) .saveState(false) .build(); reader.afterPropertiesSet(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); int i = 0; while(reader.read() != null) { i++; } reader.update(executionContext); reader.close(); assertEquals(3, i); assertEquals(0, executionContext.size()); }
@Test public void testSaveStateDisabled() throws Exception { this.mappingLdifReader = new MappingLdifReaderBuilder<LdapAttributes>() .saveState(false) .recordMapper(new TestMapper()) .resource(context.getResource("classpath:/test.ldif")) .build(); ExecutionContext executionContext = new ExecutionContext(); firstRead(executionContext); this.mappingLdifReader.update(executionContext); assertEquals("ExecutionContext should have been empty", 0, executionContext.size()); }
@Test public void testMaxRows() throws Exception { JdbcCursorItemReader<Foo> reader = new JdbcCursorItemReaderBuilder<Foo>() .dataSource(this.dataSource) .name("fooReader") .sql("SELECT * FROM FOO ORDER BY FIRST") .maxRows(2) .saveState(false) .rowMapper((rs, rowNum) -> { Foo foo = new Foo(); foo.setFirst(rs.getInt("FIRST")); foo.setSecond(rs.getString("SECOND")); foo.setThird(rs.getString("THIRD")); return foo; }) .build(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); validateFoo(reader.read(), 1, "2", "3"); validateFoo(reader.read(), 4, "5", "6"); assertNull(reader.read()); reader.close(); assertEquals(0, executionContext.size()); }
@SuppressWarnings("serial") @Test public void testUnexpectedRollback() throws Exception { taskletStep.setTransactionManager(new ResourcelessTransactionManager() { @Override protected void doCommit(DefaultTransactionStatus status) throws TransactionException { super.doRollback(status); throw new UnexpectedRollbackException("bar"); } }); taskletStep.setTasklet(new Tasklet() { @Override public RepeatStatus execute(StepContribution contribution, ChunkContext attributes) throws Exception { attributes.getStepContext().getStepExecution().getExecutionContext().putString("foo", "bar"); return RepeatStatus.FINISHED; } }); taskletStep.execute(stepExecution); assertEquals(FAILED, stepExecution.getStatus()); Throwable e = stepExecution.getFailureExceptions().get(0); assertEquals("bar", e.getMessage()); assertEquals(0, stepExecution.getCommitCount()); assertEquals(1, stepExecution.getRollbackCount()); // Failed transaction // counts as // rollback assertEquals(2, stepExecution.getExecutionContext().size()); assertTrue(stepExecution.getExecutionContext().containsKey(Step.STEP_TYPE_KEY)); assertTrue(stepExecution.getExecutionContext().containsKey(TaskletStep.TASKLET_TYPE_KEY)); }
@Test public void testNoSaveState() throws Exception { StoredProcedureItemReader<Foo> reader = new StoredProcedureItemReaderBuilder<Foo>() .dataSource(this.dataSource) .procedureName("read_foos") .rowMapper(new FooRowMapper()) .verifyCursorPosition(false) .saveState(false) .build(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); reader.read(); reader.read(); reader.update(executionContext); assertEquals(0, executionContext.size()); reader.close(); }
@Test public void testAdvancedDelimited() throws Exception { final List<String> skippedLines = new ArrayList<>(); FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>() .name("fooReader") .resource(getResource("1,2,3\n4,5,$1,2,3$\n@this is a comment\n6,7, 8")) .delimited() .quoteCharacter('$') .names("first", "second", "third") .targetType(Foo.class) .linesToSkip(1) .skippedLinesCallback(skippedLines::add) .addComment("@") .build(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); Foo item = reader.read(); assertEquals(4, item.getFirst()); assertEquals(5, item.getSecond()); assertEquals("1,2,3", item.getThird()); item = reader.read(); assertEquals(6, item.getFirst()); assertEquals(7, item.getSecond()); assertEquals("8", item.getThird()); reader.update(executionContext); assertNull(reader.read()); assertEquals("1,2,3", skippedLines.get(0)); assertEquals(1, skippedLines.size()); assertEquals(1, executionContext.size()); }
@Test public void testConfiguration() throws Exception { Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller(); unmarshaller.setClassesToBeBound(Foo.class); StaxEventItemReader<Foo> reader = new StaxEventItemReaderBuilder<Foo>() .name("fooReader") .resource(getResource(SIMPLE_XML)) .addFragmentRootElements("foo") .currentItemCount(1) .maxItemCount(2) .unmarshaller(unmarshaller) .xmlInputFactory(XMLInputFactory.newInstance()) .build(); reader.afterPropertiesSet(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); Foo item = reader.read(); assertNull(reader.read()); reader.update(executionContext); reader.close(); assertEquals(4, item.getFirst()); assertEquals("five", item.getSecond()); assertEquals("six", item.getThird()); assertEquals(2, executionContext.size()); }
@Test public void testSaveState() throws Exception { Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller(); unmarshaller.setClassesToBeBound(Foo.class); StaxEventItemReader<Foo> reader = new StaxEventItemReaderBuilder<Foo>() .name("fooReader") .resource(getResource(SIMPLE_XML)) .addFragmentRootElements("foo") .unmarshaller(unmarshaller) .saveState(false) .build(); reader.afterPropertiesSet(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); Foo item = reader.read(); item = reader.read(); item = reader.read(); assertNull(reader.read()); reader.update(executionContext); reader.close(); assertEquals(7, item.getFirst()); assertEquals("eight", item.getSecond()); assertEquals("nine", item.getThird()); assertEquals(0, executionContext.size()); }
@Test public void testConfiguration() throws Exception { HibernateCursorItemReader<Foo> reader = new HibernateCursorItemReaderBuilder<Foo>() .name("fooReader") .sessionFactory(this.sessionFactory) .fetchSize(2) .currentItemCount(2) .maxItemCount(4) .queryName("allFoos") .useStatelessSession(true) .build(); reader.afterPropertiesSet(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); Foo item1 = reader.read(); Foo item2 = reader.read(); assertNull(reader.read()); reader.update(executionContext); reader.close(); assertEquals(3, item1.getId()); assertEquals("bar3", item1.getName()); assertEquals(3, item1.getValue()); assertEquals(4, item2.getId()); assertEquals("bar4", item2.getName()); assertEquals(4, item2.getValue()); assertEquals(2, executionContext.size()); }
@Test public void testConfiguration() throws Exception { JpaPagingItemReader<Foo> reader = new JpaPagingItemReaderBuilder<Foo>() .name("fooReader") .entityManagerFactory(this.entityManagerFactory) .currentItemCount(2) .maxItemCount(4) .pageSize(5) .transacted(false) .queryString("select f from Foo f ") .build(); reader.afterPropertiesSet(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); Foo item1 = reader.read(); Foo item2 = reader.read(); assertNull(reader.read()); reader.update(executionContext); reader.close(); assertEquals(3, item1.getId()); assertEquals("bar3", item1.getName()); assertEquals(3, item1.getValue()); assertEquals(4, item2.getId()); assertEquals("bar4", item2.getName()); assertEquals(4, item2.getValue()); assertEquals(2, executionContext.size()); assertEquals(5, ReflectionTestUtils.getField(reader, "pageSize")); assertFalse((Boolean) ReflectionTestUtils.getField(reader, "transacted")); }
@Test public void testAdvancedFixedLength() throws Exception { FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>() .name("fooReader") .resource(getResource("1 2%\n 3\n4 5%\n 6\n@this is a comment\n7 8%\n 9\n")) .fixedLength() .columns(new Range(1, 2), new Range(3, 5), new Range(6)) .names("first", "second", "third") .targetType(Foo.class) .recordSeparatorPolicy(new DefaultRecordSeparatorPolicy("\"", "%")) .maxItemCount(2) .saveState(false) .build(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); Foo item = reader.read(); assertEquals(1, item.getFirst()); assertEquals(2, item.getSecond()); assertEquals("3", item.getThird()); item = reader.read(); assertEquals(4, item.getFirst()); assertEquals(5, item.getSecond()); assertEquals("6", item.getThird()); reader.update(executionContext); assertNull(reader.read()); assertEquals(0, executionContext.size()); }