You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@storm.apache.org by bo...@apache.org on 2016/01/30 19:47:45 UTC

[06/14] storm git commit: Added in the ability to mock Zookeeper.mkdirs

Added in the ability to mock Zookeeper.mkdirs


Project: http://git-wip-us.apache.org/repos/asf/storm/repo
Commit: http://git-wip-us.apache.org/repos/asf/storm/commit/394fefff
Tree: http://git-wip-us.apache.org/repos/asf/storm/tree/394fefff
Diff: http://git-wip-us.apache.org/repos/asf/storm/diff/394fefff

Branch: refs/heads/master
Commit: 394fefffd5753f382b3b6c60ebef8617c0d297ef
Parents: 3f1cefc
Author: Robert (Bobby) Evans <ev...@yahoo-inc.com>
Authored: Thu Jan 28 13:26:57 2016 -0600
Committer: Robert (Bobby) Evans <ev...@yahoo-inc.com>
Committed: Thu Jan 28 13:26:57 2016 -0600

----------------------------------------------------------------------
 .../testing/staticmocking/MockedZookeeper.java  | 31 ++++++++++++++++++
 .../org/apache/storm/zookeeper/Zookeeper.java   | 34 ++++++++++++++++++--
 .../test/clj/org/apache/storm/cluster_test.clj  | 18 ++++++-----
 3 files changed, 72 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/storm/blob/394fefff/storm-core/src/jvm/org/apache/storm/testing/staticmocking/MockedZookeeper.java
----------------------------------------------------------------------
diff --git a/storm-core/src/jvm/org/apache/storm/testing/staticmocking/MockedZookeeper.java b/storm-core/src/jvm/org/apache/storm/testing/staticmocking/MockedZookeeper.java
new file mode 100644
index 0000000..ca54f6f
--- /dev/null
+++ b/storm-core/src/jvm/org/apache/storm/testing/staticmocking/MockedZookeeper.java
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version
+ * 2.0 (the "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.storm.testing.staticmocking;
+
+import org.apache.storm.zookeeper.Zookeeper;
+
+public class MockedZookeeper implements AutoCloseable {
+
+    public MockedZookeeper(Zookeeper inst) {
+        Zookeeper.setInstance(inst);
+    }
+
+    @Override
+    public void close() throws Exception {
+        Zookeeper.resetInstance();
+    }
+}

http://git-wip-us.apache.org/repos/asf/storm/blob/394fefff/storm-core/src/jvm/org/apache/storm/zookeeper/Zookeeper.java
----------------------------------------------------------------------
diff --git a/storm-core/src/jvm/org/apache/storm/zookeeper/Zookeeper.java b/storm-core/src/jvm/org/apache/storm/zookeeper/Zookeeper.java
index ccbe76b..e8f306a 100644
--- a/storm-core/src/jvm/org/apache/storm/zookeeper/Zookeeper.java
+++ b/storm-core/src/jvm/org/apache/storm/zookeeper/Zookeeper.java
@@ -58,9 +58,33 @@ import java.util.Map;
 import java.util.concurrent.atomic.AtomicReference;
 
 public class Zookeeper {
-
     private static Logger LOG = LoggerFactory.getLogger(Zookeeper.class);
 
+    // A singleton instance allows us to mock delegated static methods in our
+    // tests by subclassing.
+    private static final Zookeeper INSTANCE = new Zookeeper();
+    private static Zookeeper _instance = INSTANCE;
+
+    /**
+     * Provide an instance of this class for delegates to use.  To mock out
+     * delegated methods, provide an instance of a subclass that overrides the
+     * implementation of the delegated method.
+     *
+     * @param u a Zookeeper instance
+     */
+    public static void setInstance(Zookeeper u) {
+        _instance = u;
+    }
+
+    /**
+     * Resets the singleton instance to the default. This is helpful to reset
+     * the class to its original functionality when mocking is no longer
+     * desired.
+     */
+    public static void resetInstance() {
+        _instance = INSTANCE;
+    }
+
     public static CuratorFramework mkClient(Map conf, List<String> servers, Object port, String root) {
         return mkClient(conf, servers, port, root, new DefaultWatcherCallBack());
     }
@@ -148,7 +172,11 @@ public class Zookeeper {
         }
     }
 
-    public static void mkdirs(CuratorFramework zk, String path, List<ACL> acls){
+    public static void mkdirs(CuratorFramework zk, String path, List<ACL> acls) {
+        _instance.mkdirsImpl(zk, path, acls);
+    }
+
+    protected void mkdirsImpl(CuratorFramework zk, String path, List<ACL> acls) {
         String npath = normalizePath(path);
         if (npath.equals("/")) {
             return;
@@ -394,4 +422,4 @@ public class Zookeeper {
         return ret;
     }
 
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/storm/blob/394fefff/storm-core/test/clj/org/apache/storm/cluster_test.clj
----------------------------------------------------------------------
diff --git a/storm-core/test/clj/org/apache/storm/cluster_test.clj b/storm-core/test/clj/org/apache/storm/cluster_test.clj
index bdbda75..689cf0c 100644
--- a/storm-core/test/clj/org/apache/storm/cluster_test.clj
+++ b/storm-core/test/clj/org/apache/storm/cluster_test.clj
@@ -25,6 +25,7 @@
   (:import [org.apache.storm.utils Utils TestUtils ZookeeperAuthInfo ConfigUtils])
   (:import [org.apache.storm.cluster ClusterState])
   (:import [org.apache.storm.zookeeper Zookeeper])
+  (:import [org.apache.storm.testing.staticmocking MockedZookeeper])
   (:require [org.apache.storm [zookeeper :as zk]])
   (:require [conjure.core])
   (:use [conjure core])
@@ -309,14 +310,15 @@
 
 (deftest test-cluster-state-default-acls
   (testing "The default ACLs are empty."
-    (stubbing [Zookeeper/mkdirs nil
-               zk/mk-client (reify CuratorFramework (^void close [this] nil))]
-      (mk-distributed-cluster-state {})
-      (verify-call-times-for Zookeeper/mkdirs 1)
-      (verify-first-call-args-for-indices Zookeeper/mkdirs [2] nil))
+    (let [zk-mock (Mockito/mock Zookeeper)]
+      ;; No need for when clauses because we just want to return nil
+      (with-open [_ (MockedZookeeper. zk-mock)]
+        (stubbing [zk/mk-client (reify CuratorFramework (^void close [this] nil))]
+          (mk-distributed-cluster-state {})
+          (.mkdirs (Mockito/verify zk-mock (Mockito/times 1)) (Mockito/any) (Mockito/anyString) nil))))
     (stubbing [mk-distributed-cluster-state (reify ClusterState
                                               (register [this callback] nil)
                                               (mkdirs [this path acls] nil))]
-      (mk-storm-cluster-state {})
-      (verify-call-times-for mk-distributed-cluster-state 1)
-      (verify-first-call-args-for-indices mk-distributed-cluster-state [4] nil))))
+     (mk-storm-cluster-state {})
+     (verify-call-times-for mk-distributed-cluster-state 1)
+     (verify-first-call-args-for-indices mk-distributed-cluster-state [4] nil))))