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; 032import org.junit.Ignore; 033 034/** 035 * A generic JUnit test which tests {@code toString()} operations on a map. Can't be invoked 036 * directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}. 037 * 038 * @author Kevin Bourrillion 039 * @author Louis Wasserman 040 */ 041@GwtCompatible 042@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 043public class MapToStringTester<K, V> extends AbstractMapTester<K, V> { 044 public void testToString_minimal() { 045 assertNotNull("toString() should not return null", getMap().toString()); 046 } 047 048 @CollectionSize.Require(ZERO) 049 @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING) 050 public void testToString_size0() { 051 assertEquals("emptyMap.toString should return {}", "{}", getMap().toString()); 052 } 053 054 @CollectionSize.Require(ONE) 055 @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING) 056 public void testToString_size1() { 057 assertEquals("size1Map.toString should return {entry}", "{" + e0() + "}", getMap().toString()); 058 } 059 060 @CollectionSize.Require(absent = ZERO) 061 @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING) 062 @MapFeature.Require(ALLOWS_NULL_KEYS) 063 public void testToStringWithNullKey() { 064 initMapWithNullKey(); 065 testToString_formatting(); 066 } 067 068 @CollectionSize.Require(absent = ZERO) 069 @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING) 070 @MapFeature.Require(ALLOWS_NULL_VALUES) 071 public void testToStringWithNullValue() { 072 initMapWithNullValue(); 073 testToString_formatting(); 074 } 075 076 @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING) 077 public void testToString_formatting() { 078 assertEquals( 079 "map.toString() incorrect", expectedToString(getMap().entrySet()), getMap().toString()); 080 } 081 082 private String expectedToString(Set<Entry<K, V>> entries) { 083 Map<K, V> reference = new LinkedHashMap<>(); 084 for (Entry<K, V> entry : entries) { 085 reference.put(entry.getKey(), entry.getValue()); 086 } 087 return reference.toString(); 088 } 089}