You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by re...@apache.org on 2015/10/01 13:51:52 UTC

svn commit: r1706218 - /jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentDiscoveryLiteServiceTest.java

Author: reschke
Date: Thu Oct  1 11:51:51 2015
New Revision: 1706218

URL: http://svn.apache.org/viewvc?rev=1706218&view=rev
Log:
OAK-3449: test case bug fix: restore default behavior after hacking the class using reflection

Modified:
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentDiscoveryLiteServiceTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentDiscoveryLiteServiceTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentDiscoveryLiteServiceTest.java?rev=1706218&r1=1706217&r2=1706218&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentDiscoveryLiteServiceTest.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentDiscoveryLiteServiceTest.java Thu Oct  1 11:51:51 2015
@@ -602,30 +602,38 @@ public class DocumentDiscoveryLiteServic
      * http://stackoverflow.com/questions/3301635/change-private-static-final-
      * field-using-java-reflection
      */
-    static void setFinalStatic(Field field, Object newValue) throws Exception {
+    static Object setFinalStatic(Field field, Object newValue) throws Exception {
         field.setAccessible(true);
 
         Field modifiersField = Field.class.getDeclaredField("modifiers");
         modifiersField.setAccessible(true);
         modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
 
+        Object prev = field.get(null);
         field.set(null, newValue);
+        return prev;
     }
 
     // subsequent tests should get a DocumentDiscoveryLiteService setup from the
     // start
     private DocumentNodeStore createNodeStore(String workingDir) throws SecurityException, Exception {
-        // ensure that we always get a fresh cluster[node]id
-        setFinalStatic(ClusterNodeInfo.class.getDeclaredField("WORKING_DIR"), workingDir);
-
-        // then create the DocumentNodeStore
-        DocumentMK mk1 = createMK(
-                0 /* to make sure the clusterNodes collection is used **/,
-                500 /* asyncDelay: background interval */);
-
-        logger.info("createNodeStore: created DocumentNodeStore with cid=" + mk1.nodeStore.getClusterId() + ", workingDir="
-                + workingDir);
-        return mk1.nodeStore;
+        Object prevWorkingDir = System.getProperty("user.dir", "");
+        try {
+            // ensure that we always get a fresh cluster[node]id
+            prevWorkingDir = setFinalStatic(ClusterNodeInfo.class.getDeclaredField("WORKING_DIR"), workingDir);
+
+            // then create the DocumentNodeStore
+            DocumentMK mk1 = createMK(
+                    0 /* to make sure the clusterNodes collection is used **/,
+                    500 /* asyncDelay: background interval */);
+
+            logger.info("createNodeStore: created DocumentNodeStore with cid=" + mk1.nodeStore.getClusterId() + ", workingDir="
+                    + workingDir);
+            return mk1.nodeStore;
+        }
+        finally {
+            setFinalStatic(ClusterNodeInfo.class.getDeclaredField("WORKING_DIR"), prevWorkingDir);
+        }
     }
 
     private SimplifiedInstance createInstance() throws Exception {