17
17
package com .google .common .base ;
18
18
19
19
import static com .google .common .base .ReflectionFreeAssertThrows .assertThrows ;
20
- import static com .google .common .collect .Lists .newArrayList ;
21
- import static java .util .Collections .unmodifiableList ;
22
20
23
21
import com .google .common .annotations .GwtCompatible ;
24
22
import com .google .common .annotations .GwtIncompatible ;
25
23
import com .google .common .annotations .J2ktIncompatible ;
26
24
import com .google .common .base .Joiner .MapJoiner ;
27
- import com .google .common .collect .ForwardingList ;
28
25
import com .google .common .collect .ImmutableMap ;
29
26
import com .google .common .collect .ImmutableMultimap ;
27
+ import com .google .common .collect .Lists ;
30
28
import com .google .common .collect .Maps ;
31
29
import com .google .common .testing .NullPointerTester ;
32
30
import java .io .IOException ;
33
31
import java .util .Arrays ;
34
- import java .util .List ;
35
32
import java .util .Map ;
36
33
import java .util .Map .Entry ;
37
34
import java .util .Set ;
35
+ import junit .framework .AssertionFailedError ;
38
36
import junit .framework .TestCase ;
39
37
import org .checkerframework .checker .nullness .qual .Nullable ;
40
38
@@ -62,56 +60,12 @@ public class JoinerTest extends TestCase {
62
60
private static final Iterable <@ Nullable Integer > ITERABLE_FOUR_NULLS =
63
61
Arrays .asList ((Integer ) null , null , null , null );
64
62
65
- /*
66
- * Both of these fields *are* immutable/constant. They don't use the type ImmutableList because
67
- * they need to behave slightly differently.
68
- */
69
- @ SuppressWarnings ("ConstantCaseForConstants" )
70
- private static final List <Integer > UNDERREPORTING_SIZE_LIST ;
71
-
72
- @ SuppressWarnings ("ConstantCaseForConstants" )
73
- private static final List <Integer > OVERREPORTING_SIZE_LIST ;
74
-
75
- static {
76
- List <Integer > collection123 = Arrays .asList (1 , 2 , 3 );
77
- UNDERREPORTING_SIZE_LIST = unmodifiableList (new MisleadingSizeList <>(collection123 , -1 ));
78
- OVERREPORTING_SIZE_LIST = unmodifiableList (new MisleadingSizeList <>(collection123 , 1 ));
79
- }
80
-
81
- /*
82
- * c.g.c.collect.testing.Helpers.misleadingSizeList has a broken Iterator, so we can't use it. (I
83
- * mean, ideally we'd fix it....) Also, we specifically need a List so that we trigger the fast
84
- * path in join(Iterable).
85
- */
86
- private static final class MisleadingSizeList <E extends @ Nullable Object >
87
- extends ForwardingList <E > {
88
- final List <E > delegate ;
89
- final int delta ;
90
-
91
- MisleadingSizeList (List <E > delegate , int delta ) {
92
- this .delegate = delegate ;
93
- this .delta = delta ;
94
- }
95
-
96
- @ Override
97
- protected List <E > delegate () {
98
- return delegate ;
99
- }
100
-
101
- @ Override
102
- public int size () {
103
- return delegate .size () + delta ;
104
- }
105
- }
106
-
107
63
@ SuppressWarnings ("JoinIterableIterator" ) // explicitly testing iterator overload, too
108
64
public void testNoSpecialNullBehavior () {
109
65
checkNoOutput (J , ITERABLE_ );
110
66
checkResult (J , ITERABLE_1 , "1" );
111
67
checkResult (J , ITERABLE_12 , "1-2" );
112
68
checkResult (J , ITERABLE_123 , "1-2-3" );
113
- checkResult (J , UNDERREPORTING_SIZE_LIST , "1-2-3" );
114
- checkResult (J , OVERREPORTING_SIZE_LIST , "1-2-3" );
115
69
116
70
assertThrows (NullPointerException .class , () -> J .join (ITERABLE_NULL ));
117
71
assertThrows (NullPointerException .class , () -> J .join (ITERABLE_1_NULL_2 ));
@@ -126,8 +80,6 @@ public void testOnCharOverride() {
126
80
checkResult (onChar , ITERABLE_1 , "1" );
127
81
checkResult (onChar , ITERABLE_12 , "1-2" );
128
82
checkResult (onChar , ITERABLE_123 , "1-2-3" );
129
- checkResult (J , UNDERREPORTING_SIZE_LIST , "1-2-3" );
130
- checkResult (J , OVERREPORTING_SIZE_LIST , "1-2-3" );
131
83
}
132
84
133
85
public void testSkipNulls () {
@@ -139,8 +91,6 @@ public void testSkipNulls() {
139
91
checkResult (skipNulls , ITERABLE_1 , "1" );
140
92
checkResult (skipNulls , ITERABLE_12 , "1-2" );
141
93
checkResult (skipNulls , ITERABLE_123 , "1-2-3" );
142
- checkResult (J , UNDERREPORTING_SIZE_LIST , "1-2-3" );
143
- checkResult (J , OVERREPORTING_SIZE_LIST , "1-2-3" );
144
94
checkResult (skipNulls , ITERABLE_NULL_1 , "1" );
145
95
checkResult (skipNulls , ITERABLE_1_NULL , "1" );
146
96
checkResult (skipNulls , ITERABLE_1_NULL_2 , "1-2" );
@@ -152,8 +102,6 @@ public void testUseForNull() {
152
102
checkResult (zeroForNull , ITERABLE_1 , "1" );
153
103
checkResult (zeroForNull , ITERABLE_12 , "1-2" );
154
104
checkResult (zeroForNull , ITERABLE_123 , "1-2-3" );
155
- checkResult (J , UNDERREPORTING_SIZE_LIST , "1-2-3" );
156
- checkResult (J , OVERREPORTING_SIZE_LIST , "1-2-3" );
157
105
checkResult (zeroForNull , ITERABLE_NULL , "0" );
158
106
checkResult (zeroForNull , ITERABLE_NULL_NULL , "0-0" );
159
107
checkResult (zeroForNull , ITERABLE_NULL_1 , "0-1" );
@@ -166,7 +114,7 @@ private static void checkNoOutput(Joiner joiner, Iterable<Integer> set) {
166
114
assertEquals ("" , joiner .join (set ));
167
115
assertEquals ("" , joiner .join (set .iterator ()));
168
116
169
- Object [] array = newArrayList (set ).toArray (new Integer [0 ]);
117
+ Object [] array = Lists . newArrayList (set ).toArray (new Integer [0 ]);
170
118
assertEquals ("" , joiner .join (array ));
171
119
172
120
StringBuilder sb1FromIterable = new StringBuilder ();
@@ -231,8 +179,7 @@ private static void checkResult(Joiner joiner, Iterable<Integer> parts, String e
231
179
joiner .appendTo (sb1FromIterator , parts .iterator ());
232
180
assertEquals ("x" + expected , sb1FromIterator .toString ());
233
181
234
- // The use of iterator() works around J2KT b/381065164.
235
- Integer [] partsArray = newArrayList (parts .iterator ()).toArray (new Integer [0 ]);
182
+ Integer [] partsArray = Lists .newArrayList (parts ).toArray (new Integer [0 ]);
236
183
assertEquals (expected , joiner .join (partsArray ));
237
184
238
185
StringBuilder sb2 = new StringBuilder ().append ('x' );
@@ -322,6 +269,36 @@ public void test_skipNulls_onMap() {
322
269
assertThrows (UnsupportedOperationException .class , () -> j .withKeyValueSeparator ("/" ));
323
270
}
324
271
272
+ private static class DontStringMeBro implements CharSequence {
273
+ @ Override
274
+ public int length () {
275
+ return 3 ;
276
+ }
277
+
278
+ @ Override
279
+ public char charAt (int index ) {
280
+ return "foo" .charAt (index );
281
+ }
282
+
283
+ @ Override
284
+ public CharSequence subSequence (int start , int end ) {
285
+ return "foo" .subSequence (start , end );
286
+ }
287
+
288
+ @ Override
289
+ public String toString () {
290
+ throw new AssertionFailedError ("shouldn't be invoked" );
291
+ }
292
+ }
293
+
294
+ @ GwtIncompatible // StringBuilder.append in GWT invokes Object.toString(), unlike the JRE version.
295
+ public void testDontConvertCharSequenceToString () {
296
+ assertEquals ("foo,foo" , Joiner .on ("," ).join (new DontStringMeBro (), new DontStringMeBro ()));
297
+ assertEquals (
298
+ "foo,bar,foo" ,
299
+ Joiner .on ("," ).useForNull ("bar" ).join (new DontStringMeBro (), null , new DontStringMeBro ()));
300
+ }
301
+
325
302
@ J2ktIncompatible
326
303
@ GwtIncompatible // NullPointerTester
327
304
public void testNullPointers () {
0 commit comments