11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2009 The Guava Authors 31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); 51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License. 61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of the License at 71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0 91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software 111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS, 121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and 141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License. 151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.base; 181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtCompatible; 201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtIncompatible; 211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.collect.ImmutableMap; 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport junit.framework.TestCase; 241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Arrays; 261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Map; 271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/** 291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Tests for {@link Objects#toStringHelper(Object)}. 301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Jason Lee 321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible 341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic class ToStringHelperTest extends TestCase { 351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Class names are obfuscated in GWT") 371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testConstructor_instance() { 381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(this).toString(); 391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals("ToStringHelperTest{}", toTest); 401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testConstructorLenient_instance() { 431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(this).toString(); 441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(toTest, toTest.matches(".*\\{\\}")); 451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Class names are obfuscated in GWT") 481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testConstructor_innerClass() { 491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new TestClass()).toString(); 501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals("TestClass{}", toTest); 511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testConstructorLenient_innerClass() { 541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new TestClass()).toString(); 551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(toTest, toTest.matches(".*\\{\\}")); 561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Class names are obfuscated in GWT") 591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testConstructor_anonymousClass() { 601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new Object() {}).toString(); 611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals("{}", toTest); 621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testConstructorLenient_anonymousClass() { 651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new Object() {}).toString(); 661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(toTest, toTest.matches(".*\\{\\}")); 671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Class names are obfuscated in GWT") 701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testConstructor_classObject() { 711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(TestClass.class).toString(); 721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals("TestClass{}", toTest); 731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testConstructorLenient_classObject() { 761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(TestClass.class).toString(); 771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(toTest, toTest.matches(".*\\{\\}")); 781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testConstructor_stringObject() { 811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper("FooBar").toString(); 821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals("FooBar{}", toTest); 831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Class names are obfuscated in GWT") 861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToStringHelper_localInnerClass() { 871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Local inner classes have names ending like "Outer.$1Inner" 881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert class LocalInnerClass {} 891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new LocalInnerClass()).toString(); 901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals("LocalInnerClass{}", toTest); 911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToStringHelperLenient_localInnerClass() { 941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert class LocalInnerClass {} 951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new LocalInnerClass()).toString(); 961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(toTest, toTest.matches(".*\\{\\}")); 971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Class names are obfuscated in GWT") 1001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToStringHelper_localInnerNestedClass() { 1011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert class LocalInnerClass { 1021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert class LocalInnerNestedClass {} 1031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new LocalInnerClass().new LocalInnerNestedClass()) 1051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 1061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals("LocalInnerNestedClass{}", toTest); 1071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToStringHelperLenient_localInnerNestedClass() { 1101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert class LocalInnerClass { 1111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert class LocalInnerNestedClass {} 1121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new LocalInnerClass().new LocalInnerNestedClass()) 1141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 1151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(toTest, toTest.matches(".*\\{\\}")); 1161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Class names are obfuscated in GWT") 1191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToStringHelper_moreThanNineAnonymousClasses() { 1201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // The nth anonymous class has a name ending like "Outer.$n" 1211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o1 = new Object() {}; 1221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o2 = new Object() {}; 1231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o3 = new Object() {}; 1241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o4 = new Object() {}; 1251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o5 = new Object() {}; 1261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o6 = new Object() {}; 1271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o7 = new Object() {}; 1281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o8 = new Object() {}; 1291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o9 = new Object() {}; 1301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o10 = new Object() {}; 1311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(o10).toString(); 1321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals("{}", toTest); 1331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToStringHelperLenient_moreThanNineAnonymousClasses() { 1361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // The nth anonymous class has a name ending like "Outer.$n" 1371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o1 = new Object() {}; 1381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o2 = new Object() {}; 1391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o3 = new Object() {}; 1401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o4 = new Object() {}; 1411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o5 = new Object() {}; 1421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o6 = new Object() {}; 1431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o7 = new Object() {}; 1441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o8 = new Object() {}; 1451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o9 = new Object() {}; 1461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Object o10 = new Object() {}; 1471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(o10).toString(); 1481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(toTest, toTest.matches(".*\\{\\}")); 1491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // all remaining test are on an inner class with various fields 1521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Class names are obfuscated in GWT") 1531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToString_oneField() { 1541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new TestClass()) 1551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field1", "Hello") 1561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 1571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals("TestClass{field1=Hello}", toTest); 1581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Class names are obfuscated in GWT") 1611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToString_oneIntegerField() { 1621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new TestClass()) 1631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field1", new Integer(42)) 1641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 1651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals("TestClass{field1=42}", toTest); 1661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Class names are obfuscated in GWT") 1691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToString_nullInteger() { 1701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new TestClass()) 1711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field1", (Integer) null) 1721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 1731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals("TestClass{field1=null}", toTest); 1741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToStringLenient_oneField() { 1771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new TestClass()) 1781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field1", "Hello") 1791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 1801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(toTest, toTest.matches(".*\\{field1\\=Hello\\}")); 1811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToStringLenient_oneIntegerField() { 1841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new TestClass()) 1851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field1", new Integer(42)) 1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 1871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(toTest, toTest.matches(".*\\{field1\\=42\\}")); 1881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToStringLenient_nullInteger() { 1911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new TestClass()) 1921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field1", (Integer) null) 1931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 1941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(toTest, toTest.matches(".*\\{field1\\=null\\}")); 1951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Class names are obfuscated in GWT") 1981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToString_complexFields() { 1991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Map<String, Integer> map = ImmutableMap.<String, Integer>builder() 2011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .put("abc", 1) 2021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .put("def", 2) 2031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .put("ghi", 3) 2041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .build(); 2051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new TestClass()) 2061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field1", "This is string.") 2071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field2", Arrays.asList("abc", "def", "ghi")) 2081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field3", map) 2091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 2101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final String expected = "TestClass{" 2111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert + "field1=This is string., field2=[abc, def, ghi], field3={abc=1, def=2, ghi=3}}"; 2121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals(expected, toTest); 2141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToStringLenient_complexFields() { 2171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Map<String, Integer> map = ImmutableMap.<String, Integer>builder() 2191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .put("abc", 1) 2201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .put("def", 2) 2211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .put("ghi", 3) 2221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .build(); 2231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new TestClass()) 2241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field1", "This is string.") 2251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field2", Arrays.asList("abc", "def", "ghi")) 2261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field3", map) 2271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 2281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final String expectedRegex = ".*\\{" 2291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert + "field1\\=This is string\\., " 2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert + "field2\\=\\[abc, def, ghi\\], " 2311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert + "field3=\\{abc\\=1, def\\=2, ghi\\=3\\}\\}"; 2321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(toTest, toTest.matches(expectedRegex)); 2341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToString_addWithNullName() { 2371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Objects.ToStringHelper helper = Objects.toStringHelper(new TestClass()); 2381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 2391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert helper.add(null, "Hello"); 2401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert fail("No exception was thrown."); 2411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (NullPointerException expected) { 2421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Class names are obfuscated in GWT") 2461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToString_addWithNullValue() { 2471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final String result = Objects.toStringHelper(new TestClass()) 2481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("Hello", null) 2491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 2501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals("TestClass{Hello=null}", result); 2521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToStringLenient_addWithNullValue() { 2551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final String result = Objects.toStringHelper(new TestClass()) 2561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("Hello", null) 2571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 2581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(result, result.matches(".*\\{Hello\\=null\\}")); 2591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Class names are obfuscated in GWT") 2621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToString_ToStringTwice() { 2631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Objects.ToStringHelper helper = Objects.toStringHelper(new TestClass()) 2641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field1", 1) 2651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addValue("value1") 2661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field2", "value2"); 2671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final String expected = "TestClass{field1=1, value1, field2=value2}"; 2681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals(expected, helper.toString()); 2701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Call toString again 2711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals(expected, helper.toString()); 2721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Make sure the cached value is reset when we modify the helper at all 2741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final String expected2 = "TestClass{field1=1, value1, field2=value2, 2}"; 2751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert helper.addValue(2); 2761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals(expected2, helper.toString()); 2771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Class names are obfuscated in GWT") 2801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToString_addValue() { 2811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new TestClass()) 2821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field1", 1) 2831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addValue("value1") 2841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field2", "value2") 2851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addValue(2) 2861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 2871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final String expected = "TestClass{field1=1, value1, field2=value2, 2}"; 2881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals(expected, toTest); 2901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToStringLenient_addValue() { 2931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String toTest = Objects.toStringHelper(new TestClass()) 2941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field1", 1) 2951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addValue("value1") 2961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .add("field2", "value2") 2971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addValue(2) 2981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 2991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final String expected = ".*\\{field1\\=1, value1, field2\\=value2, 2\\}"; 3001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(toTest, toTest.matches(expected)); 3021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Class names are obfuscated in GWT") 3051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToString_addValueWithNullValue() { 3061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final String result = Objects.toStringHelper(new TestClass()) 3071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addValue(null) 3081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addValue("Hello") 3091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addValue(null) 3101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 3111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final String expected = "TestClass{null, Hello, null}"; 3121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals(expected, result); 3141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testToStringLenient_addValueWithNullValue() { 3171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final String result = Objects.toStringHelper(new TestClass()) 3181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addValue(null) 3191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addValue("Hello") 3201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .addValue(null) 3211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .toString(); 3221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final String expected = ".*\\{null, Hello, null\\}"; 3231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(result, result.matches(expected)); 3251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 3281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Test class for testing formatting of inner classes. 3291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 3301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class TestClass {} 3311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert} 333