nifi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattyb...@apache.org
Subject [nifi] branch master updated: Added tests for commons utilities StringUtils and FormatUtils These tests were written using Diffblue Cover.
Date Wed, 06 Mar 2019 19:00:33 GMT
This is an automated email from the ASF dual-hosted git repository.

mattyb149 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/master by this push:
     new 08a307c  Added tests for commons utilities StringUtils and FormatUtils These tests
were written using Diffblue Cover.
08a307c is described below

commit 08a307cc6443de15713daa6ed29ed36a4b3e549f
Author: Freddy Tuxworth <freddytuxworth@gmail.com>
AuthorDate: Wed Mar 6 12:27:00 2019 +0000

    Added tests for commons utilities StringUtils and FormatUtils
    These tests were written using Diffblue Cover.
    
    Fixed bug in FormatUtils.formatNanos
    
    Fix import asterisk warning
    
    Add Apache license header.
    
    Signed-off-by: Matthew Burgess <mattyb149@apache.org>
    
    This closes #3354
---
 .../java/org/apache/nifi/util/StringUtilsTest.java | 100 +++++++++++++++++++++
 .../java/org/apache/nifi/util/FormatUtils.java     |   4 +-
 .../org/apache/nifi/processor/TestFormatUtils.java |  50 ++++++++++-
 3 files changed, 149 insertions(+), 5 deletions(-)

diff --git a/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/StringUtilsTest.java
b/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/StringUtilsTest.java
new file mode 100644
index 0000000..5c689c6
--- /dev/null
+++ b/nifi-commons/nifi-properties/src/test/java/org/apache/nifi/util/StringUtilsTest.java
@@ -0,0 +1,100 @@
+/*
+ * 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.nifi.util;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.ArrayList;
+import org.junit.Test;
+
+public class StringUtilsTest {
+
+  @Test
+  public void testIsBlank() {
+    assertFalse(StringUtils.isBlank("0"));
+    assertFalse(StringUtils.isBlank("\u0000"));
+    assertFalse(StringUtils.isBlank(" \u0000 "));
+    assertFalse(StringUtils.isBlank(" \u5678 "));
+
+    assertTrue(StringUtils.isBlank(" "));
+    assertTrue(StringUtils.isBlank(""));
+  }
+
+  @Test
+  public void testStartsWith() {
+    assertFalse(StringUtils.startsWith("!", "something"));
+
+    assertTrue(StringUtils.startsWith("!!!!!test", "!!!!"));
+    assertTrue(StringUtils.startsWith("!something", "!"));
+    assertTrue(StringUtils.startsWith(null, null));
+  }
+
+  @Test
+  public void testPadRight() {
+    assertEquals("sample", StringUtils.padRight("sample", 0, '0'));
+    assertEquals("sample0000", StringUtils.padRight("sample", 10, '0'));
+    assertEquals("0000000000", StringUtils.padRight("", 10, '0'));
+
+    assertNull(StringUtils.padRight(null, 0, '0'));
+  }
+
+  @Test
+  public void testPadLeft() {
+    assertEquals("sample", StringUtils.padLeft("sample", 0, '0'));
+    assertEquals("0000sample", StringUtils.padLeft("sample", 10, '0'));
+    assertEquals("0000000000", StringUtils.padLeft("", 10, '0'));
+
+    assertNull(StringUtils.padLeft(null, 0, '0'));
+  }
+
+  @Test
+  public void testIsEmpty() {
+    assertFalse(StringUtils.isEmpty("AAAAAAAA"));
+    assertFalse(StringUtils.isEmpty(" "));
+
+    assertTrue(StringUtils.isEmpty(""));
+    assertTrue(StringUtils.isEmpty(null));
+  }
+
+  @Test
+  public void testSubstringAfter() {
+    assertEquals("", StringUtils.substringAfter("", ""));
+    assertEquals("", StringUtils.substringAfter("", ">>"));
+    assertEquals("after", StringUtils.substringAfter("substring>>after", ">>"));
+    assertEquals("after>>another", StringUtils.substringAfter("substring>>after>>another",
">>"));
+    assertEquals("", StringUtils.substringAfter("substring>>after", null));
+    assertEquals("", StringUtils.substringAfter("substring.after", ">>"));
+  }
+
+  @Test
+  public void testJoin() {
+    final ArrayList<String> collection = new ArrayList<>();
+    assertEquals("", StringUtils.join(collection, ","));
+
+    collection.add("test1");
+    assertEquals("test1", StringUtils.join(collection, ","));
+
+    collection.add("test2");
+    assertEquals("test1,test2", StringUtils.join(collection, ","));
+
+    collection.add(null);
+    assertEquals("test1,test2,null", StringUtils.join(collection, ","));
+  }
+}
diff --git a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/FormatUtils.java b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/FormatUtils.java
index 7d2992f..d7fdf5f 100644
--- a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/FormatUtils.java
+++ b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/FormatUtils.java
@@ -399,8 +399,8 @@ public class FormatUtils {
     public static String formatNanos(final long nanos, final boolean includeTotalNanos) {
         final StringBuilder sb = new StringBuilder();
 
-        final long seconds = nanos > 1000000000L ? nanos / 1000000000L : 0L;
-        long millis = nanos > 1000000L ? nanos / 1000000L : 0L;
+        final long seconds = nanos >= 1000000000L ? nanos / 1000000000L : 0L;
+        long millis = nanos >= 1000000L ? nanos / 1000000L : 0L;
         final long nanosLeft = nanos % 1000000L;
 
         if (seconds > 0) {
diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java
b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java
index 68748e6..fc80d85 100644
--- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java
+++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java
@@ -47,9 +47,9 @@ public class TestFormatUtils {
         assertEquals("00:00:07.777", FormatUtils.formatHoursMinutesSeconds(7777, TimeUnit.MILLISECONDS));
 
         assertEquals("20:11:36.897", FormatUtils.formatHoursMinutesSeconds(TimeUnit.MILLISECONDS.convert(20,
TimeUnit.HOURS)
-                        + TimeUnit.MILLISECONDS.convert(11, TimeUnit.MINUTES)
-                        + TimeUnit.MILLISECONDS.convert(36, TimeUnit.SECONDS)
-                        + TimeUnit.MILLISECONDS.convert(897, TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS));
+                + TimeUnit.MILLISECONDS.convert(11, TimeUnit.MINUTES)
+                + TimeUnit.MILLISECONDS.convert(36, TimeUnit.SECONDS)
+                + TimeUnit.MILLISECONDS.convert(897, TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS));
 
         assertEquals("1000:01:01.001", FormatUtils.formatHoursMinutesSeconds(TimeUnit.MILLISECONDS.convert(999,
TimeUnit.HOURS)
                 + TimeUnit.MILLISECONDS.convert(60, TimeUnit.MINUTES)
@@ -57,4 +57,48 @@ public class TestFormatUtils {
                 + TimeUnit.MILLISECONDS.convert(1001, TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS));
     }
 
+
+    @Test
+    public void testFormatNanos() {
+        assertEquals("0 nanos", FormatUtils.formatNanos(0L, false));
+        assertEquals("0 nanos (0 nanos)", FormatUtils.formatNanos(0L, true));
+
+        assertEquals("1 millis, 0 nanos", FormatUtils.formatNanos(1_000_000L, false));
+        assertEquals("1 millis, 0 nanos (1000000 nanos)", FormatUtils.formatNanos(1_000_000L,
true));
+
+        assertEquals("1 millis, 1 nanos", FormatUtils.formatNanos(1_000_001L, false));
+        assertEquals("1 millis, 1 nanos (1000001 nanos)", FormatUtils.formatNanos(1_000_001L,
true));
+
+        assertEquals("1 seconds, 0 millis, 0 nanos", FormatUtils.formatNanos(1_000_000_000L,
false));
+        assertEquals(
+            "1 seconds, 0 millis, 0 nanos (1000000000 nanos)",
+            FormatUtils.formatNanos(1_000_000_000L, true));
+
+        assertEquals("1 seconds, 1 millis, 0 nanos", FormatUtils.formatNanos(1_001_000_000L,
false));
+        assertEquals(
+            "1 seconds, 1 millis, 0 nanos (1001000000 nanos)",
+            FormatUtils.formatNanos(1_001_000_000L, true));
+
+        assertEquals("1 seconds, 1 millis, 1 nanos", FormatUtils.formatNanos(1_001_000_001L,
false));
+        assertEquals(
+            "1 seconds, 1 millis, 1 nanos (1001000001 nanos)",
+            FormatUtils.formatNanos(1_001_000_001L, true));
+    }
+
+    @Test
+    public void testFormatDataSize() {
+        assertEquals("0 bytes", FormatUtils.formatDataSize(0d));
+        assertEquals("10.4 bytes", FormatUtils.formatDataSize(10.4d));
+        assertEquals("1,024 bytes", FormatUtils.formatDataSize(1024d));
+
+        assertEquals("1 KB", FormatUtils.formatDataSize(1025d));
+        assertEquals("1.95 KB", FormatUtils.formatDataSize(2000d));
+        assertEquals("195.31 KB", FormatUtils.formatDataSize(200_000d));
+
+        assertEquals("190.73 MB", FormatUtils.formatDataSize(200_000_000d));
+
+        assertEquals("186.26 GB", FormatUtils.formatDataSize(200_000_000_000d));
+
+        assertEquals("181.9 TB", FormatUtils.formatDataSize(200_000_000_000_000d));
+    }
 }


Mime
View raw message