Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON committed May 30, 2019
1 parent 552d568 commit 2dff623
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 27 deletions.
51 changes: 49 additions & 2 deletions stream/src/test/java/com/annimon/stream/ExceptionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,18 @@ public String apply(Integer value) throws Throwable {
.getOrThrow();
}

@Test(expected = RuntimeException.class)
@Test
public void testIfPresent() {
final Integer[] data = { 0 };
Exceptional
.of(tenSupplier)
.ifPresent(new Consumer<Integer>() {
@Override
public void accept(Integer value) {
throw new RuntimeException();
data[0] = value;
}
});
assertThat(data[0], is(10));
}

@Test
Expand Down Expand Up @@ -325,6 +327,18 @@ public void accept(Throwable value) {
}
}

@Test
public void testIfExceptionOnNormalState() {
Exceptional
.of(tenSupplier)
.ifException(new Consumer<Throwable>() {
@Override
public void accept(Throwable value) {
fail();
}
});
}

@Test
public void testIfExceptionIs() {
final int INTERRUPTED = 0;
Expand Down Expand Up @@ -378,6 +392,20 @@ public Integer apply(Throwable throwable) {
assertEquals(10, value);
}

@Test
public void testRecoverOnNormalState() {
int value = Exceptional
.of(tenSupplier)
.recover(new ThrowableFunction<Throwable, Integer, Throwable>() {
@Override
public Integer apply(Throwable throwable) {
return 20;
}
})
.get();
assertEquals(10, value);
}

@Test(expected = FileNotFoundException.class)
public void testRecoverError() throws Throwable {
Exceptional
Expand Down Expand Up @@ -427,6 +455,25 @@ public Exceptional<Integer> apply(Throwable throwable) {
assertEquals(10, value);
}

@Test
public void testRecoverWithOnNormalState() {
int value = Exceptional
.of(new ThrowableSupplier<Integer, Throwable>() {
@Override
public Integer get() throws Throwable {
return 42;
}
})
.recoverWith(new Function<Throwable, Exceptional<Integer>>() {
@Override
public Exceptional<Integer> apply(Throwable throwable) {
return Exceptional.of(tenSupplier);
}
})
.get();
assertEquals(42, value);
}

@Test(expected = IOException.class)
public void testRecoverWithError() throws Throwable {
Exceptional
Expand Down
3 changes: 2 additions & 1 deletion stream/src/test/java/com/annimon/stream/IntPairTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ public void testEqualsTransitive() {
assertEquals(p1, p3);
}

@SuppressWarnings({"ConstantConditions", "SimplifiableJUnitAssertion"})
@Test
public void testEqualsWithNull() {
final IntPair<String> p = new IntPair<String>(1, "first");
assertNotEquals(null, p);
assertFalse(p.equals(null));
}

@Test
Expand Down
6 changes: 6 additions & 0 deletions stream/src/test/java/com/annimon/stream/ObjectsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public void testHashRepeated() {
assertEquals(initial, Objects.hash(value, "test", 10, true, value, null, 50));
}

@SuppressWarnings("ConfusingArgumentToVarargsMethod")
@Test
public void testHashOnNull() {
assertEquals(0, Objects.hash(null));
}

@Test
public void testToString() {
assertEquals("10", Objects.toString(10, ""));
Expand Down
19 changes: 14 additions & 5 deletions stream/src/test/java/com/annimon/stream/OptionalBooleanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ public void run() {
});
}

@Test(expected = RuntimeException.class)
@Test
public void testIfPresentOrElseWhenValueAbsent() {
final boolean[] data = { false };
OptionalBoolean.empty().ifPresentOrElse(new BooleanConsumer() {
@Override
public void accept(boolean value) {
Expand All @@ -93,9 +94,10 @@ public void accept(boolean value) {
}, new Runnable() {
@Override
public void run() {
throw new RuntimeException();
data[0] = true;
}
});
assertTrue(data[0]);
}

@Test
Expand Down Expand Up @@ -162,15 +164,17 @@ public void accept(boolean value) {
});
}

@Test(expected = RuntimeException.class)
@Test
public void testExecuteIfAbsent() {
final boolean[] data = { false };
OptionalBoolean.empty()
.executeIfAbsent(new Runnable() {
@Override
public void run() {
throw new RuntimeException();
data[0] = true;
}
});
assertTrue(data[0]);
}

@Test
Expand Down Expand Up @@ -339,6 +343,11 @@ public OptionalBoolean get() {

@Test
public void testOrElse() {
assertTrue(OptionalBoolean.of(true).orElse(false));
}

@Test
public void testOrElseOnEmptyOptional() {
assertTrue(OptionalBoolean.empty().orElse(true));
assertFalse(OptionalBoolean.empty().orElse(false));
}
Expand Down Expand Up @@ -395,7 +404,6 @@ public NoSuchElementException get() {
});
}

@SuppressWarnings("EqualsBetweenInconvertibleTypes")
@Test
public void testEquals() {
assertEquals(OptionalBoolean.empty(), OptionalBoolean.empty());
Expand Down Expand Up @@ -429,6 +437,7 @@ public void testHashCode() {
@Test
public void testToString() {
assertEquals(OptionalBoolean.empty().toString(), "OptionalBoolean.empty");
assertEquals(OptionalBoolean.of(false).toString(), "OptionalBoolean[false]");
assertEquals(OptionalBoolean.of(true).toString(), "OptionalBoolean[true]");
}

Expand Down
12 changes: 8 additions & 4 deletions stream/src/test/java/com/annimon/stream/OptionalDoubleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ public void run() {
});
}

@Test(expected = RuntimeException.class)
@Test
public void testIfPresentOrElseWhenValueAbsent() {
final Integer[] data = { 0 };
OptionalDouble.empty().ifPresentOrElse(new DoubleConsumer() {
@Override
public void accept(double value) {
Expand All @@ -96,9 +97,10 @@ public void accept(double value) {
}, new Runnable() {
@Override
public void run() {
throw new RuntimeException();
data[0] = 1;
}
});
assertThat(data[0], is(1));
}

@Test
Expand Down Expand Up @@ -165,15 +167,17 @@ public void accept(double value) {
});
}

@Test(expected = RuntimeException.class)
@Test
public void testExecuteIfAbsent() {
final Integer[] data = { 0 };
OptionalDouble.empty()
.executeIfAbsent(new Runnable() {
@Override
public void run() {
throw new RuntimeException();
data[0] = 1;
}
});
assertThat(data[0], is(1));
}

@Test
Expand Down
12 changes: 8 additions & 4 deletions stream/src/test/java/com/annimon/stream/OptionalIntTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ public void run() {
});
}

@Test(expected = RuntimeException.class)
@Test
public void testIfPresentOrElseWhenValueAbsent() {
final Integer[] data = { 0 };
OptionalInt.empty().ifPresentOrElse(new IntConsumer() {
@Override
public void accept(int value) {
Expand All @@ -98,9 +99,10 @@ public void accept(int value) {
}, new Runnable() {
@Override
public void run() {
throw new RuntimeException();
data[0] = 1;
}
});
assertThat(data[0], is(1));
}

@Test
Expand Down Expand Up @@ -167,15 +169,17 @@ public void accept(int value) {
});
}

@Test(expected = RuntimeException.class)
@Test
public void testExecuteIfAbsent() {
final Integer[] data = { 0 };
OptionalInt.empty()
.executeIfAbsent(new Runnable() {
@Override
public void run() {
throw new RuntimeException();
data[0] = 1;
}
});
assertThat(data[0], is(1));
}

@Test
Expand Down
12 changes: 8 additions & 4 deletions stream/src/test/java/com/annimon/stream/OptionalLongTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ public void run() {
});
}

@Test(expected = RuntimeException.class)
@Test
public void testIfPresentOrElseWhenValueAbsent() {
final Integer[] data = { 0 };
OptionalLong.empty().ifPresentOrElse(new LongConsumer() {
@Override
public void accept(long value) {
Expand All @@ -95,9 +96,10 @@ public void accept(long value) {
}, new Runnable() {
@Override
public void run() {
throw new RuntimeException();
data[0] = 1;
}
});
assertThat(data[0], is(1));
}

@Test
Expand Down Expand Up @@ -164,15 +166,17 @@ public void accept(long value) {
});
}

@Test(expected = RuntimeException.class)
@Test
public void testExecuteIfAbsent() {
final Integer[] data = { 0 };
OptionalLong.empty()
.executeIfAbsent(new Runnable() {
@Override
public void run() {
throw new RuntimeException();
data[0] = 1;
}
});
assertThat(data[0], is(1));
}

@Test
Expand Down
Loading

0 comments on commit 2dff623

Please sign in to comment.