1 /* 2 * Copyright (C) 2009 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 com.google.common.annotations.GwtCompatible; 20 import com.google.common.annotations.GwtIncompatible; 21 import com.google.common.annotations.J2ktIncompatible; 22 import com.google.common.collect.ImmutableMap; 23 import java.util.Arrays; 24 import java.util.Map; 25 import junit.framework.TestCase; 26 27 /** 28 * Tests for {@link MoreObjects#toStringHelper(Object)}. 29 * 30 * @author Jason Lee 31 */ 32 @GwtCompatible 33 public class ToStringHelperTest extends TestCase { 34 35 @J2ktIncompatible 36 @GwtIncompatible // Class names are obfuscated in GWT testConstructor_instance()37 public void testConstructor_instance() { 38 String toTest = MoreObjects.toStringHelper(this).toString(); 39 assertEquals("ToStringHelperTest{}", toTest); 40 } 41 testConstructorLenient_instance()42 public void testConstructorLenient_instance() { 43 String toTest = MoreObjects.toStringHelper(this).toString(); 44 assertTrue(toTest, toTest.matches(".*\\{\\}")); 45 } 46 47 @J2ktIncompatible 48 @GwtIncompatible // Class names are obfuscated in GWT testConstructor_innerClass()49 public void testConstructor_innerClass() { 50 String toTest = MoreObjects.toStringHelper(new TestClass()).toString(); 51 assertEquals("TestClass{}", toTest); 52 } 53 testConstructorLenient_innerClass()54 public void testConstructorLenient_innerClass() { 55 String toTest = MoreObjects.toStringHelper(new TestClass()).toString(); 56 assertTrue(toTest, toTest.matches(".*\\{\\}")); 57 } 58 59 @J2ktIncompatible 60 @GwtIncompatible // Class names are obfuscated in GWT testConstructor_anonymousClass()61 public void testConstructor_anonymousClass() { 62 String toTest = MoreObjects.toStringHelper(new Object() {}).toString(); 63 assertEquals("{}", toTest); 64 } 65 testConstructorLenient_anonymousClass()66 public void testConstructorLenient_anonymousClass() { 67 String toTest = MoreObjects.toStringHelper(new Object() {}).toString(); 68 assertTrue(toTest, toTest.matches(".*\\{\\}")); 69 } 70 71 @J2ktIncompatible 72 @GwtIncompatible // Class names are obfuscated in GWT testConstructor_classObject()73 public void testConstructor_classObject() { 74 String toTest = MoreObjects.toStringHelper(TestClass.class).toString(); 75 assertEquals("TestClass{}", toTest); 76 } 77 testConstructorLenient_classObject()78 public void testConstructorLenient_classObject() { 79 String toTest = MoreObjects.toStringHelper(TestClass.class).toString(); 80 assertTrue(toTest, toTest.matches(".*\\{\\}")); 81 } 82 testConstructor_stringObject()83 public void testConstructor_stringObject() { 84 String toTest = MoreObjects.toStringHelper("FooBar").toString(); 85 assertEquals("FooBar{}", toTest); 86 } 87 88 @J2ktIncompatible 89 @GwtIncompatible // Class names are obfuscated in GWT testToStringHelper_localInnerClass()90 public void testToStringHelper_localInnerClass() { 91 // Local inner classes have names ending like "Outer.$1Inner" 92 class LocalInnerClass {} 93 String toTest = MoreObjects.toStringHelper(new LocalInnerClass()).toString(); 94 assertEquals("LocalInnerClass{}", toTest); 95 } 96 testToStringHelperLenient_localInnerClass()97 public void testToStringHelperLenient_localInnerClass() { 98 class LocalInnerClass {} 99 String toTest = MoreObjects.toStringHelper(new LocalInnerClass()).toString(); 100 assertTrue(toTest, toTest.matches(".*\\{\\}")); 101 } 102 103 @J2ktIncompatible 104 @GwtIncompatible // Class names are obfuscated in GWT testToStringHelper_localInnerNestedClass()105 public void testToStringHelper_localInnerNestedClass() { 106 class LocalInnerClass { 107 class LocalInnerNestedClass {} 108 } 109 String toTest = 110 MoreObjects.toStringHelper(new LocalInnerClass().new LocalInnerNestedClass()).toString(); 111 assertEquals("LocalInnerNestedClass{}", toTest); 112 } 113 testToStringHelperLenient_localInnerNestedClass()114 public void testToStringHelperLenient_localInnerNestedClass() { 115 class LocalInnerClass { 116 class LocalInnerNestedClass {} 117 } 118 String toTest = 119 MoreObjects.toStringHelper(new LocalInnerClass().new LocalInnerNestedClass()).toString(); 120 assertTrue(toTest, toTest.matches(".*\\{\\}")); 121 } 122 123 @J2ktIncompatible 124 @GwtIncompatible // Class names are obfuscated in GWT testToStringHelper_moreThanNineAnonymousClasses()125 public void testToStringHelper_moreThanNineAnonymousClasses() { 126 // The nth anonymous class has a name ending like "Outer.$n" 127 Object unused1 = new Object() {}; 128 Object unused2 = new Object() {}; 129 Object unused3 = new Object() {}; 130 Object unused4 = new Object() {}; 131 Object unused5 = new Object() {}; 132 Object unused6 = new Object() {}; 133 Object unused7 = new Object() {}; 134 Object unused8 = new Object() {}; 135 Object unused9 = new Object() {}; 136 Object o10 = new Object() {}; 137 String toTest = MoreObjects.toStringHelper(o10).toString(); 138 assertEquals("{}", toTest); 139 } 140 testToStringHelperLenient_moreThanNineAnonymousClasses()141 public void testToStringHelperLenient_moreThanNineAnonymousClasses() { 142 // The nth anonymous class has a name ending like "Outer.$n" 143 Object unused1 = new Object() {}; 144 Object unused2 = new Object() {}; 145 Object unused3 = new Object() {}; 146 Object unused4 = new Object() {}; 147 Object unused5 = new Object() {}; 148 Object unused6 = new Object() {}; 149 Object unused7 = new Object() {}; 150 Object unused8 = new Object() {}; 151 Object unused9 = new Object() {}; 152 Object o10 = new Object() {}; 153 String toTest = MoreObjects.toStringHelper(o10).toString(); 154 assertTrue(toTest, toTest.matches(".*\\{\\}")); 155 } 156 157 // all remaining test are on an inner class with various fields 158 @J2ktIncompatible 159 @GwtIncompatible // Class names are obfuscated in GWT testToString_oneField()160 public void testToString_oneField() { 161 String toTest = MoreObjects.toStringHelper(new TestClass()).add("field1", "Hello").toString(); 162 assertEquals("TestClass{field1=Hello}", toTest); 163 } 164 165 @J2ktIncompatible 166 @GwtIncompatible // Class names are obfuscated in GWT testToString_oneIntegerField()167 public void testToString_oneIntegerField() { 168 String toTest = 169 MoreObjects.toStringHelper(new TestClass()).add("field1", new Integer(42)).toString(); 170 assertEquals("TestClass{field1=42}", toTest); 171 } 172 173 @J2ktIncompatible 174 @GwtIncompatible // Class names are obfuscated in GWT testToString_nullInteger()175 public void testToString_nullInteger() { 176 String toTest = 177 MoreObjects.toStringHelper(new TestClass()).add("field1", (Integer) null).toString(); 178 assertEquals("TestClass{field1=null}", toTest); 179 } 180 testToStringLenient_oneField()181 public void testToStringLenient_oneField() { 182 String toTest = MoreObjects.toStringHelper(new TestClass()).add("field1", "Hello").toString(); 183 assertTrue(toTest, toTest.matches(".*\\{field1\\=Hello\\}")); 184 } 185 testToStringLenient_oneIntegerField()186 public void testToStringLenient_oneIntegerField() { 187 String toTest = 188 MoreObjects.toStringHelper(new TestClass()).add("field1", new Integer(42)).toString(); 189 assertTrue(toTest, toTest.matches(".*\\{field1\\=42\\}")); 190 } 191 testToStringLenient_nullInteger()192 public void testToStringLenient_nullInteger() { 193 String toTest = 194 MoreObjects.toStringHelper(new TestClass()).add("field1", (Integer) null).toString(); 195 assertTrue(toTest, toTest.matches(".*\\{field1\\=null\\}")); 196 } 197 198 @J2ktIncompatible 199 @GwtIncompatible // Class names are obfuscated in GWT testToString_complexFields()200 public void testToString_complexFields() { 201 202 Map<String, Integer> map = 203 ImmutableMap.<String, Integer>builder().put("abc", 1).put("def", 2).put("ghi", 3).build(); 204 String toTest = 205 MoreObjects.toStringHelper(new TestClass()) 206 .add("field1", "This is string.") 207 .add("field2", Arrays.asList("abc", "def", "ghi")) 208 .add("field3", map) 209 .toString(); 210 final String expected = 211 "TestClass{" 212 + "field1=This is string., field2=[abc, def, ghi], field3={abc=1, def=2, ghi=3}}"; 213 214 assertEquals(expected, toTest); 215 } 216 testToStringLenient_complexFields()217 public void testToStringLenient_complexFields() { 218 219 Map<String, Integer> map = 220 ImmutableMap.<String, Integer>builder().put("abc", 1).put("def", 2).put("ghi", 3).build(); 221 String toTest = 222 MoreObjects.toStringHelper(new TestClass()) 223 .add("field1", "This is string.") 224 .add("field2", Arrays.asList("abc", "def", "ghi")) 225 .add("field3", map) 226 .toString(); 227 final String expectedRegex = 228 ".*\\{" 229 + "field1\\=This is string\\., " 230 + "field2\\=\\[abc, def, ghi\\], " 231 + "field3=\\{abc\\=1, def\\=2, ghi\\=3\\}\\}"; 232 233 assertTrue(toTest, toTest.matches(expectedRegex)); 234 } 235 testToString_addWithNullName()236 public void testToString_addWithNullName() { 237 MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(new TestClass()); 238 try { 239 helper.add(null, "Hello"); 240 fail("No exception was thrown."); 241 } catch (NullPointerException expected) { 242 } 243 } 244 245 @J2ktIncompatible 246 @GwtIncompatible // Class names are obfuscated in GWT testToString_addWithNullValue()247 public void testToString_addWithNullValue() { 248 final String result = MoreObjects.toStringHelper(new TestClass()).add("Hello", null).toString(); 249 250 assertEquals("TestClass{Hello=null}", result); 251 } 252 testToStringLenient_addWithNullValue()253 public void testToStringLenient_addWithNullValue() { 254 final String result = MoreObjects.toStringHelper(new TestClass()).add("Hello", null).toString(); 255 assertTrue(result, result.matches(".*\\{Hello\\=null\\}")); 256 } 257 258 @J2ktIncompatible 259 @GwtIncompatible // Class names are obfuscated in GWT testToString_ToStringTwice()260 public void testToString_ToStringTwice() { 261 MoreObjects.ToStringHelper helper = 262 MoreObjects.toStringHelper(new TestClass()) 263 .add("field1", 1) 264 .addValue("value1") 265 .add("field2", "value2"); 266 final String expected = "TestClass{field1=1, value1, field2=value2}"; 267 268 assertEquals(expected, helper.toString()); 269 // Call toString again 270 assertEquals(expected, helper.toString()); 271 272 // Make sure the cached value is reset when we modify the helper at all 273 final String expected2 = "TestClass{field1=1, value1, field2=value2, 2}"; 274 helper.addValue(2); 275 assertEquals(expected2, helper.toString()); 276 } 277 278 @J2ktIncompatible 279 @GwtIncompatible // Class names are obfuscated in GWT testToString_addValue()280 public void testToString_addValue() { 281 String toTest = 282 MoreObjects.toStringHelper(new TestClass()) 283 .add("field1", 1) 284 .addValue("value1") 285 .add("field2", "value2") 286 .addValue(2) 287 .toString(); 288 final String expected = "TestClass{field1=1, value1, field2=value2, 2}"; 289 290 assertEquals(expected, toTest); 291 } 292 testToStringLenient_addValue()293 public void testToStringLenient_addValue() { 294 String toTest = 295 MoreObjects.toStringHelper(new TestClass()) 296 .add("field1", 1) 297 .addValue("value1") 298 .add("field2", "value2") 299 .addValue(2) 300 .toString(); 301 final String expected = ".*\\{field1\\=1, value1, field2\\=value2, 2\\}"; 302 303 assertTrue(toTest, toTest.matches(expected)); 304 } 305 306 @J2ktIncompatible 307 @GwtIncompatible // Class names are obfuscated in GWT testToString_addValueWithNullValue()308 public void testToString_addValueWithNullValue() { 309 final String result = 310 MoreObjects.toStringHelper(new TestClass()) 311 .addValue(null) 312 .addValue("Hello") 313 .addValue(null) 314 .toString(); 315 final String expected = "TestClass{null, Hello, null}"; 316 317 assertEquals(expected, result); 318 } 319 testToStringLenient_addValueWithNullValue()320 public void testToStringLenient_addValueWithNullValue() { 321 final String result = 322 MoreObjects.toStringHelper(new TestClass()) 323 .addValue(null) 324 .addValue("Hello") 325 .addValue(null) 326 .toString(); 327 final String expected = ".*\\{null, Hello, null\\}"; 328 329 assertTrue(result, result.matches(expected)); 330 } 331 332 @J2ktIncompatible 333 @GwtIncompatible // Class names are obfuscated in GWT testToStringOmitNullValues_oneField()334 public void testToStringOmitNullValues_oneField() { 335 String toTest = 336 MoreObjects.toStringHelper(new TestClass()).omitNullValues().add("field1", null).toString(); 337 assertEquals("TestClass{}", toTest); 338 } 339 340 @J2ktIncompatible 341 @GwtIncompatible // Class names are obfuscated in GWT testToStringOmitNullValues_manyFieldsFirstNull()342 public void testToStringOmitNullValues_manyFieldsFirstNull() { 343 String toTest = 344 MoreObjects.toStringHelper(new TestClass()) 345 .omitNullValues() 346 .add("field1", null) 347 .add("field2", "Googley") 348 .add("field3", "World") 349 .toString(); 350 assertEquals("TestClass{field2=Googley, field3=World}", toTest); 351 } 352 353 @J2ktIncompatible 354 @GwtIncompatible // Class names are obfuscated in GWT testToStringOmitNullValues_manyFieldsOmitAfterNull()355 public void testToStringOmitNullValues_manyFieldsOmitAfterNull() { 356 String toTest = 357 MoreObjects.toStringHelper(new TestClass()) 358 .add("field1", null) 359 .add("field2", "Googley") 360 .add("field3", "World") 361 .omitNullValues() 362 .toString(); 363 assertEquals("TestClass{field2=Googley, field3=World}", toTest); 364 } 365 366 @J2ktIncompatible 367 @GwtIncompatible // Class names are obfuscated in GWT testToStringOmitNullValues_manyFieldsLastNull()368 public void testToStringOmitNullValues_manyFieldsLastNull() { 369 String toTest = 370 MoreObjects.toStringHelper(new TestClass()) 371 .omitNullValues() 372 .add("field1", "Hello") 373 .add("field2", "Googley") 374 .add("field3", null) 375 .toString(); 376 assertEquals("TestClass{field1=Hello, field2=Googley}", toTest); 377 } 378 379 @J2ktIncompatible 380 @GwtIncompatible // Class names are obfuscated in GWT testToStringOmitEmptyValues_oneValue()381 public void testToStringOmitEmptyValues_oneValue() { 382 String toTest = 383 MoreObjects.toStringHelper(new TestClass()).omitNullValues().addValue(null).toString(); 384 assertEquals("TestClass{}", toTest); 385 } 386 387 @J2ktIncompatible 388 @GwtIncompatible // Class names are obfuscated in GWT testToStringOmitNullValues_manyValuesFirstNull()389 public void testToStringOmitNullValues_manyValuesFirstNull() { 390 String toTest = 391 MoreObjects.toStringHelper(new TestClass()) 392 .omitNullValues() 393 .addValue(null) 394 .addValue("Googley") 395 .addValue("World") 396 .toString(); 397 assertEquals("TestClass{Googley, World}", toTest); 398 } 399 400 @J2ktIncompatible 401 @GwtIncompatible // Class names are obfuscated in GWT testToStringOmitNullValues_manyValuesLastNull()402 public void testToStringOmitNullValues_manyValuesLastNull() { 403 String toTest = 404 MoreObjects.toStringHelper(new TestClass()) 405 .omitNullValues() 406 .addValue("Hello") 407 .addValue("Googley") 408 .addValue(null) 409 .toString(); 410 assertEquals("TestClass{Hello, Googley}", toTest); 411 } 412 413 @J2ktIncompatible 414 @GwtIncompatible // Class names are obfuscated in GWT testToStringOmitNullValues_differentOrder()415 public void testToStringOmitNullValues_differentOrder() { 416 String expected = "TestClass{field1=Hello, field2=Googley, field3=World}"; 417 String toTest1 = 418 MoreObjects.toStringHelper(new TestClass()) 419 .omitNullValues() 420 .add("field1", "Hello") 421 .add("field2", "Googley") 422 .add("field3", "World") 423 .toString(); 424 String toTest2 = 425 MoreObjects.toStringHelper(new TestClass()) 426 .add("field1", "Hello") 427 .add("field2", "Googley") 428 .omitNullValues() 429 .add("field3", "World") 430 .toString(); 431 assertEquals(expected, toTest1); 432 assertEquals(expected, toTest2); 433 } 434 435 @J2ktIncompatible 436 @GwtIncompatible // Class names are obfuscated in GWT testToStringOmitNullValues_canBeCalledManyTimes()437 public void testToStringOmitNullValues_canBeCalledManyTimes() { 438 String toTest = 439 MoreObjects.toStringHelper(new TestClass()) 440 .omitNullValues() 441 .omitNullValues() 442 .add("field1", "Hello") 443 .omitNullValues() 444 .add("field2", "Googley") 445 .omitNullValues() 446 .add("field3", "World") 447 .toString(); 448 assertEquals("TestClass{field1=Hello, field2=Googley, field3=World}", toTest); 449 } 450 451 /** Test class for testing formatting of inner classes. */ 452 private static class TestClass {} 453 } 454