You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by gu...@apache.org on 2018/12/14 03:32:17 UTC

[kafka] branch trunk updated: Fix the missing ApiUtils tests in streams module. (#6003)

This is an automated email from the ASF dual-hosted git repository.

guozhang pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 88443b4  Fix the missing ApiUtils tests in streams module. (#6003)
88443b4 is described below

commit 88443b4a3718a848188db9f5152c1077b15f1159
Author: Srinivas Reddy <mr...@users.noreply.github.com>
AuthorDate: Fri Dec 14 11:31:58 2018 +0800

    Fix the missing ApiUtils tests in streams module. (#6003)
    
    Reviewers: Matthias J. Sax <ma...@confluent.io>, Guozhang Wang <gu...@confluent.io>, Bill Bejeck <bi...@confluent.io>
---
 .../apache/kafka/streams/internals/ApiUtils.java   |  10 +-
 .../org/apache/kafka/streams/KeyValueTest.java     |   2 +-
 .../kafka/streams/internals/ApiUtilsTest.java      | 110 +++++++++++++++++++++
 3 files changed, 117 insertions(+), 5 deletions(-)

diff --git a/streams/src/main/java/org/apache/kafka/streams/internals/ApiUtils.java b/streams/src/main/java/org/apache/kafka/streams/internals/ApiUtils.java
index 86c9fc5..2ef3aff 100644
--- a/streams/src/main/java/org/apache/kafka/streams/internals/ApiUtils.java
+++ b/streams/src/main/java/org/apache/kafka/streams/internals/ApiUtils.java
@@ -24,6 +24,8 @@ import static java.lang.String.format;
 public final class ApiUtils {
 
     private static final String MILLISECOND_VALIDATION_FAIL_MSG_FRMT = "Invalid value for parameter \"%s\" (value was: %s). ";
+    private static final String VALIDATE_MILLISECOND_NULL_SUFFIX = "It shouldn't be null.";
+    private static final String VALIDATE_MILLISECOND_OVERFLOW_SUFFIX = "It can't be converted to milliseconds.";
 
     private ApiUtils() {
     }
@@ -37,12 +39,12 @@ public final class ApiUtils {
     public static long validateMillisecondDuration(final Duration duration, final String messagePrefix) {
         try {
             if (duration == null) {
-                throw new IllegalArgumentException(messagePrefix + "It shouldn't be null.");
+                throw new IllegalArgumentException(messagePrefix + VALIDATE_MILLISECOND_NULL_SUFFIX);
             }
 
             return duration.toMillis();
         } catch (final ArithmeticException e) {
-            throw new IllegalArgumentException(messagePrefix + "It can't be converted to milliseconds.", e);
+            throw new IllegalArgumentException(messagePrefix + VALIDATE_MILLISECOND_OVERFLOW_SUFFIX, e);
         }
     }
 
@@ -55,12 +57,12 @@ public final class ApiUtils {
     public static long validateMillisecondInstant(final Instant instant, final String messagePrefix) {
         try {
             if (instant == null) {
-                throw new IllegalArgumentException(messagePrefix + "It shouldn't be null.");
+                throw new IllegalArgumentException(messagePrefix + VALIDATE_MILLISECOND_NULL_SUFFIX);
             }
 
             return instant.toEpochMilli();
         } catch (final ArithmeticException e) {
-            throw new IllegalArgumentException(messagePrefix + "It can't be converted to milliseconds.", e);
+            throw new IllegalArgumentException(messagePrefix + VALIDATE_MILLISECOND_OVERFLOW_SUFFIX, e);
         }
     }
 
diff --git a/streams/src/test/java/org/apache/kafka/streams/KeyValueTest.java b/streams/src/test/java/org/apache/kafka/streams/KeyValueTest.java
index 7681068..24f7d5d 100644
--- a/streams/src/test/java/org/apache/kafka/streams/KeyValueTest.java
+++ b/streams/src/test/java/org/apache/kafka/streams/KeyValueTest.java
@@ -24,7 +24,7 @@ import static org.junit.Assert.assertTrue;
 public class KeyValueTest {
 
     @Test
-    public void shouldHaveSaneEqualsAndHashCode() {
+    public void shouldHaveSameEqualsAndHashCode() {
         final KeyValue<String, Long> kv = KeyValue.pair("key1", 1L);
         final KeyValue<String, Long> copyOfKV = KeyValue.pair(kv.key, kv.value);
 
diff --git a/streams/src/test/java/org/apache/kafka/streams/internals/ApiUtilsTest.java b/streams/src/test/java/org/apache/kafka/streams/internals/ApiUtilsTest.java
new file mode 100644
index 0000000..5fe30dd
--- /dev/null
+++ b/streams/src/test/java/org/apache/kafka/streams/internals/ApiUtilsTest.java
@@ -0,0 +1,110 @@
+/*
+ * 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.kafka.streams.internals;
+
+import org.junit.Test;
+
+import java.time.Duration;
+import java.time.Instant;
+
+import static org.apache.kafka.streams.internals.ApiUtils.prepareMillisCheckFailMsgPrefix;
+import static org.apache.kafka.streams.internals.ApiUtils.validateMillisecondDuration;
+import static org.apache.kafka.streams.internals.ApiUtils.validateMillisecondInstant;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+
+public class ApiUtilsTest {
+
+    // This is the maximum limit that Duration accepts but fails when it converts to milliseconds.
+    private static final long MAX_ACCEPTABLE_DAYS_FOR_DURATION = 106751991167300L;
+    // This is the maximum limit that Duration accepts and converts to milliseconds with out fail.
+    private static final long MAX_ACCEPTABLE_DAYS_FOR_DURATION_TO_MILLIS = 106751991167L;
+
+    @Test
+    public void shouldThrowNullPointerExceptionForNullDuration() {
+        final String nullDurationPrefix = prepareMillisCheckFailMsgPrefix(null, "nullDuration");
+
+        try {
+            validateMillisecondDuration(null, nullDurationPrefix);
+            fail("Expected exception when null passed to duration.");
+        } catch (final IllegalArgumentException e) {
+            assertThat(e.getMessage(), containsString(nullDurationPrefix));
+        }
+    }
+
+    @Test
+    public void shouldThrowArithmeticExceptionForMaxDuration() {
+        final Duration maxDurationInDays = Duration.ofDays(MAX_ACCEPTABLE_DAYS_FOR_DURATION);
+        final String maxDurationPrefix = prepareMillisCheckFailMsgPrefix(maxDurationInDays, "maxDuration");
+
+        try {
+            validateMillisecondDuration(maxDurationInDays, maxDurationPrefix);
+            fail("Expected exception when maximum days passed for duration, because of long overflow");
+        } catch (final IllegalArgumentException e) {
+            assertThat(e.getMessage(), containsString(maxDurationPrefix));
+        }
+    }
+
+    @Test
+    public void shouldThrowNullPointerExceptionForNullInstant() {
+        final String nullInstantPrefix = prepareMillisCheckFailMsgPrefix(null, "nullInstant");
+
+        try {
+            validateMillisecondInstant(null, nullInstantPrefix);
+            fail("Expected exception when null value passed for instant.");
+        } catch (final IllegalArgumentException e) {
+            assertThat(e.getMessage(), containsString(nullInstantPrefix));
+        }
+    }
+
+    @Test
+    public void shouldThrowArithmeticExceptionForMaxInstant() {
+        final String maxInstantPrefix = prepareMillisCheckFailMsgPrefix(Instant.MAX, "maxInstant");
+
+        try {
+            validateMillisecondInstant(Instant.MAX, maxInstantPrefix);
+            fail("Expected exception when maximum value passed for instant, because of long overflow.");
+        } catch (final IllegalArgumentException e) {
+            assertThat(e.getMessage(), containsString(maxInstantPrefix));
+        }
+    }
+
+    @Test
+    public void shouldReturnMillisecondsOnValidDuration() {
+        final Duration sampleDuration = Duration.ofDays(MAX_ACCEPTABLE_DAYS_FOR_DURATION_TO_MILLIS);
+
+        assertEquals(sampleDuration.toMillis(), validateMillisecondDuration(sampleDuration, "sampleDuration"));
+    }
+
+    @Test
+    public void shouldReturnMillisecondsOnValidInstant() {
+        final Instant sampleInstant = Instant.now();
+
+        assertEquals(sampleInstant.toEpochMilli(), validateMillisecondInstant(sampleInstant, "sampleInstant"));
+    }
+
+    @Test
+    public void shouldContainsNameAndValueInFailMsgPrefix() {
+        final String failMsgPrefix = prepareMillisCheckFailMsgPrefix("someValue", "variableName");
+
+        assertThat(failMsgPrefix, containsString("variableName"));
+        assertThat(failMsgPrefix, containsString("someValue"));
+    }
+}
\ No newline at end of file