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