下面列出了怎么用com.google.common.collect.testing.TestStringMapGenerator的API类实例代码及写法,或者点击链接到github查看源代码。
public Test testsForPatriciaTrie() {
return MapTestSuiteBuilder
.using(new TestStringMapGenerator() {
@Override
protected Map<String, String> create(
final Entry<String, String>[] entries) {
return populate(new PatriciaTrie<String>(), entries);
}
})
.named("PatriciaTrie")
.withFeatures(
MapFeature.GENERAL_PURPOSE,
MapFeature.ALLOWS_NULL_ENTRY_QUERIES,
MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
// Assumes Insertion Order if you don't implement SortedMap
// CollectionFeature.KNOWN_ORDER,
CollectionFeature.SERIALIZABLE,
CollectionSize.ANY)
.suppressing(suppressForPatriciaTrie())
.createTestSuite();
}
public Test testsForPauselessHashMap() {
return MapTestSuiteBuilder
.using(new TestStringMapGenerator() {
@Override
protected Map<String, String> create(
Map.Entry<String, String>[] entries) {
return toHashMap(entries);
}
})
.named("HashMap")
.withFeatures(
MapFeature.GENERAL_PURPOSE,
MapFeature.ALLOWS_NULL_KEYS,
MapFeature.ALLOWS_NULL_VALUES,
MapFeature.ALLOWS_ANY_NULL_QUERIES,
MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
CollectionFeature.SERIALIZABLE,
CollectionSize.ANY)
.suppressing(suppressForHashMap())
.createTestSuite();
}
public static TestSuite suite() {
return MapTestSuiteBuilder
// The create method is called with an array of elements
// that should populate the collection.
.using(new TestStringMapGenerator() {
@Override
protected Map<String, String> create(Entry<String, String>[] source) {
Map<String, String> left = Maps.newHashMap();
Map<String, String> right = Maps.newHashMap();
for(int i = 0; i < source.length; i++) {
Entry<String, String> entry = source[i];
if (right.containsKey(entry.getKey())) {
left.put(entry.getKey(), right.get(entry.getKey()));
right.put(entry.getKey(), entry.getValue());
} else if (i % 2 != 0) {
left.put(entry.getKey(), entry.getValue());
} else {
right.put(entry.getKey(), entry.getValue());
if (i % 4 != 0) {
left.put(entry.getKey(), "will be ignored");
}
}
}
return new UnmodifiableMergingMapView(left, right);
}
}).named("Guava-based UnmodifiableMergingMapView tests")
.withFeatures(
MapFeature.ALLOWS_NULL_KEYS,
MapFeature.ALLOWS_NULL_VALUES,
MapFeature.ALLOWS_ANY_NULL_QUERIES,
MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
CollectionFeature.ALLOWS_NULL_QUERIES,
CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
CollectionSize.ANY)
.createTestSuite();
}
/** Returns a map generator for synchronous values. */
public static TestStringMapGenerator synchronousGenerator(
Supplier<Map<String, String>> supplier) {
return new TestStringMapGenerator() {
@Override protected Map<String, String> create(Map.Entry<String, String>[] entries) {
Map<String, String> map = supplier.get();
for (Map.Entry<String, String> entry : entries) {
map.put(entry.getKey(), entry.getValue());
}
return map;
}
};
}