From 327a6af66fc000c3deb599f814b456598030564e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 12 Nov 2022 19:44:39 +0100 Subject: [PATCH] Continued: - added utilities class EnumUtils for enumerations - added unit tests for method EnumUtils.compare() - sorted members - ignored some temporary files --- .gitignore | 1 + .../jcoreutils/bool/BooleanUtils.java | 5 +- .../mxchange/jcoreutils/enums/EnumUtils.java | 57 +++++++ .../jcoreutils/bool/BooleanUtilsTest.java | 42 ++--- .../jcoreutils/enums/EnumUtilsTest.java | 144 ++++++++++++++++++ 5 files changed, 226 insertions(+), 23 deletions(-) create mode 100644 src/org/mxchange/jcoreutils/enums/EnumUtils.java create mode 100644 test/org/mxchange/jcoreutils/enums/EnumUtilsTest.java diff --git a/.gitignore b/.gitignore index 59b501c..9c64bb2 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ nbproject/*~ .~lock* .gitcommits /*.log +jacoco.exec-* diff --git a/src/org/mxchange/jcoreutils/bool/BooleanUtils.java b/src/org/mxchange/jcoreutils/bool/BooleanUtils.java index c1e041d..d4547e0 100644 --- a/src/org/mxchange/jcoreutils/bool/BooleanUtils.java +++ b/src/org/mxchange/jcoreutils/bool/BooleanUtils.java @@ -19,14 +19,15 @@ package org.mxchange.jcoreutils.bool; import java.util.Objects; /** - * Utilities class for Boolean instances (not primitive boolean) + * Utilities class for Boolean instances (not primitive boolean), most methods + * here are null-safe or throw an NPE at you. *

* @author Roland Häder */ public class BooleanUtils { /** - * Compares two Boolean instances with each other + * Compares two Boolean instances null-safe with each other *

* @param boolean1 Boolean instance 1 * @param boolean2 Boolean instance 2 diff --git a/src/org/mxchange/jcoreutils/enums/EnumUtils.java b/src/org/mxchange/jcoreutils/enums/EnumUtils.java new file mode 100644 index 0000000..0d34b32 --- /dev/null +++ b/src/org/mxchange/jcoreutils/enums/EnumUtils.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2022 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcoreutils.enums; + +/** + * A utilities class for enumerations + *

+ * @author Roland Häder + */ +public class EnumUtils { + + /** + * Compares two enumerations with each other in a null-safe manner + *

+ * @param enum1 Enumeration 1 + * @param enum2 Enumeration 2 + *

+ * @return Comparison value + */ + public static int compare (final Enum enum1, final Enum enum2) { + // Check parameters on null + if (enum1 == enum2) { + // Both are equal + return 0; + } else if (null == enum1) { + // Left one is null + return -1; + } else if (null == enum2) { + // Right one is null + return 1; + } + + // Compare both enumeration's names + return enum1.name().compareTo(enum2.name()); + } + + /** + * No instances from utility classes + */ + private EnumUtils () { + } + +} diff --git a/test/org/mxchange/jcoreutils/bool/BooleanUtilsTest.java b/test/org/mxchange/jcoreutils/bool/BooleanUtilsTest.java index 2fafac9..76c96ff 100644 --- a/test/org/mxchange/jcoreutils/bool/BooleanUtilsTest.java +++ b/test/org/mxchange/jcoreutils/bool/BooleanUtilsTest.java @@ -50,6 +50,19 @@ public class BooleanUtilsTest { }; } + @DataProvider (name = "left-null-boolean-provider") + public Object[][] createLeftNullBooleans () { + return new Object[][]{ + { + null, + BOOLEAN1 + }, { + null, + BOOLEAN2 + } + }; + } + @DataProvider (name = "right-null-boolean-provider") public Object[][] createRightNullBooleans () { return new Object[][]{ @@ -76,23 +89,10 @@ public class BooleanUtilsTest { }; } - @DataProvider (name = "left-null-boolean-provider") - public Object[][] createLeftNullBooleans () { - return new Object[][]{ - { - null, - BOOLEAN1 - }, { - null, - BOOLEAN2 - } - }; - } - - @Test (description = "Tests method BooleanUtils.compare() when same boolean is provided", dataProvider = "same-boolean-provider") - public void testSameBoolean (final Boolean boolean1, final Boolean boolean2) { - // Should always return 0 - Assert.assertEquals(BooleanUtils.compare(boolean1, boolean2), 0); + @Test (description = "Tests method BooleanUtils.compare() when different boolean is provided", dataProvider = "different-boolean-provider") + public void testDifferentBoolean (final Boolean boolean1, final Boolean boolean2) { + // Should never return 0 + Assert.assertNotEquals(BooleanUtils.compare(boolean1, boolean2), 0); } @Test (description = "Tests method BooleanUtils.compare() when left-null boolean is provided", dataProvider = "left-null-boolean-provider") @@ -107,10 +107,10 @@ public class BooleanUtilsTest { Assert.assertEquals(BooleanUtils.compare(boolean1, boolean2), 1); } - @Test (description = "Tests method BooleanUtils.compare() when different boolean is provided", dataProvider = "different-boolean-provider") - public void testDifferentBoolean (final Boolean boolean1, final Boolean boolean2) { - // Should never return 0 - Assert.assertNotEquals(BooleanUtils.compare(boolean1, boolean2), 0); + @Test (description = "Tests method BooleanUtils.compare() when same boolean is provided", dataProvider = "same-boolean-provider") + public void testSameBoolean (final Boolean boolean1, final Boolean boolean2) { + // Should always return 0 + Assert.assertEquals(BooleanUtils.compare(boolean1, boolean2), 0); } } diff --git a/test/org/mxchange/jcoreutils/enums/EnumUtilsTest.java b/test/org/mxchange/jcoreutils/enums/EnumUtilsTest.java new file mode 100644 index 0000000..59fcd25 --- /dev/null +++ b/test/org/mxchange/jcoreutils/enums/EnumUtilsTest.java @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2022 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcoreutils.enums; + +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/** + * A test case for utilities class EnumUtils + *

+ * @author Roland Häder + */ +public class EnumUtilsTest { + + /** + * Default constructor + */ + public EnumUtilsTest () { + } + + @DataProvider (name = "different-null-enum-provider") + public Object[][] createDifferentNullEnum () { + return new Object[][]{ + { + TestEnum.TEST_VALUE1, + TestEnum.TEST_VALUE2 + }, { + TestEnum.TEST_VALUE2, + TestEnum.TEST_VALUE1 + } + }; + } + + @DataProvider (name = "left-null-enum-provider") + public Object[][] createLeftNullEnum () { + return new Object[][]{ + { + null, + TestEnum.TEST_VALUE1}, { + null, + TestEnum.TEST_VALUE2 + } + }; + } + + @DataProvider (name = "right-null-enum-provider") + public Object[][] createRightNullEnum () { + return new Object[][]{ + { + TestEnum.TEST_VALUE1, + null + }, { + TestEnum.TEST_VALUE2, + null + } + }; + } + + @DataProvider (name = "same-null-enum-provider") + public Object[][] createSameNullEnum () { + return new Object[][]{ + { + TestEnum.TEST_VALUE1, + TestEnum.TEST_VALUE1 + }, { + TestEnum.TEST_VALUE2, + TestEnum.TEST_VALUE2 + } + }; + } + + @Test (description = "Tests method EnumUtils.compare() with different enum", dataProvider = "different-null-enum-provider") + public void testEnumCompareDifferentEnum (final TestEnum enum1, final TestEnum enum2) { + // Should never return zero + Assert.assertNotEquals(EnumUtils.compare(enum1, enum2), 0); + } + + @Test (description = "Tests method EnumUtils.compare() with left-null enum", dataProvider = "left-null-enum-provider") + public void testEnumCompareLeftNullEnum (final TestEnum enum1, final TestEnum enum2) { + // Should always return -1 + Assert.assertEquals(EnumUtils.compare(enum1, enum2), -1); + } + + @Test (description = "Tests method EnumUtils.compare() with right-null enum", dataProvider = "right-null-enum-provider") + public void testEnumCompareRightNullEnum (final TestEnum enum1, final TestEnum enum2) { + // Should always return 1 + Assert.assertEquals(EnumUtils.compare(enum1, enum2), 1); + } + + @Test (description = "Tests method EnumUtils.compare() with same enum", dataProvider = "same-null-enum-provider") + public void testEnumCompareSameEnum (final TestEnum enum1, final TestEnum enum2) { + // Should always return zero + Assert.assertEquals(EnumUtils.compare(enum1, enum2), 0); + } + + /** + * Enumeration with a parameter for testing purposes + */ + private enum TestEnum { + // First test value + TEST_VALUE1("SOME_FOO"), + // Second test value + TEST_VALUE2("SOME_BAR"); + + /** + * Test string + */ + private final String testString; + + /** + * Constructor + *

+ * @param testString Test string + */ + private TestEnum (final String testString) { + this.testString = testString; + } + + /** + * Getter for test string + *

+ * @return Test string + */ + public String getTestString () { + return this.testString; + } + } + +} -- 2.39.5