use of com.tangosol.util.aggregator.LongSum in project oracle-bedrock by coherence-community.
the class AbstractCoherenceCacheServerTest method shouldAccessNamedCache.
/**
* Ensure that we can create and use a NamedCache via a CoherenceCacheServer.
*/
@Test
public void shouldAccessNamedCache() {
Platform platform = getPlatform();
try (CoherenceCacheServer server = platform.launch(CoherenceCacheServer.class, ClusterPort.automatic(), LocalHost.only(), Console.system())) {
assertThat(server, new GetLocalMemberId(), is(1));
assertThat(server, new GetClusterSize(), is(1));
NamedCache namedCache = server.getCache("dist-example");
// ----- use NamedCache.clear -----
namedCache.clear();
assertThat(namedCache.size(), is(0));
// ----- use NamedCache.put and NamedCache.get -----
namedCache.put("key", "hello");
assertThat(namedCache.size(), is(1));
assertThat((String) namedCache.get("key"), is("hello"));
// ----- use NamedCache.invokeAll -----
Map map = namedCache.invokeAll(PresentFilter.INSTANCE, new GetProcessor());
assertThat(map, notNullValue());
assertThat(map.size(), is(1));
assertThat((String) map.get("key"), is("hello"));
// ----- use NamedCache.keySet -----
Set keySet = namedCache.keySet();
assertThat(keySet, notNullValue());
assertThat(keySet.size(), is(1));
assertThat(keySet.contains("key"), is(true));
// ----- use NamedCache.entrySet -----
Set entrySet = namedCache.entrySet();
assertThat(entrySet, notNullValue());
assertThat(entrySet.size(), is(1));
assertThat(entrySet.contains(new AbstractMap.SimpleEntry("key", "hello")), is(true));
// ----- use NamedCache.values -----
Collection values = namedCache.values();
assertThat(values, notNullValue());
assertThat(values.size(), is(1));
assertThat(values.contains("hello"), is(true));
namedCache.clear();
assertThat(invoking(namedCache).size(), is(0));
namedCache.put("key", "hello");
assertThat(namedCache.size(), is(1));
assertThat((String) namedCache.get("key"), is("hello"));
// ----- use NamedCache.remove -----
namedCache.remove("key");
assertThat(namedCache.size(), is(0));
// ----- use NamedCache.putAll -----
HashMap<String, Integer> putAllMap = new HashMap<>();
long sum = 0;
for (int i = 1; i < 5; i++) {
String key = Integer.toString(i);
putAllMap.put(key, new Integer(i));
sum += i;
}
namedCache.putAll(putAllMap);
assertThat(namedCache.size(), is(putAllMap.size()));
// ----- use NamedCache.getAll -----
Map<String, Integer> getAllResults = namedCache.getAll(putAllMap.keySet());
assertThat(getAllResults, MapMatcher.sameAs(putAllMap));
Map<String, Integer> otherGetAllResults = namedCache.getAll(namedCache.keySet());
assertThat(otherGetAllResults, MapMatcher.sameAs(putAllMap));
// ----- use NamedCache.aggregate -----
long longSum = (Long) namedCache.aggregate(PresentFilter.INSTANCE, new LongSum(IdentityExtractor.INSTANCE));
assertThat(longSum, is(sum));
long anotherLongSum = (Long) namedCache.aggregate(putAllMap.keySet(), new LongSum(IdentityExtractor.INSTANCE));
assertThat(anotherLongSum, is(sum));
}
}
Aggregations