You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@twill.apache.org by ch...@apache.org on 2015/07/24 20:29:43 UTC

[09/25] incubator-twill git commit: TWILL-136 Override equals and hashCode for JvmOptions.DebugOptions to test equality

TWILL-136 Override equals and hashCode for JvmOptions.DebugOptions to test equality

The current code for JvmOptions.DebugOptions does not have equals and hashCode overriden for equality test.

This would cause fail comparison for DebugOptions.NO_DEBUG when being used in YarnTwillPreparer:

final class YarnTwillPreparer implements TwillPreparer {

...

  @Override
  public TwillPreparer enableDebugging(boolean doSuspend, String... runnables) {
    this.debugOptions = new JvmOptions.DebugOptions(true, doSuspend, ImmutableSet.copyOf(runnables));
    return this;
  }

....
  private void saveJvmOptions(Map<String, LocalFile> localFiles) throws IOException {
    if ((extraOptions == null || extraOptions.isEmpty()) &&
      JvmOptions.DebugOptions.NO_DEBUG.equals(this.debugOptions)) {
      // If no vm options, no need to localize the file.
      return;
    }
    ...
  }

...
}

This closes #43 on GitHub

Signed-off-by: Terence Yim <ch...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/incubator-twill/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-twill/commit/feee57ae
Tree: http://git-wip-us.apache.org/repos/asf/incubator-twill/tree/feee57ae
Diff: http://git-wip-us.apache.org/repos/asf/incubator-twill/diff/feee57ae

Branch: refs/heads/site
Commit: feee57aeb403d2d3fa7f0c5ee327c8a6c63284d6
Parents: 2054c5f
Author: hsaputra <hs...@apache.org>
Authored: Wed Jun 17 15:08:41 2015 -0700
Committer: Terence Yim <ch...@apache.org>
Committed: Thu Jun 18 10:09:23 2015 -0700

----------------------------------------------------------------------
 .../org/apache/twill/internal/JvmOptions.java   | 32 +++++++
 .../apache/twill/internal/DebugOptionsTest.java | 99 ++++++++++++++++++++
 2 files changed, 131 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-twill/blob/feee57ae/twill-core/src/main/java/org/apache/twill/internal/JvmOptions.java
----------------------------------------------------------------------
diff --git a/twill-core/src/main/java/org/apache/twill/internal/JvmOptions.java b/twill-core/src/main/java/org/apache/twill/internal/JvmOptions.java
index f827433..945561b 100644
--- a/twill-core/src/main/java/org/apache/twill/internal/JvmOptions.java
+++ b/twill-core/src/main/java/org/apache/twill/internal/JvmOptions.java
@@ -19,7 +19,9 @@ package org.apache.twill.internal;
 
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
+import com.google.common.primitives.Booleans;
 
+import java.util.Objects;
 import java.util.Set;
 
 /**
@@ -83,5 +85,35 @@ public final class JvmOptions {
     public boolean doDebug(String runnable) {
       return doDebug && (runnables == null || runnables.contains(runnable));
     }
+
+    @Override
+    public boolean equals(Object object) {
+      if (this == object) {
+        return true;
+      }
+      if (!(object instanceof DebugOptions)) {
+        return false;
+      }
+
+      DebugOptions that = (DebugOptions) object;
+      return (this.doDebug == that.doDebug()) && (this.doSuspend == this.doSuspend()) &&
+        Objects.equals(this.runnables, that.getRunnables());
+    }
+
+    @Override
+    public int hashCode() {
+      int hash = 17;
+      hash = 31 *  hash + Booleans.hashCode(doDebug);
+      hash = 31 *  hash + Booleans.hashCode(doSuspend);
+      hash = 31 *  hash + Objects.hashCode(runnables);
+      return hash;
+    }
+
+    @Override
+    public String toString() {
+      return "{\"doDebug\":" + doDebug + ",\"doSuspend\":" + doSuspend + ",\"runnables\":" + (runnables != null ?
+      runnables.toString() : "none") + "}";
+    }
+
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-twill/blob/feee57ae/twill-core/src/test/java/org/apache/twill/internal/DebugOptionsTest.java
----------------------------------------------------------------------
diff --git a/twill-core/src/test/java/org/apache/twill/internal/DebugOptionsTest.java b/twill-core/src/test/java/org/apache/twill/internal/DebugOptionsTest.java
new file mode 100644
index 0000000..2a07987
--- /dev/null
+++ b/twill-core/src/test/java/org/apache/twill/internal/DebugOptionsTest.java
@@ -0,0 +1,99 @@
+/*
+ * 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.twill.internal;
+
+import com.google.common.collect.Sets;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Set;
+
+/**
+ * Unit test for {@link org.apache.twill.internal.JvmOptions.DebugOptions} class
+ */
+public class DebugOptionsTest {
+
+  @Test
+  public void testNoDebug() {
+    JvmOptions.DebugOptions noDebug = new JvmOptions.DebugOptions(false, false, null);
+
+    Assert.assertEquals(JvmOptions.DebugOptions.NO_DEBUG, noDebug);
+  }
+
+  @Test
+  public void testWithNull() {
+    JvmOptions.DebugOptions noDebug = new JvmOptions.DebugOptions(false, false, null);
+
+    Assert.assertNotEquals(noDebug, null);
+  }
+
+  @Test
+  public void testDoDebug() {
+    JvmOptions.DebugOptions option1 = new JvmOptions.DebugOptions(true, false, null);
+
+    Assert.assertNotEquals(JvmOptions.DebugOptions.NO_DEBUG, option1);
+
+    JvmOptions.DebugOptions option2 = new JvmOptions.DebugOptions(true, false, null);
+
+    Assert.assertEquals(option1, option2);
+  }
+
+  @Test
+  public void testDoSuspend() {
+    JvmOptions.DebugOptions option1 = new JvmOptions.DebugOptions(true, true, null);
+
+    Assert.assertNotEquals(JvmOptions.DebugOptions.NO_DEBUG, option1);
+
+    JvmOptions.DebugOptions option2 = new JvmOptions.DebugOptions(true, true, null);
+
+    Assert.assertEquals(option1, option2);
+
+  }
+
+  @Test
+  public void testSameRunnables() {
+    Set<String> runnables = Sets.newHashSet();
+    runnables.add("runnable1");
+    runnables.add("runnable2");
+
+    JvmOptions.DebugOptions option1 = new JvmOptions.DebugOptions(false, false, runnables);
+
+    JvmOptions.DebugOptions option2 = new JvmOptions.DebugOptions(false, false, runnables);
+
+    Assert.assertEquals(option1, option2);
+  }
+
+  @Test
+  public void testDifferentRunnables() {
+    Set<String> runnables1 = Sets.newHashSet();
+    runnables1.add("runnable1");
+
+    JvmOptions.DebugOptions option1 = new JvmOptions.DebugOptions(true, false, runnables1);
+
+    Assert.assertNotEquals(option1, JvmOptions.DebugOptions.NO_DEBUG);
+
+    Set<String> runnables2 = Sets.newHashSet();
+    runnables2.add("runnable2-1");
+    runnables2.add("runnable2-2");
+
+    JvmOptions.DebugOptions option2 = new JvmOptions.DebugOptions(true, false, runnables2);
+
+    Assert.assertNotEquals(option1, option2);
+
+  }
+}