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.Iterator;
8 import java.util.NoSuchElementException;
9 import java.util.Objects;
10 import java.util.Optional;
11 import java.util.concurrent.atomic.AtomicInteger;
12 import java.util.function.Function;
13 import java.util.stream.Stream;
14
15 import static java.util.function.Function.identity;
16 import static java.util.stream.Collectors.toList;
17 import static org.hamcrest.Matchers.contains;
18 import static org.hamcrest.Matchers.notNullValue;
19 import static org.hamcrest.core.Is.is;
20 import static org.junit.Assert.assertThat;
21
22 public class TrySuccessTest {
23
24 @Rule public ExpectedException thrown = ExpectedException.none();
25
26 private final Integer STARTING_VALUE = 1;
27 private final Integer ANOTHER_VALUE = 99;
28 private final Try<Integer> t = Checked.now(() -> STARTING_VALUE);
29 private final Function<Integer, String> f = Object::toString;
30 private final Function<String, Integer> g = Integer::valueOf;
31 private final Checked.Function<Integer, String, Exception> fChecked = Object::toString;
32 private final Function<Integer, String> fThrows = x -> {
33 throw new TestException();
34 };
35 private final Function<Integer, Try<String>> fTryThrows = x -> {
36 throw new TestException();
37 };
38
39 @Test public void isFailure() {
40 assertThat(t.isFailure(), is(false));
41 }
42
43 @Test public void isSuccess() {
44 assertThat(t.isSuccess(), is(true));
45 }
46
47 @Test public void map() {
48 assertThat(t.map(f).map(g), is(t));
49 }
50
51 @Test public void mapThrowingFunctionRetunsFailure() {
52
53 Try<Integer> result = t.map(fThrows).map(g);
54
55 assertThat(result.isFailure(), is(true));
56 }
57
58 @Test public void flatMap() {
59 Try<String> t2 = t.flatMap(i -> Checked.now(() -> fChecked.apply(i)));
60
61 assertThat(t2, is(Checked.now(() -> "1")));
62 }
63
64 @Test public void flatMapThrowingFunctionThrows() {
65 thrown.expect(TestException.class);
66
67 t.flatMap(fTryThrows);
68 }
69
70 @Test public void recover() {
71 assertThat(t.recover(e -> 1), is(t));
72 }
73
74 @Test public void recoverExceptionType() {
75 assertThat(t.recover(Exception.class, e -> 1), is(t));
76 }
77
78 @Test public void recoverWith() {
79 assertThat(t.recoverWith(e -> Checked.now(() -> 1)), is(t));
80 }
81
82 @Test public void recoverWithExceptionType() {
83 assertThat(t.recoverWith(Exception.class, e -> Checked.now(() -> 1)), is(t));
84 }
85
86 @Test public void getOrElse() {
87 assertThat(t.getOrElse(() -> 1), is(STARTING_VALUE));
88 }
89
90 @Test public void fold() {
91 Integer i = t.fold(v -> {
92 throw new RuntimeException();
93 }, identity());
94
95 assertThat(i, is(STARTING_VALUE));
96 }
97
98 @Test public void foldPassedThrowingFunctionThrows() {
99 thrown.expect(TestException.class);
100
101 t.fold(x -> "x", fThrows);
102 }
103
104 @Test public void toEither() {
105 assertThat(t.toEither(), is(Either.right(STARTING_VALUE)));
106 }
107
108 @Test public void toOption() {
109 assertThat(t.toOption(), is(Option.some(STARTING_VALUE)));
110 }
111
112 @Test public void liftingFunctionReturnsSuccessIfNoExceptionThrow() {
113 Try<Integer> result = Checked.lift(String::length).apply("test");
114
115 assertThat(result.isSuccess(), is(true));
116
117 final int val = result.fold(f -> {
118 throw new NoSuchElementException();
119 }, identity());
120 assertThat(val, is(4));
121 }
122
123 @Test public void toOptional() {
124 assertThat(t.toOptional(), is(Optional.of(STARTING_VALUE)));
125 }
126
127 @Test public void toStream() {
128 final Stream<Integer> stream = t.toStream();
129 assertThat(stream, notNullValue());
130 assertThat(stream.collect(toList()), contains(STARTING_VALUE));
131 }
132
133 @Test public void forEach() {
134 final AtomicInteger invoked = new AtomicInteger(STARTING_VALUE);
135 t.forEach(v -> invoked.set(ANOTHER_VALUE + v));
136
137 assertThat(invoked.get(), is(STARTING_VALUE + ANOTHER_VALUE));
138 }
139
140 @Test public void orElseSuccessInstance() {
141 final Try<Integer> orElse = t.orElse(Try.successful(ANOTHER_VALUE));
142 assertThat(orElse, is(t));
143 }
144
145 @Test public void orElseSuccessSupplier() {
146 final Try<Integer> orElse = t.orElse(() -> Try.successful(ANOTHER_VALUE));
147 assertThat(orElse, is(t));
148 }
149
150 @Test public void orElseFailureInstance() {
151 final Try<Integer> orElse = t.orElse(Try.failure(new TestException()));
152 assertThat(orElse, is(t));
153 }
154
155 @Test public void orElseFailureSupplier() {
156 final Try<Integer> orElse = t.orElse(() -> Try.failure(new TestException()));
157 assertThat(orElse, is(t));
158 }
159
160 @Test public void filterOrElseTrueSuccessful() {
161 final Try<Integer> filter = t.filterOrElse(value -> Objects.equals(value, STARTING_VALUE), TestException::new);
162 assertThat(filter, is(t));
163 }
164
165 @Test public void filterOrElseFalseFailure() {
166 TestException testException = new TestException();
167 final Try<Integer> filter = t.filterOrElse(value -> !Objects.equals(value, STARTING_VALUE), () -> testException);
168 assertThat(filter, is(Try.failure(testException)));
169 }
170
171 @Test public void iteratorNotEmpty() {
172 Iterator<Integer> iterator = t.iterator();
173 assertThat(iterator.hasNext(), is(true));
174 assertThat(iterator.next(), is(STARTING_VALUE));
175 assertThat(iterator.hasNext(), is(false));
176 }
177
178 private class TestException extends RuntimeException {}
179
180 }