/*
* Copyright 2011 The Antil Open Source Project
*
* 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 antil.cache;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.core.IsNull.nullValue;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/**
* @param <T> the type of cache under test.
*/
public abstract class CacheTest<T extends Cache<String, String>> {
@Rule
public ExpectedException thrown = ExpectedException.none();
/**
* Cache under test.
*/
protected T cache;
@Before
public void setUp() throws Exception {
cache = createCacheForTesting();
}
@Test
public void clearsCacheWithManyEntries() {
cache.put("1", "a");
cache.put("2", "b");
assertThat(cache.get("1"), is(notNullValue()));
cache.clear();
assertThat(cache.get("1"), is(nullValue()));
assertThat(cache.get("2"), is(nullValue()));
}
@Test
public void clearsCacheWithOneEntry() {
cache.put("1", "a");
assertThat(cache.get("1"), is(notNullValue()));
cache.clear();
assertThat(cache.get("1"), is(nullValue()));
}
@Test
public void getThrowsExceptionOnNullKey() {
thrown.expect(NullPointerException.class);
thrown.expectMessage(exceptionMessageForNullKey());
cache.get(null);
}
@Test
public void putAssociatesKeysWithValues() {
cache.put("1", "a");
cache.put("2", "b");
assertThat(cache.get("1"), is("a"));
assertThat(cache.get("2"), is("b"));
}
@Test
public void putAssociatesKeyWithValue() {
cache.put("1", "a");
assertThat(cache.get("1"), is("a"));
}
@Test
public void putReplacesOldValueWithNew() {
cache.put("1", "a");
cache.put("1", "b");
assertThat(cache.get("1"), is("b"));
}
@Test
public void putThrowsExceptionOnNullKey() {
thrown.expect(NullPointerException.class);
thrown.expectMessage(exceptionMessageForNullKey());
cache.put(null, "a");
}
@Test
public void putThrowsExceptionOnNullValue() {
thrown.expect(NullPointerException.class);
thrown.expectMessage(exceptionMessageForNullValue());
cache.put("1", null);
}
@Test
public void removesAnEntry() {
cache.put("1", "a");
assertThat(cache.get("1"), is(notNullValue()));
cache.remove("1");
assertThat(cache.get("1"), is(nullValue()));
}
@Test
public void removeThrowsExceptioOnNullKey() {
thrown.expect(NullPointerException.class);
thrown.expectMessage(exceptionMessageForNullKey());
cache.remove(null);
}
@Test
public void removingAnAbsentKeyIsOk() {
cache.remove("1"); // No errors
}
/**
* @return a new cache for testing.
*/
protected abstract T createCacheForTesting();
/**
* @return exception message for a <code>null</code> key.
*/
protected String exceptionMessageForNullKey() {
return "Key cannot be null";
}
/**
* @return exception message for a <code>null</code> value.
*/
protected String exceptionMessageForNullValue() {
return "Value cannot be null";
}
}
|