From b22fd149918bd087a7ebfedfe5ad5f1f38418092 Mon Sep 17 00:00:00 2001
From: Vikas Saurabh <vsaurabh@adobe.com>
Date: Wed, 7 Oct 2015 16:46:10 +0530
Subject: [PATCH] OAK-3494 Test case

---
 .../oak/plugins/document/JournalEntryTest.java     | 45 ++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/JournalEntryTest.java b/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/JournalEntryTest.java
index 1be71fa..2c50194 100644
--- a/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/JournalEntryTest.java
+++ b/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/JournalEntryTest.java
@@ -21,6 +21,7 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Random;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
@@ -30,11 +31,14 @@ import org.apache.jackrabbit.oak.commons.json.JsopReader;
 import org.apache.jackrabbit.oak.commons.json.JsopTokenizer;
 import org.apache.jackrabbit.oak.commons.sort.StringSort;
 import org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore;
+import org.junit.Ignore;
 import org.junit.Test;
 
 import static org.apache.jackrabbit.oak.plugins.document.Collection.JOURNAL;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -65,6 +69,27 @@ public class JournalEntryTest {
         sort.close();
     }
 
+    //OAK-3494
+    @Ignore("OAK-3494")
+    @Test
+    public void useParentDiff() throws Exception {
+        DiffCache cache = new MemoryDiffCache(new DocumentMK.Builder());
+        List<String> paths = Lists.newArrayList("/content/changed", "/content/changed1/child1");
+        StringSort sort = JournalEntry.newSorter();
+        add(sort, paths);
+        Revision from = new Revision(1, 0, 1);
+        Revision to = new Revision(2, 0, 1);
+        Revision unjournalled = new Revision(3, 0, 1);
+        sort.sort();
+        JournalEntry.applyTo(sort, cache, from, to);
+
+        validateCacheUsage(cache, from, to, "/topUnchanged", true);
+        validateCacheUsage(cache, from, to, "/content/changed/unchangedLeaf", true);
+        validateCacheUsage(cache, from, to, "/content/changed1/child2", true);
+
+        validateCacheUsage(cache, from, unjournalled, "/unjournalledPath", false);
+    }
+
     @Test
     public void fillExternalChanges() throws Exception {
         DocumentStore store = new MemoryDocumentStore();
@@ -146,4 +171,24 @@ public class JournalEntryTest {
             }
         }
     }
+
+    private void validateCacheUsage(DiffCache cache, Revision from, Revision to, String path, boolean cacheExpected) {
+        String nonLoaderDiff = cache.getChanges(from, to, path, null);
+        final AtomicBoolean loaderCalled = new AtomicBoolean(false);
+        cache.getChanges(from, to, path, new DiffCache.Loader() {
+            @Override
+            public String call() {
+                loaderCalled.set(true);
+                return "";
+            }
+        });
+
+        if (cacheExpected) {
+            assertNotNull(nonLoaderDiff);
+            assertFalse(loaderCalled.get());
+        } else {
+            assertNull(nonLoaderDiff);
+            assertTrue(loaderCalled.get());
+        }
+    }
 }
-- 
2.1.4

