Re #48: Changes and fixes to replace methods not available in Java 8.

This commit is contained in:
tomasz.kowalczewski committed 2021-04-22 09:29:29 +02:00
1 parent 217527b3a1
commit b5b2ac8560
6 files changed
+189 -24

No files matched your search

@@ -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
)
);
}
}
}
@@ -1,7 +1,6 @@
package pl.tkowalcz.tjahzi.utils;
import java.util.Arrays;
import java.util.Objects;
public class TextBuilder implements Appendable, CharSequence {
@@ -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");
}
}
@@ -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<String, String> asEntry() {
return Map.entry(getName(), getValue());
}
public static boolean hasValidName(String label) {
return LABEL_NAME_PATTER.matcher(label).matches();
}
@@ -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<String, String> 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<String, String> asEntry(Label label) {
return Map.entry(label.getName(), label.getValue());
}
}
+7 -8
View File
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
@@ -12,12 +13,14 @@
<url>https://github.com/tkowalcz/tjahzi</url>
<modules>
<module>core</module>
<module>loki-protobuf</module>
<module>log4j2-appender</module>
<module>log4j2-appender-nodep</module>
<module>logback-appender</module>
<module>logback-appender-nodep</module>
<module>core</module>
</modules>
<scm>
@@ -56,8 +59,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compiler.source.level>8</compiler.source.level>
<compiler.target.level>8</compiler.target.level>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.testSource>9</maven.compiler.testSource>
<maven.compiler.testTarget>9</maven.compiler.testTarget>
@@ -113,10 +116,6 @@
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${compiler.source.level}</source>
<target>${compiler.target.level}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>