001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.testers;
018
019import static com.google.common.collect.testing.features.CollectionSize.ONE;
020import static com.google.common.collect.testing.features.CollectionSize.ZERO;
021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
023import static com.google.common.collect.testing.features.MapFeature.REJECTS_DUPLICATES_AT_CREATION;
024
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.annotations.GwtIncompatible;
027import com.google.common.collect.testing.AbstractMapTester;
028import com.google.common.collect.testing.Helpers;
029import com.google.common.collect.testing.features.CollectionSize;
030import com.google.common.collect.testing.features.MapFeature;
031import java.lang.reflect.Method;
032import java.util.Arrays;
033import java.util.List;
034import java.util.Map.Entry;
035import org.junit.Ignore;
036
037/**
038 * A generic JUnit test which tests creation (typically through a constructor or static factory
039 * method) of a map. Can't be invoked directly; please see {@link
040 * com.google.common.collect.testing.MapTestSuiteBuilder}.
041 *
042 * @author Chris Povirk
043 * @author Kevin Bourrillion
044 */
045@GwtCompatible(emulated = true)
046@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
047public class MapCreationTester<K, V> extends AbstractMapTester<K, V> {
048  @MapFeature.Require(ALLOWS_NULL_KEYS)
049  @CollectionSize.Require(absent = ZERO)
050  public void testCreateWithNullKeySupported() {
051    initMapWithNullKey();
052    expectContents(createArrayWithNullKey());
053  }
054
055  @MapFeature.Require(absent = ALLOWS_NULL_KEYS)
056  @CollectionSize.Require(absent = ZERO)
057  public void testCreateWithNullKeyUnsupported() {
058    try {
059      initMapWithNullKey();
060      fail("Creating a map containing a null key should fail");
061    } catch (NullPointerException expected) {
062    }
063  }
064
065  @MapFeature.Require(ALLOWS_NULL_VALUES)
066  @CollectionSize.Require(absent = ZERO)
067  public void testCreateWithNullValueSupported() {
068    initMapWithNullValue();
069    expectContents(createArrayWithNullValue());
070  }
071
072  @MapFeature.Require(absent = ALLOWS_NULL_VALUES)
073  @CollectionSize.Require(absent = ZERO)
074  public void testCreateWithNullValueUnsupported() {
075    try {
076      initMapWithNullValue();
077      fail("Creating a map containing a null value should fail");
078    } catch (NullPointerException expected) {
079    }
080  }
081
082  @MapFeature.Require({ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES})
083  @CollectionSize.Require(absent = ZERO)
084  public void testCreateWithNullKeyAndValueSupported() {
085    Entry<K, V>[] entries = createSamplesArray();
086    entries[getNullLocation()] = entry(null, null);
087    resetMap(entries);
088    expectContents(entries);
089  }
090
091  @MapFeature.Require(value = ALLOWS_NULL_KEYS, absent = REJECTS_DUPLICATES_AT_CREATION)
092  @CollectionSize.Require(absent = {ZERO, ONE})
093  public void testCreateWithDuplicates_nullDuplicatesNotRejected() {
094    expectFirstRemoved(getEntriesMultipleNullKeys());
095  }
096
097  @MapFeature.Require(absent = REJECTS_DUPLICATES_AT_CREATION)
098  @CollectionSize.Require(absent = {ZERO, ONE})
099  public void testCreateWithDuplicates_nonNullDuplicatesNotRejected() {
100    expectFirstRemoved(getEntriesMultipleNonNullKeys());
101  }
102
103  @MapFeature.Require({ALLOWS_NULL_KEYS, REJECTS_DUPLICATES_AT_CREATION})
104  @CollectionSize.Require(absent = {ZERO, ONE})
105  public void testCreateWithDuplicates_nullDuplicatesRejected() {
106    Entry<K, V>[] entries = getEntriesMultipleNullKeys();
107    try {
108      resetMap(entries);
109      fail("Should reject duplicate null elements at creation");
110    } catch (IllegalArgumentException expected) {
111    }
112  }
113
114  @MapFeature.Require(REJECTS_DUPLICATES_AT_CREATION)
115  @CollectionSize.Require(absent = {ZERO, ONE})
116  public void testCreateWithDuplicates_nonNullDuplicatesRejected() {
117    Entry<K, V>[] entries = getEntriesMultipleNonNullKeys();
118    try {
119      resetMap(entries);
120      fail("Should reject duplicate non-null elements at creation");
121    } catch (IllegalArgumentException expected) {
122    }
123  }
124
125  private Entry<K, V>[] getEntriesMultipleNullKeys() {
126    Entry<K, V>[] entries = createArrayWithNullKey();
127    entries[0] = entry(null, entries[0].getValue());
128    return entries;
129  }
130
131  private Entry<K, V>[] getEntriesMultipleNonNullKeys() {
132    Entry<K, V>[] entries = createSamplesArray();
133    entries[0] = entry(k1(), v0());
134    return entries;
135  }
136
137  private void expectFirstRemoved(Entry<K, V>[] entries) {
138    resetMap(entries);
139
140    List<Entry<K, V>> expectedWithDuplicateRemoved =
141        Arrays.asList(entries).subList(1, getNumElements());
142    expectContents(expectedWithDuplicateRemoved);
143  }
144
145  /**
146   * Returns the {@link Method} instance for {@link #testCreateWithNullKeyUnsupported()} so that
147   * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
148   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
149   */
150  @GwtIncompatible // reflection
151  public static Method getCreateWithNullKeyUnsupportedMethod() {
152    return Helpers.getMethod(MapCreationTester.class, "testCreateWithNullKeyUnsupported");
153  }
154}