/** * Validate the configuration and build a new {@link JsonItemReader}. * @return a new instance of the {@link JsonItemReader} */ public JsonItemReader<T> build() { Assert.notNull(this.jsonObjectReader, "A json object reader is required."); Assert.notNull(this.resource, "A resource is required."); if (this.saveState) { Assert.state(StringUtils.hasText(this.name), "A name is required when saveState is set to true."); } JsonItemReader<T> reader = new JsonItemReader<>(this.resource, this.jsonObjectReader); reader.setName(this.name); reader.setStrict(this.strict); reader.setSaveState(this.saveState); reader.setMaxItemCount(this.maxItemCount); reader.setCurrentItemCount(this.currentItemCount); return reader; } }
@Override protected ItemReader<Foo> getItemReader() { ByteArrayResource resource = new ByteArrayResource(FOOS.getBytes()); JsonObjectReader<Foo> jsonObjectReader = getJsonObjectReader(); JsonItemReader<Foo> itemReader = new JsonItemReader<>(resource, jsonObjectReader); itemReader.setName("fooJsonItemReader"); return itemReader; }
@Test public void testReadItem() throws Exception { // given Resource resource = new ByteArrayResource("[]".getBytes()); itemReader = new JsonItemReader<>(resource, this.jsonObjectReader); // when itemReader.read(); // then Mockito.verify(this.jsonObjectReader).read(); }
@Test public void testValidation() { try { new JsonItemReader<>(null, this.jsonObjectReader); fail("A resource is required."); } catch (IllegalArgumentException iae) { assertEquals("The resource must not be null.", iae.getMessage()); } try { new JsonItemReader<>(new ByteArrayResource("[{}]".getBytes()), null); fail("A json object reader is required."); } catch (IllegalArgumentException iae) { assertEquals("The json object reader must not be null.", iae.getMessage()); } }
@Test public void testNonExistentResource() { // given this.expectedException.expect(ItemStreamException.class); this.expectedException.expectMessage("Failed to initialize the reader"); this.expectedException.expectCause(Matchers.instanceOf(IllegalStateException.class)); this.itemReader = new JsonItemReader<>(new NonExistentResource(), this.jsonObjectReader); // when this.itemReader.open(new ExecutionContext()); // then // expected exception }
@Test public void testNonReadableResource() { // given this.expectedException.expect(ItemStreamException.class); this.expectedException.expectMessage("Failed to initialize the reader"); this.expectedException.expectCause(Matchers.instanceOf(IllegalStateException.class)); this.itemReader = new JsonItemReader<>(new NonReadableResource(), this.jsonObjectReader); // when this.itemReader.open(new ExecutionContext()); // then // expected exception }
/** * Validate the configuration and build a new {@link JsonItemReader}. * @return a new instance of the {@link JsonItemReader} */ public JsonItemReader<T> build() { Assert.notNull(this.jsonObjectReader, "A json object reader is required."); Assert.notNull(this.resource, "A resource is required."); if (this.saveState) { Assert.state(StringUtils.hasText(this.name), "A name is required when saveState is set to true."); } JsonItemReader<T> reader = new JsonItemReader<>(this.resource, this.jsonObjectReader); reader.setName(this.name); reader.setStrict(this.strict); reader.setSaveState(this.saveState); reader.setMaxItemCount(this.maxItemCount); reader.setCurrentItemCount(this.currentItemCount); return reader; } }