Advertisement
tpeierls

JUnit test for ConcurrentCacheLoader

Mar 10th, 2012
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.32 KB | None | 0 0
  1. package com.hazelcast.util;
  2.  
  3. import com.google.common.base.Function;
  4. import com.google.common.base.Splitter;
  5. import com.google.common.collect.ImmutableList;
  6. import com.google.common.util.concurrent.MoreExecutors;
  7.  
  8. import com.hazelcast.core.Hazelcast;
  9.  
  10. import java.util.*;
  11.  
  12. import org.junit.*;
  13. import static org.junit.Assert.assertEquals;
  14.  
  15.  
  16. public class ConcurrentCacheLoaderTest {
  17.  
  18.     @AfterClass public static void shutdown() {
  19.         Hazelcast.shutdownAll();
  20.     }
  21.  
  22.     final Iterable<String> inputs = ImmutableList.of(
  23.         "abc def",
  24.         "ghij klm n op",
  25.         "qr st uv",
  26.         "wxyz"
  27.     );
  28.  
  29.     @Test public void testConcurrentCacheLoader() throws InterruptedException {
  30.         ConcurrentCacheLoader<String, List<String>> ccl =
  31.             ConcurrentCacheLoader.<String, List<String>>newBuilder()
  32.                 .build(new NotSerializableSplitFunction());
  33.         commonAssertions(ccl.loadAll(inputs));
  34.     }
  35.  
  36.     @Test public void testDistributedCacheLoader() throws InterruptedException {
  37.         ConcurrentCacheLoader<String, List<String>> ccl =
  38.             ConcurrentCacheLoader.<String, List<String>>newBuilder()
  39.                 .usingDefaultDistributedService()
  40.                 .build(new SerializableSplitFunction());
  41.         commonAssertions(ccl.loadAll(inputs));
  42.     }
  43.  
  44.     @Test(expected=IllegalArgumentException.class)
  45.     public void testKeyFunctionWithStandardExec() throws InterruptedException {
  46.         ConcurrentCacheLoader<String, List<String>> ccl =
  47.             ConcurrentCacheLoader.<String, List<String>>newBuilder()
  48.                 .using(MoreExecutors.sameThreadExecutor())
  49.                 .byKey()
  50.                 .build(new SerializableSplitFunction());
  51.     }
  52.  
  53.     @Test(expected=IllegalArgumentException.class)
  54.     public void testFunctionNotSerializableSplitFunction() throws InterruptedException {
  55.         ConcurrentCacheLoader<String, List<String>> ccl =
  56.             ConcurrentCacheLoader.<String, List<String>>newBuilder()
  57.                 .usingDefaultDistributedService()
  58.                 .byKey()
  59.                 .build(new NotSerializableSplitFunction());
  60.     }
  61.  
  62.     @Test(expected=IllegalArgumentException.class)
  63.     public void testNonHazelcastExecutorWithKeyFunction() throws InterruptedException {
  64.         ConcurrentCacheLoader<String, List<String>> ccl =
  65.             ConcurrentCacheLoader.<String, List<String>>newBuilder()
  66.                 .usingDefaultDistributedService()
  67.                 .byKey()
  68.                 .using(MoreExecutors.sameThreadExecutor())
  69.                 .build(new NotSerializableSplitFunction());
  70.     }
  71.  
  72.     private void commonAssertions(Map<String, List<String>> result) {
  73.         assertEquals(4, result.size());
  74.         assertEquals(ImmutableList.of("abc", "def"), result.get("abc def"));
  75.         assertEquals(ImmutableList.of("wxyz"), result.get("wxyz"));
  76.     }
  77.  
  78.  
  79.     static class NotSerializableSplitFunction implements Function<String, List<String>> {
  80.         public List<String> apply(String text) {
  81.             return ImmutableList.copyOf(SPLITTER.split(text));
  82.         }
  83.     }
  84.  
  85.     static class SerializableSplitFunction extends NotSerializableSplitFunction
  86.                                            implements java.io.Serializable {
  87.     }
  88.  
  89.     private static final Splitter SPLITTER = Splitter.on(" ").trimResults();
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement