-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: an explicit test of N5CacheTest
- Loading branch information
Showing
1 changed file
with
121 additions
and
0 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
src/test/java/org/janelia/saalfeldlab/n5/cache/N5CacheTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package org.janelia.saalfeldlab.n5.cache; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
import com.google.gson.JsonElement; | ||
|
||
public class N5CacheTest { | ||
|
||
@Test | ||
public void cacheBackingTest() { | ||
|
||
final DummyBackingStorage backingStorage = new DummyBackingStorage(); | ||
final N5JsonCache cache = new N5JsonCache(backingStorage::backingAttrs, backingStorage::exists, | ||
backingStorage::isGroup, backingStorage::isDataset, backingStorage::list); | ||
|
||
// check existance, ensure backing storage is only called once | ||
assertEquals(0, backingStorage.existsCallCount); | ||
cache.exists("a"); | ||
assertEquals(1, backingStorage.existsCallCount); | ||
cache.exists("a"); | ||
assertEquals(1, backingStorage.existsCallCount); | ||
|
||
// check existance of new group, ensure backing storage is only called one more time | ||
cache.exists("b"); | ||
assertEquals(2, backingStorage.existsCallCount); | ||
cache.exists("b"); | ||
assertEquals(2, backingStorage.existsCallCount); | ||
|
||
// check isDataset, ensure backing storage is only called when expected | ||
// isDataset is called by exists, so should have been called twice here | ||
assertEquals(2, backingStorage.isDatasetCallCount); | ||
cache.isDataset("a"); | ||
assertEquals(2, backingStorage.isDatasetCallCount); | ||
|
||
assertEquals(2, backingStorage.isDatasetCallCount); | ||
cache.isDataset("b"); | ||
assertEquals(2, backingStorage.isDatasetCallCount); | ||
|
||
// check isGroup, ensure backing storage is only called when expected | ||
// isGroup is called by exists, so should have been called twice here | ||
assertEquals(2, backingStorage.isGroupCallCount); | ||
cache.isDataset("a"); | ||
assertEquals(2, backingStorage.isGroupCallCount); | ||
|
||
assertEquals(2, backingStorage.isGroupCallCount); | ||
cache.isDataset("b"); | ||
assertEquals(2, backingStorage.isGroupCallCount); | ||
|
||
// similarly check list, ensure backing storage is only called when expected | ||
// list is called by exists, so should have been called twice here | ||
assertEquals(2, backingStorage.listCallCount); | ||
cache.list("a"); | ||
assertEquals(2, backingStorage.listCallCount); | ||
|
||
assertEquals(2, backingStorage.listCallCount); | ||
cache.list("b"); | ||
assertEquals(2, backingStorage.listCallCount); | ||
|
||
// finally check getAttributes | ||
// it is not called by exists (since it needs the cache key) | ||
assertEquals(0, backingStorage.attrCallCount); | ||
cache.getAttributes("a", "foo"); | ||
assertEquals(1, backingStorage.attrCallCount); | ||
cache.getAttributes("a", "foo"); | ||
assertEquals(1, backingStorage.attrCallCount); | ||
cache.getAttributes("a", "bar"); | ||
assertEquals(2, backingStorage.attrCallCount); | ||
cache.getAttributes("a", "bar"); | ||
assertEquals(2, backingStorage.attrCallCount); | ||
|
||
cache.getAttributes("b", "foo"); | ||
assertEquals(3, backingStorage.attrCallCount); | ||
cache.getAttributes("b", "foo"); | ||
assertEquals(3, backingStorage.attrCallCount); | ||
cache.getAttributes("b", "bar"); | ||
assertEquals(4, backingStorage.attrCallCount); | ||
cache.getAttributes("b", "bar"); | ||
assertEquals(4, backingStorage.attrCallCount); | ||
|
||
} | ||
|
||
protected static class DummyBackingStorage { | ||
|
||
int attrCallCount = 0; | ||
int existsCallCount = 0; | ||
int isGroupCallCount = 0; | ||
int isDatasetCallCount = 0; | ||
int listCallCount = 0; | ||
|
||
public DummyBackingStorage() { | ||
} | ||
|
||
public JsonElement backingAttrs(final String key, final String cacheKey) { | ||
attrCallCount++; | ||
return null; | ||
} | ||
|
||
public boolean exists(final String key) { | ||
existsCallCount++; | ||
return true; | ||
} | ||
|
||
public boolean isGroup(final String key) { | ||
isGroupCallCount++; | ||
return true; | ||
} | ||
|
||
public boolean isDataset(final String key) { | ||
isDatasetCallCount++; | ||
return true; | ||
} | ||
|
||
public String[] list(final String key) { | ||
listCallCount++; | ||
return new String[] { "list" }; | ||
} | ||
} | ||
|
||
} |