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 testBucketMarshalling() throws Exception {
    ImmortalCacheEntry entry1 =
        (ImmortalCacheEntry)
            TestInternalCacheEntryFactory.create(
                "key",
                "value",
                System.currentTimeMillis() - 1000,
                -1,
                System.currentTimeMillis(),
                -1);
    MortalCacheEntry entry2 =
        (MortalCacheEntry)
            TestInternalCacheEntryFactory.create(
                "key",
                "value",
                System.currentTimeMillis() - 1000,
                200000,
                System.currentTimeMillis(),
                -1);
    TransientCacheEntry entry3 =
        (TransientCacheEntry)
            TestInternalCacheEntryFactory.create(
                "key",
                "value",
                System.currentTimeMillis() - 1000,
                -1,
                System.currentTimeMillis(),
                4000000);
    TransientMortalCacheEntry entry4 =
        (TransientMortalCacheEntry)
            TestInternalCacheEntryFactory.create(
                "key",
                "value",
                System.currentTimeMillis() - 1000,
                200000,
                System.currentTimeMillis(),
                4000000);
    Bucket b = new Bucket();
    b.setBucketId(0);
    b.addEntry(entry1);
    b.addEntry(entry2);
    b.addEntry(entry3);
    b.addEntry(entry4);

    byte[] bytes = marshaller.objectToByteBuffer(b);
    Bucket rb = (Bucket) marshaller.objectFromByteBuffer(bytes);
    assert rb.getEntries().equals(b.getEntries())
        : "Writen["
            + b.getEntries()
            + "] and read["
            + rb.getEntries()
            + "] objects should be the same";
  }
  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);
  }
  public void testLoading() throws CacheLoaderException {
    assertNotInCacheAndStore("k1", "k2", "k3", "k4");
    for (int i = 1; i < 5; i++) store.store(TestInternalCacheEntryFactory.create("k" + i, "v" + i));
    for (int i = 1; i < 5; i++) assert cache.get("k" + i).equals("v" + i);
    // make sure we have no stale locks!!
    assertNoLocks(cache);

    for (int i = 1; i < 5; i++) cache.evict("k" + i);
    // make sure we have no stale locks!!
    assertNoLocks(cache);

    assert cache.putIfAbsent("k1", "v1-SHOULD-NOT-STORE").equals("v1");
    assert cache.remove("k2").equals("v2");
    assert cache.replace("k3", "v3-REPLACED").equals("v3");
    assert cache.replace("k4", "v4", "v4-REPLACED");
    // make sure we have no stale locks!!
    assertNoLocks(cache);

    assert cache.size() == 3
        : "Expected the cache to contain 3 elements but contained " + cache.size();

    for (int i = 1; i < 5; i++) cache.evict("k" + i);
    // make sure we have no stale locks!!
    assertNoLocks(cache);

    assert cache.isEmpty(); // cache size ops will not trigger a load

    cache.clear(); // this should propagate to the loader though
    assertNotInCacheAndStore("k1", "k2", "k3", "k4");
    // make sure we have no stale locks!!
    assertNoLocks(cache);
  }
  public void testInternalCacheEntryMarshalling() throws Exception {
    ImmortalCacheEntry entry1 =
        (ImmortalCacheEntry)
            TestInternalCacheEntryFactory.create(
                "key",
                "value",
                System.currentTimeMillis() - 1000,
                -1,
                System.currentTimeMillis(),
                -1);
    marshallAndAssertEquality(entry1);

    MortalCacheEntry entry2 =
        (MortalCacheEntry)
            TestInternalCacheEntryFactory.create(
                "key",
                "value",
                System.currentTimeMillis() - 1000,
                200000,
                System.currentTimeMillis(),
                -1);
    marshallAndAssertEquality(entry2);

    TransientCacheEntry entry3 =
        (TransientCacheEntry)
            TestInternalCacheEntryFactory.create(
                "key",
                "value",
                System.currentTimeMillis() - 1000,
                -1,
                System.currentTimeMillis(),
                4000000);
    marshallAndAssertEquality(entry3);

    TransientMortalCacheEntry entry4 =
        (TransientMortalCacheEntry)
            TestInternalCacheEntryFactory.create(
                "key",
                "value",
                System.currentTimeMillis() - 1000,
                200000,
                System.currentTimeMillis(),
                4000000);
    marshallAndAssertEquality(entry4);
  }
  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 testLoadingToMemory() throws CacheLoaderException {
    assertNotInCacheAndStore("k1", "k2");
    store.store(TestInternalCacheEntryFactory.create("k1", "v1"));
    store.store(TestInternalCacheEntryFactory.create("k2", "v2"));

    assertInStoreNotInCache("k1", "k2");

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

    assertInCacheAndStore("k1", "v1");
    assertInCacheAndStore("k2", "v2");

    store.remove("k1");
    store.remove("k2");

    assertInCacheAndNotInStore("k1", "k2");
    assert "v1".equals(cache.get("k1"));
    assert "v2".equals(cache.get("k2"));
  }
  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;
  }
  public void testStateTransferControlCommand() throws Exception {
    Cache<Object, Object> cache = cm.getCache();

    String cacheName = EmbeddedCacheManager.DEFAULT_CACHE_NAME;
    ImmortalCacheEntry entry1 =
        (ImmortalCacheEntry)
            TestInternalCacheEntryFactory.create(
                "key",
                "value",
                System.currentTimeMillis() - 1000,
                -1,
                System.currentTimeMillis(),
                -1);
    Collection<InternalCacheEntry> state = new ArrayList<InternalCacheEntry>();
    state.add(entry1);
    Address a1 = new JGroupsAddress(UUID.randomUUID());
    Address a2 = new JGroupsAddress(UUID.randomUUID());
    Address a3 = new JGroupsAddress(UUID.randomUUID());
    List<Address> oldAddresses = new ArrayList<Address>();
    oldAddresses.add(a1);
    oldAddresses.add(a2);
    DefaultConsistentHashFactory chf = new DefaultConsistentHashFactory();
    DefaultConsistentHash oldCh = chf.create(new MurmurHash3(), 2, 3, oldAddresses);
    List<Address> newAddresses = new ArrayList<Address>();
    newAddresses.add(a1);
    newAddresses.add(a2);
    newAddresses.add(a3);
    DefaultConsistentHash newCh = chf.create(new MurmurHash3(), 2, 3, newAddresses);
    StateRequestCommand c14 =
        new StateRequestCommand(
            cacheName, StateRequestCommand.Type.START_STATE_TRANSFER, a1, 99, null);
    byte[] bytes = marshaller.objectToByteBuffer(c14);
    marshaller.objectFromByteBuffer(bytes);

    bytes = marshaller.objectToByteBuffer(c14);
    marshaller.objectFromByteBuffer(bytes);

    bytes = marshaller.objectToByteBuffer(c14);
    marshaller.objectFromByteBuffer(bytes);
  }
  public void testInternalCacheValueMarshalling() throws Exception {
    ImmortalCacheValue value1 =
        (ImmortalCacheValue)
            TestInternalCacheEntryFactory.createValue(
                "value", System.currentTimeMillis() - 1000, -1, System.currentTimeMillis(), -1);
    byte[] bytes = marshaller.objectToByteBuffer(value1);
    ImmortalCacheValue rvalue1 = (ImmortalCacheValue) marshaller.objectFromByteBuffer(bytes);
    assert rvalue1.getValue().equals(value1.getValue())
        : "Writen["
            + rvalue1.getValue()
            + "] and read["
            + value1.getValue()
            + "] objects should be the same";

    MortalCacheValue value2 =
        (MortalCacheValue)
            TestInternalCacheEntryFactory.createValue(
                "value", System.currentTimeMillis() - 1000, 200000, System.currentTimeMillis(), -1);
    bytes = marshaller.objectToByteBuffer(value2);
    MortalCacheValue rvalue2 = (MortalCacheValue) marshaller.objectFromByteBuffer(bytes);
    assert rvalue2.getValue().equals(value2.getValue())
        : "Writen["
            + rvalue2.getValue()
            + "] and read["
            + value2.getValue()
            + "] objects should be the same";

    TransientCacheValue value3 =
        (TransientCacheValue)
            TestInternalCacheEntryFactory.createValue(
                "value",
                System.currentTimeMillis() - 1000,
                -1,
                System.currentTimeMillis(),
                4000000);
    bytes = marshaller.objectToByteBuffer(value3);
    TransientCacheValue rvalue3 = (TransientCacheValue) marshaller.objectFromByteBuffer(bytes);
    assert rvalue3.getValue().equals(value3.getValue())
        : "Writen["
            + rvalue3.getValue()
            + "] and read["
            + value3.getValue()
            + "] objects should be the same";

    TransientMortalCacheValue value4 =
        (TransientMortalCacheValue)
            TestInternalCacheEntryFactory.createValue(
                "value",
                System.currentTimeMillis() - 1000,
                200000,
                System.currentTimeMillis(),
                4000000);
    bytes = marshaller.objectToByteBuffer(value4);
    TransientMortalCacheValue rvalue4 =
        (TransientMortalCacheValue) marshaller.objectFromByteBuffer(bytes);
    assert rvalue4.getValue().equals(value4.getValue())
        : "Writen["
            + rvalue4.getValue()
            + "] and read["
            + value4.getValue()
            + "] objects should be the same";
  }