From bea432d2931e531feb6e2ea6609cc11207d3a815 Mon Sep 17 00:00:00 2001 From: Louis Mills Date: Wed, 14 Nov 2018 16:19:52 +0000 Subject: [PATCH 1/2] Add unit tests for com.vip.vjtools.vjtop.util.Formats --- vjtop/pom.xml | 7 + .../vip/vjtools/vjtop/util/FormatsTest.java | 249 ++++++++++++++++++ 2 files changed, 256 insertions(+) create mode 100644 vjtop/src/test/java/com/vip/vjtools/vjtop/util/FormatsTest.java diff --git a/vjtop/pom.xml b/vjtop/pom.xml index d426e151..22aadf82 100644 --- a/vjtop/pom.xml +++ b/vjtop/pom.xml @@ -16,6 +16,7 @@ ${java.version} ${java.version} 1.6 + 4.12 @@ -31,6 +32,12 @@ system ${toolsjar} + + junit + junit + ${junit.version} + test + 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 new file mode 100644 index 00000000..cb44d63e --- /dev/null +++ b/vjtop/src/test/java/com/vip/vjtools/vjtop/util/FormatsTest.java @@ -0,0 +1,249 @@ +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; + +public class FormatsTest { + + @Rule public ExpectedException thrown = ExpectedException.none(); + + @Test + public void joinInput0NotNullOutputNotNull() { + + // Arrange + 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() { + + // 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); + } + + @Test + public void leftStrInputNotNullNegativeOutputStringIndexOutOfBoundsException() { + + // Arrange + final String str = "!"; + final int length = -536_870_911; + + // Act + thrown.expect(StringIndexOutOfBoundsException.class); + Formats.leftStr(str, length); + + // 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); + } + + @Test + public void rightStrInputNotNullNegativeOutputStringIndexOutOfBoundsException() { + + // Arrange + final String str = "!!!!!!!!"; + final int length = -1_048_568; + + // Act + thrown.expect(StringIndexOutOfBoundsException.class); + Formats.rightStr(str, length); + + // 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; + + // Act + thrown.expect(StringIndexOutOfBoundsException.class); + Formats.shortName(str, length, rightLength); + + // 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); + } + + @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); + } + + @Test + public void toMBInputNegativeOutputNotNull() { + + // Arrange + final long bytes = -8L; + + // Act + final String retval = Formats.toMB(bytes); + + // Assert result + Assert.assertEquals("NaN", retval); + } + + @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 + thrown.expect(NullPointerException.class); + Formats.toSizeUnitWithColor(size, warning); + + // Method is not expected to return due to exception thrown + } + +} From 2d4ef7a13f539cb306a4880dd27e78d6ee9f3818 Mon Sep 17 00:00:00 2001 From: Louis Mills Date: Wed, 14 Nov 2018 16:20:43 +0000 Subject: [PATCH 2/2] Add unit tests for com.vip.vjtools.vjtop.util.Utils --- .../com/vip/vjtools/vjtop/util/UtilsTest.java | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 vjtop/src/test/java/com/vip/vjtools/vjtop/util/UtilsTest.java diff --git a/vjtop/src/test/java/com/vip/vjtools/vjtop/util/UtilsTest.java b/vjtop/src/test/java/com/vip/vjtools/vjtop/util/UtilsTest.java new file mode 100644 index 00000000..a05ec955 --- /dev/null +++ b/vjtop/src/test/java/com/vip/vjtools/vjtop/util/UtilsTest.java @@ -0,0 +1,124 @@ +package com.vip.vjtools.vjtop.util; + +import com.vip.vjtools.vjtop.util.Utils; +import org.junit.Assert; +import org.junit.Test; + +public class UtilsTest { + + @Test + public void calcLoadInputNegativeZeroZeroOutputZero() { + + // Arrange + final Long deltaCpuTime = -4_194_304L; + final long deltaUptime = 0L; + final long factor = 0L; + + // Act + final double retval = Utils.calcLoad(deltaCpuTime, deltaUptime, factor); + + // Assert result + Assert.assertEquals(0.0, retval, 0.0); + } + + @Test + public void calcLoadInputPositivePositiveOutputPositive() { + + // Arrange + final long deltaCpuTime = 558_348_370L; + final long deltaUptime = 1L; + + // Act + final double retval = Utils.calcLoad(deltaCpuTime, deltaUptime); + + // Assert result + Assert.assertEquals(0x1.a0008001p+35 /* 5.58348e+10 */, retval, 0.0); + } + + @Test + public void calcLoadInputPositivePositiveZeroOutputPositiveInfinity() { + + // Arrange + final Long deltaCpuTime = 4_194_304L; + final long deltaUptime = 4_611_686_018_427_387_904L; + final long factor = 0L; + + // Act + final double retval = Utils.calcLoad(deltaCpuTime, deltaUptime, factor); + + // Assert result + Assert.assertEquals(Double.POSITIVE_INFINITY, retval, 0.0); + } + + @Test + public void calcLoadInputPositiveZeroOutputZero() { + + // Arrange + final long deltaCpuTime = 558_348_370L; + final long deltaUptime = 0L; + + // Act + final double retval = Utils.calcLoad(deltaCpuTime, deltaUptime); + + // Assert result + Assert.assertEquals(0.0, retval, 0.0); + } + + @Test + public void calcLoadInputPositiveZeroZeroOutputZero() { + + // Arrange + final Long deltaCpuTime = 4_194_304L; + final long deltaUptime = 0L; + final long factor = 0L; + + // Act + final double retval = Utils.calcLoad(deltaCpuTime, deltaUptime, factor); + + // Assert result + Assert.assertEquals(0.0, retval, 0.0); + } + + @Test + public void calcLoadInputZeroZeroOutputZero() { + + // Arrange + final long deltaCpuTime = 0L; + final long deltaUptime = 0L; + + // Act + final double retval = Utils.calcLoad(deltaCpuTime, deltaUptime); + + // Assert result + Assert.assertEquals(0.0, retval, 0.0); + } + + @Test + public void calcMemoryUtilizationInputNegativePositiveOutputNegative() { + + // Arrange + final Long threadBytes = -507_680_768_073_012_547L; + final long totalBytes = 1_147_301_170_758_571_992L; + + // Act + final double retval = Utils.calcMemoryUtilization(threadBytes, totalBytes); + + // Assert result + Assert.assertEquals(-0x1.6200000024f82p+5 /* -44.25 */, retval, 0.0); + } + + @Test + public void calcMemoryUtilizationInputPositiveZeroOutputZero() { + + // Arrange + final Long threadBytes = 1L; + final long totalBytes = 0L; + + // Act + final double retval = Utils.calcMemoryUtilization(threadBytes, totalBytes); + + // Assert result + Assert.assertEquals(0.0, retval, 0.0); + } + +}