View Javadoc

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.CoreMatchers.instanceOf;
12  import static org.hamcrest.core.Is.is;
13  import static org.junit.Assert.assertThat;
14  
15  public class TryFailureTest {
16  
17    @Rule public ExpectedException thrown = ExpectedException.none();
18  
19    private class TestException extends RuntimeException {
20      TestException(final String message) {
21        super(message);
22      }
23    }
24  
25    private static final String MESSAGE = "known exception message";
26    private final Try<Integer> t = Checked.of(() -> {
27      throw new TestException(MESSAGE);
28    });
29    private final Function<Exception, String> fThrows = x -> {
30      throw new TestException(MESSAGE);
31    };
32  
33    private final Function<Exception, Try<Integer>> fTryThrows = x -> {
34      throw new RuntimeException();
35    };
36  
37    @Test public void map() throws Exception {
38      assertThat(t.map(x -> true), is(t));
39    }
40  
41    @Test public void isFailure() throws Exception {
42      assertThat(t.isFailure(), is(true));
43    }
44  
45    @Test public void isSuccess() throws Exception {
46      assertThat(t.isSuccess(), is(false));
47    }
48  
49    @Test public void flatMap() throws Exception {
50      assertThat(t.map(x -> true), is(t));
51    }
52  
53    @Test public void recover() throws Exception {
54      assertThat(t.recover(x -> 0), is(Checked.of(() -> 0)));
55    }
56  
57    @Test public void recoverMatchingException() throws Exception {
58      assertThat(t.recover(TestException.class, x -> 0), is(t.recover(x -> 0)));
59    }
60  
61    @Test public void recoverMismatchingException() throws Exception {
62      assertThat(t.recover(IllegalStateException.class, x -> 0), is(t));
63    }
64  
65    @Test public void recoverWith() throws Exception {
66      assertThat(t.recoverWith(x -> Checked.of(() -> 0)), is(Checked.of(() -> 0)));
67    }
68  
69    @Test public void recoverWithMatchingException() throws Exception {
70      assertThat(t.recoverWith(TestException.class, x -> Checked.of(() -> 0)), is(t.recoverWith(x -> Checked.of(() -> 0))));
71    }
72  
73    @Test public void recoverWithMismatchingException() throws Exception {
74      assertThat(t.recoverWith(IllegalStateException.class, x -> Checked.of(() -> 0)), is(t));
75    }
76  
77    @Test public void recoverWithPassedThrowingFunctionThrows() throws Exception {
78      thrown.expect(RuntimeException.class);
79  
80      t.recoverWith(fTryThrows);
81    }
82  
83    @Test public void getOrElse() throws Exception {
84      assertThat(t.getOrElse(() -> 0), is(0));
85    }
86  
87    @Test public void fold() throws Exception {
88      Exception e = t.fold(identity(), v -> {
89        throw new RuntimeException();
90      });
91  
92      assertThat(e, instanceOf(TestException.class));
93      assertThat(e.getMessage(), is(MESSAGE));
94    }
95  
96    @Test public void foldPassedThrowingExceptionsThrows() throws Exception {
97      thrown.expect(TestException.class);
98  
99      String s = t.fold(fThrows, x -> "x");
100   }
101 
102   @Test public void toEither() throws Exception {
103     final Either<Exception, Integer> e = t.toEither();
104 
105     assertThat(e.isLeft(), is(true));
106     assertThat(e.left().get(), instanceOf(TestException.class));
107     assertThat(e.left().get().getMessage(), is(MESSAGE));
108   }
109 
110   @Test public void toOption() throws Exception {
111     assertThat(t.toOption(), is(Option.none()));
112   }
113 
114   @Test public void liftingFunctionThatThrowsReturnsFailure() {
115     Try<Integer> result = Checked.<String, Integer, TestException> lift(x -> {
116       throw new TestException(MESSAGE);
117     }).apply("test");
118 
119     assertThat(result.isFailure(), is(true));
120 
121     final Exception e = result.fold(identity(), x -> {
122       throw new NoSuchElementException();
123     });
124     assertThat(e, instanceOf(TestException.class));
125     assertThat(e.getMessage(), is(MESSAGE));
126   }
127 
128 }