1 /* 2 * Copyright (C) 2016 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.collect; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.annotations.GwtCompatible; 22 import java.util.NoSuchElementException; 23 import java.util.stream.Stream; 24 import junit.framework.TestCase; 25 import org.checkerframework.checker.nullness.qual.Nullable; 26 27 /** 28 * Tests for {@code MoreCollectors}. 29 * 30 * @author Louis Wasserman 31 */ 32 @GwtCompatible 33 @ElementTypesAreNonnullByDefault 34 public class MoreCollectorsTest extends TestCase { testToOptionalEmpty()35 public void testToOptionalEmpty() { 36 assertThat(Stream.empty().collect(MoreCollectors.toOptional())).isEmpty(); 37 } 38 testToOptionalSingleton()39 public void testToOptionalSingleton() { 40 assertThat(Stream.of(1).collect(MoreCollectors.toOptional())).hasValue(1); 41 } 42 testToOptionalNull()43 public void testToOptionalNull() { 44 Stream<@Nullable Object> stream = Stream.of((Object) null); 45 try { 46 stream.collect(MoreCollectors.toOptional()); 47 fail("Expected NullPointerException"); 48 } catch (NullPointerException expected) { 49 } 50 } 51 testToOptionalMultiple()52 public void testToOptionalMultiple() { 53 try { 54 Stream.of(1, 2).collect(MoreCollectors.toOptional()); 55 fail("Expected IllegalArgumentException"); 56 } catch (IllegalArgumentException expected) { 57 assertThat(expected.getMessage()).contains("1, 2"); 58 } 59 } 60 testToOptionalMultipleWithNull()61 public void testToOptionalMultipleWithNull() { 62 try { 63 Stream.of(1, null).collect(MoreCollectors.toOptional()); 64 fail("Expected NullPointerException"); 65 } catch (NullPointerException expected) { 66 } 67 } 68 testToOptionalMany()69 public void testToOptionalMany() { 70 try { 71 Stream.of(1, 2, 3, 4, 5, 6).collect(MoreCollectors.toOptional()); 72 fail("Expected IllegalArgumentException"); 73 } catch (IllegalArgumentException expected) { 74 assertThat(expected.getMessage()).contains("1, 2, 3, 4, 5, ..."); 75 } 76 } 77 testOnlyElement()78 public void testOnlyElement() { 79 try { 80 Stream.empty().collect(MoreCollectors.onlyElement()); 81 fail("Expected NoSuchElementException"); 82 } catch (NoSuchElementException expected) { 83 } 84 } 85 testOnlyElementSingleton()86 public void testOnlyElementSingleton() { 87 assertThat(Stream.of(1).collect(MoreCollectors.onlyElement())).isEqualTo(1); 88 } 89 testOnlyElementNull()90 public void testOnlyElementNull() { 91 assertThat(Stream.<@Nullable Object>of((Object) null).collect(MoreCollectors.onlyElement())) 92 .isNull(); 93 } 94 testOnlyElementMultiple()95 public void testOnlyElementMultiple() { 96 try { 97 Stream.of(1, 2).collect(MoreCollectors.onlyElement()); 98 fail("Expected IllegalArgumentException"); 99 } catch (IllegalArgumentException expected) { 100 assertThat(expected.getMessage()).contains("1, 2"); 101 } 102 } 103 testOnlyElementMany()104 public void testOnlyElementMany() { 105 try { 106 Stream.of(1, 2, 3, 4, 5, 6).collect(MoreCollectors.onlyElement()); 107 fail("Expected IllegalArgumentException"); 108 } catch (IllegalArgumentException expected) { 109 assertThat(expected.getMessage()).contains("1, 2, 3, 4, 5, ..."); 110 } 111 } 112 } 113