1 package io.atlassian.fugue;
2
3 import org.junit.Rule;
4 import org.junit.Test;
5 import org.junit.rules.ExpectedException;
6
7 import java.util.NoSuchElementException;
8 import java.util.function.Function;
9
10 import static java.util.function.Function.identity;
11 import static org.hamcrest.core.Is.is;
12 import static org.hamcrest.core.Is.isA;
13 import static org.junit.Assert.*;
14
15 public class TrySuccessTest {
16
17 @Rule public ExpectedException thrown = ExpectedException.none();
18
19 private final Integer STARTING_VALUE = 0;
20 private final Try<Integer> t = Checked.of(() -> STARTING_VALUE);
21 private final Function<Integer, String> f = Object::toString;
22 private final Function<String, Integer> g = Integer::valueOf;
23 private final Checked.Function<Integer, String, Exception> fChecked = Object::toString;
24 private final Checked.Function<String, Integer, Exception> gChecked = Integer::valueOf;
25 private final Function<Integer, String> fThrows = x -> {
26 throw new TestException();
27 };
28 private final Function<Integer, Try<String>> fTryThrows = x -> {
29 throw new TestException();
30 };
31
32 @Test public void isFailure() throws Exception {
33 assertThat(t.isFailure(), is(false));
34 }
35
36 @Test public void isSuccess() throws Exception {
37 assertThat(t.isSuccess(), is(true));
38 }
39
40 @Test public void map() throws Exception {
41 assertThat(t.map(f).map(g), is(t));
42 }
43
44 @Test public void mapThrowingFunctionRetunsFailure() throws Exception {
45
46 Try<Integer> result = t.map(fThrows).map(g);
47
48 assertThat(result.isFailure(), is(true));
49 }
50
51 @Test public void flatMap() throws Exception {
52 Try<String> t2 = t.flatMap(i -> Checked.of(() -> fChecked.apply(i)));
53
54 assertThat(t2, is(Checked.of(() -> "0")));
55 }
56
57 @Test public void flatMapThrowingFunctionThrows() {
58 thrown.expect(TestException.class);
59
60 t.flatMap(fTryThrows);
61 }
62
63 @Test public void recover() throws Exception {
64 assertThat(t.recover(e -> 1), is(t));
65 }
66
67 @Test public void recoverExceptionType() throws Exception {
68 assertThat(t.recover(Exception.class, e -> 1), is(t));
69 }
70
71 @Test public void recoverWith() throws Exception {
72 assertThat(t.recoverWith(e -> Checked.of(() -> 1)), is(t));
73 }
74
75 @Test public void recoverWithExceptionType() throws Exception {
76 assertThat(t.recoverWith(Exception.class, e -> Checked.of(() -> 1)), is(t));
77 }
78
79 @Test public void getOrElse() throws Exception {
80 assertThat(t.getOrElse(() -> 1), is(STARTING_VALUE));
81 }
82
83 @Test public void fold() throws Exception {
84 Integer i = t.fold(v -> {
85 throw new RuntimeException();
86 }, identity());
87
88 assertThat(i, is(STARTING_VALUE));
89 }
90
91 @Test public void foldPassedThrowingFunctionThrows() throws Exception {
92 thrown.expect(TestException.class);
93
94 String e = t.fold(x -> "x", fThrows);
95 }
96
97 @Test public void toEither() throws Exception {
98 assertThat(t.toEither(), is(Either.right(STARTING_VALUE)));
99 }
100
101 @Test public void toOption() throws Exception {
102 assertThat(t.toOption(), is(Option.some(STARTING_VALUE)));
103 }
104
105 @Test public void liftingFunctionReturnsSuccessIfNoExceptionThrow() {
106 Try<Integer> result = Checked.lift(String::length).apply("test");
107
108 assertThat(result.isSuccess(), is(true));
109
110 final int val = result.fold(f -> {
111 throw new NoSuchElementException();
112 }, identity());
113 assertThat(val, is(4));
114 }
115
116 private class TestException extends RuntimeException {}
117
118 }