Implemented a new cache with on disk storage and auto-update.

Change-Id: I41a35e9b02af4b905ff724cd13e4795f5c409d81
diff --git a/src/test/java/de/ids_mannheim/korap/cache/TestCache.java b/src/test/java/de/ids_mannheim/korap/cache/TestCache.java
index 06a882d..857765a 100644
--- a/src/test/java/de/ids_mannheim/korap/cache/TestCache.java
+++ b/src/test/java/de/ids_mannheim/korap/cache/TestCache.java
@@ -1,18 +1,18 @@
 package de.ids_mannheim.korap.cache;
 
+import static org.junit.Assert.assertEquals;
+
 import java.util.Collections;
 
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
 import net.sf.jsr107cache.Cache;
 import net.sf.jsr107cache.CacheException;
 import net.sf.jsr107cache.CacheFactory;
 import net.sf.jsr107cache.CacheManager;
 
-import static org.junit.Assert.*;
-import org.junit.Test;
-import org.junit.Ignore;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
-
 @RunWith(JUnit4.class)
 public class TestCache {
 
diff --git a/src/test/java/de/ids_mannheim/korap/cache/TestVirtualCorpusCache.java b/src/test/java/de/ids_mannheim/korap/cache/TestVirtualCorpusCache.java
new file mode 100644
index 0000000..bd65ab2
--- /dev/null
+++ b/src/test/java/de/ids_mannheim/korap/cache/TestVirtualCorpusCache.java
@@ -0,0 +1,130 @@
+package de.ids_mannheim.korap.cache;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+
+import de.ids_mannheim.korap.Krill;
+import de.ids_mannheim.korap.KrillIndex;
+import de.ids_mannheim.korap.collection.DocBits;
+import de.ids_mannheim.korap.response.Result;
+import de.ids_mannheim.korap.util.QueryException;
+
+public class TestVirtualCorpusCache {
+
+    private KrillIndex ki;
+    private String queryRefJson;
+
+    public TestVirtualCorpusCache () throws IOException {
+        ki = createIndex();
+
+        String file = "/queries/collections/vc-ref/query-with-vc-ref.jsonld";
+        InputStream is = getClass().getResourceAsStream(file);
+        queryRefJson = IOUtils.toString(is, "utf-8");
+    }
+
+
+    private KrillIndex createIndex () throws IOException {
+        KrillIndex ki = new KrillIndex();
+        String[] docIds = new String[] { "00001", "00002", "00003" };
+        int uid = 1;
+        for (String i : docIds) {
+            ki.addDoc(uid++,
+                    getClass().getResourceAsStream("/wiki/" + i + ".json.gz"),
+                    true);
+        }
+        ki.commit();
+
+        ki.addDoc(uid++, getClass().getResourceAsStream("/wiki/00004.json.gz"),
+                true);
+        ki.commit();
+        return ki;
+    }
+
+
+    @Test
+    public void testStoreUncachedVC () throws IOException, QueryException {
+        String vcId = "named-vc4";
+
+        File f = new File(VirtualCorpusCache.CACHE_LOCATION + "/" + vcId);
+        assertFalse(f.exists());
+
+        VirtualCorpusCache.store(vcId, ki);
+        assertTrue(VirtualCorpusCache.contains(vcId));
+
+        Map<String, DocBits> docIdMap = VirtualCorpusCache.retrieve(vcId);
+        assertEquals(2, docIdMap.size());
+
+        VirtualCorpusCache.reset();
+    }
+
+
+    @Test
+    public void testReferToUncachedVC () throws IOException, QueryException {
+        String vcId = "named-vc1";
+        assertFalse(VirtualCorpusCache.contains(vcId));
+
+        Krill krill = new Krill(queryRefJson);
+        Result result = krill.apply(ki);
+        assertEquals(27, result.getTotalResults());
+
+        assertTrue(VirtualCorpusCache.contains(vcId));
+        Map<String, DocBits> vc1 = VirtualCorpusCache.retrieve(vcId);
+        assertNotNull(vc1);
+
+        VirtualCorpusCache.reset();
+    }
+
+
+    @Test
+    public void testUpdateCachedVC () throws IOException {
+        // VC cache will be marked for cleaning up 
+        // because of storing a new VC
+        KrillIndex ki = createIndex();
+        Krill krill = new Krill(queryRefJson);
+        Result result = krill.apply(ki);
+        assertEquals(27, result.getTotalResults());
+
+        assertEquals(2,
+                VirtualCorpusCache.map.get("named-vc1").keySet().size());
+
+        ki.delDoc(2);
+        ki.commit();
+
+        // VC cache will be marked for cleaning up again
+        // because of index change.
+        krill = new Krill(queryRefJson);
+        result = krill.apply(ki);
+        assertEquals(17, result.getTotalResults());
+
+        // The old leaf fingerprint should be cleaned up, thus the map 
+        // should have the same size. But the fingerprints should be 
+        // different from before the 1st cleaning up
+        assertEquals(2,
+                VirtualCorpusCache.map.get("named-vc1").keySet().size());
+
+        // VC cache will be cleaned up for the 2nd time 
+        // resulting the same leaf-fingerprints
+        krill = new Krill(queryRefJson);
+        result = krill.apply(ki);
+        assertEquals(17, result.getTotalResults());
+
+        assertEquals(2,
+                VirtualCorpusCache.map.get("named-vc1").keySet().size());
+
+        ki.close();
+
+        VirtualCorpusCache.reset();
+    }
+
+
+}
diff --git a/src/test/java/de/ids_mannheim/korap/collection/TestCollectionBuilder.java b/src/test/java/de/ids_mannheim/korap/collection/TestCollectionBuilder.java
index 2ee8cb1..9c500f1 100644
--- a/src/test/java/de/ids_mannheim/korap/collection/TestCollectionBuilder.java
+++ b/src/test/java/de/ids_mannheim/korap/collection/TestCollectionBuilder.java
@@ -97,10 +97,8 @@
     public void builderReferenceNested () throws IOException {
         CollectionBuilder kc = new CollectionBuilder();
 
-        // The group can't stringify, because the filtering
-        // phase won't work. This is acceptable.
         assertEquals(
-            "",
+            "OrGroup(VirtualCorpusReferenceFilter(example) opennlp:check)",
             kc.orGroup().with(
                 kc.referTo("example")
                 ).with(
diff --git a/src/test/java/de/ids_mannheim/korap/collection/TestCollectionCache.java b/src/test/java/de/ids_mannheim/korap/collection/TestCollectionCache.java
deleted file mode 100644
index ee3fef4..0000000
--- a/src/test/java/de/ids_mannheim/korap/collection/TestCollectionCache.java
+++ /dev/null
@@ -1,29 +0,0 @@
-package de.ids_mannheim.korap.collection;
-
-import static org.junit.Assert.assertTrue;
-
-import java.io.IOException;
-
-import org.junit.Test;
-
-import de.ids_mannheim.korap.KrillCollection;
-import de.ids_mannheim.korap.KrillIndex;
-import de.ids_mannheim.korap.index.FieldDocument;
-import net.sf.ehcache.Cache;
-
-public class TestCollectionCache {
-
-    @Test
-    public void testNullCache() throws IOException{
-        KrillCollection kc = new KrillCollection();
-        Cache temp = KrillCollection.cache;
-        assertTrue(KrillCollection.cache != null);
-        
-        KrillCollection.cache = null;
-        KrillIndex ki = new KrillIndex();
-        ki.addDoc(new FieldDocument());
-        ki.commit();
-        
-        KrillCollection.cache = temp;
-    }
-}
diff --git a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionIndex.java b/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionIndex.java
index 69e8d9f..7e80ba2 100644
--- a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionIndex.java
@@ -2,17 +2,13 @@
 
 import static de.ids_mannheim.korap.TestSimple.getJsonString;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
-import java.util.Properties;
 
 import org.apache.lucene.index.Term;
 import org.apache.lucene.search.spans.SpanQuery;
 import org.apache.lucene.search.spans.SpanTermQuery;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
@@ -24,10 +20,7 @@
 import de.ids_mannheim.korap.query.QueryBuilder;
 import de.ids_mannheim.korap.response.Result;
 import de.ids_mannheim.korap.response.SearchContext;
-import de.ids_mannheim.korap.util.KrillProperties;
-import de.ids_mannheim.korap.util.QueryException;
 import de.ids_mannheim.korap.util.StatusCodes;
-import net.sf.ehcache.Element;
 
 
 @RunWith(JUnit4.class)
@@ -621,13 +614,12 @@
 
 		// This test was adopted from TestVCCaching,
 		// But does not fail anymore for deserialization
-        String json = _getJSONString("unknown-vc-ref.jsonld");
+        String json = _getJSONString("vc-ref/unknown-vc-ref.jsonld");
 
         KrillCollection kc = new KrillCollection(json);
 		assertEquals("referTo(https://korap.ids-mannheim.de/@ndiewald/MyCorpus)", kc.getBuilder().toString());
 
-		// Fails on filtering
-		assertEquals("", kc.toString());
+		assertEquals("VirtualCorpusReferenceFilter(https://korap.ids-mannheim.de/@ndiewald/MyCorpus)",kc.toString());
 		
         QueryBuilder kq = new QueryBuilder("field");
 		
@@ -637,433 +629,10 @@
 		Result result = krill.apply(ki);
 
 		assertEquals(StatusCodes.MISSING_COLLECTION, result.getError(0).getCode());
+		assertTrue(result.getError(0).getMessage().startsWith("Collection is not found"));
 	};
 
     @Test
-    @Ignore
-    public void testCache () throws IOException {
-
-		Properties prop = KrillProperties.loadDefaultProperties();
-
-		String vcPath = getClass().getResource(path + "named-vcs").getFile();
-		String tempVC = prop.getProperty("krill.namedVC");
-		prop.setProperty("krill.namedVC", vcPath);
-
-		ki = new KrillIndex();
-		ki.addDoc(createDoc1());
-		ki.addDoc(createDoc2());
-		ki.commit();
-		
-        testManualAddToCache(ki, "named-vcs/named-vc1.jsonld", "named-vc1");
-        testManualAddToCache(ki, "named-vcs/named-vc2.jsonld", "named-vc2");
-        
-        Element element = KrillCollection.cache.get("named-vc1");
-        CachedVCData cc = (CachedVCData) element.getObjectValue();
-        assertTrue(cc.getDocIdMap().size() > 0);
-
-        element = KrillCollection.cache.get("named-vc2");
-        cc = (CachedVCData) element.getObjectValue();
-        assertTrue(cc.getDocIdMap().size() > 0);
-
-		// Check for cache location
-        assertFalse(KrillCollection.cache.isElementInMemory("named-vc1"));
-        assertTrue(KrillCollection.cache.isElementOnDisk("named-vc1"));
-        assertTrue(KrillCollection.cache.isElementInMemory("named-vc2"));
-        assertTrue(KrillCollection.cache.isElementOnDisk("named-vc2"));
-
-        // testSearchCachedVC();
-		String json = _getJSONString("query-with-vc-ref.jsonld");
-		// references named-vc1: ID eq ["doc-2","doc-3"]
-
-		Krill krill = new Krill(json);
-		// TODO: Better keep the reference
-		testManualAddToCache(ki, "named-vcs/named-vc1.jsonld", "named-vc1");
-		assertEquals("referTo(cached:named-vc1)", krill.getCollection().toString());
-
-		Result result = krill.apply(ki);
-		assertEquals("[[a]] c d", result.getMatch(0).getSnippetBrackets());
-		assertEquals(result.getMatch(0).getUID(), 2);
-		assertEquals(result.getMatches().size(), 1);
-		
-        // testAddDocToIndex();
-		ki.addDoc(createDoc3());
-		ki.commit();
-
-		// Cache is removed after index change
-        element = KrillCollection.cache.get("named-vc1");
-        assertNull(element);
-
-		// Restart search - this time it's not precached
-        krill = new Krill(json);
-		assertEquals("referTo(named-vc1)", krill.getCollection().toString());
-		result = krill.apply(ki);
-
-		assertEquals("[[a]] c d", result.getMatch(0).getSnippetBrackets());
-		assertEquals("[[a]] d e", result.getMatch(1).getSnippetBrackets());
-		assertEquals(result.getMatches().size(), 2);
-
-		// testAutoCachingMatch
-		// Check autocache
-		element = KrillCollection.cache.get("named-vc1");
-		cc = (CachedVCData) element.getObjectValue();
-        assertTrue(cc.getDocIdMap().size() > 0);
-		
-		// Because of autocaching, this should work now
-        krill = new Krill(json);
-		assertEquals("referTo(cached:named-vc1)", krill.getCollection().toString());
-		result = krill.apply(ki);
-		assertEquals("[[a]] c d", result.getMatch(0).getSnippetBrackets());
-		assertEquals("[[a]] d e", result.getMatch(1).getSnippetBrackets());
-		assertEquals(result.getMatches().size(), 2);
-
-		// Cache is removed on deletion
-		ki.addDoc(createDoc1());
-		ki.commit();
-
-		// Check cache
-		element = KrillCollection.cache.get("named-vc1");
-        assertNull(element);
-
-		// Rerun query
-        krill = new Krill(json);
-		assertEquals("referTo(named-vc1)", krill.getCollection().toString());
-		result = krill.apply(ki);
-		assertEquals("[[a]] c d", result.getMatch(0).getSnippetBrackets());
-		assertEquals("[[a]] d e", result.getMatch(1).getSnippetBrackets());
-		assertEquals(result.getMatches().size(), 2);
-		
-		// testClearCache
-		KrillCollection.cache.removeAll();
-
-        element = KrillCollection.cache.get("named-vc1");
-        assertNull(element);
-
-		prop.setProperty("krill.namedVC", tempVC);
-    };
-
-	@Test
-	@Ignore
-    public void testNestedNamedVCs () throws IOException {
-	    KrillCollection.initializeCache();
-	    
-        Properties prop = KrillProperties.loadDefaultProperties();
-
-        String vcPath = getClass().getResource(path + "named-vcs").getFile();
-        String tempVC = prop.getProperty("krill.namedVC");
-        prop.setProperty("krill.namedVC", vcPath);
-        
-        ki = new KrillIndex();
-        ki.addDoc(createDoc1());
-        ki.addDoc(createDoc2());
-        ki.addDoc(createDoc3());
-        ki.commit();
-
-        // Check cache
-        Element element = KrillCollection.cache.get("named-vc1");
-        assertNull(element);
-
-        element = KrillCollection.cache.get("named-vc2");
-        assertNull(element);
-
-        QueryBuilder kq = new QueryBuilder("tokens");
-        KrillCollection kc = new KrillCollection(ki);
-        CollectionBuilder cb = kc.build();
-        Krill krill = new Krill(kq.seg("i:a"));
-
-        kc.fromBuilder(
-            cb.orGroup().with(
-                cb.referTo("named-vc1")
-                ).with(
-                    cb.referTo("named-vc2")
-                    )
-            );
-        krill.setCollection(kc);
-        // named-vc1: UID:[2,3]
-        // named-vc2: author:Frank (doc-1)
-
-        assertEquals("OrGroup(referTo(named-vc1) referTo(named-vc2))",
-                     krill.getCollection().toString());
-
-        assertEquals("tokens:i:a", krill.getSpanQuery().toString());
-        
-        Result result = krill.apply(ki);
-        assertEquals("[[a]] b c", result.getMatch(0).getSnippetBrackets());
-        assertEquals("[[a]] c d", result.getMatch(1).getSnippetBrackets());
-        assertEquals("[[a]] d e", result.getMatch(2).getSnippetBrackets());
-        assertEquals(3, result.getMatches().size());
-
-        element = KrillCollection.cache.get("named-vc2");
-        CachedVCData cc = (CachedVCData) element.getObjectValue();
-        assertTrue(cc.getDocIdMap().size() > 0);
-        
-        kc.fromBuilder(
-            cb.orGroup().with(
-                cb.referTo("named-vc1")
-                ).with(
-                    cb.referTo("named-vc2")
-                    )
-            );
-        
-        assertEquals("OrGroup(referTo(cached:named-vc1) referTo(cached:named-vc2))",
-                     krill.getCollection().toString());
-
-        result = krill.apply(ki);
-        assertEquals("[[a]] b c", result.getMatch(0).getSnippetBrackets());
-        assertEquals("[[a]] c d", result.getMatch(1).getSnippetBrackets());
-        assertEquals("[[a]] d e", result.getMatch(2).getSnippetBrackets());
-        assertEquals(3, result.getMatches().size());
-
-        kc.fromBuilder(
-            cb.orGroup().with(
-                cb.referTo("named-vc1")
-                ).with(
-                    cb.referTo("named-vc2")
-                    )
-            );
-        
-        assertEquals("OrGroup(referTo(cached:named-vc1) referTo(cached:named-vc2))",
-                     krill.getCollection().toString());
-
-        result = krill.apply(ki);
-        assertEquals("[[a]] b c", result.getMatch(0).getSnippetBrackets());
-        assertEquals("[[a]] c d", result.getMatch(1).getSnippetBrackets());
-        assertEquals("[[a]] d e", result.getMatch(2).getSnippetBrackets());
-        assertEquals(3, result.getMatches().size());
-
-        kc.fromBuilder(cb.referTo("named-vc1"));
-        
-        assertEquals("referTo(cached:named-vc1)",
-                     krill.getCollection().toString());
-
-        result = krill.apply(ki);
-        assertEquals("[[a]] c d", result.getMatch(0).getSnippetBrackets());
-        assertEquals("[[a]] d e", result.getMatch(1).getSnippetBrackets());
-        assertEquals(2, result.getMatches().size());
-
-
-        kc.fromBuilder(cb.referTo("named-vc2"));
-        
-        assertEquals("referTo(cached:named-vc2)",
-                     krill.getCollection().toString());
-
-        result = krill.apply(ki);
-        assertEquals("[[a]] b c", result.getMatch(0).getSnippetBrackets());
-        assertEquals(1, result.getMatches().size());
-        
-        prop.setProperty("krill.namedVC", tempVC);
-    };
-
-
-	@Test
-	@Ignore
-    public void testNamedVCsAfterQueryWithMissingDocs () throws IOException {
-	    KrillCollection.initializeCache();
-        Properties prop = KrillProperties.loadDefaultProperties();
-
-        String vcPath = getClass().getResource(path + "named-vcs").getFile();
-        String tempVC = prop.getProperty("krill.namedVC");
-        prop.setProperty("krill.namedVC", vcPath);
-        
-        ki = new KrillIndex();
-        ki.addDoc(createDoc1());
-        ki.commit();
-        ki.addDoc(createDoc2());
-        ki.commit();
-        ki.addDoc(createDoc3());
-        ki.commit();
-
-        // Check cache
-        Element element = KrillCollection.cache.get("named-vc1");
-        assertNull(element);
-
-        element = KrillCollection.cache.get("named-vc2");
-        assertNull(element);
-
-        QueryBuilder kq = new QueryBuilder("tokens");
-        KrillCollection kc = new KrillCollection(ki);
-        CollectionBuilder cb = kc.build();
-
-        // Check only for c and cache
-        Krill krill = new Krill(kq.seg("i:c"));
-
-        kc.fromBuilder(
-            cb.orGroup().with(
-                cb.referTo("named-vc1")
-                ).with(
-                    cb.referTo("named-vc2")
-                    )
-            );
-        krill.setCollection(kc);
-        // named-vc1: UID:[2,3]
-        // named-vc2: author:Frank (doc-1)
-
-        assertEquals("OrGroup(referTo(named-vc1) referTo(named-vc2))",
-                     krill.getCollection().toString());
-
-        assertEquals("tokens:i:c", krill.getSpanQuery().toString());
-        
-        Result result = krill.apply(ki);
-        assertEquals("a b [[c]]", result.getMatch(0).getSnippetBrackets());
-        assertEquals("a [[c]] d", result.getMatch(1).getSnippetBrackets());
-        assertEquals(2, result.getMatches().size());
-
-        element = KrillCollection.cache.get("named-vc2");
-        CachedVCData cc = (CachedVCData) element.getObjectValue();
-        assertTrue(cc.getDocIdMap().size() > 0);
-        
-        kc.fromBuilder(
-            cb.orGroup().with(
-                cb.referTo("named-vc1")
-                ).with(
-                    cb.referTo("named-vc2")
-                    )
-            );
-        
-        assertEquals("OrGroup(referTo(cached:named-vc1) referTo(cached:named-vc2))",
-                     krill.getCollection().toString());
-
-        // Check again for c with cache
-        result = krill.apply(ki);
-        assertEquals("a b [[c]]", result.getMatch(0).getSnippetBrackets());
-        assertEquals("a [[c]] d", result.getMatch(1).getSnippetBrackets());
-        assertEquals(2, result.getMatches().size());
-
-        // Check for a with cache
-        krill = new Krill(kq.seg("i:a"));
-        krill.setCollection(kc);
-
-        assertEquals("OrGroup(referTo(cached:named-vc1) referTo(cached:named-vc2))",
-                     krill.getCollection().toString());
-
-        // Check again for c with cache
-        result = krill.apply(ki);
-        assertEquals("[[a]] b c", result.getMatch(0).getSnippetBrackets());
-        assertEquals("[[a]] c d", result.getMatch(1).getSnippetBrackets());
-        assertEquals("[[a]] d e", result.getMatch(2).getSnippetBrackets());
-        assertEquals(3, result.getMatches().size());
-                
-        prop.setProperty("krill.namedVC", tempVC);
-    };
-
-
-	@Ignore
-    public void testNamedVCsAfterCorpusWithMissingDocs () throws IOException {
-        Properties prop = KrillProperties.loadDefaultProperties();
-
-        String vcPath = getClass().getResource(path + "named-vcs").getFile();
-        String tempVC = prop.getProperty("krill.namedVC");
-        prop.setProperty("krill.namedVC", vcPath);
-        
-        ki = new KrillIndex();
-        ki.addDoc(createDoc1());
-        ki.commit();
-        ki.addDoc(createDoc2());
-        ki.commit();
-        ki.addDoc(createDoc3());
-        ki.commit();
-
-        // Check cache
-        Element element = KrillCollection.cache.get("named-vc1");
-        assertNull(element);
-
-        element = KrillCollection.cache.get("named-vc2");
-        assertNull(element);
-
-        QueryBuilder kq = new QueryBuilder("tokens");
-        KrillCollection kc = new KrillCollection(ki);
-        CollectionBuilder cb = kc.build();
-
-        // Check only for c and cache
-        Krill krill = new Krill(kq.seg("i:a"));
-
-        kc.fromBuilder(
-            cb.andGroup().with(
-                cb.term("textClass","kultur")
-                ).with(
-                    cb.orGroup().with(
-                        cb.referTo("named-vc1")
-                        ).with(
-                            cb.referTo("named-vc2")
-                            )
-                    )
-            );
-        krill.setCollection(kc);
-        // named-vc1: UID:[2,3]
-        // named-vc2: author:Frank (doc-1)
-        // textClass:kultur (doc-1,doc-2)
-
-        assertEquals(
-            "AndGroup(textClass:kultur OrGroup(referTo(named-vc1) referTo(named-vc2)))",
-            krill.getCollection().toString());
-        
-        assertEquals("tokens:i:a", krill.getSpanQuery().toString());
-        
-        Result result = krill.apply(ki);
-        assertEquals("[[a]] b c", result.getMatch(0).getSnippetBrackets());
-        assertEquals("[[a]] c d", result.getMatch(1).getSnippetBrackets());
-        assertEquals(2, result.getMatches().size());
-
-        element = KrillCollection.cache.get("named-vc1");
-        CachedVCData cc = (CachedVCData) element.getObjectValue();
-        assertTrue(cc.getDocIdMap().size() > 0);
-
-        element = KrillCollection.cache.get("named-vc2");
-        cc = (CachedVCData) element.getObjectValue();
-        assertTrue(cc.getDocIdMap().size() > 0);
-
-        kc.fromBuilder(
-            cb.orGroup().with(
-                cb.referTo("named-vc1")
-                ).with(
-                    cb.referTo("named-vc2")
-                    )
-            );
-        
-        assertEquals("OrGroup(referTo(cached:named-vc1) referTo(cached:named-vc2))",
-                     krill.getCollection().toString());
-
-        // Check again for c with cache
-        result = krill.apply(ki);
-		assertEquals("[[a]] b c", result.getMatch(0).getSnippetBrackets());
-        assertEquals("[[a]] c d", result.getMatch(1).getSnippetBrackets());
-        assertEquals("[[a]] d e", result.getMatch(2).getSnippetBrackets());
-        assertEquals(3, result.getMatches().size());
-
-        prop.setProperty("krill.namedVC", tempVC);
-    };
-    
-    @Test
-    public void testCollectionWithVCRefAndPubDate () throws IOException {
-
-        KrillCollection.initializeCache();
-
-        ki = new KrillIndex();
-        ki.addDoc(createDoc2());
-        ki.addDoc(createDoc3());
-        ki.addDoc(createDoc5000());
-        ki.commit();
-
-        testManualAddToCache(ki, "named-vcs/named-vc3.jsonld", "named-vc3");
-
-        Element element = KrillCollection.cache.get("named-vc3");
-        CachedVCData cc = (CachedVCData) element.getObjectValue();
-        assertTrue(cc.getDocIdMap().size() > 0);
-            
-        String json = _getJSONString("collection-with-vc-ref-and-pubDate.jsonld");
-
-        KrillCollection kc = new KrillCollection(json);
-        kc.setIndex(ki);
-        assertEquals(2, kc.numberOf("documents"));
-        
-        // testAddDocToIndex();
-        ki.addDoc(createDoc1());
-        ki.commit();
-        // Cache is removed after index change
-
-    }
-    
-
-    @Test
     public void filterExampleFromLegacy () throws Exception {
 
         // Construct index
@@ -1654,20 +1223,6 @@
         return fd;
     };
 
-    
-    private void testManualAddToCache (KrillIndex index, String filename, String vcName) throws IOException {
-		String json = _getJSONString(filename);
-
-        KrillCollection kc = new KrillCollection(json);
-        kc.setIndex(index);
-		try {
-			kc.storeInCache(vcName);
-		}
-		catch (QueryException qe) {
-			System.err.println(qe.getLocalizedMessage());
-		};
-    };
-
     private String _getJSONString (String file) {
         return getJsonString(getClass().getResource(path + file).getFile());
     };
diff --git a/src/test/resources/krill.properties b/src/test/resources/krill.properties
index ea60670..eb7f374 100644
--- a/src/test/resources/krill.properties
+++ b/src/test/resources/krill.properties
@@ -2,4 +2,6 @@
 krill.name = ${project.name}
 krill.indexDir = test-output
 krill.namedVC = vc
-krill.index.commit.count = 15
\ No newline at end of file
+krill.index.commit.count = 15
+
+krill.namedVC=src/test/resources/queries/collections/named-vcs/
\ No newline at end of file
diff --git a/src/test/resources/queries/collections/named-vcs/named-vc4.jsonld b/src/test/resources/queries/collections/named-vcs/named-vc4.jsonld
new file mode 100644
index 0000000..7f19bb0
--- /dev/null
+++ b/src/test/resources/queries/collections/named-vcs/named-vc4.jsonld
@@ -0,0 +1,11 @@
+{"collection": {
+    "@type": "koral:doc",
+    "key": "textSigle",
+    "match": "match:eq",
+    "type" : "type:string",
+    "value": [
+          "WPD/AAA/00001",
+          "WPD/AAA/00002",
+          "WPD/AAA/00003"
+    ]
+}}
diff --git a/src/test/resources/queries/collections/query-with-vc-ref.jsonld b/src/test/resources/queries/collections/vc-ref/query-with-vc-ref.jsonld
similarity index 91%
rename from src/test/resources/queries/collections/query-with-vc-ref.jsonld
rename to src/test/resources/queries/collections/vc-ref/query-with-vc-ref.jsonld
index 86547ac..40d2c2c 100644
--- a/src/test/resources/queries/collections/query-with-vc-ref.jsonld
+++ b/src/test/resources/queries/collections/vc-ref/query-with-vc-ref.jsonld
@@ -3,7 +3,7 @@
     "wrap":{
       "@type":"koral:term",
       "layer":"orth",
-      "key":"a",
+      "key":"der",
       "match":"match:eq"
     }
   },
diff --git a/src/test/resources/queries/collections/unknown-vc-ref.jsonld b/src/test/resources/queries/collections/vc-ref/unknown-vc-ref.jsonld
similarity index 100%
rename from src/test/resources/queries/collections/unknown-vc-ref.jsonld
rename to src/test/resources/queries/collections/vc-ref/unknown-vc-ref.jsonld