public void testReadOnlyCacheStore() throws CacheLoaderException {
    // ignore modifications
    store.store(TestInternalCacheEntryFactory.create("k1", "v1"));
    store.store(TestInternalCacheEntryFactory.create("k2", "v2"));

    assert !store.containsKey("k1") : "READ ONLY - Store should NOT contain k1 key.";
    assert !store.containsKey("k2") : "READ ONLY - Store should NOT contain k2 key.";

    // put into cache but not into read only store
    cache.put("k1", "v1");
    cache.put("k2", "v2");
    assert "v1".equals(cache.get("k1"));
    assert "v2".equals(cache.get("k2"));

    assert !store.containsKey("k1") : "READ ONLY - Store should NOT contain k1 key.";
    assert !store.containsKey("k2") : "READ ONLY - Store should NOT contain k2 key.";

    assert !store.remove("k1") : "READ ONLY - Remove operation should return false (no op)";
    assert !store.remove("k2") : "READ ONLY - Remove operation should return false (no op)";
    assert !store.remove("k3") : "READ ONLY - Remove operation should return false (no op)";

    assert "v1".equals(cache.get("k1"));
    assert "v2".equals(cache.get("k2"));
    cache.remove("k1");
    cache.remove("k2");
    assert cache.get("k1") == null;
    assert cache.get("k2") == null;
  }
  public void testSkipCacheFlagUsage() throws CacheLoaderException {
    CountingCacheStore countingCS = getCountingCacheStore();

    store.store(TestInternalCacheEntryFactory.create("k1", "v1"));

    assert countingCS.numLoads == 0;
    assert countingCS.numContains == 0;
    // load using SKIP_CACHE_STORE should not find the object in the store
    assert cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).get("k1") == null;
    assert countingCS.numLoads == 0;
    assert countingCS.numContains == 0;

    // counter-verify that the object was actually in the store:
    assert "v1".equals(cache.get("k1"));
    assert countingCS.numLoads == 1 : "Expected 1, was " + countingCS.numLoads;
    assert countingCS.numContains == 0 : "Expected 0, was " + countingCS.numContains;

    // now check that put won't return the stored value
    store.store(TestInternalCacheEntryFactory.create("k2", "v2"));
    Object putReturn =
        cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).put("k2", "v2-second");
    assert putReturn == null;
    assert countingCS.numLoads == 1 : "Expected 1, was " + countingCS.numLoads;
    assert countingCS.numContains == 0 : "Expected 0, was " + countingCS.numContains;
    // but it inserted it in the cache:
    assert "v2-second".equals(cache.get("k2"));
    // perform the put in the cache & store, using same value:
    putReturn = cache.put("k2", "v2-second");
    // returned value from the cache:
    assert "v2-second".equals(putReturn);
    // and verify that the put operation updated the store too:
    assert "v2-second".equals(store.load("k2").getValue());
    assert countingCS.numLoads == 2 : "Expected 2, was " + countingCS.numLoads;

    assert countingCS.numContains == 0 : "Expected 0, was " + countingCS.numContains;
    cache.containsKey("k1");
    assert countingCS.numContains == 0 : "Expected 0, was " + countingCS.numContains;
    assert false == cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).containsKey("k3");
    assert countingCS.numContains == 0 : "Expected 0, was " + countingCS.numContains;
    assert countingCS.numLoads == 2 : "Expected 2, was " + countingCS.numLoads;

    // now with batching:
    boolean batchStarted = cache.getAdvancedCache().startBatch();
    assert batchStarted;
    assert null == cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).get("k1batch");
    assert countingCS.numLoads == 2 : "Expected 2, was " + countingCS.numLoads;
    assert null == cache.getAdvancedCache().get("k2batch");
    assert countingCS.numLoads == 3 : "Expected 3, was " + countingCS.numLoads;
    cache.endBatch(true);
  }
  @Test
  public void testInvalidPut() throws Exception {
    Cache cache = cacheManagers.get(0).getCache("P006");

    // add 1st 4 elements
    for (int i = 0; i < 4; i++) {
      cache.put(cacheManagers.get(0).getAddress().toString() + "-" + i, "42");
    }

    // lets check if all elements arrived
    CacheStore cs1 =
        cache
            .getAdvancedCache()
            .getComponentRegistry()
            .getComponent(CacheLoaderManager.class)
            .getCacheStore();
    Set<Object> keys = cs1.loadAllKeys(null);

    Assert.assertEquals(keys.size(), 4);

    // now start 2nd node
    addClusterEnabledCacheManager(getCB());
    waitForClusterToForm("P006");

    cache = cacheManagers.get(1).getCache("P006");

    // add next 4 elements
    for (int i = 0; i < 4; i++) {
      cache.put(cacheManagers.get(1).getAddress().toString() + "-" + i, "42");
    }

    Set mergedKeys = new HashSet();
    // add keys from all cache stores
    CacheStore cs2 =
        cache
            .getAdvancedCache()
            .getComponentRegistry()
            .getComponent(CacheLoaderManager.class)
            .getCacheStore();
    log.debugf("Load from cache store via cache 1");
    mergedKeys.addAll(cs1.loadAllKeys(null));
    log.debugf("Load from cache store via cache 2");
    mergedKeys.addAll(cs2.loadAllKeys(null));

    Assert.assertEquals(mergedKeys.size(), 8);
  }
  public void testSkipCacheLoadFlagUsage() throws CacheLoaderException {
    CountingCacheStore countingCS = getCountingCacheStore();

    store.store(TestInternalCacheEntryFactory.create("home", "Vermezzo"));
    store.store(TestInternalCacheEntryFactory.create("home-second", "Newcastle Upon Tyne"));

    assert countingCS.numLoads == 0;
    // load using SKIP_CACHE_LOAD should not find the object in the store
    assert cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD).get("home") == null;
    assert countingCS.numLoads == 0;

    assert cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD).put("home", "Newcastle")
        == null;
    assert countingCS.numLoads == 0;

    final Object put = cache.getAdvancedCache().put("home-second", "Newcastle Upon Tyne, second");
    assertEquals(put, "Newcastle Upon Tyne");
    assert countingCS.numLoads == 1;
  }
  public void testRepeatedLoads() throws CacheLoaderException {
    CountingCacheStore countingCS = getCountingCacheStore();
    store.store(TestInternalCacheEntryFactory.create("k1", "v1"));

    assert countingCS.numLoads == 0;
    assert countingCS.numContains == 0;

    assert "v1".equals(cache.get("k1"));

    assert countingCS.numLoads == 1 : "Expected 1, was " + countingCS.numLoads;
    assert countingCS.numContains == 0 : "Expected 0, was " + countingCS.numContains;

    assert "v1".equals(cache.get("k1"));

    assert countingCS.numLoads == 1 : "Expected 1, was " + countingCS.numLoads;
    assert countingCS.numContains == 0 : "Expected 0, was " + countingCS.numContains;
  }