You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2020/09/23 10:33:29 UTC
[isis] branch master updated: ISIS-2437: Commons: new text
processing primitives (_Text)
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push:
new 78c90a2 ISIS-2437: Commons: new text processing primitives (_Text)
78c90a2 is described below
commit 78c90a2c594518626eb45d2322d9294ece1d230e
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Sep 23 12:33:15 2020 +0200
ISIS-2437: Commons: new text processing primitives (_Text)
---
.../apache/isis/commons/internal/base/_Text.java | 224 +++++++++++++++++++++
.../isis/commons/internal/base/BytesTest.java | 138 ++++++-------
.../isis/commons/internal/base/NullSafeTest.java | 50 +++--
.../isis/commons/internal/base/ReductionTest.java | 19 +-
.../isis/commons/internal/base/StringsTest.java | 95 +++++----
.../isis/commons/internal/base/TextTest.java | 75 +++++++
6 files changed, 448 insertions(+), 153 deletions(-)
diff --git a/commons/src/main/java/org/apache/isis/commons/internal/base/_Text.java b/commons/src/main/java/org/apache/isis/commons/internal/base/_Text.java
new file mode 100644
index 0000000..32d35e3
--- /dev/null
+++ b/commons/src/main/java/org/apache/isis/commons/internal/base/_Text.java
@@ -0,0 +1,224 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.commons.internal.base;
+
+import java.util.function.ToIntFunction;
+import java.util.stream.Stream;
+
+import javax.annotation.Nullable;
+
+import org.apache.isis.commons.collections.Can;
+
+import lombok.Getter;
+import lombok.NonNull;
+import lombok.Setter;
+
+/**
+ * <h1>- internal use only -</h1>
+ * <p>
+ * Provides common text processing algorithms.
+ * </p>
+ * <p>
+ * <b>WARNING</b>: Do <b>NOT</b> use any of the classes provided by this package! <br/>
+ * These may be changed or removed without notice!
+ * </p>
+ *
+ * @since 2.0
+ */
+public final class _Text {
+
+ private _Text() {}
+
+ /**
+ * Converts given {@code text} into a {@link Stream} of lines,
+ * removing new line characters {@code \n,\r} in the process.
+ * @param text - nullable
+ * @return non-null
+ * @apiNote Java 11+ provides {@code String.lines()}
+ */
+ public static Stream<String> streamLines(final @Nullable String text){
+ return _Strings.splitThenStream(text, "\n")
+ .map(s->s.replace("\r", ""));
+ }
+
+ /**
+ * Converts given {@code text} into a {@link Can} of lines,
+ * removing new line characters {@code \n,\r} in the process.
+ * @param text - nullable
+ * @return non-null
+ */
+ public static Can<String> getLines(final @Nullable String text){
+ return Can.ofStream(streamLines(text));
+ }
+
+ // -- NORMALIZING
+
+ /**
+ * Converts given {@code lines} into a {@link Can} of lines,
+ * with any empty lines removed that appear
+ * <ul>
+ * <li>before the first non-empty line</li>
+ * <li>immediately after an empty line</li>
+ * <li>after the last non-empty line</li>
+ * </ul>
+ * A line is considered non-empty,
+ * if it contains non-whitespace characters.
+ *
+ * @param lines
+ * @return non-null
+ */
+ public static Can<String> normalize(final @NonNull Can<String> lines) {
+ return removeRepeatedEmptyLines(removeTrailingEmptyLines(removeLeadingEmptyLines(lines)));
+ }
+
+ /**
+ * Converts given {@code lines} into a {@link Can} of lines,
+ * with any empty lines removed that appear before the first non-empty line.
+ * A line is considered non-empty,
+ * if it contains non-whitespace characters.
+ *
+ * @param lines
+ * @return non-null
+ */
+ public static Can<String> removeLeadingEmptyLines(final @NonNull Can<String> lines) {
+
+ if(lines.isEmpty()) {
+ return lines;
+ }
+
+ final int[] nonEmptyLineCount = {0};
+
+ return lines.stream()
+ // peek with side-effect
+ .peek(line->{
+ if(hasNonWhiteSpaceChars(line)) nonEmptyLineCount[0]++;
+ })
+ .filter(line->nonEmptyLineCount[0]>0)
+ .collect(Can.toCan());
+ }
+
+ /**
+ * Converts given {@code lines} into a {@link Can} of lines,
+ * with any empty lines removed that appear after the last non-empty line.
+ * A line is considered non-empty,
+ * if it contains non-whitespace characters.
+ *
+ * @param lines
+ * @return non-null
+ */
+ public static Can<String> removeTrailingEmptyLines(final @NonNull Can<String> lines) {
+
+ if(lines.isEmpty()) {
+ return lines;
+ }
+
+ final int lastLineIndex = lines.size()-1;
+
+ final int lastNonEmptyLineIndex = lines.stream()
+ .mapToInt(indexAndlineToIntFunction((index, line)->hasNonWhiteSpaceChars(line) ? index : -1))
+ .max()
+ .orElse(-1);
+
+ if(lastLineIndex == lastNonEmptyLineIndex) {
+ return lines; // reuse immutable object
+ }
+
+ return lines.stream().limit(lastNonEmptyLineIndex+1).collect(Can.toCan());
+ }
+
+ /**
+ * Converts given {@code lines} into a {@link Can} of lines,
+ * with any empty lines removed that appear immediately after an empty line.
+ * A line is considered non-empty,
+ * if it contains non-whitespace characters.
+ *
+ * @param lines
+ * @return non-null
+ */
+ public static Can<String> removeRepeatedEmptyLines(final @NonNull Can<String> lines) {
+
+ // we need at least 2 lines
+ if(lines.size()<2) {
+ return lines;
+ }
+
+ final int[] latestEmptyLineIndex = {-2};
+
+ return streamLineObjects(lines)
+ .peek(line->{
+ if(!line.isEmpty()) {
+ return; // ignore
+ }
+ if(latestEmptyLineIndex[0] == line.getIndex()-1) {
+ line.setMarkedForRemoval(true);
+ }
+ latestEmptyLineIndex[0] = line.getIndex();
+ })
+ .filter(line->!line.isMarkedForRemoval())
+ .map(Line::getString)
+ .collect(Can.toCan());
+
+ }
+
+ // -- HELPER
+
+ private static boolean hasNonWhiteSpaceChars(String s) {
+ if(s==null) {
+ return false;
+ }
+ return !s.trim().isEmpty();
+ }
+
+ //XXX Java records to the rescue please!
+ @Getter
+ private static class Line {
+ private final int index; // zero based
+ private final String string;
+ private final boolean empty; // whether has no non-whitespace characters
+ @Setter private boolean markedForRemoval;
+
+ public Line(int index, String string) {
+ this.index = index;
+ this.string = string;
+ this.empty = !hasNonWhiteSpaceChars(string);
+ }
+
+ }
+
+ private static Stream<Line> streamLineObjects(final @NonNull Can<String> lines) {
+ final int[] indexRef = {0};
+ return lines.stream().map(line->new Line(indexRef[0]++, line));
+ }
+
+
+ //XXX Java records to the rescue please!
+ private static interface IndexAwareLineToIntFunction {
+ public int apply(int lineIndex, String line);
+ }
+
+ private static ToIntFunction<String> indexAndlineToIntFunction(IndexAwareLineToIntFunction mapper) {
+ final int[] indexRef = {0};
+ return line->mapper.apply(indexRef[0]++, line);
+ }
+
+
+
+
+}
diff --git a/commons/src/test/java/org/apache/isis/commons/internal/base/BytesTest.java b/commons/src/test/java/org/apache/isis/commons/internal/base/BytesTest.java
index 9e7b208..e00dfc9 100644
--- a/commons/src/test/java/org/apache/isis/commons/internal/base/BytesTest.java
+++ b/commons/src/test/java/org/apache/isis/commons/internal/base/BytesTest.java
@@ -22,21 +22,22 @@ package org.apache.isis.commons.internal.base;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameter;
-import org.junit.runners.Parameterized.Parameters;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThan;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
import org.apache.isis.commons.internal._Constants;
-import org.apache.isis.commons.internal.base._Bytes;
-import org.apache.isis.commons.internal.base._Strings;
-public class BytesTest {
+import lombok.val;
+
+class BytesTest {
final int n = 256;
private final byte[] allBytes = new byte[n];
@@ -50,8 +51,8 @@ public class BytesTest {
+ "additionalLinkItem-0-additionalLink",
StandardCharsets.UTF_8);
- @Before
- public void before() {
+ @BeforeEach
+ void before() {
for(int i=0; i<n; ++i) {
allBytes[i] = (byte)i;
}
@@ -60,111 +61,102 @@ public class BytesTest {
// -- PREPEND/APPEND
@Test
- public void concatNullWithNull() throws Exception {
- Assert.assertNull(_Bytes.append(null, null));
- Assert.assertNull(_Bytes.prepend(null, null));
+ void concatNullWithNull() throws Exception {
+ assertNull(_Bytes.append(null, null));
+ assertNull(_Bytes.prepend(null, null));
}
@Test
- public void concatNullWithEmpty() throws Exception {
- Assert.assertArrayEquals(_Constants.emptyBytes, _Bytes.append(null));
- Assert.assertArrayEquals(_Constants.emptyBytes, _Bytes.prepend(null));
+ void concatNullWithEmpty() throws Exception {
+ assertArrayEquals(_Constants.emptyBytes, _Bytes.append(null));
+ assertArrayEquals(_Constants.emptyBytes, _Bytes.prepend(null));
}
@Test
- public void concatWithNull() throws Exception {
+ void concatWithNull() throws Exception {
assertArrayEqualsButNotSame(allBytes, _Bytes.append(allBytes, null));
assertArrayEqualsButNotSame(allBytes, _Bytes.prepend(allBytes, null));
}
@Test
- public void concatWithEmpty() throws Exception {
+ void concatWithEmpty() throws Exception {
assertArrayEqualsButNotSame(allBytes, _Bytes.append(allBytes));
assertArrayEqualsButNotSame(allBytes, _Bytes.prepend(allBytes));
}
@Test
- public void concatHappyCase() throws Exception {
+ void concatHappyCase() throws Exception {
assertArrayEqualsButNotSame(new byte[] {1,2,3,4,5}, _Bytes.append(new byte[] {1,2,3}, (byte)4, (byte)5));
assertArrayEqualsButNotSame(new byte[] {4,5,1,2,3}, _Bytes.prepend(new byte[] {1,2,3}, (byte)4, (byte)5));
}
@Test
- public void compressIdentityWithNull() throws Exception {
- Assert.assertNull(_Bytes.decompress(_Bytes.compress(null)));
+ void compressIdentityWithNull() throws Exception {
+ assertNull(_Bytes.decompress(_Bytes.compress(null)));
}
@Test
- public void compressIdentityWithByteRange() throws Exception {
- Assert.assertArrayEquals(allBytes,
+ void compressIdentityWithByteRange() throws Exception {
+ assertArrayEquals(allBytes,
_Bytes.decompress(_Bytes.compress(allBytes)));
}
@Test
- public void compressIdentityWithTestimonial() throws Exception {
- Assert.assertArrayEquals(testimonial,
+ void compressIdentityWithTestimonial() throws Exception {
+ assertArrayEquals(testimonial,
_Bytes.decompress(_Bytes.compress(testimonial)));
}
@Test
- public void compressionRatio() throws Exception {
+ void compressionRatio() throws Exception {
// lower is better
final double compressionRatio = (double)_Bytes.compress(testimonial).length / testimonial.length;
- Assert.assertThat(compressionRatio, lessThan(0.7));
+ assertThat(compressionRatio, lessThan(0.7));
}
// -- COMPRESSION
- @RunWith(Parameterized.class)
- public static class CompressionTest {
-
-
- @Parameters
- public static Object[] data() {
- return new Object[] {
- (byte[]) null,
- new byte[] { },
- new byte[] { 0 },
- new byte[] { 0, 1 },
- new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 17
- new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 18
- new byte[] { 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 19
- };
- }
-
- @Parameter
- public byte[] input;
-
-
- @Test
- public void compressIdentity() throws Exception {
- Assert.assertArrayEquals(input,
- _Bytes.decompress(_Bytes.compress(input)));
- }
-
+ private static byte[] data(int index) {
+ return (byte[]) new Object[] {
+ (byte[]) null,
+ new byte[] { },
+ new byte[] { 0 },
+ new byte[] { 0, 1 },
+ new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 17
+ new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 18
+ new byte[] { 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 19
+ }[index];
+ }
+
+ @ParameterizedTest
+ @ValueSource(ints = {0, 1, 2, 3, 4, 5, 6}) // data indices
+ void compressionRundtrip(int dataIndex) throws Exception {
+ val input = data(dataIndex);
+ assertArrayEquals(input,
+ _Bytes.decompress(_Bytes.compress(input)));
}
// -- BASE-64
@Test
- public void base64IdentityWithNull() throws Exception {
- Assert.assertNull(_Bytes.decodeBase64(
+ void base64IdentityWithNull() throws Exception {
+ assertNull(_Bytes.decodeBase64(
Base64.getUrlDecoder(),
_Bytes.encodeToBase64(Base64.getUrlEncoder(), null)));
}
@Test
- public void base64IdentityWithByteRange() throws Exception {
- Assert.assertArrayEquals(allBytes,
+ void base64IdentityWithByteRange() throws Exception {
+ assertArrayEquals(allBytes,
_Bytes.decodeBase64(
Base64.getUrlDecoder(),
_Bytes.encodeToBase64(Base64.getUrlEncoder(), allBytes)));
}
@Test
- public void base64IdentityWithTestimonial() throws Exception {
- Assert.assertArrayEquals(testimonial,
+ void base64IdentityWithTestimonial() throws Exception {
+ assertArrayEquals(testimonial,
_Bytes.decodeBase64(
Base64.getUrlDecoder(),
_Bytes.encodeToBase64(Base64.getUrlEncoder(), testimonial)));
@@ -173,23 +165,23 @@ public class BytesTest {
// -- OPERATOR COMPOSITION
@Test
- public void composedOperatorWithNull() throws Exception {
- Assert.assertNull(_Bytes.asCompressedUrlBase64.apply(null));
- Assert.assertNull(_Bytes.ofCompressedUrlBase64.apply(null));
- Assert.assertNull(_Bytes.asUrlBase64.apply(null));
- Assert.assertNull(_Bytes.ofUrlBase64.apply(null));
+ void composedOperatorWithNull() throws Exception {
+ assertNull(_Bytes.asCompressedUrlBase64.apply(null));
+ assertNull(_Bytes.ofCompressedUrlBase64.apply(null));
+ assertNull(_Bytes.asUrlBase64.apply(null));
+ assertNull(_Bytes.ofUrlBase64.apply(null));
}
@Test
- public void composedIdentityWithByteRange() throws Exception {
- Assert.assertArrayEquals(allBytes,
+ void composedIdentityWithByteRange() throws Exception {
+ assertArrayEquals(allBytes,
_Bytes.ofCompressedUrlBase64.apply(
_Bytes.asCompressedUrlBase64.apply(allBytes)));
}
@Test
- public void composedIdentityWithTestimonial() throws Exception {
- Assert.assertArrayEquals(testimonial,
+ void composedIdentityWithTestimonial() throws Exception {
+ assertArrayEquals(testimonial,
_Bytes.ofCompressedUrlBase64.apply(
_Bytes.asCompressedUrlBase64.apply(testimonial)));
}
@@ -197,8 +189,8 @@ public class BytesTest {
// -- HELPER
private void assertArrayEqualsButNotSame(byte[] a, byte[] b) {
- Assert.assertFalse(a == b);
- Assert.assertArrayEquals(a, b);
+ assertFalse(a == b);
+ assertArrayEquals(a, b);
}
}
diff --git a/commons/src/test/java/org/apache/isis/commons/internal/base/NullSafeTest.java b/commons/src/test/java/org/apache/isis/commons/internal/base/NullSafeTest.java
index fffa677..d6c374f 100644
--- a/commons/src/test/java/org/apache/isis/commons/internal/base/NullSafeTest.java
+++ b/commons/src/test/java/org/apache/isis/commons/internal/base/NullSafeTest.java
@@ -26,14 +26,12 @@ import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
-import org.junit.Assert;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
-
-import org.apache.isis.commons.internal.base._NullSafe;
-import org.apache.isis.commons.internal.base._Strings;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
class NullSafeTest {
@@ -79,43 +77,43 @@ class NullSafeTest {
@Test
void isEmptyString() throws Exception {
- Assert.assertThat(_NullSafe.isEmpty((String)null), is(true));
- Assert.assertThat(_NullSafe.isEmpty(""), is(true));
- Assert.assertThat(_NullSafe.isEmpty(" 12 aBc"), is(false));
+ assertThat(_NullSafe.isEmpty((String)null), is(true));
+ assertThat(_NullSafe.isEmpty(""), is(true));
+ assertThat(_NullSafe.isEmpty(" 12 aBc"), is(false));
}
@Test
void isEmptyCollection() throws Exception {
- Assert.assertThat(_NullSafe.isEmpty((Collection<?>)null), is(true));
- Assert.assertThat(_NullSafe.isEmpty(Collections.emptyList()), is(true));
- Assert.assertThat(_NullSafe.isEmpty(Arrays.asList(new String[] {"foo", "bar"})), is(false));
+ assertThat(_NullSafe.isEmpty((Collection<?>)null), is(true));
+ assertThat(_NullSafe.isEmpty(Collections.emptyList()), is(true));
+ assertThat(_NullSafe.isEmpty(Arrays.asList(new String[] {"foo", "bar"})), is(false));
}
@Test
void absence() throws Exception {
- Assert.assertThat(_NullSafe.isAbsent(null), is(true));
- Assert.assertThat(_NullSafe.isAbsent(""), is(false));
+ assertThat(_NullSafe.isAbsent(null), is(true));
+ assertThat(_NullSafe.isAbsent(""), is(false));
}
@Test
void presence() throws Exception {
- Assert.assertThat(_NullSafe.isPresent(null), is(false));
- Assert.assertThat(_NullSafe.isPresent(""), is(true));
+ assertThat(_NullSafe.isPresent(null), is(false));
+ assertThat(_NullSafe.isPresent(""), is(true));
}
@Test
void emptyStreamWithArray() throws Exception {
- Assert.assertNotNull(_NullSafe.stream((String[])null));
+ assertNotNull(_NullSafe.stream((String[])null));
- Assert.assertNotNull(_NullSafe.stream(_Strings.emptyArray));
- Assert.assertEquals(0L, _NullSafe.stream(_Strings.emptyArray).count());
+ assertNotNull(_NullSafe.stream(_Strings.emptyArray));
+ assertEquals(0L, _NullSafe.stream(_Strings.emptyArray).count());
}
@Test
void streamWithArray() throws Exception {
- Assert.assertThat(
+ assertThat(
_NullSafe.stream(new String[] {"foo", "bar"})
.collect(Collectors.joining("|")),
is("foo|bar"));
@@ -124,15 +122,15 @@ class NullSafeTest {
@Test
void emptyStreamWithCollection() throws Exception {
- Assert.assertNotNull(_NullSafe.stream((List<?>)null));
+ assertNotNull(_NullSafe.stream((List<?>)null));
- Assert.assertNotNull(_NullSafe.stream(Arrays.asList(_Strings.emptyArray)));
- Assert.assertEquals(0L, _NullSafe.stream(Arrays.asList(_Strings.emptyArray)).count());
+ assertNotNull(_NullSafe.stream(Arrays.asList(_Strings.emptyArray)));
+ assertEquals(0L, _NullSafe.stream(Arrays.asList(_Strings.emptyArray)).count());
}
@Test
void streamWithCollection() throws Exception {
- Assert.assertThat(
+ assertThat(
_NullSafe.stream(Arrays.asList(new String[] {"foo", "bar"}))
.collect(Collectors.joining("|")),
is("foo|bar"));
@@ -141,15 +139,15 @@ class NullSafeTest {
@Test
void emptyStreamWithIterator() throws Exception {
- Assert.assertNotNull(_NullSafe.stream((Iterator<?>)null));
+ assertNotNull(_NullSafe.stream((Iterator<?>)null));
- Assert.assertNotNull(_NullSafe.stream(Arrays.asList(_Strings.emptyArray)).iterator());
- Assert.assertEquals(0L, _NullSafe.stream(Arrays.asList(_Strings.emptyArray).iterator()).count());
+ assertNotNull(_NullSafe.stream(Arrays.asList(_Strings.emptyArray)).iterator());
+ assertEquals(0L, _NullSafe.stream(Arrays.asList(_Strings.emptyArray).iterator()).count());
}
@Test
void streamWithIterator() throws Exception {
- Assert.assertThat(
+ assertThat(
_NullSafe.stream(Arrays.asList(new String[] {"foo", "bar"}).iterator())
.collect(Collectors.joining("|")),
is("foo|bar"));
diff --git a/commons/src/test/java/org/apache/isis/commons/internal/base/ReductionTest.java b/commons/src/test/java/org/apache/isis/commons/internal/base/ReductionTest.java
index f425e61..62e9c4e 100644
--- a/commons/src/test/java/org/apache/isis/commons/internal/base/ReductionTest.java
+++ b/commons/src/test/java/org/apache/isis/commons/internal/base/ReductionTest.java
@@ -21,18 +21,17 @@ package org.apache.isis.commons.internal.base;
import java.util.List;
-import org.junit.Assert;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
-import org.apache.isis.commons.internal.base._Reduction;
import org.apache.isis.commons.internal.collections._Lists;
-public class ReductionTest {
+class ReductionTest {
@Test
- public void findMinimum() throws Exception {
+ void findMinimum() throws Exception {
final List<Integer> values = _Lists.of(5, 4, 3, 2, 1, 2, 3, 4);
@@ -40,11 +39,11 @@ public class ReductionTest {
values.forEach(toMinReduction);
- Assert.assertThat(toMinReduction.getResult().get(), is(1));
+ assertThat(toMinReduction.getResult().get(), is(1));
}
@Test
- public void findMinimum_initialized_low() throws Exception {
+ void findMinimum_initialized_low() throws Exception {
final int initial = -1;
@@ -54,11 +53,11 @@ public class ReductionTest {
values.forEach(toMinReduction);
- Assert.assertThat(toMinReduction.getResult().get(), is(initial));
+ assertThat(toMinReduction.getResult().get(), is(initial));
}
@Test
- public void findMinimum_initialized_high() throws Exception {
+ void findMinimum_initialized_high() throws Exception {
final int initial = 3;
@@ -68,7 +67,7 @@ public class ReductionTest {
values.forEach(toMinReduction);
- Assert.assertThat(toMinReduction.getResult().get(), is(1));
+ assertThat(toMinReduction.getResult().get(), is(1));
}
diff --git a/commons/src/test/java/org/apache/isis/commons/internal/base/StringsTest.java b/commons/src/test/java/org/apache/isis/commons/internal/base/StringsTest.java
index e04bd5c..30b8fef 100644
--- a/commons/src/test/java/org/apache/isis/commons/internal/base/StringsTest.java
+++ b/commons/src/test/java/org/apache/isis/commons/internal/base/StringsTest.java
@@ -22,43 +22,42 @@ package org.apache.isis.commons.internal.base;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
-import org.junit.Assert;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.apache.isis.commons.internal._Constants;
-import org.apache.isis.commons.internal.base._Bytes;
-import org.apache.isis.commons.internal.base._Strings;
-public class StringsTest {
+class StringsTest {
@Test
public void isEmpty() throws Exception {
- Assert.assertThat(_Strings.isEmpty(" 12 aBc"), is(false));
- Assert.assertThat(_Strings.isEmpty(""), is(true));
- Assert.assertThat(_Strings.isEmpty(null), is(true));
+ assertThat(_Strings.isEmpty(" 12 aBc"), is(false));
+ assertThat(_Strings.isEmpty(""), is(true));
+ assertThat(_Strings.isEmpty(null), is(true));
}
@Test
public void isNotEmpty() throws Exception {
- Assert.assertThat(_Strings.isNotEmpty(" 12 aBc"), is(true));
- Assert.assertThat(_Strings.isNotEmpty(""), is(false));
- Assert.assertThat(_Strings.isNotEmpty(null), is(false));
+ assertThat(_Strings.isNotEmpty(" 12 aBc"), is(true));
+ assertThat(_Strings.isNotEmpty(""), is(false));
+ assertThat(_Strings.isNotEmpty(null), is(false));
}
@Test
public void lowerWithNull() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.lower(null),
nullValue());
}
@Test
public void lowerMixed() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.lower("12aBc"),
is("12abc"));
}
@@ -66,63 +65,63 @@ public class StringsTest {
@Test
public void upperWithNull() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.upper(null),
nullValue());
}
@Test
public void upperMixed() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.upper("12aBc"),
is("12ABC"));
}
@Test
public void capitalizeWithNull() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.capitalize(null),
nullValue());
}
@Test
public void capitalizeSize0() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.capitalize(""),
is(""));
}
@Test
public void capitalizeSize1() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.capitalize("a"),
is("A"));
}
@Test
public void capitalizeSize2() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.capitalize("ab"),
is("Ab"));
}
@Test
public void trimWithNull() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.trim(null),
nullValue());
}
@Test
public void trimMixed() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.trim(" 12 aBc"),
is("12 aBc"));
}
@Test
public void splitThenStreamWithNull() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.splitThenStream(null, "$")
.collect(Collectors.joining("|")),
is(""));
@@ -130,7 +129,7 @@ public class StringsTest {
@Test
public void splitThenStreamSingle() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.splitThenStream(" 12 aBc ", "$")
.collect(Collectors.joining("|")),
is(" 12 aBc "));
@@ -138,7 +137,7 @@ public class StringsTest {
@Test
public void splitThenStreamMultipleWithSeparatorAtBegin() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.splitThenStream("$ 1$2 a$Bc ", "$")
.collect(Collectors.joining("|")),
is("| 1|2 a|Bc "));
@@ -146,7 +145,7 @@ public class StringsTest {
@Test
public void splitThenStreamMultipleWithSeparatorAtEnd() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.splitThenStream(" 1$2 a$Bc $", "$")
.collect(Collectors.joining("|")),
is(" 1|2 a|Bc |"));
@@ -154,22 +153,30 @@ public class StringsTest {
@Test
public void splitThenStreamMultipleWithSeparatorsInSequence() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.splitThenStream(" 1$2 a$$Bc ", "$")
.collect(Collectors.joining("|")),
is(" 1|2 a||Bc "));
}
+
+ @Test
+ public void splitThenStreamWithnewLine() throws Exception {
+ assertThat(
+ _Strings.splitThenStream(" 1\n2 a\n\nBc ", "\n")
+ .collect(Collectors.joining("|")),
+ is(" 1|2 a||Bc "));
+ }
@Test
public void condenseWhitespacesWithNull() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.condenseWhitespaces(null,"|"),
nullValue());
}
@Test
public void condenseWhitespaces() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.condenseWhitespaces(" 12 aBc","|"),
is("|12|aBc"));
}
@@ -178,21 +185,21 @@ public class StringsTest {
@Test
public void toByteConvertWithNull() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.toBytes(null, StandardCharsets.UTF_8),
nullValue());
}
@Test
public void toByteConvertWithEmpty() throws Exception {
- Assert.assertArrayEquals(
+ assertArrayEquals(
_Constants.emptyBytes,
_Strings.toBytes("", StandardCharsets.UTF_8));
}
@Test
public void toByteConvert() throws Exception {
- Assert.assertArrayEquals(
+ assertArrayEquals(
new byte[] {48,49,50,51},
_Strings.toBytes("0123", StandardCharsets.UTF_8));
}
@@ -201,21 +208,21 @@ public class StringsTest {
@Test
public void fromByteConvertWithNull() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.ofBytes(null, StandardCharsets.UTF_8),
nullValue());
}
@Test
public void fromByteConvertWithEmpty() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.ofBytes(_Constants.emptyBytes, StandardCharsets.UTF_8),
is(""));
}
@Test
public void fromByteConvert() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.ofBytes(new byte[] {48,49,50,51}, StandardCharsets.UTF_8),
is("0123"));
}
@@ -225,11 +232,11 @@ public class StringsTest {
@Test
public void convertIdentity() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.convert(null, _Bytes.operator(), StandardCharsets.UTF_8),
nullValue());
- Assert.assertThat(
+ assertThat(
_Strings.convert("0123", _Bytes.operator(), StandardCharsets.UTF_8),
is("0123"));
}
@@ -238,21 +245,21 @@ public class StringsTest {
@Test
public void composeIdentityWithNull() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.operator().apply(null),
nullValue());
}
@Test
public void composeIdentity() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.operator().apply(" 12 aBc"),
is(" 12 aBc"));
}
@Test
public void compose2WithNull() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.operator()
.andThen(_Strings::lower)
.apply(null),
@@ -261,7 +268,7 @@ public class StringsTest {
@Test
public void compose2() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.operator()
.andThen(_Strings::lower)
.apply(" 12 aBc"),
@@ -270,7 +277,7 @@ public class StringsTest {
@Test
public void composeOperatorSequency_LastShouldWin() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.operator()
.andThen(_Strings::lower)
.andThen(_Strings::upper)
@@ -282,7 +289,7 @@ public class StringsTest {
@Test
public void asLowerDashed() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.asLowerDashed
.apply(" 12 aBc"),
is("-12-abc"));
@@ -290,7 +297,7 @@ public class StringsTest {
@Test
public void asNormalized() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.asNormalized
.apply(" 12 a B c"),
is(" 12 a B c"));
@@ -298,7 +305,7 @@ public class StringsTest {
@Test
public void asNaturalName2() throws Exception {
- Assert.assertThat(
+ assertThat(
_Strings.asNaturalName2
.apply("NextAvailableDate"),
is("Next Available Date"));
diff --git a/commons/src/test/java/org/apache/isis/commons/internal/base/TextTest.java b/commons/src/test/java/org/apache/isis/commons/internal/base/TextTest.java
new file mode 100644
index 0000000..905a2a7
--- /dev/null
+++ b/commons/src/test/java/org/apache/isis/commons/internal/base/TextTest.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.isis.commons.internal.base;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.isis.commons.collections.Can;
+
+class TextTest {
+
+ @Test
+ void lineParsingWin() {
+ assertEquals(Can.of("Hallo", "", "World"), _Text.getLines("Hallo\r\n\r\nWorld"));
+ }
+
+ @Test
+ void lineParsingUx() {
+ assertEquals(Can.of("Hallo", "", "World"), _Text.getLines("Hallo\n\nWorld"));
+ }
+
+ @Test
+ void repeatedEmptyLineRemoval() {
+
+ assertEquals(Can.of("Hallo", "", "World"), _Text.removeRepeatedEmptyLines(Can.of("Hallo", "", "World")));
+ assertEquals(Can.of("Hallo", "", "World"), _Text.removeRepeatedEmptyLines(Can.of("Hallo", "", "", "World")));
+ assertEquals(Can.of("Hallo", "", "World"), _Text.removeRepeatedEmptyLines(Can.of("Hallo", "", " \t ", "World")));
+
+ assertEquals(Can.of("", "Hallo", "", "World"), _Text.removeRepeatedEmptyLines(Can.of("", "Hallo", "", "World")));
+ assertEquals(Can.of("", "Hallo", "", "World"), _Text.removeRepeatedEmptyLines(Can.of("", "", "Hallo", "", "World")));
+
+ assertEquals(Can.of("Hallo", "", "World", ""), _Text.removeRepeatedEmptyLines(Can.of("Hallo", "", "World", "")));
+ assertEquals(Can.of("Hallo", "", "World", ""), _Text.removeRepeatedEmptyLines(Can.of("Hallo", "", "World", "", "")));
+
+ }
+
+ @Test
+ void leadingEmptyLineRemoval() {
+
+ assertEquals(Can.<String>empty(), _Text.removeLeadingEmptyLines(Can.of("", "", "")));
+
+ assertEquals(Can.of("Hallo", "", "World"), _Text.removeLeadingEmptyLines(Can.of("Hallo", "", "World")));
+ assertEquals(Can.of("Hallo", "", "World"), _Text.removeLeadingEmptyLines(Can.of("", "Hallo", "", "World")));
+ assertEquals(Can.of("Hallo", "", "World"), _Text.removeLeadingEmptyLines(Can.of("", "", "Hallo", "", "World")));
+
+ }
+
+ @Test
+ void trailingEmptyLineRemoval() {
+
+ assertEquals(Can.<String>empty(), _Text.removeTrailingEmptyLines(Can.of("", "", "")));
+
+ assertEquals(Can.of("Hallo", "", "World"), _Text.removeTrailingEmptyLines(Can.of("Hallo", "", "World")));
+ assertEquals(Can.of("Hallo", "", "World"), _Text.removeTrailingEmptyLines(Can.of("Hallo", "", "World", "")));
+ assertEquals(Can.of("Hallo", "", "World"), _Text.removeTrailingEmptyLines(Can.of("Hallo", "", "World", "", "")));
+ }
+
+}