@Test
  public void testCacheWriterBeforeRegionDestroy() throws CacheException {

    final String name = getUniqueName();
    final Object arg = "ARG";
    final String exception = "EXCEPTION";

    TestCacheWriter writer = new TestCacheWriter() {
      private boolean closed = false;
      private boolean destroyed = false;

      @Override
      public boolean wasInvoked() {
        boolean value = closed && destroyed;
        super.wasInvoked();
        return value;
      }

      @Override
      public void close2() {
        closed = true;
      }

      @Override
      public void beforeRegionDestroy2(RegionEvent event) throws CacheWriterException {

        assertEquals(name, event.getRegion().getName());
        // this should be a distributed destroy unless the region
        // is local scope
        assertTrue(event.getOperation().isRegionDestroy());
        assertFalse(event.getOperation().isExpiration());
        assertFalse(event.isOriginRemote());

        Object argument = event.getCallbackArgument();
        if (argument != null) {
          if (argument.equals(exception)) {
            String s = "Test CacheWriterException";
            throw new CacheWriterException(s);

          } else {
            assertEquals(arg, argument);
          }
        }

        destroyed = true;
      }
    };

    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    factory.setCacheWriter(writer);
    RegionAttributes attrs = factory.create();
    Region region;

    region = createRegion(name, attrs);
    region.destroyRegion();
    assertTrue(region.isDestroyed());
    assertTrue(writer.wasInvoked());


    region = createRegion(name, attrs);
    region.destroyRegion(arg);
    assertTrue(writer.wasInvoked());
    assertTrue(region.isDestroyed());


    try {
      region = createRegion(name, attrs);
      region.destroyRegion(exception);
      fail("Should have thrown a CacheWriterException");

    } catch (CacheWriterException ex) {
      // pass...
      assertTrue(writer.wasInvoked());
      assertFalse(region.isDestroyed());
      assertNull(region.getSubregion(name));
    }
  }
Tests that the <code>CacheWriter</code> is called before a region is destroyed.

@see CacheWriter#beforeRegionDestroy
@see CacheWriter#close
geode-core/src/distributedTest/java/org/apache/geode/cache30/CacheWriterTestCase.java