Exemplo n.º 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");
 }
Exemplo n.º 2
0
 @Test
 public void testGet_absent() {
   Optional<String> optional = Optional.absent();
   try {
     optional.get();
     fail();
   } catch (IllegalStateException expected) {
   }
 }
Exemplo n.º 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());
 }
Exemplo n.º 4
0
 @Test
 public void testOf_null() {
   try {
     Optional.of(null);
     fail();
   } catch (NullPointerException expected) {
   }
 }
Exemplo n.º 5
0
 @Test
 public void testAsSet_presentIsImmutable() {
   Set<String> presentAsSet = Optional.of("a").asSet();
   try {
     presentAsSet.add("b");
     fail();
   } catch (UnsupportedOperationException expected) {
   }
 }
Exemplo n.º 6
0
 @Test
 public void testAsSet_absentIsImmutable() {
   Set<Object> absentAsSet = Optional.absent().asSet();
   try {
     absentAsSet.add("foo");
     fail();
   } catch (UnsupportedOperationException expected) {
   }
 }
Exemplo n.º 7
0
 @Test
 public void testOr_T_present() {
   assertEquals("a", Optional.of("a").or("default"));
 }
Exemplo n.º 8
0
 @Test
 public void testGet_present() {
   assertEquals("training", Optional.of("training").get());
 }
Exemplo n.º 9
0
 @Test
 public void testIsPresent_yes() {
   assertTrue(Optional.of("training").isPresent());
 }
Exemplo n.º 10
0
 @Test
 public void testIsPresent_no() {
   assertFalse(Optional.absent().isPresent());
 }
Exemplo n.º 11
0
 @Test
 public void testFromNullable_null() {
   // not promised by spec, but easier to test
   assertSame(Optional.absent(), Optional.fromNullable(null));
 }
Exemplo n.º 12
0
 @Test
 public void testOrNull_present() {
   assertEquals("a", Optional.of("a").orNull());
 }
Exemplo n.º 13
0
 @Test
 public void testAsSet_absent() {
   assertTrue("Returned set should be empty", Optional.absent().asSet().isEmpty());
 }
Exemplo n.º 14
0
 @Test
 public void testPresentInstances_allAbsent() {
   List<Optional<Object>> optionals = ImmutableList.of(Optional.absent(), Optional.absent());
   assertThat(Optional.presentInstances(optionals)).isEmpty();
 }
Exemplo n.º 15
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");
 }
Exemplo n.º 16
0
 @Test
 public void testAbsent() {
   Optional<String> optionalName = Optional.absent();
   assertFalse(optionalName.isPresent());
 }
Exemplo n.º 17
0
 @Test
 public void testToString_present() {
   assertEquals("Optional.of(training)", Optional.of("training").toString());
 }
Exemplo n.º 18
0
 @Test
 public void testToString_absent() {
   assertEquals("Optional.absent()", Optional.absent().toString());
 }
Exemplo n.º 19
0
 @Test
 public void testOr_T_absent() {
   assertEquals("default", Optional.absent().or("default"));
 }
Exemplo n.º 20
0
 @Test
 public void testFromNullable() {
   Optional<String> optionalName = Optional.fromNullable("bob");
   assertEquals("bob", optionalName.get());
 }
Exemplo n.º 21
0
 @Test
 public void testOr_Optional_absent() {
   assertEquals(Optional.of("fallback"), Optional.absent().or(Optional.of("fallback")));
 }
Exemplo n.º 22
0
 // TODO(kevinb): use EqualsTester
 @Test
 public void testEqualsAndHashCode_absent() {
   assertEquals(Optional.<String>absent(), Optional.<Integer>absent());
   assertEquals(Optional.absent().hashCode(), Optional.absent().hashCode());
 }
Exemplo n.º 23
0
 @Test
 public void testOrNull_absent() {
   assertNull(Optional.absent().orNull());
 }
Exemplo n.º 24
0
 @Test
 public void testAsSet_present() {
   Set<String> expected = Collections.singleton("a");
   assertEquals(expected, Optional.of("a").asSet());
 }