Search in sources :

Example 1 with LongSum

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));
    }
}
Also used : Set(java.util.Set) LocalPlatform(com.oracle.bedrock.runtime.LocalPlatform) Platform(com.oracle.bedrock.runtime.Platform) HashMap(java.util.HashMap) LongSum(com.tangosol.util.aggregator.LongSum) NamedCache(com.tangosol.net.NamedCache) GetLocalMemberId(com.oracle.bedrock.runtime.coherence.callables.GetLocalMemberId) GetClusterSize(com.oracle.bedrock.runtime.coherence.callables.GetClusterSize) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap) AbstractTest(com.oracle.bedrock.testsupport.junit.AbstractTest) Test(org.junit.Test)

Aggregations

LocalPlatform (com.oracle.bedrock.runtime.LocalPlatform)1 Platform (com.oracle.bedrock.runtime.Platform)1 GetClusterSize (com.oracle.bedrock.runtime.coherence.callables.GetClusterSize)1 GetLocalMemberId (com.oracle.bedrock.runtime.coherence.callables.GetLocalMemberId)1 AbstractTest (com.oracle.bedrock.testsupport.junit.AbstractTest)1 NamedCache (com.tangosol.net.NamedCache)1 LongSum (com.tangosol.util.aggregator.LongSum)1 AbstractMap (java.util.AbstractMap)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 Test (org.junit.Test)1