public void testReplaceFromNonOwner() throws Exception {
    String key = "k1", value = "value", value2 = "v2";
    initAndTest();

    for (Cache<Object, String> c : caches) {
      CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
      if (isOwner(c, key)) {
        assertIsInContainerImmortal(c, key);
        assert store.load(key).getValue().equals(value);
      } else {
        assert !store.containsKey(key);
      }
    }

    Object retval = getFirstNonOwner(key).replace(key, value2);
    if (testRetVals) assert value.equals(retval);
    for (Cache<Object, String> c : caches) {
      CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
      if (isOwner(c, key)) {
        assertIsInContainerImmortal(c, key);
        assert c.get(key).equals(value2);
        assert store.load(key).getValue().equals(value2);
      } else {
        assert !store.containsKey(key);
      }
    }
  }
  public void testPropagatingTwoPhaseCommit() throws Exception {
    List<Modification> list = new LinkedList<Modification>();
    list.add(new Store(InternalEntryFactory.create("k1", "v1")));
    list.add(new Store(InternalEntryFactory.create("k2", "v2", lifespan)));
    list.add(new Store(InternalEntryFactory.create("k3", "v3")));
    list.add(new Remove("k3"));
    list.add(new Clear());
    list.add(new Store(InternalEntryFactory.create("k4", "v4")));
    list.add(new Store(InternalEntryFactory.create("k5", "v5", lifespan)));
    list.add(new Store(InternalEntryFactory.create("k6", "v6")));
    list.add(new Remove("k6"));
    GlobalTransaction tx = gtf.newGlobalTransaction(null, false);
    cs.prepare(list, tx, false);

    CacheStore[] allStores = new CacheStore[] {cs, store1, store2}; // for iteration

    for (int i = 1; i < 7; i++) {
      for (CacheStore s : allStores) assert !s.containsKey("k" + i);
    }

    cs.commit(tx);

    for (int i = 1; i < 7; i++) {
      if (i < 4 || i == 6) {
        // these have been deleted
        for (CacheStore s : allStores) assert !s.containsKey("k" + i);
      } else {
        for (CacheStore s : allStores) {
          assert s.containsKey("k" + i);
          assert s.load("k" + i).getValue().equals("v" + i);
          assert s.load("k" + i).getLifespan() == (i == 5 ? lifespan : -1);
        }
      }
    }
  }
 public void testPutFromOwner() throws Exception {
   String key = "k3", value = "value3";
   for (Cache<Object, String> c : caches) assert c.isEmpty();
   getOwners(key)[0].put(key, value);
   for (Cache<Object, String> c : caches) {
     CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
     if (isOwner(c, key)) {
       assertIsInContainerImmortal(c, key);
       assert store.containsKey(key);
     } else {
       assertIsNotInL1(c, key);
       assert !store.containsKey(key);
     }
   }
 }
  public void testPutAll() throws Exception {

    c1.putAll(makePutAllTestData());

    for (Cache<Object, String> c : caches) {
      CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
      for (String key : keys) {
        if (isOwner(c, key)) {
          assertIsInContainerImmortal(c, key);
          assert store.containsKey(key);
        } else {
          assert !store.containsKey(key);
        }
      }
    }
  }
 public void testPutFromNonOwner() throws Exception {
   String key = "k2", value = "value2";
   for (Cache<Object, String> c : caches) assert c.isEmpty();
   Cache<Object, String> nonOwner = getFirstNonOwner(key);
   Cache<Object, String> owner = getFirstOwner(key);
   CacheStore nonOwnerStore =
       TestingUtil.extractComponent(nonOwner, CacheLoaderManager.class).getCacheStore();
   CacheStore ownerStore =
       TestingUtil.extractComponent(owner, CacheLoaderManager.class).getCacheStore();
   assert !nonOwnerStore.containsKey(key);
   assert !ownerStore.containsKey(key);
   Object retval = nonOwner.put(key, value);
   assert !nonOwnerStore.containsKey(key);
   assert ownerStore.containsKey(key);
   if (testRetVals) assert retval == null;
   assertOnAllCachesAndOwnership(key, value);
 }
  public void testPropagatingWrites() throws Exception {
    // put something in the store
    cs.store(InternalEntryFactory.create("k1", "v1"));
    cs.store(InternalEntryFactory.create("k2", "v2", lifespan));

    int i = 1;
    for (CacheStore s : stores) {
      assert s.containsKey("k1") : "Key k1 missing on store " + i;
      assert s.containsKey("k2") : "Key k2 missing on store " + i;
      assert s.load("k1").getValue().equals("v1");
      assert s.load("k2").getValue().equals("v2");
      assert s.load("k1").getLifespan() == -1;
      assert s.load("k2").getLifespan() == lifespan;
      i++;
    }

    cs.remove("k1");

    for (CacheStore s : stores) {
      assert !s.containsKey("k1");
      assert s.containsKey("k2");
      assert s.load("k1") == null;
      assert s.load("k2").getValue().equals("v2");
      assert s.load("k2").getLifespan() == lifespan;
    }

    cs.clear();

    for (CacheStore s : stores) {
      assert !s.containsKey("k1");
      assert !s.containsKey("k2");
      assert s.load("k1") == null;
      assert s.load("k2") == null;
    }

    cs.store(InternalEntryFactory.create("k1", "v1"));
    cs.store(InternalEntryFactory.create("k2", "v2", lifespan));
    cs.store(InternalEntryFactory.create("k3", "v3", 1000)); // short lifespan!

    for (CacheStore s : stores) {
      assert s.containsKey("k1");
      assert s.containsKey("k2");
      assert s.containsKey("k3");
    }

    Thread.sleep(1100);

    cs.purgeExpired();

    for (CacheStore s : stores) {
      assert s.containsKey("k1");
      assert s.containsKey("k2");
      assert !s.containsKey("k3");
    }
  }
  public void testPersistence() throws CacheLoaderException, ParseException {
    verifyFullTextHasMatches(0);

    Country italy = new Country();
    italy.countryName = "Italy";
    City rome = new City();
    rome.name = "Rome";
    italy.cities.add(rome);

    cache.put("IT", italy);
    assert !store.containsKey("IT");

    verifyFullTextHasMatches(1);

    cache.evict("IT");
    assert store.containsKey("IT");

    InternalCacheEntry internalCacheEntry = cache.getAdvancedCache().getDataContainer().get("IT");
    assert internalCacheEntry == null;

    verifyFullTextHasMatches(1);

    Country country = cache.get("IT");
    assert country != null;
    assert "Italy".equals(country.countryName);

    verifyFullTextHasMatches(1);

    cache.stop();
    assert ((SearchFactoryIntegrator) search.getSearchFactory()).isStopped();
    TestingUtil.killCacheManagers(cm);

    // Now let's check the entry is not re-indexed during data preloading:
    recreateCacheManager();

    // People should generally use a persistent index; we use RAMDirectory for
    // test cleanup, so for our configuration it needs now to contain zero
    // matches: on filesystem it would be exactly one as expected (two when ISPN-1179 was open)
    verifyFullTextHasMatches(0);
  }
 public void testClear() throws Exception {
   prepareClearTest();
   c1.clear();
   for (Cache<Object, String> c : caches) assert c.isEmpty();
   for (int i = 0; i < 5; i++) {
     String key = "k" + i;
     for (Cache<Object, String> c : caches) {
       CacheStore store =
           TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
       assert !store.containsKey(key);
     }
   }
 }
  public void testPutAllWithFlags() throws Exception {
    Map<String, String> data = makePutAllTestData();
    c1.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).putAll(data);

    for (Cache<Object, String> c : caches) {
      CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
      for (String key : keys) {
        assert !store.containsKey(key);
        if (isOwner(c, key)) {
          assertIsInContainerImmortal(c, key);
        }
      }
    }
  }
 public void testRemoveFromNonOwnerWithFlags() throws Exception {
   String key = "k1", value = "value";
   initAndTest();
   Object retval =
       getFirstNonOwner(key).getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).remove(key);
   if (testRetVals) assert value.equals(retval);
   for (Cache<Object, String> c : caches) {
     if (isOwner(c, key)) {
       CacheStore store =
           TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
       assert store.containsKey(key);
     }
   }
 }
 public void testClearWithFlag() throws Exception {
   prepareClearTest();
   c1.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).clear();
   for (Cache<Object, String> c : caches) {
     assert c.isEmpty()
         : "Data container " + c + " should be empty, instead it contains keys " + c.keySet();
     CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
     for (int i = 0; i < 5; i++) {
       String key = "k" + i;
       if (isOwner(c, key)) {
         assert store.containsKey(key);
       }
     }
   }
 }
 public void testAtomicReplaceFromNonOwner() throws Exception {
   String key = "k1", value = "value", value2 = "v2";
   initAndTest();
   boolean replaced = getFirstNonOwner(key).replace(key, value2, value);
   assert !replaced;
   replaced = getFirstNonOwner(key).replace(key, value, value2);
   assert replaced;
   for (Cache<Object, String> c : caches) {
     c.get(key).equals(value2);
     if (isOwner(c, key)) {
       CacheStore store =
           TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
       assert store.containsKey(key);
       assert store.load(key).getValue().equals(value2);
     }
   }
 }
 public void testAtomicPutIfAbsentFromNonOwnerWithFlag() throws Exception {
   String key = "k1", value = "value";
   for (Cache<Object, String> c : caches) assert c.isEmpty();
   String replaced =
       getFirstNonOwner(key)
           .getAdvancedCache()
           .withFlags(Flag.SKIP_CACHE_STORE)
           .putIfAbsent("k1", value);
   assert replaced == null;
   // interesting case: fails to put as value exists, put actually missing in Store
   replaced = getFirstNonOwner(key).putIfAbsent("k1", value);
   assert value.equals(replaced);
   for (Cache<Object, String> c : caches) {
     c.get(key).equals(replaced);
     CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
     assert !store.containsKey(key);
   }
 }
  public void testPropagatingStreams()
      throws IOException, ClassNotFoundException, CacheLoaderException {
    store2.store(InternalEntryFactory.create("k1", "v1"));
    store2.store(InternalEntryFactory.create("k2", "v2", lifespan));

    assert cs.containsKey("k1");
    assert cs.containsKey("k2");

    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(byteStream);
    cs.toStream(new UnclosableObjectOutputStream(oos));
    oos.close();
    byteStream.close();
    cs.clear();

    assert !cs.containsKey("k1");
    assert !cs.containsKey("k2");
    for (CacheStore s : stores) {
      assert !s.containsKey("k1");
      assert !s.containsKey("k2");
    }

    cs.fromStream(
        new UnclosableObjectInputStream(
            new ObjectInputStream(new ByteArrayInputStream(byteStream.toByteArray()))));

    assert cs.containsKey("k1");
    assert cs.containsKey("k2");
    assert cs.load("k1").getValue().equals("v1");
    assert cs.load("k2").getValue().equals("v2");
    assert cs.load("k1").getLifespan() == -1;
    assert cs.load("k2").getLifespan() == lifespan;

    assert store2.containsKey("k1");
    assert store2.containsKey("k2");
    assert store2.load("k1").getValue().equals("v1");
    assert store2.load("k2").getValue().equals("v2");
    assert store2.load("k1").getLifespan() == -1;
    assert store2.load("k2").getLifespan() == lifespan;

    // store 1 has not been set up to support fetching state
    assert !store1.containsKey("k1");
    assert !store1.containsKey("k2");
  }
 private void prepareClearTest() throws CacheLoaderException {
   for (Cache<Object, String> c : caches)
     assert c.isEmpty()
         : "Data container " + c + " should be empty, instead it contains keys " + c.keySet();
   for (int i = 0; i < 5; i++) {
     getOwners("k" + i)[0].put("k" + i, "value" + i);
   }
   // this will fill up L1 as well
   for (int i = 0; i < 5; i++) assertOnAllCachesAndOwnership("k" + i, "value" + i);
   for (Cache<Object, String> c : caches) {
     assert !c.isEmpty();
     CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
     for (int i = 0; i < 5; i++) {
       String key = "k" + i;
       if (isOwner(c, key)) {
         assert store.containsKey(key) : "Cache store " + c + " does not contain key " + key;
       }
     }
   }
 }
 @Test(
     enabled = false,
     description = "Temporary disabled : https://issues.jboss.org/browse/ISPN-2249")
 public void testAtomicPutIfAbsentFromNonOwner() throws Exception {
   String key = "k1", value = "value", value2 = "v2";
   for (Cache<Object, String> c : caches) assert c.isEmpty();
   String replaced = getFirstNonOwner(key).putIfAbsent("k1", value);
   assert replaced == null;
   replaced = getFirstNonOwner(key).putIfAbsent("k1", value2);
   assert value.equals(replaced);
   for (Cache<Object, String> c : caches) {
     c.get(key).equals(replaced);
     if (isOwner(c, key)) {
       CacheStore store =
           TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
       assert store.containsKey(key);
       assert store.load(key).getValue().equals(value);
     }
   }
 }
 @Before
 public void setUp() throws CacheLoaderException {
   initMocks(this);
   when(mockStore.containsKey(MOCK_FIRST_CHUNK)).thenReturn(true);
   testObj = new StoreChunkInputStream(mockStore, MOCK_KEY);
 }