Java example source code file (MutableTypeToInstanceMapTest.java)
This example Java source code file (MutableTypeToInstanceMapTest.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
The MutableTypeToInstanceMapTest.java Java example source code
/*
* Copyright (C) 2012 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.reflect;
import static com.google.common.collect.Maps.immutableEntry;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.testing.MapTestSuiteBuilder;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.MapFeature;
import com.google.common.reflect.ImmutableTypeToInstanceMapTest.TestTypeToInstanceMapGenerator;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.util.Map;
import java.util.Map.Entry;
/**
* Unit test of {@link MutableTypeToInstanceMap}.
*
* @author Ben Yu
*/
public class MutableTypeToInstanceMapTest extends TestCase {
@AndroidIncompatible // problem with suite builders on Android
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(MutableTypeToInstanceMapTest.class);
suite.addTest(MapTestSuiteBuilder
.using(new TestTypeToInstanceMapGenerator() {
// Other tests will verify what real, warning-free usage looks like
// but here we have to do some serious fudging
@Override
@SuppressWarnings("unchecked")
public Map<TypeToken, Object> create(Object... elements) {
MutableTypeToInstanceMap<Object> map
= new MutableTypeToInstanceMap<Object>();
for (Object object : elements) {
Entry<TypeToken, Object> entry = (Entry) object;
map.putInstance(entry.getKey(), entry.getValue());
}
return (Map) map;
}
})
.named("MutableTypeToInstanceMap")
.withFeatures(
MapFeature.SUPPORTS_REMOVE,
MapFeature.RESTRICTS_KEYS,
MapFeature.ALLOWS_NULL_VALUES,
CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
CollectionSize.ANY,
MapFeature.ALLOWS_ANY_NULL_QUERIES)
.createTestSuite());
return suite;
}
private TypeToInstanceMap<Object> map;
@Override protected void setUp() throws Exception {
map = new MutableTypeToInstanceMap<Object>();
}
public void testPutThrows() {
try {
map.put(TypeToken.of(Integer.class), new Integer(5));
fail();
} catch (UnsupportedOperationException expected) {}
}
public void testPutAllThrows() {
try {
map.putAll(ImmutableMap.of(TypeToken.of(Integer.class), new Integer(5)));
fail();
} catch (UnsupportedOperationException expected) {}
}
public void testEntrySetMutationThrows() {
map.putInstance(String.class, "test");
assertEquals(TypeToken.of(String.class), map.entrySet().iterator().next().getKey());
assertEquals("test", map.entrySet().iterator().next().getValue());
try {
map.entrySet().iterator().next().setValue(1);
fail();
} catch (UnsupportedOperationException expected) {}
}
public void testEntrySetToArrayMutationThrows() {
map.putInstance(String.class, "test");
@SuppressWarnings("unchecked") // Should get a CCE later if cast is wrong
Entry<Object, Object> entry = (Entry
Other Java examples (source code examples)
Here is a short list of links related to this Java MutableTypeToInstanceMapTest.java source code file: