1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.base; 18 19 import static org.junit.contrib.truth.Truth.ASSERT; 20 21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.annotations.GwtIncompatible; 23 import com.google.common.collect.ImmutableList; 24 import com.google.common.testing.NullPointerTester; 25 import com.google.common.testing.SerializableTester; 26 27 import junit.framework.TestCase; 28 29 import java.util.Collections; 30 import java.util.List; 31 import java.util.Set; 32 33 /** 34 * Unit test for {@link Optional}. 35 * 36 * @author Kurt Alfred Kluever 37 */ 38 @GwtCompatible(emulated = true) 39 public final class OptionalTest extends TestCase { 40 public void testAbsent() { 41 Optional<String> optionalName = Optional.absent(); 42 assertFalse(optionalName.isPresent()); 43 } 44 45 public void testOf() { 46 assertEquals("training", Optional.of("training").get()); 47 } 48 49 public void testOf_null() { 50 try { 51 Optional.of(null); 52 fail(); 53 } catch (NullPointerException expected) { 54 } 55 } 56 57 public void testFromNullable() { 58 Optional<String> optionalName = Optional.fromNullable("bob"); 59 assertEquals("bob", optionalName.get()); 60 } 61 62 public void testFromNullable_null() { 63 // not promised by spec, but easier to test 64 assertSame(Optional.absent(), Optional.fromNullable(null)); 65 } 66 67 public void testIsPresent_no() { 68 assertFalse(Optional.absent().isPresent()); 69 } 70 71 public void testIsPresent_yes() { 72 assertTrue(Optional.of("training").isPresent()); 73 } 74 75 public void testGet_absent() { 76 Optional<String> optional = Optional.absent(); 77 try { 78 optional.get(); 79 fail(); 80 } catch (IllegalStateException expected) { 81 } 82 } 83 84 public void testGet_present() { 85 assertEquals("training", Optional.of("training").get()); 86 } 87 88 public void testOr_T_present() { 89 assertEquals("a", Optional.of("a").or("default")); 90 } 91 92 public void testOr_T_absent() { 93 assertEquals("default", Optional.absent().or("default")); 94 } 95 96 public void testOr_Supplier_present() { 97 assertEquals("a", Optional.of("a").or(Suppliers.ofInstance("fallback"))); 98 } 99 100 public void testOr_Supplier_absent() { 101 assertEquals("fallback", Optional.absent().or(Suppliers.ofInstance("fallback"))); 102 } 103 104 public void testOr_NullSupplier_absent() { 105 Supplier<Object> nullSupplier = Suppliers.ofInstance(null); 106 Optional<Object> absentOptional = Optional.absent(); 107 try { 108 absentOptional.or(nullSupplier); 109 fail(); 110 } catch (NullPointerException expected) { 111 } 112 } 113 114 public void testOr_Optional_present() { 115 assertEquals(Optional.of("a"), Optional.of("a").or(Optional.of("fallback"))); 116 } 117 118 public void testOr_Optional_absent() { 119 assertEquals(Optional.of("fallback"), Optional.absent().or(Optional.of("fallback"))); 120 } 121 122 public void testOrNull_present() { 123 assertEquals("a", Optional.of("a").orNull()); 124 } 125 126 public void testOrNull_absent() { 127 assertNull(Optional.absent().orNull()); 128 } 129 130 public void testAsSet_present() { 131 Set<String> expected = Collections.singleton("a"); 132 assertEquals(expected, Optional.of("a").asSet()); 133 } 134 135 public void testAsSet_absent() { 136 assertTrue("Returned set should be empty", Optional.absent().asSet().isEmpty()); 137 } 138 139 public void testAsSet_presentIsImmutable() { 140 Set<String> presentAsSet = Optional.of("a").asSet(); 141 try { 142 presentAsSet.add("b"); 143 fail(); 144 } catch (UnsupportedOperationException expected) { 145 } 146 } 147 148 public void testAsSet_absentIsImmutable() { 149 Set<Object> absentAsSet = Optional.absent().asSet(); 150 try { 151 absentAsSet.add("foo"); 152 fail(); 153 } catch (UnsupportedOperationException expected) { 154 } 155 } 156 157 // TODO(kevinb): use EqualsTester 158 159 public void testEqualsAndHashCode_absent() { 160 assertEquals(Optional.<String>absent(), Optional.<Integer>absent()); 161 assertEquals(Optional.absent().hashCode(), Optional.absent().hashCode()); 162 } 163 164 public void testEqualsAndHashCode_present() { 165 assertEquals(Optional.of("training"), Optional.of("training")); 166 assertFalse(Optional.of("a").equals(Optional.of("b"))); 167 assertFalse(Optional.of("a").equals(Optional.absent())); 168 assertEquals(Optional.of("training").hashCode(), Optional.of("training").hashCode()); 169 } 170 171 public void testToString_absent() { 172 assertEquals("Optional.absent()", Optional.absent().toString()); 173 } 174 175 public void testToString_present() { 176 assertEquals("Optional.of(training)", Optional.of("training").toString()); 177 } 178 179 public void testPresentInstances_allPresent() { 180 List<Optional<String>> optionals = 181 ImmutableList.of(Optional.of("a"), Optional.of("b"), Optional.of("c")); 182 ASSERT.that(Optional.presentInstances(optionals)).hasContentsInOrder("a", "b", "c"); 183 } 184 185 public void testPresentInstances_allAbsent() { 186 List<Optional<Object>> optionals = 187 ImmutableList.of(Optional.absent(), Optional.absent()); 188 ASSERT.that(Optional.presentInstances(optionals)).isEmpty(); 189 } 190 191 public void testPresentInstances_somePresent() { 192 List<Optional<String>> optionals = 193 ImmutableList.of(Optional.of("a"), Optional.<String>absent(), Optional.of("c")); 194 ASSERT.that(Optional.presentInstances(optionals)).hasContentsInOrder("a", "c"); 195 } 196 197 public void testPresentInstances_callingIteratorTwice() { 198 List<Optional<String>> optionals = 199 ImmutableList.of(Optional.of("a"), Optional.<String>absent(), Optional.of("c")); 200 Iterable<String> onlyPresent = Optional.presentInstances(optionals); 201 ASSERT.that(onlyPresent).hasContentsInOrder("a", "c"); 202 ASSERT.that(onlyPresent).hasContentsInOrder("a", "c"); 203 } 204 205 @GwtIncompatible("SerializableTester") 206 public void testSerialization() { 207 SerializableTester.reserializeAndAssert(Optional.absent()); 208 SerializableTester.reserializeAndAssert(Optional.of("foo")); 209 } 210 211 @GwtIncompatible("NullPointerTester") 212 public void testNullPointers() throws Exception { 213 NullPointerTester npTester = new NullPointerTester(); 214 npTester.testAllPublicConstructors(Optional.class); 215 npTester.testAllPublicStaticMethods(Optional.class); 216 npTester.testAllPublicInstanceMethods(Optional.absent()); 217 npTester.testAllPublicInstanceMethods(Optional.of("training")); 218 } 219 } 220