From b5b2ac856048e875b919e28b01f751c03fa90ed4 Mon Sep 17 00:00:00 2001 From: "tomasz.kowalczewski" Date: Thu, 22 Apr 2021 09:29:29 +0200 Subject: [PATCH] Re #48: Changes and fixes to replace methods not available in Java 8. --- .../pl/tkowalcz/tjahzi/utils/Objects.java | 29 ++++ .../pl/tkowalcz/tjahzi/utils/TextBuilder.java | 1 - .../pl/tkowalcz/tjahzi/utils/ObjectsTest.java | 140 ++++++++++++++++++ .../java/pl/tkowalcz/tjahzi/log4j2/Label.java | 6 - .../tjahzi/log4j2/LabelFactoryTest.java | 22 +-- pom.xml | 15 +- 6 files changed, 189 insertions(+), 24 deletions(-) create mode 100644 core/src/main/java/pl/tkowalcz/tjahzi/utils/Objects.java create mode 100644 core/src/test/java/pl/tkowalcz/tjahzi/utils/ObjectsTest.java diff --git a/core/src/main/java/pl/tkowalcz/tjahzi/utils/Objects.java b/core/src/main/java/pl/tkowalcz/tjahzi/utils/Objects.java new file mode 100644 index 0000000..5659164 --- /dev/null +++ b/core/src/main/java/pl/tkowalcz/tjahzi/utils/Objects.java @@ -0,0 +1,29 @@ +package pl.tkowalcz.tjahzi.utils; + +public class Objects { + + public static void checkIndex(int index, int length) { + if (index < 0 || index >= length) { + throw new IndexOutOfBoundsException( + String.format( + "Index out of bounds for input params: index = %d, length = %d", + index, + length + ) + ); + } + } + + public static void checkFromToIndex(int start, int end, int length) { + if (start < 0 || start > end || end > length) { + throw new IndexOutOfBoundsException( + String.format( + "Index out of bounds for input params: start = %d, end = %d, length = %d", + start, + end, + length + ) + ); + } + } +} diff --git a/core/src/main/java/pl/tkowalcz/tjahzi/utils/TextBuilder.java b/core/src/main/java/pl/tkowalcz/tjahzi/utils/TextBuilder.java index 640618d..6aff6bd 100644 --- a/core/src/main/java/pl/tkowalcz/tjahzi/utils/TextBuilder.java +++ b/core/src/main/java/pl/tkowalcz/tjahzi/utils/TextBuilder.java @@ -1,7 +1,6 @@ package pl.tkowalcz.tjahzi.utils; import java.util.Arrays; -import java.util.Objects; public class TextBuilder implements Appendable, CharSequence { diff --git a/core/src/test/java/pl/tkowalcz/tjahzi/utils/ObjectsTest.java b/core/src/test/java/pl/tkowalcz/tjahzi/utils/ObjectsTest.java new file mode 100644 index 0000000..f2b5c7c --- /dev/null +++ b/core/src/test/java/pl/tkowalcz/tjahzi/utils/ObjectsTest.java @@ -0,0 +1,140 @@ +package pl.tkowalcz.tjahzi.utils; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class ObjectsTest { + + @Test + void shouldAllowIndexSmallerThanLength() { + // Given + int index = 12; + int length = 20; + + // When & Then + assertDoesNotThrow(() -> Objects.checkIndex(index, length)); + } + + @Test + void shouldThrowIfIndexNegative() { + // Given + int index = -5; + int length = 20; + + // When & Then + Assertions.assertThat( + assertThrows(IndexOutOfBoundsException.class, () -> + Objects.checkIndex(index, length) + ) + ).hasMessage("Index out of bounds for input params: index = -5, length = 20"); + } + + @Test + void shouldThrowIfIndexEqualToLength() { + // Given + int index = 20; + int length = 20; + + // When & Then + Assertions.assertThat( + assertThrows(IndexOutOfBoundsException.class, () -> + Objects.checkIndex(index, length) + ) + ).hasMessage("Index out of bounds for input params: index = 20, length = 20"); + } + + @Test + void shouldThrowIfIndexGreaterThanLength() { + // Given + int index = 21; + int length = 20; + + // When & Then + Assertions.assertThat( + assertThrows(IndexOutOfBoundsException.class, () -> + Objects.checkIndex(index, length) + ) + ).hasMessage("Index out of bounds for input params: index = 21, length = 20"); + } + + @Test + void shouldAllowStartAndEndSmallerThanLength() { + // Given + int start = 12; + int end = 19; + int length = 20; + + // When & Then + assertDoesNotThrow(() -> Objects.checkFromToIndex(start, end, length)); + } + + @Test + void shouldThrowIfStartNegative() { + // Given + int start = -12; + int end = 12; + int length = 20; + + // When & Then + Assertions.assertThat( + assertThrows(IndexOutOfBoundsException.class, () -> + Objects.checkFromToIndex(start, end, length) + ) + ).hasMessage("Index out of bounds for input params: start = -12, end = 12, length = 20"); + } + + @Test + void shouldNotThrowIfStartEqualToEnd() { + // Given + int start = 12; + int end = 12; + int length = 20; + + // When & Then + assertDoesNotThrow(() -> Objects.checkFromToIndex(start, end, length)); + } + + @Test + void shouldThrowIfStartGreaterThanEnd() { + // Given + int start = 15; + int end = 12; + int length = 20; + + // When & Then + Assertions.assertThat( + assertThrows(IndexOutOfBoundsException.class, () -> + Objects.checkFromToIndex(start, end, length) + ) + ).hasMessage("Index out of bounds for input params: start = 15, end = 12, length = 20"); + } + + @Test + void shouldNotThrowIfEndEqualToLength() { + // Given + int start = 15; + int end = 20; + int length = 20; + + // When & Then + assertDoesNotThrow(() -> Objects.checkFromToIndex(start, end, length)); + } + + @Test + void shouldThrowIfEndEqualGreaterThanLength() { + // Given + int start = 15; + int end = 21; + int length = 20; + + // When & Then + Assertions.assertThat( + assertThrows(IndexOutOfBoundsException.class, () -> + Objects.checkFromToIndex(start, end, length) + ) + ).hasMessage("Index out of bounds for input params: start = 15, end = 21, length = 20"); + } +} diff --git a/log4j2-appender/src/main/java/pl/tkowalcz/tjahzi/log4j2/Label.java b/log4j2-appender/src/main/java/pl/tkowalcz/tjahzi/log4j2/Label.java index 81f034e..283b6e2 100644 --- a/log4j2-appender/src/main/java/pl/tkowalcz/tjahzi/log4j2/Label.java +++ b/log4j2-appender/src/main/java/pl/tkowalcz/tjahzi/log4j2/Label.java @@ -8,7 +8,6 @@ import org.apache.logging.log4j.core.config.plugins.PluginFactory; import org.apache.logging.log4j.core.config.plugins.PluginValue; import org.apache.logging.log4j.status.StatusLogger; -import java.util.Map; import java.util.regex.Pattern; @Plugin(name = "label", category = Node.CATEGORY, printObject = true) @@ -37,11 +36,6 @@ public class Label extends Property { return hasValidName(getName()); } - // @VisibleForTests - public Map.Entry asEntry() { - return Map.entry(getName(), getValue()); - } - public static boolean hasValidName(String label) { return LABEL_NAME_PATTER.matcher(label).matches(); } diff --git a/log4j2-appender/src/test/java/pl/tkowalcz/tjahzi/log4j2/LabelFactoryTest.java b/log4j2-appender/src/test/java/pl/tkowalcz/tjahzi/log4j2/LabelFactoryTest.java index 477790e..4e5a8e3 100644 --- a/log4j2-appender/src/test/java/pl/tkowalcz/tjahzi/log4j2/LabelFactoryTest.java +++ b/log4j2-appender/src/test/java/pl/tkowalcz/tjahzi/log4j2/LabelFactoryTest.java @@ -28,8 +28,8 @@ class LabelFactoryTest { // Then assertThat(actual).containsOnly( - thisShouldStay.asEntry(), - thisTooShouldStay.asEntry() + asEntry(thisShouldStay), + asEntry(thisTooShouldStay) ); } @@ -52,8 +52,8 @@ class LabelFactoryTest { // Then assertThat(actual).containsOnly( - thisShouldStay.asEntry(), - thisShouldStayToo.asEntry() + asEntry(thisShouldStay), + asEntry(thisShouldStayToo) ); } @@ -74,8 +74,8 @@ class LabelFactoryTest { // Then assertThat(actual).containsOnly( - label1.asEntry(), - label2.asEntry() + asEntry(label1), + asEntry(label2) ); } @@ -95,8 +95,8 @@ class LabelFactoryTest { HashMap labels = new HashMap<>( Map.ofEntries( - thisShouldStay.asEntry(), - thisShouldBeRemovedDueToConflict.asEntry() + asEntry(thisShouldStay), + asEntry(thisShouldBeRemovedDueToConflict) ) ); @@ -105,7 +105,7 @@ class LabelFactoryTest { // Then assertThat(labels).containsOnly( - thisShouldStay.asEntry() + asEntry(thisShouldStay) ); assertThat(actual).isEqualTo(logLevelLabel); @@ -137,4 +137,8 @@ class LabelFactoryTest { // Then assertThat(actual).isNull(); } + + public Map.Entry asEntry(Label label) { + return Map.entry(label.getName(), label.getValue()); + } } diff --git a/pom.xml b/pom.xml index 3980a36..3235a88 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,5 @@ - + 4.0.0 @@ -12,12 +13,14 @@ https://github.com/tkowalcz/tjahzi + core loki-protobuf + log4j2-appender log4j2-appender-nodep + logback-appender logback-appender-nodep - core @@ -56,8 +59,8 @@ UTF-8 - 8 - 8 + 8 + 8 9 9 @@ -113,10 +116,6 @@ maven-compiler-plugin - - ${compiler.source.level} - ${compiler.target.level} - maven-release-plugin