/*
* Copyright Terracotta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ehcache.core;
import org.ehcache.Status;
import org.ehcache.core.spi.store.StoreAccessException;
import org.ehcache.spi.loaderwriter.CacheWritingException;
import org.ehcache.spi.loaderwriter.CacheLoaderWriter;
import org.ehcache.core.statistics.CacheOperationOutcomes;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.EnumSet;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Provides testing of basic REMOVE(key) operations on an {@code EhcacheWithLoaderWriter}.
*
* @author Clifford W. Johnson
*/
public class EhcacheWithLoaderWriterBasicRemoveTest extends EhcacheBasicCrudBase {
@Mock
protected CacheLoaderWriter<String, String> cacheLoaderWriter;
@Test
public void testRemoveNull() {
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(this.cacheLoaderWriter);
try {
ehcache.remove(null);
fail();
} catch (NullPointerException e) {
// expected
}
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key not present in {@code Store}</li>
* </ul>
*/
@Test
public void testRemoveNoStoreEntry() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.<String, String>emptyMap());
this.store = spy(fakeStore);
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(this.cacheLoaderWriter);
ehcache.remove("key");
verify(this.store).compute(eq("key"), getAnyBiFunction());
verifyZeroInteractions(this.spiedResilienceStrategy);
assertThat(fakeStore.getEntryMap().containsKey("key"), is(false));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.RemoveOutcome.NOOP));
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key not present in {@code Store}</li>
* <li>key not present via {@code CacheLoaderWriter}</li>
* </ul>
*/
@Test
public void testRemoveNoStoreEntryNoCacheLoaderWriterEntry() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.<String, String>emptyMap());
this.store = spy(fakeStore);
final FakeCacheLoaderWriter fakeWriter = new FakeCacheLoaderWriter(Collections.<String, String>emptyMap());
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(fakeWriter);
ehcache.remove("key");
verify(this.store).compute(eq("key"), getAnyBiFunction());
verifyZeroInteractions(this.spiedResilienceStrategy);
assertThat(fakeStore.getEntryMap().containsKey("key"), is(false));
assertThat(fakeWriter.getEntryMap().containsKey("key"), is(false));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.RemoveOutcome.NOOP));
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key not present in {@code Store}</li>
* <li>key present via {@code CacheLoaderWriter}</li>
* </ul>
*/
@Test
public void testRemoveNoStoreEntryHasCacheLoaderWriterEntry() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.<String, String>emptyMap());
this.store = spy(fakeStore);
final FakeCacheLoaderWriter fakeWriter = new FakeCacheLoaderWriter(Collections.singletonMap("key", "oldValue"));
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(fakeWriter);
ehcache.remove("key");
verify(this.store).compute(eq("key"), getAnyBiFunction());
verifyZeroInteractions(this.spiedResilienceStrategy);
assertThat(fakeStore.getEntryMap().containsKey("key"), is(false));
assertThat(fakeWriter.getEntryMap().containsKey("key"), is(false));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.RemoveOutcome.NOOP));
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key not present in {@code Store}</li>
* <li>{@code CacheLoaderWriter.delete} throws</li>
* </ul>
*/
@Test
public void testRemoveNoStoreEntryCacheWritingException() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.<String, String>emptyMap());
this.store = spy(fakeStore);
final FakeCacheLoaderWriter fakeWriter = new FakeCacheLoaderWriter(Collections.singletonMap("key", "oldValue"));
this.cacheLoaderWriter = spy(fakeWriter);
doThrow(new Exception()).when(this.cacheLoaderWriter).delete("key");
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(this.cacheLoaderWriter);
try {
ehcache.remove("key");
fail();
} catch (CacheWritingException e) {
// Expected
}
verify(this.store).compute(eq("key"), getAnyBiFunction());
verifyZeroInteractions(this.spiedResilienceStrategy);
validateStats(ehcache, EnumSet.noneOf(CacheOperationOutcomes.RemoveOutcome.class));
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key not present in {@code Store}</li>
* <li>{@code Store.compute} throws</li>
* </ul>
*/
@Test
public void testRemoveNoStoreEntryStoreAccessException() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.<String, String>emptyMap());
this.store = spy(fakeStore);
doThrow(new StoreAccessException("")).when(this.store).compute(eq("key"), getAnyBiFunction());
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(this.cacheLoaderWriter);
ehcache.remove("key");
verify(this.store).compute(eq("key"), getAnyBiFunction());
verify(this.spiedResilienceStrategy).removeFailure(eq("key"), any(StoreAccessException.class));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.RemoveOutcome.FAILURE));
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key not present in {@code Store}</li>
* <li>{@code Store.compute} throws</li>
* <li>key not present via {@code CacheLoaderWriter}</li>
* </ul>
*/
@Test
public void testRemoveNoStoreEntryStoreAccessExceptionNoCacheLoaderWriterEntry() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.<String, String>emptyMap());
this.store = spy(fakeStore);
doThrow(new StoreAccessException("")).when(this.store).compute(eq("key"), getAnyBiFunction());
final FakeCacheLoaderWriter fakeWriter = new FakeCacheLoaderWriter(Collections.<String, String>emptyMap());
this.cacheLoaderWriter = spy(fakeWriter);
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(this.cacheLoaderWriter);
final InOrder ordered = inOrder(this.cacheLoaderWriter, this.spiedResilienceStrategy);
ehcache.remove("key");
verify(this.store).compute(eq("key"), getAnyBiFunction());
ordered.verify(this.cacheLoaderWriter).delete(eq("key"));
ordered.verify(this.spiedResilienceStrategy).removeFailure(eq("key"), any(StoreAccessException.class));
assertThat(fakeWriter.getEntryMap().containsKey("key"), is(false));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.RemoveOutcome.FAILURE));
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key not present in {@code Store}</li>
* <li>{@code Store.compute} throws</li>
* <li>key present via {@code CacheLoaderWriter}</li>
* </ul>
*/
@Test
public void testRemoveNoStoreEntryStoreAccessExceptionHasCacheLoaderWriterEntry() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.<String, String>emptyMap());
this.store = spy(fakeStore);
doThrow(new StoreAccessException("")).when(this.store).compute(eq("key"), getAnyBiFunction());
final FakeCacheLoaderWriter fakeWriter = new FakeCacheLoaderWriter(Collections.singletonMap("key", "oldValue"));
this.cacheLoaderWriter = spy(fakeWriter);
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(this.cacheLoaderWriter);
final InOrder ordered = inOrder(this.cacheLoaderWriter, this.spiedResilienceStrategy);
ehcache.remove("key");
verify(this.store).compute(eq("key"), getAnyBiFunction());
ordered.verify(this.cacheLoaderWriter).delete(eq("key"));
ordered.verify(this.spiedResilienceStrategy).removeFailure(eq("key"), any(StoreAccessException.class));
assertThat(fakeWriter.getEntryMap().containsKey("key"), is(false));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.RemoveOutcome.FAILURE));
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key not present in {@code Store}</li>
* <li>{@code Store.compute} throws</li>
* <li>{@code CacheLoaderWriter.delete} throws</li>
* </ul>
*/
@Test
public void testRemoveNoStoreEntryStoreAccessExceptionCacheLoaderWriterException() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.<String, String>emptyMap());
this.store = spy(fakeStore);
doThrow(new StoreAccessException("")).when(this.store).compute(eq("key"), getAnyBiFunction());
final FakeCacheLoaderWriter fakeWriter = new FakeCacheLoaderWriter(Collections.singletonMap("key", "oldValue"));
this.cacheLoaderWriter = spy(fakeWriter);
doThrow(new Exception()).when(this.cacheLoaderWriter).delete("key");
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(this.cacheLoaderWriter);
final InOrder ordered = inOrder(this.cacheLoaderWriter, this.spiedResilienceStrategy);
try {
ehcache.remove("key");
fail();
} catch (CacheWritingException e) {
// Expected
}
verify(this.store).compute(eq("key"), getAnyBiFunction());
ordered.verify(this.cacheLoaderWriter).delete(eq("key"));
ordered.verify(this.spiedResilienceStrategy)
.removeFailure(eq("key"), any(StoreAccessException.class), any(CacheWritingException.class));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.RemoveOutcome.FAILURE));
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key present in {@code Store}</li>
* </ul>
*/
@Test
public void testRemoveHasStoreEntry() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "oldValue"));
this.store = spy(fakeStore);
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(this.cacheLoaderWriter);
ehcache.remove("key");
verify(this.store).compute(eq("key"), getAnyBiFunction());
verifyZeroInteractions(this.spiedResilienceStrategy);
assertThat(fakeStore.getEntryMap().containsKey("key"), is(false));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.RemoveOutcome.SUCCESS));
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key present in {@code Store}</li>
* <li>key not present via {@code CacheLoaderWriter}</li>
* </ul>
*/
@Test
public void testRemoveHasStoreEntryNoCacheLoaderWriterEntry() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "oldValue"));
this.store = spy(fakeStore);
final FakeCacheLoaderWriter fakeWriter = new FakeCacheLoaderWriter(Collections.<String, String>emptyMap());
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(fakeWriter);
ehcache.remove("key");
verify(this.store).compute(eq("key"), getAnyBiFunction());
verifyZeroInteractions(this.spiedResilienceStrategy);
assertThat(fakeStore.getEntryMap().containsKey("key"), is(false));
assertThat(fakeWriter.getEntryMap().containsKey("key"), is(false));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.RemoveOutcome.SUCCESS));
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key present in {@code Store}</li>
* <li>key present via {@code CacheLoaderWriter}</li>
* </ul>
*/
@Test
public void testRemoveHasStoreEntryHasCacheLoaderWriterEntry() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "oldValue"));
this.store = spy(fakeStore);
final FakeCacheLoaderWriter fakeWriter = new FakeCacheLoaderWriter(Collections.singletonMap("key", "oldValue"));
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(fakeWriter);
ehcache.remove("key");
verify(this.store).compute(eq("key"), getAnyBiFunction());
verifyZeroInteractions(this.spiedResilienceStrategy);
assertThat(fakeStore.getEntryMap().containsKey("key"), is(false));
assertThat(fakeWriter.getEntryMap().containsKey("key"), is(false));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.RemoveOutcome.SUCCESS));
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key present in {@code Store}</li>
* <li>{@code CacheLoaderWriter.delete} throws</li>
* </ul>
*/
@Test
public void testRemoveHasStoreEntryCacheWritingException() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "oldValue"));
this.store = spy(fakeStore);
final FakeCacheLoaderWriter fakeWriter = new FakeCacheLoaderWriter(Collections.singletonMap("key", "oldValue"));
this.cacheLoaderWriter = spy(fakeWriter);
doThrow(new Exception()).when(this.cacheLoaderWriter).delete("key");
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(this.cacheLoaderWriter);
try {
ehcache.remove("key");
fail();
} catch (CacheWritingException e) {
// Expected
}
verify(this.store).compute(eq("key"), getAnyBiFunction());
verifyZeroInteractions(this.spiedResilienceStrategy);
validateStats(ehcache, EnumSet.noneOf(CacheOperationOutcomes.RemoveOutcome.class));
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key present in {@code Store}</li>
* <li>{@code Store.compute} throws</li>
* </ul>
*/
@Test
public void testRemoveHasStoreEntryStoreAccessException() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "oldValue"));
this.store = spy(fakeStore);
doThrow(new StoreAccessException("")).when(this.store).compute(eq("key"), getAnyBiFunction());
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(this.cacheLoaderWriter);
ehcache.remove("key");
verify(this.store).compute(eq("key"), getAnyBiFunction());
verify(this.spiedResilienceStrategy).removeFailure(eq("key"), any(StoreAccessException.class));
assertThat(fakeStore.getEntryMap().containsKey("key"), is(false));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.RemoveOutcome.FAILURE));
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key present in {@code Store}</li>
* <li>{@code Store.compute} throws</li>
* <li>key not present via {@code CacheLoaderWriter}</li>
* </ul>
*/
@Test
public void testRemoveHasStoreEntryStoreAccessExceptionNoCacheLoaderWriterEntry() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "oldValue"));
this.store = spy(fakeStore);
doThrow(new StoreAccessException("")).when(this.store).compute(eq("key"), getAnyBiFunction());
final FakeCacheLoaderWriter fakeWriter = new FakeCacheLoaderWriter(Collections.<String, String>emptyMap());
this.cacheLoaderWriter = spy(fakeWriter);
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(this.cacheLoaderWriter);
final InOrder ordered = inOrder(this.cacheLoaderWriter, this.spiedResilienceStrategy);
ehcache.remove("key");
verify(this.store).compute(eq("key"), getAnyBiFunction());
ordered.verify(this.cacheLoaderWriter).delete(eq("key"));
ordered.verify(this.spiedResilienceStrategy).removeFailure(eq("key"), any(StoreAccessException.class));
assertThat(fakeWriter.getEntryMap().containsKey("key"), is(false));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.RemoveOutcome.FAILURE));
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key present in {@code Store}</li>
* <li>{@code Store.compute} throws</li>
* <li>key present via {@code CacheLoaderWriter}</li>
* </ul>
*/
@Test
public void testRemoveHasStoreEntryStoreAccessExceptionHasCacheLoaderWriterEntry() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "oldValue"));
this.store = spy(fakeStore);
doThrow(new StoreAccessException("")).when(this.store).compute(eq("key"), getAnyBiFunction());
final FakeCacheLoaderWriter fakeWriter = new FakeCacheLoaderWriter(Collections.singletonMap("key", "oldValue"));
this.cacheLoaderWriter = spy(fakeWriter);
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(this.cacheLoaderWriter);
final InOrder ordered = inOrder(this.cacheLoaderWriter, this.spiedResilienceStrategy);
ehcache.remove("key");
verify(this.store).compute(eq("key"), getAnyBiFunction());
ordered.verify(this.cacheLoaderWriter).delete(eq("key"));
ordered.verify(this.spiedResilienceStrategy).removeFailure(eq("key"), any(StoreAccessException.class));
assertThat(fakeWriter.getEntryMap().containsKey("key"), is(false));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.RemoveOutcome.FAILURE));
}
/**
* Tests the effect of a {@link EhcacheWithLoaderWriter#remove(Object)} for
* <ul>
* <li>key present in {@code Store}</li>
* <li>{@code Store.compute} throws</li>
* <li>{@code CacheLoaderWriter.delete} throws</li>
* </ul>
*/
@Test
public void testRemoveHasStoreEntryStoreAccessExceptionCacheLoaderWriterException() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "oldValue"));
this.store = spy(fakeStore);
doThrow(new StoreAccessException("")).when(this.store).compute(eq("key"), getAnyBiFunction());
final FakeCacheLoaderWriter fakeWriter = new FakeCacheLoaderWriter(Collections.singletonMap("key", "oldValue"));
this.cacheLoaderWriter = spy(fakeWriter);
doThrow(new Exception()).when(this.cacheLoaderWriter).delete("key");
final EhcacheWithLoaderWriter<String, String> ehcache = this.getEhcache(this.cacheLoaderWriter);
final InOrder ordered = inOrder(this.cacheLoaderWriter, this.spiedResilienceStrategy);
try {
ehcache.remove("key");
fail();
} catch (CacheWritingException e) {
// Expected
}
verify(this.store).compute(eq("key"), getAnyBiFunction());
ordered.verify(this.cacheLoaderWriter).delete(eq("key"));
ordered.verify(this.spiedResilienceStrategy)
.removeFailure(eq("key"), any(StoreAccessException.class), any(CacheWritingException.class));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.RemoveOutcome.FAILURE));
}
/**
* Gets an initialized {@link EhcacheWithLoaderWriter Ehcache} instance using the
* {@link CacheLoaderWriter} provided.
*
* @param cacheLoaderWriter the {@code CacheLoaderWriter} to use; may be {@code null}
*
* @return a new {@code EhcacheWithLoaderWriter} instance
*/
private EhcacheWithLoaderWriter<String, String> getEhcache(final CacheLoaderWriter<String, String> cacheLoaderWriter) {
final EhcacheWithLoaderWriter<String, String> ehcache = new EhcacheWithLoaderWriter<String, String>(CACHE_CONFIGURATION, this.store, cacheLoaderWriter, cacheEventDispatcher, LoggerFactory.getLogger(EhcacheWithLoaderWriter.class + "-" + "EhcacheWithLoaderWriterBasicRemoveTest"));
ehcache.init();
assertThat("cache not initialized", ehcache.getStatus(), CoreMatchers.is(Status.AVAILABLE));
this.spiedResilienceStrategy = this.setResilienceStrategySpy(ehcache);
return ehcache;
}
}