Java

Форк
0
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

15
package com.google.common.collect.testing.testers;
16

17
import static com.google.common.collect.testing.features.CollectionFeature.NON_STANDARD_TOSTRING;
18
import static com.google.common.collect.testing.features.CollectionSize.ONE;
19
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
21
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
22

23
import com.google.common.annotations.GwtCompatible;
24
import com.google.common.collect.testing.AbstractMapTester;
25
import com.google.common.collect.testing.features.CollectionFeature;
26
import com.google.common.collect.testing.features.CollectionSize;
27
import com.google.common.collect.testing.features.MapFeature;
28
import java.util.LinkedHashMap;
29
import java.util.Map;
30
import java.util.Map.Entry;
31
import java.util.Set;
32
import 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")
44
public class MapToStringTester<K, V> extends AbstractMapTester<K, V> {
45
  public void testToString_minimal() {
46
    assertNotNull("toString() should not return null", getMap().toString());
47
  }
48

49
  @CollectionSize.Require(ZERO)
50
  @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
51
  public void testToString_size0() {
52
    assertEquals("emptyMap.toString should return {}", "{}", getMap().toString());
53
  }
54

55
  @CollectionSize.Require(ONE)
56
  @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
57
  public void testToString_size1() {
58
    assertEquals("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)
64
  public void testToStringWithNullKey() {
65
    initMapWithNullKey();
66
    testToString_formatting();
67
  }
68

69
  @CollectionSize.Require(absent = ZERO)
70
  @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
71
  @MapFeature.Require(ALLOWS_NULL_VALUES)
72
  public void testToStringWithNullValue() {
73
    initMapWithNullValue();
74
    testToString_formatting();
75
  }
76

77
  @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING)
78
  public void testToString_formatting() {
79
    assertEquals(
80
        "map.toString() incorrect", expectedToString(getMap().entrySet()), getMap().toString());
81
  }
82

83
  private String expectedToString(Set<Entry<K, V>> entries) {
84
    Map<K, V> reference = new LinkedHashMap<>();
85
    for (Entry<K, V> entry : entries) {
86
      reference.put(entry.getKey(), entry.getValue());
87
    }
88
    return reference.toString();
89
  }
90
}
91

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.