guava
89 строк · 3.4 Кб
1/*
2* Copyright (C) 2013 The Guava Authors
3*
4* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5* in compliance with the License. You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software distributed under the License
10* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11* or implied. See the License for the specific language governing permissions and limitations under
12* the License.
13*/
14
15package com.google.common.collect.testing.testers;16
17import static com.google.common.collect.testing.features.CollectionFeature.NON_STANDARD_TOSTRING;18import static com.google.common.collect.testing.features.CollectionSize.ONE;19import static com.google.common.collect.testing.features.CollectionSize.ZERO;20import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;21import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;22
23import com.google.common.annotations.GwtCompatible;24import com.google.common.collect.testing.AbstractMapTester;25import com.google.common.collect.testing.features.CollectionFeature;26import com.google.common.collect.testing.features.CollectionSize;27import com.google.common.collect.testing.features.MapFeature;28import java.util.LinkedHashMap;29import java.util.Map;30import java.util.Map.Entry;31import java.util.Set;32import org.junit.Ignore;33
34/**
35* A generic JUnit test which tests {@code toString()} operations on a map. Can't be invoked
36* directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
37*
38* @author Kevin Bourrillion
39* @author Louis Wasserman
40*/
41@GwtCompatible
42@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.43public class MapToStringTester<K, V> extends AbstractMapTester<K, V> {44public void testToString_minimal() {45assertNotNull("toString() should not return null", getMap().toString());46}47
48@CollectionSize.Require(ZERO)49@CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)50public void testToString_size0() {51assertEquals("emptyMap.toString should return {}", "{}", getMap().toString());52}53
54@CollectionSize.Require(ONE)55@CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)56public void testToString_size1() {57assertEquals("size1Map.toString should return {entry}", "{" + e0() + "}", getMap().toString());58}59
60@CollectionSize.Require(absent = ZERO)61@CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)62@MapFeature.Require(ALLOWS_NULL_KEYS)63public void testToStringWithNullKey() {64initMapWithNullKey();65testToString_formatting();66}67
68@CollectionSize.Require(absent = ZERO)69@CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)70@MapFeature.Require(ALLOWS_NULL_VALUES)71public void testToStringWithNullValue() {72initMapWithNullValue();73testToString_formatting();74}75
76@CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)77public void testToString_formatting() {78assertEquals(79"map.toString() incorrect", expectedToString(getMap().entrySet()), getMap().toString());80}81
82private String expectedToString(Set<Entry<K, V>> entries) {83Map<K, V> reference = new LinkedHashMap<>();84for (Entry<K, V> entry : entries) {85reference.put(entry.getKey(), entry.getValue());86}87return reference.toString();88}89}
90