/*
* 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 java.util.Collections;
import java.util.EnumSet;
import org.ehcache.Status;
import org.ehcache.core.statistics.CacheOperationOutcomes;
import org.ehcache.core.spi.store.StoreAccessException;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.slf4j.LoggerFactory;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
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.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* @author Abhilash
*
*/
public class EhcacheBasicReplaceValueTest extends EhcacheBasicCrudBase {
@Test
public void testReplaceValueNullNullNull() {
final Ehcache<String, String> ehcache = this.getEhcache();
try {
ehcache.replace(null, null, null);
fail();
} catch (NullPointerException e) {
// expected
}
}
@Test
public void testReplaceKeyNullNull() {
final Ehcache<String, String> ehcache = this.getEhcache();
try {
ehcache.replace("key", null, null);
fail();
} catch (NullPointerException e) {
// expected
}
}
@Test
public void testReplaceKeyValueNull() {
final Ehcache<String, String> ehcache = this.getEhcache();
try {
ehcache.replace("key", "oldValue", null);
fail();
} catch (NullPointerException e) {
// expected
}
}
@Test
public void testReplaceKeyNullValue() {
final Ehcache<String, String> ehcache = this.getEhcache();
try {
ehcache.replace("key", null, "newValue");
fail();
} catch (NullPointerException e) {
// expected
}
}
@Test
public void testReplaceNullValueNull() {
final Ehcache<String, String> ehcache = this.getEhcache();
try {
ehcache.replace(null, "oldValue", null);
fail();
} catch (NullPointerException e) {
// expected
}
}
@Test
public void testReplaceNullValueValue() {
final Ehcache<String, String> ehcache = this.getEhcache();
try {
ehcache.replace(null, "oldValue", "newValue");
fail();
} catch (NullPointerException e) {
// expected
}
}
@Test
public void testReplaceNullNullValue() {
final Ehcache<String, String> ehcache = this.getEhcache();
try {
ehcache.replace(null, null, "newValue");
fail();
} catch (NullPointerException e) {
// expected
}
}
/**
* Tests the effect of a {@link Ehcache#replace(Object, Object, Object)} for
* <ul>
* <li>key not present in {@code Store}</li>
* </ul>
*/
@Test
public void testReplaceValueNoStoreEntry() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.<String, String>emptyMap());
this.store = spy(fakeStore);
final Ehcache<String, String> ehcache = this.getEhcache();
assertFalse(ehcache.replace("key", "oldValue", "newValue"));
verify(this.store).replace(eq("key"), eq("oldValue"), eq("newValue"));
verifyZeroInteractions(this.spiedResilienceStrategy);
assertThat(fakeStore.getEntryMap().containsKey("key"), is(false));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.ReplaceOutcome.MISS_NOT_PRESENT));
}
/**
* Tests the effect of a {@link Ehcache#replace(Object, Object, Object)} for
* <ul>
* <li>key with unequal value in {@code Store}</li>
* </ul>
*/
@Test
public void testReplaceValueUnequalStoreEntry() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "unequalValue"));
this.store = spy(fakeStore);
final Ehcache<String, String> ehcache = this.getEhcache();
assertFalse(ehcache.replace("key", "oldValue", "newValue"));
verify(this.store).replace(eq("key"), eq("oldValue"), eq("newValue"));
verifyZeroInteractions(this.spiedResilienceStrategy);
assertThat(fakeStore.getEntryMap().get("key"), is(equalTo("unequalValue")));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.ReplaceOutcome.MISS_PRESENT));
}
/**
* Tests the effect of a {@link Ehcache#replace(Object, Object, Object)} for
* <ul>
* <li>key with equal value in {@code Store}</li>
* </ul>
*/
@Test
public void testReplaceValueEqualStoreEntry() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "oldValue"));
this.store = spy(fakeStore);
final Ehcache<String, String> ehcache = this.getEhcache();
assertTrue(ehcache.replace("key", "oldValue", "newValue"));
verify(this.store).replace(eq("key"), eq("oldValue"), eq("newValue"));
verifyZeroInteractions(this.spiedResilienceStrategy);
assertThat(fakeStore.getEntryMap().get("key"), is(equalTo("newValue")));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.ReplaceOutcome.HIT));
}
/**
* Tests the effect of a {@link Ehcache#replace(Object, Object, Object)} for
* <ul>
* <li>key not present in {@code Store}</li>
* <li>>{@code Store.replace} throws</li>
* </ul>
*/
@Test
public void testReplaceValueNoStoreEntryStoreAccessException() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.<String, String>emptyMap());
this.store = spy(fakeStore);
doThrow(new StoreAccessException("")).when(this.store).replace(eq("key"), eq("oldValue"), eq("newValue"));
final Ehcache<String, String> ehcache = this.getEhcache();
ehcache.replace("key", "oldValue", "newValue");
verify(this.store).replace(eq("key"), eq("oldValue"), eq("newValue"));
verify(this.spiedResilienceStrategy)
.replaceFailure(eq("key"), eq("oldValue"), eq("newValue"), any(StoreAccessException.class), eq(false));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.ReplaceOutcome.FAILURE));
}
/**
* Tests the effect of a {@link Ehcache#replace(Object, Object, Object)} for
* <ul>
* <li>key with unequal value present in {@code Store}</li>
* <li>>{@code Store.replace} throws</li>
* </ul>
*/
@Test
public void testReplaceValueUnequalStoreEntryStoreAccessException() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "unequalValue"));
this.store = spy(fakeStore);
doThrow(new StoreAccessException("")).when(this.store).replace(eq("key"), eq("oldValue"), eq("newValue"));
final Ehcache<String, String> ehcache = this.getEhcache();
ehcache.replace("key", "oldValue", "newValue");
verify(this.store).replace(eq("key"), eq("oldValue"), eq("newValue"));
verify(this.spiedResilienceStrategy)
.replaceFailure(eq("key"), eq("oldValue"), eq("newValue"), any(StoreAccessException.class), eq(false));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.ReplaceOutcome.FAILURE));
}
/**
* Tests the effect of a {@link Ehcache#replace(Object, Object, Object)} for
* <ul>
* <li>key with equal value present in {@code Store}</li>
* <li>>{@code Store.replace} throws</li>
* </ul>
*/
@Test
public void testReplaceValueEqualStoreEntryStoreAccessException() throws Exception {
final FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "oldValue"));
this.store = spy(fakeStore);
doThrow(new StoreAccessException("")).when(this.store).replace(eq("key"), eq("oldValue"), eq("newValue"));
final Ehcache<String, String> ehcache = this.getEhcache();
ehcache.replace("key", "oldValue", "newValue");
verify(this.store).replace(eq("key"), eq("oldValue"), eq("newValue"));
verify(this.spiedResilienceStrategy)
.replaceFailure(eq("key"), eq("oldValue"), eq("newValue"), any(StoreAccessException.class), eq(false));
validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.ReplaceOutcome.FAILURE));
}
/**
* Gets an initialized {@link Ehcache Ehcache} instance
*
* @return a new {@code Ehcache} instance
*/
private Ehcache<String, String> getEhcache() {
final Ehcache<String, String> ehcache = new Ehcache<String, String>(CACHE_CONFIGURATION, this.store, cacheEventDispatcher, LoggerFactory.getLogger(Ehcache.class + "-" + "EhcacheBasicReplaceValueTest"));
ehcache.init();
assertThat("cache not initialized", ehcache.getStatus(), CoreMatchers.is(Status.AVAILABLE));
this.spiedResilienceStrategy = this.setResilienceStrategySpy(ehcache);
return ehcache;
}
}