@Before
  public void init() {
    CacheConfiguration<Long, String> cacheConfiguration =
        CacheConfigurationBuilder.newCacheConfigurationBuilder()
            .withResourcePools(
                ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES).build())
            .buildConfig(Long.class, String.class);

    service = new DefaultSharedManagementService();

    cacheManager1 =
        CacheManagerBuilder.newCacheManagerBuilder()
            .withCache("aCache1", cacheConfiguration)
            .using(service)
            .using(new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCM1"))
            .build(true);

    cacheManager2 =
        CacheManagerBuilder.newCacheManagerBuilder()
            .withCache("aCache2", cacheConfiguration)
            .withCache("aCache3", cacheConfiguration)
            .using(service)
            .using(new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCM2"))
            .build(true);

    // this serie of calls make sure the registry still works after a full init / close / init loop
    cacheManager1.close();
    cacheManager1.init();
    cacheManager2.close();
    cacheManager2.init();
  }
Exemplo n.º 2
0
  @Test
  public void testStoreByValue() {
    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(false);
    cacheManager.init();

    DefaultCopierConfiguration<String> copierConfiguration =
        new DefaultCopierConfiguration(SerializingCopier.class, CopierConfiguration.Type.VALUE);
    final Cache<Long, String> cache1 =
        cacheManager.createCache(
            "cache1",
            CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class)
                .withResourcePools(
                    ResourcePoolsBuilder.newResourcePoolsBuilder().heap(1, EntryUnit.ENTRIES))
                .build());
    performAssertions(cache1, true);

    final Cache<Long, String> cache2 =
        cacheManager.createCache(
            "cache2",
            CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class)
                .add(copierConfiguration)
                .build());
    performAssertions(cache2, false);

    final Cache<Long, String> cache3 =
        cacheManager.createCache(
            "cache3",
            CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class)
                .build());
    performAssertions(cache3, true);

    cacheManager.close();
  }
 @Test
 public void testAddingCacheEventListenerConfigurationAtCacheLevel() {
   CacheManagerBuilder<CacheManager> cacheManagerBuilder =
       CacheManagerBuilder.newCacheManagerBuilder();
   CacheEventListenerConfiguration cacheEventListenerConfiguration =
       CacheEventListenerConfigurationBuilder.newEventListenerConfiguration(
               ListenerObject.class, EventType.CREATED)
           .unordered()
           .asynchronous()
           .build();
   CacheManager cacheManager = cacheManagerBuilder.build(true);
   final Cache<Long, String> cache =
       cacheManager.createCache(
           "cache",
           CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class)
               .add(cacheEventListenerConfiguration)
               .withResourcePools(
                   ResourcePoolsBuilder.newResourcePoolsBuilder()
                       .heap(100, EntryUnit.ENTRIES)
                       .build())
               .build());
   Collection<ServiceConfiguration<?>> serviceConfiguration =
       cache.getRuntimeConfiguration().getServiceConfigurations();
   assertThat(
       serviceConfiguration,
       IsCollectionContaining.<ServiceConfiguration<?>>hasItem(
           instanceOf(DefaultCacheEventListenerConfiguration.class)));
   cacheManager.close();
 }
  @Test
  public void testCanGetContext() {
    CacheConfiguration<Long, String> cacheConfiguration =
        CacheConfigurationBuilder.newCacheConfigurationBuilder()
            .withResourcePools(
                ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES).build())
            .buildConfig(Long.class, String.class);

    ManagementRegistry managementRegistry =
        new DefaultManagementRegistry(
            new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCM"));

    CacheManager cacheManager1 =
        CacheManagerBuilder.newCacheManagerBuilder()
            .withCache("aCache", cacheConfiguration)
            .using(managementRegistry)
            .build(true);

    assertThat(managementRegistry.getContext().getName(), equalTo("cacheManagerName"));
    assertThat(managementRegistry.getContext().getValue(), equalTo("myCM"));
    assertThat(managementRegistry.getContext().getSubContexts(), hasSize(1));
    assertThat(
        managementRegistry.getContext().getSubContexts().iterator().next().getName(),
        equalTo("cacheName"));
    assertThat(
        managementRegistry.getContext().getSubContexts().iterator().next().getValue(),
        equalTo("aCache"));

    cacheManager1.close();
  }
  @Test
  public void testCopiers() throws Exception {
    Configuration configuration =
        new XmlConfiguration(this.getClass().getResource("/configs/cache-copiers.xml"));
    final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
    cacheManager.init();

    Cache<Description, Person> bar = cacheManager.getCache("bar", Description.class, Person.class);
    Description desc = new Description(1234, "foo");
    Person person = new Person("Bar", 24);
    bar.put(desc, person);
    assertEquals(person, bar.get(desc));
    assertNotSame(person, bar.get(desc));

    Cache<Long, Person> baz = cacheManager.getCache("baz", Long.class, Person.class);
    baz.put(1L, person);
    assertEquals(person, baz.get(1L));
    assertNotSame(person, baz.get(1L));

    Employee empl = new Employee(1234, "foo", 23);
    Cache<Long, Employee> bak = cacheManager.getCache("bak", Long.class, Employee.class);
    bak.put(1L, empl);
    assertSame(empl, bak.get(1L));
    cacheManager.close();
  }
  @Test
  public void testThreadPoolsUsingDefaultPool() throws Exception {
    Configuration configuration =
        new XmlConfiguration(this.getClass().getResource("/configs/thread-pools.xml"));
    final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
    cacheManager.init();
    try {
      Cache<String, String> cache =
          cacheManager.createCache(
              "testThreadPools",
              newCacheConfigurationBuilder()
                  .add(
                      new DefaultCacheLoaderWriterConfiguration(
                          ThreadRememberingLoaderWriter.class))
                  .add(newUnBatchedWriteBehindConfiguration())
                  .buildConfig(String.class, String.class));

      cache.put("foo", "bar");

      ThreadRememberingLoaderWriter.USED.acquireUninterruptibly();

      assertThat(ThreadRememberingLoaderWriter.LAST_SEEN_THREAD.getName(), containsString("[big]"));
    } finally {
      cacheManager.close();
    }
  }
  @Test
  public void testCanGetStats() {
    CacheConfiguration<Long, String> cacheConfiguration =
        CacheConfigurationBuilder.newCacheConfigurationBuilder()
            .withResourcePools(
                ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES).build())
            .buildConfig(Long.class, String.class);

    ManagementRegistry managementRegistry =
        new DefaultManagementRegistry(
            new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCM"));

    CacheManager cacheManager1 =
        CacheManagerBuilder.newCacheManagerBuilder()
            .withCache("aCache1", cacheConfiguration)
            .withCache("aCache2", cacheConfiguration)
            .using(managementRegistry)
            .build(true);

    Map<String, String> context1 = new HashMap<String, String>();
    context1.put("cacheManagerName", "myCM");
    context1.put("cacheName", "aCache1");

    Map<String, String> context2 = new HashMap<String, String>();
    context2.put("cacheManagerName", "myCM");
    context2.put("cacheName", "aCache2");

    cacheManager1.getCache("aCache1", Long.class, String.class).put(1L, "1");
    cacheManager1.getCache("aCache1", Long.class, String.class).put(2L, "2");
    cacheManager1.getCache("aCache2", Long.class, String.class).put(3L, "3");
    cacheManager1.getCache("aCache2", Long.class, String.class).put(4L, "4");
    cacheManager1.getCache("aCache2", Long.class, String.class).put(5L, "5");

    Collection<Counter> counters =
        managementRegistry.collectStatistics(context1, "StatisticsCapability", "PutCounter");

    assertThat(counters, hasSize(1));
    assertThat(counters.iterator().next().getValue(), equalTo(2L));

    List<Collection<Counter>> allCounters =
        managementRegistry.collectStatistics(
            Arrays.asList(context1, context2), "StatisticsCapability", "PutCounter");

    assertThat(allCounters, hasSize(2));
    assertThat(allCounters.get(0), hasSize(1));
    assertThat(allCounters.get(1), hasSize(1));
    assertThat(allCounters.get(0).iterator().next().getValue(), equalTo(2L));
    assertThat(allCounters.get(1).iterator().next().getValue(), equalTo(3L));

    cacheManager1.close();
  }
  @Test
  public void testCanGetCapabilities() {
    CacheConfiguration<Long, String> cacheConfiguration =
        CacheConfigurationBuilder.newCacheConfigurationBuilder()
            .withResourcePools(
                ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES).build())
            .buildConfig(Long.class, String.class);

    ManagementRegistry managementRegistry =
        new DefaultManagementRegistry(
            new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCM"));

    CacheManager cacheManager1 =
        CacheManagerBuilder.newCacheManagerBuilder()
            .withCache("aCache", cacheConfiguration)
            .using(managementRegistry)
            .build(true);

    assertThat(managementRegistry.getCapabilities(), hasSize(2));
    assertThat(
        new ArrayList<Capability>(managementRegistry.getCapabilities()).get(0).getName(),
        equalTo("ActionsCapability"));
    assertThat(
        new ArrayList<Capability>(managementRegistry.getCapabilities()).get(1).getName(),
        equalTo("StatisticsCapability"));

    assertThat(
        new ArrayList<Capability>(managementRegistry.getCapabilities()).get(0).getDescriptions(),
        hasSize(4));
    assertThat(
        new ArrayList<Capability>(managementRegistry.getCapabilities()).get(1).getDescriptions(),
        hasSize(16));

    assertThat(
        new ArrayList<Capability>(managementRegistry.getCapabilities())
            .get(0)
            .getCapabilityContext()
            .getAttributes(),
        hasSize(2));
    assertThat(
        new ArrayList<Capability>(managementRegistry.getCapabilities())
            .get(1)
            .getCapabilityContext()
            .getAttributes(),
        hasSize(2));

    cacheManager1.close();
  }
  @Test
  public void testSerializers() throws Exception {
    Configuration configuration =
        new XmlConfiguration(this.getClass().getResource("/configs/default-serializer.xml"));
    final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
    cacheManager.init();

    Cache<Long, Double> bar = cacheManager.getCache("bar", Long.class, Double.class);
    bar.put(1L, 1.0);
    assertThat(bar.get(1L), equalTo(1.0));

    Cache<String, String> baz = cacheManager.getCache("baz", String.class, String.class);
    baz.put("1", "one");
    assertThat(baz.get("1"), equalTo("one"));

    Cache<String, Object> bam =
        cacheManager.createCache(
            "bam", newCacheConfigurationBuilder().buildConfig(String.class, Object.class));
    bam.put("1", "one");
    assertThat(bam.get("1"), equalTo((Object) "one"));

    cacheManager.close();
  }
 @After()
 public void close() {
   cacheManager2.close();
   cacheManager1.close();
 }