001/* 002 * Copyright (C) 2013 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.collect.testing.testers; 016 017import static com.google.common.collect.testing.features.CollectionFeature.NON_STANDARD_TOSTRING; 018import static com.google.common.collect.testing.features.CollectionSize.ONE; 019import static com.google.common.collect.testing.features.CollectionSize.ZERO; 020import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 022 023import com.google.common.annotations.GwtCompatible; 024import com.google.common.collect.testing.AbstractMapTester; 025import com.google.common.collect.testing.features.CollectionFeature; 026import com.google.common.collect.testing.features.CollectionSize; 027import com.google.common.collect.testing.features.MapFeature; 028import java.util.LinkedHashMap; 029import java.util.Map; 030import java.util.Map.Entry; 031import java.util.Set; 032 033/** 034 * A generic JUnit test which tests {@code toString()} operations on a map. Can't be invoked 035 * directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}. 036 * 037 * @author Kevin Bourrillion 038 * @author Louis Wasserman 039 */ 040@GwtCompatible 041public class MapToStringTester<K, V> extends AbstractMapTester<K, V> { 042 public void testToString_minimal() { 043 assertNotNull("toString() should not return null", getMap().toString()); 044 } 045 046 @CollectionSize.Require(ZERO) 047 @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING) 048 public void testToString_size0() { 049 assertEquals("emptyMap.toString should return {}", "{}", getMap().toString()); 050 } 051 052 @CollectionSize.Require(ONE) 053 @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING) 054 public void testToString_size1() { 055 assertEquals("size1Map.toString should return {entry}", "{" + e0() + "}", getMap().toString()); 056 } 057 058 @CollectionSize.Require(absent = ZERO) 059 @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING) 060 @MapFeature.Require(ALLOWS_NULL_KEYS) 061 public void testToStringWithNullKey() { 062 initMapWithNullKey(); 063 testToString_formatting(); 064 } 065 066 @CollectionSize.Require(absent = ZERO) 067 @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING) 068 @MapFeature.Require(ALLOWS_NULL_VALUES) 069 public void testToStringWithNullValue() { 070 initMapWithNullValue(); 071 testToString_formatting(); 072 } 073 074 @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING) 075 public void testToString_formatting() { 076 assertEquals( 077 "map.toString() incorrect", expectedToString(getMap().entrySet()), getMap().toString()); 078 } 079 080 private String expectedToString(Set<Entry<K, V>> entries) { 081 Map<K, V> reference = new LinkedHashMap<>(); 082 for (Entry<K, V> entry : entries) { 083 reference.put(entry.getKey(), entry.getValue()); 084 } 085 return reference.toString(); 086 } 087}