Skip to content

Commit

Permalink
Resolved a bunch of ide-warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Nov 22, 2023
1 parent 2c18476 commit 91c4978
Showing 1 changed file with 51 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,34 @@
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

@SuppressWarnings("DataFlowIssue")
@Slf4j
public class JsonArrayIteratorTest {

@Test
public void test() throws IOException {

//Jackson2Mapper.getInstance().writeValue(System.out, new Change("bla", false));
JsonArrayIterator<Change> it = JsonArrayIterator.<Change>builder().inputStream(getClass().getResourceAsStream("/changes.json")).valueClass(Change.class).objectMapper(Jackson2Mapper.getInstance()).build();
assertThat(it.next().getMid()).isEqualTo("POMS_NCRV_1138990"); // 1
assertThat(it.getCount()).isEqualTo(1);
assertThat(it.getSize()).hasValueSatisfying(size -> assertThat(size).isEqualTo(14));
for (int i = 0; i < 9; i++) {
assertThat(it.hasNext()).isTrue();

Change change = it.next(); // 10
Optional<Long> size = it.getSize();
if (size.isPresent()) {
log.info(it.getCount() + "/" + size.get() + " :" + change);
}
if (!change.isDeleted()) {
assertThat(change.getMedia()).isNotNull();
try (JsonArrayIterator<Change> it = JsonArrayIterator.<Change>builder().inputStream(getClass().getResourceAsStream("/changes.json")).valueClass(Change.class).objectMapper(Jackson2Mapper.getInstance()).build()) {
assertThat(it.next().getMid()).isEqualTo("POMS_NCRV_1138990"); // 1
assertThat(it.getCount()).isEqualTo(1);
assertThat(it.getSize()).hasValueSatisfying(size -> assertThat(size).isEqualTo(14));
for (int i = 0; i < 9; i++) {
assertThat(it.hasNext()).isTrue();

Change change = it.next(); // 10
Optional<Long> size = it.getSize();
if (size.isPresent()) {
log.info(it.getCount() + "/" + size.get() + " :" + change);
}
if (!change.isDeleted()) {
assertThat(change.getMedia()).isNotNull();
}
}
assertThat(it.hasNext()).isTrue(); // 11
assertThat(it.next().getMid()).isEqualTo("POMS_VPRO_1139788");
assertThat(it.hasNext()).isFalse();
}
assertThat(it.hasNext()).isTrue(); // 11
assertThat(it.next().getMid()).isEqualTo("POMS_VPRO_1139788");
assertThat(it.hasNext()).isFalse();
}

@SuppressWarnings("ConstantConditions")
Expand Down Expand Up @@ -119,54 +121,61 @@ public void callback() throws IOException {

@Test
public void write() throws IOException, JSONException {
JsonArrayIterator<Change> it = JsonArrayIterator
try (JsonArrayIterator<Change> it = JsonArrayIterator
.<Change>builder()
.inputStream(getClass().getResourceAsStream("/changes.json"))
.valueClass(Change.class)
.objectMapper(Jackson2Mapper.getInstance())
.skipNulls(false)
.build();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream()) {

it.write(out, (c) -> {
log.info("{}", c);
});
String expected = "{'array': " + IOUtils.resourceToString("/array_from_changes.json", StandardCharsets.UTF_8) + "}";
JSONAssert.assertEquals(expected, out.toString(), JSONCompareMode.STRICT);
it.write(out, (c) -> {
log.info("{}", c);
});
String expected = "{'array': " + IOUtils.resourceToString("/array_from_changes.json", StandardCharsets.UTF_8) + "}";
JSONAssert.assertEquals(expected, out.toString(), JSONCompareMode.STRICT);
}
}

@Test
public void writeArray() throws IOException, JSONException {
JsonArrayIterator<Change> it = JsonArrayIterator
try (JsonArrayIterator<Change> it = JsonArrayIterator
.<Change>builder()
.inputStream(getClass().getResourceAsStream("/changes.json"))
.valueClass(Change.class)
.objectMapper(Jackson2Mapper.getInstance())
.logger(log)
.skipNulls(false)
.build();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream()) {

it.writeArray(out, (c) -> {
log.info("{}", c);
});
String expected = IOUtils.resourceToString("/array_from_changes.json", StandardCharsets.UTF_8);
JSONAssert.assertEquals(expected, out.toString(), JSONCompareMode.STRICT);
it.writeArray(out, (c) -> {
log.info("{}", c);
});
String expected = IOUtils.resourceToString("/array_from_changes.json", StandardCharsets.UTF_8);
JSONAssert.assertEquals(expected, out.toString(), JSONCompareMode.STRICT);
}
}

@Test
public void illegalConstruction() {
assertThatThrownBy(() -> {
JsonArrayIterator.builder().build();
try (JsonArrayIterator<Object> js = JsonArrayIterator.builder().build()) {
log.info("{}", js);
}
}).isInstanceOf(IllegalArgumentException.class);

assertThatThrownBy(() -> {
JsonArrayIterator
try (JsonArrayIterator<Change> js = JsonArrayIterator
.<Change>builder()
.inputStream(requireNonNull(getClass().getResourceAsStream("/changes.json")))
.valueClass(Change.class)
.valueCreator((jp, on) -> null)
.build();
.build()) {
log.info("{}", js);

}
}).isInstanceOf(IllegalArgumentException.class);
}

Expand All @@ -175,7 +184,7 @@ public void illegalConstruction() {
public void interrupt() throws IOException {
byte[] bytes = "[{},{},{},{},{},{}]".getBytes(StandardCharsets.UTF_8);
final String[] callback = new String[1];
JsonArrayIterator<Simple> i = JsonArrayIterator.<Simple>builder()
try (JsonArrayIterator<Simple> i = JsonArrayIterator.<Simple>builder()
.inputStream(new InputStream() {
int i = 0;
@Override
Expand All @@ -194,13 +203,13 @@ public int read() throws IOException {
callback[0] = "called";
})
.valueClass(Simple.class)
.build();
log.info("{}", i.next());
log.info("{}", i.next());
assertThatThrownBy(() -> {
i.next();
}).isInstanceOf(RuntimeException.class);
assertThat(callback[0]).isEqualTo("called");
.build()) {
log.info("{}", i.next());
log.info("{}", i.next());
assertThatThrownBy(i::next)
.isInstanceOf(RuntimeException.class);
assertThat(callback[0]).isEqualTo("called");
}
}

@XmlAccessorType(XmlAccessType.FIELD)
Expand Down

0 comments on commit 91c4978

Please sign in to comment.