Skip to content
This repository was archived by the owner on Jul 22, 2021. It is now read-only.

Commit 7df786a

Browse files
committed
Run the DatabaseCacheTests using all cache types
Extend the DatabaseCacheTests in from the cache project in the cache-redid and cache-in-process projects so the end-to-end tests can be run with all the cache implementations.
1 parent 805e78b commit 7df786a

3 files changed

Lines changed: 79 additions & 3 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2016 IBM Corp. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
* either express or implied. See the License for the specific language governing permissions
12+
* and limitations under the License.
13+
*/
14+
15+
package com.cloudant.client.cache.tests.inprocess;
16+
17+
import com.cloudant.client.cache.inprocess.InProcessCache;
18+
import com.cloudant.client.cache.tests.DatabaseCacheTests;
19+
20+
public class DatabaseInProcessCacheTests extends DatabaseCacheTests {
21+
22+
@Override
23+
protected InProcessCache<String, Object> getNewCacheInstance() {
24+
return new InProcessCache<>(CACHE_SIZE, CACHE_LIFETIME);
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2016 IBM Corp. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
* either express or implied. See the License for the specific language governing permissions
12+
* and limitations under the License.
13+
*/
14+
15+
package com.cloudant.client.cache.tests.redis;
16+
17+
import com.cloudant.client.cache.redis.RedisCache;
18+
import com.cloudant.client.cache.tests.DatabaseCacheTests;
19+
20+
public class DatabaseRedisCacheTests extends DatabaseCacheTests {
21+
22+
@Override
23+
protected RedisCache<String, Object> getNewCacheInstance() {
24+
return new RedisCache<>("localhost", 6379, 60, CACHE_LIFETIME);
25+
}
26+
}

cloudant-client-cache/src/test/java/com/cloudant/client/cache/tests/DatabaseCacheTests.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@
3434
import org.junit.BeforeClass;
3535
import org.junit.Test;
3636

37+
import java.io.Serializable;
3738
import java.net.URL;
3839
import java.util.ArrayList;
3940
import java.util.List;
4041
import java.util.UUID;
42+
import java.util.concurrent.TimeUnit;
4143

4244
/**
4345
* This class provides tests for the DatabaseCache, validating that the cache can operate
@@ -46,6 +48,15 @@
4648
public class DatabaseCacheTests {
4749

4850
// Class resources
51+
/**
52+
* The maximum size of the cache, we only need 10 entries for the tests.
53+
*/
54+
protected static final int CACHE_SIZE = 10;
55+
/**
56+
* The default cache lifetime for an CacheWithLifetimes based caches. This time needs to be
57+
* longer than any of the individual tests, 1 minute should be more than enough.
58+
*/
59+
protected static final long CACHE_LIFETIME = TimeUnit.MINUTES.toMillis(1);
4960
private static CloudantClient client;
5061

5162
// Test instance resources
@@ -88,12 +99,21 @@ public static void shutdownClient() {
8899
*/
89100
@Before
90101
public void setupForTest() {
91-
cache = new LRUCache<>(10);
102+
cache = getNewCacheInstance();
92103
dbName = "database-cache-tests-" + UUID.randomUUID().toString();
93104
db = new DatabaseCache(client.database(dbName, true), cache);
94105
foo = new Foo(UUID.randomUUID().toString());
95106
}
96107

108+
protected Cache<String, Object> getNewCacheInstance() {
109+
return new LRUCache<>(CACHE_SIZE);
110+
}
111+
112+
@After
113+
public void clearCache() {
114+
cache.clear();
115+
}
116+
97117
@After
98118
public void deleteDatabase() {
99119
client.deleteDB(dbName);
@@ -324,7 +344,8 @@ public void testBulkCachePutWithError() {
324344

325345
// Assert that foo1 was not updated in the cache because of the error
326346
Foo foo1FromCache = (Foo) cache.get(createdFoo1._id);
327-
assertNotEquals("The cached foo1 should not be the latest foo1", foosToSave.get(0), foo1FromCache);
347+
assertNotEquals("The cached foo1 should not be the latest foo1", foosToSave.get(0),
348+
foo1FromCache);
328349
assertNull("The testField should be null", foo1FromCache.testField);
329350
}
330351

@@ -387,7 +408,10 @@ private List<Foo> generateFoos(int n) {
387408
return foos;
388409
}
389410

390-
private static final class Foo {
411+
private static final class Foo implements Serializable {
412+
413+
static final long serialVersionUID = 1l;
414+
391415
String _id;
392416
String _rev;
393417
String testField;

0 commit comments

Comments
 (0)