Example #1
0
 @Test
 public void testPresentInstances_callingIteratorTwice() {
   List<Optional<String>> optionals =
       ImmutableList.of(Optional.of("a"), Optional.<String>absent(), Optional.of("c"));
   Iterable<String> onlyPresent = Optional.presentInstances(optionals);
   assertThat(onlyPresent).containsAllOf("a", "c");
 }
Example #2
0
 @Test
 public void testOf_null() {
   try {
     Optional.of(null);
     fail();
   } catch (NullPointerException expected) {
   }
 }
Example #3
0
 @Test
 public void testEqualsAndHashCode_present() {
   assertEquals(Optional.of("training"), Optional.of("training"));
   assertFalse(Optional.of("a").equals(Optional.of("b")));
   assertFalse(Optional.of("a").equals(Optional.absent()));
   assertEquals(Optional.of("training").hashCode(), Optional.of("training").hashCode());
 }
Example #4
0
 @Test
 public void testAsSet_presentIsImmutable() {
   Set<String> presentAsSet = Optional.of("a").asSet();
   try {
     presentAsSet.add("b");
     fail();
   } catch (UnsupportedOperationException expected) {
   }
 }
Example #5
0
 @Test
 public void testOrNull_present() {
   assertEquals("a", Optional.of("a").orNull());
 }
Example #6
0
 @Test
 public void testOr_Optional_absent() {
   assertEquals(Optional.of("fallback"), Optional.absent().or(Optional.of("fallback")));
 }
Example #7
0
 @Test
 public void testOr_T_present() {
   assertEquals("a", Optional.of("a").or("default"));
 }
Example #8
0
 @Test
 public void testGet_present() {
   assertEquals("training", Optional.of("training").get());
 }
Example #9
0
 @Test
 public void testIsPresent_yes() {
   assertTrue(Optional.of("training").isPresent());
 }
Example #10
0
 @Test
 public void testPresentInstances_somePresent() {
   List<Optional<String>> optionals =
       ImmutableList.of(Optional.of("a"), Optional.<String>absent(), Optional.of("c"));
   assertThat(Optional.presentInstances(optionals)).containsAllOf("a", "c");
 }
Example #11
0
 @Test
 public void testToString_present() {
   assertEquals("Optional.of(training)", Optional.of("training").toString());
 }
Example #12
0
 @Test
 public void testAsSet_present() {
   Set<String> expected = Collections.singleton("a");
   assertEquals(expected, Optional.of("a").asSet());
 }