@Test
  public void testFailedIndexUpdateOnCommitForPut() throws Exception {
    Person.THROW_ON_INDEX = true;
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(DataPolicy.REPLICATE);
    SimpleListener sl = new SimpleListener();
    af.setCacheListener(sl);
    Region region = cache.createRegion("sample", af.create());
    qs.createIndex("foo", IndexType.FUNCTIONAL, "index", "/sample");
    Context ctx = cache.getJNDIContext();

    Integer x = new Integer(0);
    region.getCache().getCacheTransactionManager().begin();
    region.create(x, new Person("xyz", 45));
    try {
      region.getCache().getCacheTransactionManager().commit();
      fail("commit should have thrown an exception because the index maintenance threw");
    } catch (com.gemstone.gemfire.cache.query.IndexMaintenanceException ie) {
      // this is the desired case
    }
    Person p = (Person) region.get(x);
    assertEquals("object shouldn't have made it into region", null, p);
    assertEquals(0, sl.creates);
    assertEquals(0, sl.updates);
  }
 /** Creates entries on the server */
 public static void createEntriesK1andK2() {
   try {
     Region r1 = cache.getRegion(Region.SEPARATOR + REGION_NAME);
     assertNotNull(r1);
     if (!r1.containsKey("key1")) {
       r1.create("key1", "key-1");
     }
     if (!r1.containsKey("key2")) {
       r1.create("key2", "key-2");
     }
     assertEquals(r1.getEntry("key1").getValue(), "key-1");
     assertEquals(r1.getEntry("key2").getValue(), "key-2");
   } catch (Exception ex) {
     fail("failed while createEntries()", ex);
   }
 }
 private void createInRegion(int i, Region region) {
   Object key = ObjectHelper.createName(this.keyType, i);
   String objectType = CachePerfPrms.getObjectType();
   Object val = ObjectHelper.createObject(objectType, i);
   long start = this.statistics.startCreate();
   region.create(key, val);
   this.statistics.endCreate(start, this.isMainWorkload, this.histogram);
 }
  /**
   * Tests that a local writer receives a modified version of the callback argument on an update.
   */
  public void testLocalUpdateModifiedCallbackArgument() throws CacheException {

    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object value = "VALUE";
    final Object one = "ONE";
    final Object two = "TWO";

    TestCacheLoader loader =
        new TestCacheLoader() {
          public Object load2(LoaderHelper helper) throws CacheLoaderException {

            Object[] array = (Object[]) helper.getArgument();
            assertEquals(one, array[0]);
            array[0] = two;

            return value;
          }
        };

    TestCacheWriter writer =
        new TestCacheWriter() {
          public void beforeCreate2(EntryEvent event) throws CacheWriterException {}

          public void beforeUpdate2(EntryEvent event) throws CacheWriterException {

            Object[] array = (Object[]) event.getCallbackArgument();
            assertEquals(two, array[0]);
          }
        };

    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    factory.setCacheLoader(loader);
    factory.setCacheWriter(writer);
    Region region = createRegion(name, factory.create());

    region.create(key, null);
    assertFalse(loader.wasInvoked());
    assertTrue(writer.wasInvoked());

    Object[] array = new Object[] {one};
    assertEquals(value, region.get(key, array));
    assertTrue(loader.wasInvoked());
    assertTrue(writer.wasInvoked());
  }
 // @Test
 public void _testFailedIndexUpdateOnJTACommitForPut() throws Exception {
   Person.THROW_ON_INDEX = true;
   AttributesFactory af = new AttributesFactory();
   af.setDataPolicy(DataPolicy.REPLICATE);
   Region region = cache.createRegion("sample", af.create());
   qs.createIndex("foo", IndexType.FUNCTIONAL, "index", "/sample");
   Context ctx = cache.getJNDIContext();
   UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
   Integer x = new Integer(0);
   utx.begin();
   region.create(x, new Person("xyz", 45));
   try {
     utx.commit();
     fail("Commit should have thrown an exception because the index update threw");
   } catch (Exception e) {
     // this is desired
   }
 }
 @Test
 public void testIndexOnCommitForDestroy() throws Exception {
   AttributesFactory af = new AttributesFactory();
   af.setDataPolicy(DataPolicy.REPLICATE);
   Region region = cache.createRegion("sample", af.create());
   qs.createIndex("foo", IndexType.FUNCTIONAL, "age", "/sample");
   Context ctx = cache.getJNDIContext();
   UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
   Integer x = new Integer(0);
   utx.begin();
   region.create(x, new Person("xyz", 45));
   utx.commit();
   Query q = qs.newQuery("select * from /sample where age < 50");
   assertEquals(1, ((SelectResults) q.execute()).size());
   Person dsample = (Person) CopyHelper.copy(region.get(x));
   dsample.setAge(55);
   utx.begin();
   region.destroy(x);
   utx.commit();
   CacheUtils.log(((Person) region.get(x)));
   assertEquals(0, ((SelectResults) q.execute()).size());
 }
  /**
   * check distributed ops that originate in a PROXY are correctly distributed to non-proxy regions.
   */
  private void distributedOps(DataPolicy dp, InterestPolicy ip) throws CacheException {
    initOtherId();
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(dp);
    af.setSubscriptionAttributes(new SubscriptionAttributes(ip));
    af.setScope(Scope.DISTRIBUTED_ACK);
    Region r = createRootRegion("ProxyDUnitTest", af.create());

    doCreateOtherVm();

    r.put("putkey", "putvalue1");

    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check put") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(true, r.containsKey("putkey"));
                assertEquals("putvalue1", r.getEntry("putkey").getValue());
                r.put("putkey", "putvalue2");
              }
            });

    assertEquals(false, r.containsKey("putkey"));
    assertEquals("putvalue2", r.get("putkey")); // netsearch

    r.invalidate("putkey");

    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check invalidate") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(true, r.containsKey("putkey"));
                assertEquals(null, r.getEntry("putkey").getValue());
              }
            });
    assertEquals(null, r.get("putkey")); // invalid so total miss

    r.destroy("putkey");

    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check destroy") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(false, r.containsKey("putkey"));
              }
            });

    assertEquals(null, r.get("putkey")); // total miss

    r.create("createKey", "createValue1");
    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check create") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(true, r.containsKey("createKey"));
                assertEquals("createValue1", r.getEntry("createKey").getValue());
              }
            });
    {
      Map m = new HashMap();
      m.put("putAllKey1", "putAllValue1");
      m.put("putAllKey2", "putAllValue2");
      r.putAll(m, "putAllCallback");
    }
    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check putAll") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(true, r.containsKey("putAllKey1"));
                assertEquals("putAllValue1", r.getEntry("putAllKey1").getValue());
                assertEquals(true, r.containsKey("putAllKey2"));
                assertEquals("putAllValue2", r.getEntry("putAllKey2").getValue());
              }
            });
    r.clear();
    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check clear") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(0, r.size());
              }
            });

    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("install CacheWriter") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                AttributesMutator am = r.getAttributesMutator();
                CacheWriter cw =
                    new CacheWriterAdapter() {
                      public void beforeCreate(EntryEvent event) throws CacheWriterException {
                        throw new CacheWriterException("expected");
                      }
                    };
                am.setCacheWriter(cw);
              }
            });
    try {
      r.put("putkey", "putvalue");
      fail("expected CacheWriterException");
    } catch (CacheWriterException expected) {
    }
    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check clear") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(0, r.size());
              }
            });

    assertEquals(null, r.get("loadkey")); // total miss
    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("install CacheLoader") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                AttributesMutator am = r.getAttributesMutator();
                am.setCacheWriter(null); // clear csche writer
                CacheLoader cl =
                    new CacheLoader() {
                      public Object load(LoaderHelper helper) throws CacheLoaderException {
                        if (helper.getKey().equals("loadkey")) {
                          return "loadvalue";
                        } else if (helper.getKey().equals("loadexception")) {
                          throw new CacheLoaderException("expected");
                        } else {
                          return null;
                        }
                      }

                      public void close() {}
                    };
                am.setCacheLoader(cl);
              }
            });
    assertEquals("loadvalue", r.get("loadkey")); // net load
    assertEquals(null, r.get("foobar")); // total miss
    try {
      r.get("loadexception");
      fail("expected CacheLoaderException");
    } catch (CacheLoaderException expected) {
    }
    r.destroyRegion();
    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check clear") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(null, r);
              }
            });
  }