@Override public void run2() throws CacheException { Region<Object, Object> region = createRegion(name); setWriter(new TestCacheWriter<Object, Object>() {}); region.getAttributesMutator().setCacheWriter(writer()); } };
@Test public void executeFunctionGivenARegionWithNonJdbcWriterDoesNotRemoveTheWriter() { when(regionAttributes.getCacheWriter()).thenReturn(mock(CacheWriter.class)); when(service.getMappingForRegion(eq(regionName))).thenReturn(mapping); function.executeFunction(context); verify(regionMutator, never()).setCacheWriter(null); }
@Test public void executeFunctionGivenARegionWithJdbcWriterRemovesTheWriter() { when(regionAttributes.getCacheWriter()).thenReturn(mock(JdbcWriter.class)); when(service.getMappingForRegion(eq(regionName))).thenReturn(mapping); function.executeFunction(context); verify(regionMutator, times(1)).setCacheWriter(null); }
mutator.setCacheWriter(new CacheWriter<String, String>() { @Override public void close() {
@Test public void updateWithNoCacheWriter() { regionAttributes.setCacheWriter(DeclarableType.EMPTY); function.alterRegion(cache, config); verify(mutator).setCacheWriter(null); verify(mutator, times(0)).setCacheLoader(any()); } }
@Test public void updateWithCacheWriter() { DeclarableType newCacheWriterDeclarable = mock(DeclarableType.class); when(newCacheWriterDeclarable.getClassName()).thenReturn(MyCacheWriter.class.getName()); regionAttributes.setCacheWriter(newCacheWriterDeclarable); function.alterRegion(cache, config); verify(mutator).setCacheWriter(notNull()); verify(mutator, times(0)).setCacheLoader(any()); }
@Test public void executeWithSynchronousAltersRegionWriter() throws Exception { setupSynchronous(); function.executeFunction(context); AttributesMutator mutator = region.getAttributesMutator(); verify(mutator, times(1)).setCacheWriter(any()); }
/** * Change the existing region to have * the JdbcLoader as its cache-loader * and the given async-event-queue as one of its queues. */ private void alterRegion(Region<?, ?> region, String queueName, boolean synchronous) { region.getAttributesMutator().setCacheLoader(new JdbcLoader()); if (synchronous) { region.getAttributesMutator().setCacheWriter(new JdbcWriter()); } else { region.getAttributesMutator().addAsyncEventQueueId(queueName); } }
if (cacheWriter != null) { if (cacheWriter.equals(DeclarableType.EMPTY)) { mutator.setCacheWriter(null); } else { mutator.setCacheWriter(DeclarableTypeInstantiator.newInstance(cacheWriter, cache));
private void cleanupRegionAndQueue(Cache cache, String regionName) { String queueName = CreateMappingCommand.createAsyncEventQueueName(regionName); Region<?, ?> region = cache.getRegion(regionName); if (region != null) { CacheLoader<?, ?> loader = region.getAttributes().getCacheLoader(); if (loader instanceof JdbcLoader) { region.getAttributesMutator().setCacheLoader(null); } CacheWriter<?, ?> writer = region.getAttributes().getCacheWriter(); if (writer instanceof JdbcWriter) { region.getAttributesMutator().setCacheWriter(null); } Set<String> queueIds = region.getAttributes().getAsyncEventQueueIds(); if (queueIds.contains(queueName)) { region.getAttributesMutator().removeAsyncEventQueueId(queueName); } } InternalAsyncEventQueue queue = (InternalAsyncEventQueue) cache.getAsyncEventQueue(queueName); if (queue != null) { queue.stop(); queue.destroy(); } } }
mutator.setCacheWriter(attrs.getCacheWriter());
/** * Post-process the {@link Region} created by this {@link ClientRegionFactoryBean}. * * @param region {@link Region} to process. * @see org.apache.geode.cache.Region */ @Override protected Region<K, V> postProcess(Region<K, V> region) { super.postProcess(region); Optional.ofNullable(this.cacheLoader) .ifPresent(cacheLoader -> region.getAttributesMutator().setCacheLoader(cacheLoader)); Optional.ofNullable(this.cacheWriter) .ifPresent(cacheWriter -> region.getAttributesMutator().setCacheWriter(cacheWriter)); return region; }
/** * Post-process the {@link Region} created by this {@link ClientRegionFactoryBean}. * * @param region {@link Region} to process. * @see org.apache.geode.cache.Region */ @Override protected Region<K, V> postProcess(Region<K, V> region) { super.postProcess(region); Optional.ofNullable(this.cacheLoader) .ifPresent(cacheLoader -> region.getAttributesMutator().setCacheLoader(cacheLoader)); Optional.ofNullable(this.cacheWriter) .ifPresent(cacheWriter -> region.getAttributesMutator().setCacheWriter(cacheWriter)); return region; }
.when(mockAttributesMutator).setCacheWriter(any(CacheWriter.class));