You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@heron.apache.org by GitBox <gi...@apache.org> on 2018/11/05 19:40:10 UTC

[GitHub] nlu90 closed pull request #3095: [Java Streamlet API] Extend validations

nlu90 closed pull request #3095: [Java Streamlet API] Extend validations
URL: https://github.com/apache/incubator-heron/pull/3095
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/heron/api/src/java/BUILD b/heron/api/src/java/BUILD
index c87f46c8a2..cdab255fcf 100644
--- a/heron/api/src/java/BUILD
+++ b/heron/api/src/java/BUILD
@@ -33,7 +33,7 @@ java_library(
     javacopts = DOCLINT_HTML_AND_SYNTAX,
     deps = api_deps_files + [
         ":api-java-low-level",
-        "//third_party/java:kryo-neverlink",
+        "//third_party/java:kryo-neverlink"
     ]
 )
 
@@ -48,7 +48,7 @@ java_library(
 java_binary(
     name = "api-unshaded",
     srcs = glob(["org/apache/heron/api/**/*.java", "org/apache/heron/streamlet/**/*.java"]),
-    deps = api_deps_files + ["//third_party/java:kryo-neverlink"],
+    deps = api_deps_files + ["//third_party/java:kryo-neverlink"]
 )
 
 jarjar_binary(
diff --git a/heron/api/src/java/org/apache/heron/streamlet/impl/BuilderImpl.java b/heron/api/src/java/org/apache/heron/streamlet/impl/BuilderImpl.java
index 936e46b312..0eb06d9f1a 100644
--- a/heron/api/src/java/org/apache/heron/streamlet/impl/BuilderImpl.java
+++ b/heron/api/src/java/org/apache/heron/streamlet/impl/BuilderImpl.java
@@ -29,6 +29,7 @@
 import org.apache.heron.streamlet.SerializableSupplier;
 import org.apache.heron.streamlet.Source;
 import org.apache.heron.streamlet.Streamlet;
+import org.apache.heron.streamlet.impl.utils.StreamletUtils;
 
 /**
  * BuilderImpl implements the Builder interface.
@@ -44,6 +45,7 @@ public BuilderImpl() {
 
   @Override
   public <R> Streamlet<R> newSource(SerializableSupplier<R> supplier) {
+    StreamletUtils.require(supplier != null, "supplier must not be null.");
     StreamletImpl<R> retval = StreamletImpl.createSupplierStreamlet(supplier);
     retval.setNumPartitions(1);
     sources.add(retval);
@@ -52,6 +54,7 @@ public BuilderImpl() {
 
   @Override
   public <R> Streamlet<R> newSource(Source<R> generator) {
+    StreamletUtils.require(generator != null, "source must not be null.");
     StreamletImpl<R> retval = StreamletImpl.createGeneratorStreamlet(generator);
     retval.setNumPartitions(1);
     sources.add(retval);
diff --git a/heron/api/src/java/org/apache/heron/streamlet/impl/StreamletImpl.java b/heron/api/src/java/org/apache/heron/streamlet/impl/StreamletImpl.java
index 677b34ef96..24d5075ff6 100644
--- a/heron/api/src/java/org/apache/heron/streamlet/impl/StreamletImpl.java
+++ b/heron/api/src/java/org/apache/heron/streamlet/impl/StreamletImpl.java
@@ -61,6 +61,7 @@
 import org.apache.heron.streamlet.impl.streamlets.SupplierStreamlet;
 import org.apache.heron.streamlet.impl.streamlets.TransformStreamlet;
 import org.apache.heron.streamlet.impl.streamlets.UnionStreamlet;
+import org.apache.heron.streamlet.impl.utils.StreamletUtils;
 
 /**
  * A Streamlet is a (potentially unbounded) ordered collection of tuples.
@@ -151,7 +152,7 @@ public String toString() {
    */
   @Override
   public Streamlet<R> setName(String sName) {
-    require(sName != null && !sName.trim().isEmpty(),
+    StreamletUtils.require(sName != null && !sName.trim().isEmpty(),
         "Streamlet name cannot be null/blank");
     this.name = sName;
     return this;
@@ -190,7 +191,7 @@ protected void setDefaultNameIfNone(StreamletNamePrefix prefix, Set<String> stag
    */
   @Override
   public Streamlet<R> setNumPartitions(int numPartitions) {
-    require(numPartitions > 0,
+    StreamletUtils.require(numPartitions > 0,
         "Streamlet's partitions number should be > 0");
     this.nPartitions = numPartitions;
     return this;
@@ -329,7 +330,9 @@ private String defaultNameCalculator(StreamletNamePrefix prefix, Set<String> sta
    */
   @Override
   public List<Streamlet<R>> clone(int numClones) {
-    List<Streamlet<R>> retval = new ArrayList<>();
+    StreamletUtils.require(numClones > 0,
+        "Streamlet's clone number should be > 0");
+    List<Streamlet<R>> retval = new ArrayList<>(numClones);
     for (int i = 0; i < numClones; ++i) {
       retval.add(repartition(getNumPartitions()));
     }
@@ -532,15 +535,4 @@ public void toSink(Sink<R> sink) {
     return customStreamlet;
   }
 
-  /**
-   * Verifies the requirement as the utility function.
-   * @param requirement The requirement to verify
-   * @param errorMessage The error message
-   * @throws IllegalArgumentException if the requirement fails
-   */
-  private void require(Boolean requirement, String errorMessage) {
-    if (!requirement) {
-      throw new IllegalArgumentException(errorMessage);
-    }
-  }
 }
diff --git a/heron/api/src/java/org/apache/heron/streamlet/impl/utils/StreamletUtils.java b/heron/api/src/java/org/apache/heron/streamlet/impl/utils/StreamletUtils.java
new file mode 100644
index 0000000000..79104394c7
--- /dev/null
+++ b/heron/api/src/java/org/apache/heron/streamlet/impl/utils/StreamletUtils.java
@@ -0,0 +1,39 @@
+/**
+ * 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.heron.streamlet.impl.utils;
+
+public final class StreamletUtils {
+
+  private StreamletUtils() {
+  }
+
+  /**
+   * Verifies the requirement as the utility function.
+   * @param requirement The requirement to verify
+   * @param errorMessage The error message
+   * @throws IllegalArgumentException if the requirement fails
+   */
+  public static void require(Boolean requirement, String errorMessage) {
+    if (!requirement) {
+      throw new IllegalArgumentException(errorMessage);
+    }
+  }
+
+}
diff --git a/heron/api/tests/java/BUILD b/heron/api/tests/java/BUILD
index 20eefa9aa0..03c1f2bb5e 100644
--- a/heron/api/tests/java/BUILD
+++ b/heron/api/tests/java/BUILD
@@ -31,6 +31,7 @@ java_tests(
     "org.apache.heron.streamlet.impl.operators.JoinOperatorTest",
     "org.apache.heron.streamlet.impl.operators.ReduceByKeyAndWindowOperatorTest",
     "org.apache.heron.streamlet.impl.operators.GeneralReduceByKeyAndWindowOperatorTest",
+    "org.apache.heron.streamlet.impl.utils.StreamletUtilsTest",
     "org.apache.heron.api.ConfigTest",
     "org.apache.heron.api.HeronSubmitterTest",
     "org.apache.heron.api.utils.UtilsTest"
diff --git a/heron/api/tests/java/org/apache/heron/streamlet/impl/StreamletImplTest.java b/heron/api/tests/java/org/apache/heron/streamlet/impl/StreamletImplTest.java
index 87af426de2..c19dbd2592 100644
--- a/heron/api/tests/java/org/apache/heron/streamlet/impl/StreamletImplTest.java
+++ b/heron/api/tests/java/org/apache/heron/streamlet/impl/StreamletImplTest.java
@@ -143,7 +143,7 @@ public void testRepartitionStreamlet() throws Exception {
 
   @Test
   @SuppressWarnings("unchecked")
-  public void testCloneStreamlet() throws Exception {
+  public void testCloneStreamlet() {
     Streamlet<Double> baseStreamlet = StreamletImpl.createSupplierStreamlet(() -> Math.random());
     List<Streamlet<Double>> streamlets = baseStreamlet.setNumPartitions(20).clone(2);
     assertEquals(streamlets.size(), 2);
@@ -457,6 +457,13 @@ public void testSetNumPartitionsWithInvalidValue() {
     streamlet.setNumPartitions(0);
   }
 
+  @Test(expected = IllegalArgumentException.class)
+  @SuppressWarnings("unchecked")
+  public void testCloneStreamletWithInvalidNumberOfClone() {
+    Streamlet<Double> baseStreamlet = StreamletImpl.createSupplierStreamlet(() -> Math.random());
+    baseStreamlet.setNumPartitions(20).clone(0);
+  }
+
   private void testByFunction(Function<String, Streamlet<Double>> function, String sName) {
     try {
       function.apply(sName);
diff --git a/heron/api/tests/java/org/apache/heron/streamlet/impl/utils/StreamletUtilsTest.java b/heron/api/tests/java/org/apache/heron/streamlet/impl/utils/StreamletUtilsTest.java
new file mode 100644
index 0000000000..c1b93ce478
--- /dev/null
+++ b/heron/api/tests/java/org/apache/heron/streamlet/impl/utils/StreamletUtilsTest.java
@@ -0,0 +1,38 @@
+/**
+ * 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.heron.streamlet.impl.utils;
+
+import org.junit.Test;
+
+public class StreamletUtilsTest {
+
+  @Test
+  public void testRequire() {
+    String text = "test_text";
+    StreamletUtils.require(!text.isEmpty(), "text should not be blank");
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testRequireWithNegativeCase() {
+    String text = "";
+    StreamletUtils.require(!text.isEmpty(), "text should not be blank");
+  }
+
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services