From 63fb8523c87e289dbeca5cd49661ea127748da96 Mon Sep 17 00:00:00 2001 From: Peter Sear Date: Thu, 18 Apr 2019 17:29:13 +0100 Subject: [PATCH 1/2] Refactor existing unit tests. --- .../vip/vjtools/vjtop/util/FormatsTest.java | 224 +++--------------- 1 file changed, 28 insertions(+), 196 deletions(-) diff --git a/vjtop/src/test/java/com/vip/vjtools/vjtop/util/FormatsTest.java b/vjtop/src/test/java/com/vip/vjtools/vjtop/util/FormatsTest.java index cb44d63e..fe277661 100644 --- a/vjtop/src/test/java/com/vip/vjtools/vjtop/util/FormatsTest.java +++ b/vjtop/src/test/java/com/vip/vjtools/vjtop/util/FormatsTest.java @@ -1,248 +1,80 @@ package com.vip.vjtools.vjtop.util; -import com.vip.vjtools.vjtop.WarningRule.LongWarning; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import java.util.ArrayList; +import static com.vip.vjtools.vjtop.util.Formats.*; + public class FormatsTest { @Rule public ExpectedException thrown = ExpectedException.none(); @Test - public void joinInput0NotNullOutputNotNull() { - - // Arrange + public void testJoin() { final ArrayList list = new ArrayList(); final String delim = "AAAAAAAA"; - // Act - final String retval = Formats.join(list, delim); - - // Assert result - Assert.assertEquals("", retval); - } - - @Test - public void joinInput1NullOutputNotNull() { + Assert.assertEquals("", join(list, delim)); - // Arrange - final ArrayList list = new ArrayList(); list.add(""); - final String delim = null; - - // Act - final String retval = Formats.join(list, delim); - - // Assert result - Assert.assertEquals("", retval); + Assert.assertEquals("", join(list, delim)); } @Test - public void leftStrInputNotNullNegativeOutputStringIndexOutOfBoundsException() { - - // Arrange - final String str = "!"; - final int length = -536_870_911; + public void testLeftStr() { + Assert.assertEquals("", leftStr("!!!!!!!!", 0)); - // Act thrown.expect(StringIndexOutOfBoundsException.class); - Formats.leftStr(str, length); - + leftStr("!", -536_870_911); // Method is not expected to return due to exception thrown } @Test - public void leftStrInputNotNullZeroOutputNotNull() { - - // Arrange - final String str = "!!!!!!!!"; - final int length = 0; - - // Act - final String retval = Formats.leftStr(str, length); - - // Assert result - Assert.assertEquals("", retval); - } + public void testRightStr() { + Assert.assertEquals("!!!!!!!!", rightStr("!!!!!!!!", 2_147_221_512)); - @Test - public void rightStrInputNotNullNegativeOutputStringIndexOutOfBoundsException() { - - // Arrange - final String str = "!!!!!!!!"; - final int length = -1_048_568; - - // Act thrown.expect(StringIndexOutOfBoundsException.class); - Formats.rightStr(str, length); - + rightStr("!!!!!!!!", -1_048_568); // Method is not expected to return due to exception thrown } @Test - public void rightStrInputNotNullPositiveOutputNotNull() { - - // Arrange - final String str = "!!!!!!!!"; - final int length = 2_147_221_512; - - // Act - final String retval = Formats.rightStr(str, length); - - // Assert result - Assert.assertEquals("!!!!!!!!", retval); - } - - @Test - public void shortNameInputNotNullPositivePositiveOutputNotNull() { - - // Arrange - final String str = "!"; - final int length = 1; - final int rightLength = 6; - - // Act - final String retval = Formats.shortName(str, length, rightLength); - - // Assert result - Assert.assertEquals("!", retval); - } - - @Test - public void shortNameInputNotNullPositiveZeroOutputNotNull() { - - // Arrange - final String str = "!!!!!!!!!!"; - final int length = 4; - final int rightLength = 0; - - // Act - final String retval = Formats.shortName(str, length, rightLength); - - // Assert result - Assert.assertEquals("!...", retval); - } - - @Test - public void shortNameInputNotNullZeroNegativeOutputStringIndexOutOfBoundsException() { - - // Arrange - final String str = "!!!!!!!!!!"; - final int length = 0; - final int rightLength = -19; + public void testShortName() { + Assert.assertEquals("!", shortName("!", 1, 6)); + Assert.assertEquals("!...", shortName("!!!!!!!!!!", 4, 0)); - // Act thrown.expect(StringIndexOutOfBoundsException.class); - Formats.shortName(str, length, rightLength); - + shortName("!!!!!!!!!!", 0, -19); // Method is not expected to return due to exception thrown } @Test - public void toFixLengthSizeUnitInputNullOutputNotNull() { - - // Arrange - final Long size = null; - - // Act - final String retval = Formats.toFixLengthSizeUnit(size); - - // Assert result - Assert.assertEquals("NaN", retval); + public void testToFixLengthSizeUnit() { + Assert.assertEquals("NaN", toFixLengthSizeUnit(null)); + Assert.assertEquals("4194304t", + toFixLengthSizeUnit(4_611_686_018_427_387_906L)); } @Test - public void toFixLengthSizeUnitInputPositiveOutputNotNull() { - - // Arrange - final Long size = 4_611_686_018_427_387_906L; - - // Act - final String retval = Formats.toFixLengthSizeUnit(size); - - // Assert result - Assert.assertEquals("4194304t", retval); + public void testToMB() { + Assert.assertEquals("NaN", toMB(-8L)); + Assert.assertEquals("0m", toMB(8L)); } @Test - public void toMBInputNegativeOutputNotNull() { - - // Arrange - final long bytes = -8L; - - // Act - final String retval = Formats.toMB(bytes); - - // Assert result - Assert.assertEquals("NaN", retval); + public void testToSizeUnit() { + Assert.assertEquals("-1023", toSizeUnit(-1023L)); + Assert.assertEquals("NaN", toSizeUnit(null)); + Assert.assertEquals("44k", toSizeUnit(45_312L)); } @Test - public void toMBInputPositiveOutputNotNull() { - - // Arrange - final long bytes = 8L; - - // Act - final String retval = Formats.toMB(bytes); - - // Assert result - Assert.assertEquals("0m", retval); - } - - @Test - public void toSizeUnitInputNegativeOutputNotNull() { - - // Arrange - final Long size = -1023L; - - // Act - final String retval = Formats.toSizeUnit(size); - - // Assert result - Assert.assertEquals("-1023", retval); - } - - @Test - public void toSizeUnitInputNullOutputNotNull() { - - // Arrange - final Long size = null; - - // Act - final String retval = Formats.toSizeUnit(size); - - // Assert result - Assert.assertEquals("NaN", retval); - } - - @Test - public void toSizeUnitInputPositiveOutputNotNull() { - - // Arrange - final Long size = 45_312L; - - // Act - final String retval = Formats.toSizeUnit(size); - - // Assert result - Assert.assertEquals("44k", retval); - } - - @Test - public void toSizeUnitWithColorInputNullNullOutputNullPointerException() { - - // Arrange - final Long size = null; - final LongWarning warning = null; - - // Act + public void testToSizeUnitWithColor() { thrown.expect(NullPointerException.class); - Formats.toSizeUnitWithColor(size, warning); - + toSizeUnitWithColor(null, null); // Method is not expected to return due to exception thrown } From b78b7e917775b587211d2d9879c582e652c99a3e Mon Sep 17 00:00:00 2001 From: Peter Sear Date: Tue, 23 Apr 2019 16:10:59 +0100 Subject: [PATCH 2/2] Add unit tests for com.vip.vjtools.vjtop.util.Formats These tests were written using Diffblue Cover. --- .../vip/vjtools/vjtop/util/FormatsTest.java | 76 +++++++++++++++---- 1 file changed, 61 insertions(+), 15 deletions(-) diff --git a/vjtop/src/test/java/com/vip/vjtools/vjtop/util/FormatsTest.java b/vjtop/src/test/java/com/vip/vjtools/vjtop/util/FormatsTest.java index fe277661..f66b17f3 100644 --- a/vjtop/src/test/java/com/vip/vjtools/vjtop/util/FormatsTest.java +++ b/vjtop/src/test/java/com/vip/vjtools/vjtop/util/FormatsTest.java @@ -1,40 +1,69 @@ package com.vip.vjtools.vjtop.util; -import org.junit.Assert; +import com.vip.vjtools.vjtop.VMInfo.Usage; +import com.vip.vjtools.vjtop.WarningRule.LongWarning; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import java.util.ArrayList; import static com.vip.vjtools.vjtop.util.Formats.*; +import static org.junit.Assert.*; public class FormatsTest { @Rule public ExpectedException thrown = ExpectedException.none(); + @Test + public void testFormatUsage() { + assertEquals("0m/0m", formatUsage(new Usage(1L, 2L, 2L))); + assertEquals("0m/0m/0m", formatUsage(new Usage(1L, 1L, 2L))); + } + + @Test + public void testFormatUsageWithColor() { + assertEquals("0m/0m", + formatUsageWithColor(new Usage(1L, 2L, 2L), new LongWarning())); + } + @Test public void testJoin() { final ArrayList list = new ArrayList(); final String delim = "AAAAAAAA"; - Assert.assertEquals("", join(list, delim)); + assertEquals("", join(list, delim)); list.add(""); - Assert.assertEquals("", join(list, delim)); + assertEquals("", join(list, delim)); } @Test public void testLeftStr() { - Assert.assertEquals("", leftStr("!!!!!!!!", 0)); + assertEquals("", leftStr("!!!!!!!!", 0)); thrown.expect(StringIndexOutOfBoundsException.class); leftStr("!", -536_870_911); // Method is not expected to return due to exception thrown } + @Test + public void testParseFromSize() { + assertEquals(-1L, parseFromSize(null)); + assertEquals(1024L, parseFromSize("1k")); + assertEquals(1024L, parseFromSize("1kb")); + assertEquals(1_048_576L, parseFromSize("1mb")); + assertEquals(1_048_576L, parseFromSize("1m")); + assertEquals(1_073_741_824L, parseFromSize("1gb")); + assertEquals(1_073_741_824L, parseFromSize("1g")); + assertEquals(1_099_511_627_776L, parseFromSize("1tb")); + assertEquals(1_099_511_627_776L, parseFromSize("1t")); + assertEquals(44L, parseFromSize("44bytes")); + assertEquals(-1L, parseFromSize("!!!")); + } + @Test public void testRightStr() { - Assert.assertEquals("!!!!!!!!", rightStr("!!!!!!!!", 2_147_221_512)); + assertEquals("!!!!!!!!", rightStr("!!!!!!!!", 2_147_221_512)); thrown.expect(StringIndexOutOfBoundsException.class); rightStr("!!!!!!!!", -1_048_568); @@ -43,8 +72,8 @@ public void testRightStr() { @Test public void testShortName() { - Assert.assertEquals("!", shortName("!", 1, 6)); - Assert.assertEquals("!...", shortName("!!!!!!!!!!", 4, 0)); + assertEquals("!", shortName("!", 1, 6)); + assertEquals("!...", shortName("!!!!!!!!!!", 4, 0)); thrown.expect(StringIndexOutOfBoundsException.class); shortName("!!!!!!!!!!", 0, -19); @@ -53,29 +82,46 @@ public void testShortName() { @Test public void testToFixLengthSizeUnit() { - Assert.assertEquals("NaN", toFixLengthSizeUnit(null)); - Assert.assertEquals("4194304t", - toFixLengthSizeUnit(4_611_686_018_427_387_906L)); + assertEquals("NaN", toFixLengthSizeUnit(null)); + assertEquals(" 123", toFixLengthSizeUnit(123L)); + assertEquals(" 120k", toFixLengthSizeUnit(123_456L)); + assertEquals(" 117m", toFixLengthSizeUnit(123_456_789L)); + assertEquals(" 114g", toFixLengthSizeUnit(123_456_789_000L)); + assertEquals("4194304t", toFixLengthSizeUnit(4_611_686_018_427_387_906L)); } @Test public void testToMB() { - Assert.assertEquals("NaN", toMB(-8L)); - Assert.assertEquals("0m", toMB(8L)); + assertEquals("NaN", toMB(-8L)); + assertEquals("0m", toMB(8L)); + assertEquals("114g", toMB(123_456_789_000L)); } @Test public void testToSizeUnit() { - Assert.assertEquals("-1023", toSizeUnit(-1023L)); - Assert.assertEquals("NaN", toSizeUnit(null)); - Assert.assertEquals("44k", toSizeUnit(45_312L)); + assertEquals("-1023", toSizeUnit(-1023L)); + assertEquals("NaN", toSizeUnit(null)); + assertEquals("44k", toSizeUnit(45_312L)); + assertEquals("117m", toSizeUnit(123_456_789L)); + assertEquals("114g", toSizeUnit(123_456_789_000L)); + assertEquals("112t", toSizeUnit(123_456_789_000_000L)); } @Test public void testToSizeUnitWithColor() { + assertEquals("123", toSizeUnitWithColor(123L, new LongWarning())); + thrown.expect(NullPointerException.class); toSizeUnitWithColor(null, null); // Method is not expected to return due to exception thrown } + @Test + public void testToTimeUnit() { + assertEquals("01s", toTimeUnit(1_000L)); + assertEquals("02m00s", toTimeUnit(120_000L)); + assertEquals("02h00m", toTimeUnit(7_200_000L)); + assertEquals("2d00h", toTimeUnit(172_800_000L)); + } + }