View Javadoc
1   package com.github.davidmoten.guavamini;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertFalse;
5   import static org.junit.Assert.assertNull;
6   import static org.junit.Assert.assertTrue;
7   
8   import org.junit.Test;
9   
10  import com.github.davidmoten.guavamini.Optional.NotPresentException;
11  
12  public class OptionalTest {
13  
14      @Test
15      public void testOf() {
16          assertEquals(1L, (long) Optional.of(1L).get());
17      }
18  
19      @Test(expected = NotPresentException.class)
20      public void testAbsentGetThrowsException() {
21          Optional.<Long> absent().get();
22      }
23  
24      @Test
25      public void testOfNullIsOk() {
26          assertNull(Optional.of(null).get());
27      }
28  
29      @Test
30      public void testOfIsPresent() {
31          assertTrue(Optional.of(1).isPresent());
32      }
33  
34      @Test
35      public void testAbsentIsNotPresent() {
36          assertFalse(Optional.absent().isPresent());
37      }
38  
39      @Test
40      public void testFromNullableFromNullReturnsAbsent() {
41          assertFalse(Optional.fromNullable(null).isPresent());
42      }
43  
44      @Test
45      public void testFromNullableFromNonNullReturnsPresent() {
46          assertTrue(Optional.fromNullable(1).isPresent());
47      }
48  
49      @Test
50      public void testOrWhenPresent() {
51          assertEquals(1L, (long) Optional.of(1).or(2));
52      }
53  
54      @Test
55      public void testOrWhenNotPresent() {
56          assertEquals(2L, (long) Optional.<Long> absent().or(2L));
57      }
58  
59      @Test
60      public void testToStringWhenAbsent() {
61          assertEquals("Optional.absent", Optional.absent().toString());
62      }
63  
64      @Test
65      public void testToStringWhenPresent() {
66          assertEquals("Optional.of(1)", Optional.of(1).toString());
67      }
68  
69  }