From 05d39754aa31e17e40d3425c2fefb65e39d1ef34 Mon Sep 17 00:00:00 2001 From: John Bergqvist <31512273+JohnLBergqvist@users.noreply.github.com> Date: Wed, 10 Jul 2019 18:02:04 +0100 Subject: [PATCH] Add unit tests for org.apache.jorphan.util.JOrphanUtils (#472) These tests were written using Diffblue Cover --- .../apache/jorphan/util/TestJorphanUtils.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test/src/org/apache/jorphan/util/TestJorphanUtils.java b/test/src/org/apache/jorphan/util/TestJorphanUtils.java index 9bd5133934..3d9189f3d0 100644 --- a/test/src/org/apache/jorphan/util/TestJorphanUtils.java +++ b/test/src/org/apache/jorphan/util/TestJorphanUtils.java @@ -255,6 +255,12 @@ public class TestJorphanUtils { assertEquals("abc",JOrphanUtils.trim("abc ;", " ;")); } + @Test + public void testGetByteArraySlice() throws Exception { + Assert.assertArrayEquals(new byte[] {1, 2}, + JOrphanUtils.getByteArraySlice(new byte[]{0, 1, 2, 3}, 1, 2)); + } + @Test public void testbaToHexString(){ assertEquals("",JOrphanUtils.baToHexString(new byte[]{})); @@ -262,6 +268,13 @@ public class TestJorphanUtils { assertEquals("0f107f8081ff",JOrphanUtils.baToHexString(new byte[]{15,16,127,-128,-127,-1})); } + @Test + public void testBaToHexStringSeparator(){ + assertEquals("", JOrphanUtils.baToHexString(new byte[]{}, '-')); + assertEquals("00", JOrphanUtils.baToHexString(new byte[]{0}, '-')); + assertEquals("0f-10-7f-80-81-ff", JOrphanUtils.baToHexString(new byte[]{15,16,127,-128,-127,-1}, '-')); + } + @Test public void testbaToByte() throws Exception{ assertEqualsArray(new byte[]{},JOrphanUtils.baToHexBytes(new byte[]{})); @@ -277,6 +290,13 @@ public class TestJorphanUtils { } } + @Test + public void testNullifyIfEmptyTrimmed() { + Assert.assertNull(JOrphanUtils.nullifyIfEmptyTrimmed(null)); + Assert.assertNull(JOrphanUtils.nullifyIfEmptyTrimmed("\u0001")); + assertEquals("1234", JOrphanUtils.nullifyIfEmptyTrimmed("1234")); + } + @Test public void testIsBlank() { assertTrue(JOrphanUtils.isBlank("")); @@ -295,6 +315,24 @@ public class TestJorphanUtils { assertEquals("baulpismuth", JOrphanUtils.rightAlign(in, 6).toString()); in = new StringBuilder("A"); assertEquals(" A", JOrphanUtils.rightAlign(in, 8).toString()); + assertEquals(" foo", + JOrphanUtils.rightAlign(new StringBuilder("foo"), 39).toString()); + } + + @Test + public void testLeftAlign() { + assertEquals("foo ", + JOrphanUtils.leftAlign(new StringBuilder("foo"), 5).toString()); + assertEquals("foo", + JOrphanUtils.leftAlign(new StringBuilder("foo"), 2).toString()); + assertEquals("foo ", + JOrphanUtils.leftAlign(new StringBuilder("foo"), 39).toString()); + } + + @Test + public void testBooleanToSTRING() { + assertEquals("TRUE", JOrphanUtils.booleanToSTRING(true)); + assertEquals("FALSE", JOrphanUtils.booleanToSTRING(false)); } @Test @@ -357,4 +395,11 @@ public class TestJorphanUtils { public void testReplaceValueWithNullSetterThatGetsCalled() { JOrphanUtils.replaceValue("\\d+", "${port}", true, "80", null); } + + @Test + public void testUnsplit() { + assertEquals("", JOrphanUtils.unsplit(new Object[] {null, null}, 0)); + assertEquals("11", JOrphanUtils.unsplit(new Object[] {null, 1}, 1)); + assertEquals("-26738698", JOrphanUtils.unsplit(new Object[] {-26_738_698}, 1)); + } }