Java
90 строк · 3.5 Кб
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.
43@SuppressWarnings("JUnit4ClassUsedInJUnit3")
44public class MapToStringTester<K, V> extends AbstractMapTester<K, V> {
45public void testToString_minimal() {
46assertNotNull("toString() should not return null", getMap().toString());
47}
48
49@CollectionSize.Require(ZERO)
50@CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
51public void testToString_size0() {
52assertEquals("emptyMap.toString should return {}", "{}", getMap().toString());
53}
54
55@CollectionSize.Require(ONE)
56@CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
57public void testToString_size1() {
58assertEquals("size1Map.toString should return {entry}", "{" + e0() + "}", getMap().toString());
59}
60
61@CollectionSize.Require(absent = ZERO)
62@CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
63@MapFeature.Require(ALLOWS_NULL_KEYS)
64public void testToStringWithNullKey() {
65initMapWithNullKey();
66testToString_formatting();
67}
68
69@CollectionSize.Require(absent = ZERO)
70@CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
71@MapFeature.Require(ALLOWS_NULL_VALUES)
72public void testToStringWithNullValue() {
73initMapWithNullValue();
74testToString_formatting();
75}
76
77@CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
78public void testToString_formatting() {
79assertEquals(
80"map.toString() incorrect", expectedToString(getMap().entrySet()), getMap().toString());
81}
82
83private String expectedToString(Set<Entry<K, V>> entries) {
84Map<K, V> reference = new LinkedHashMap<>();
85for (Entry<K, V> entry : entries) {
86reference.put(entry.getKey(), entry.getValue());
87}
88return reference.toString();
89}
90}
91