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 2018/01/22 18:30:04 UTC
[isis] branch ISIS-1846_internal_utils updated: ISIS-1846 test
cases added for new utility classes
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch ISIS-1846_internal_utils
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/ISIS-1846_internal_utils by this push:
new 4a90358 ISIS-1846 test cases added for new utility classes
4a90358 is described below
commit 4a90358d4d4147b69a63d750e64a8769d18c9cc4
Author: Andi Huber <ah...@apache.org>
AuthorDate: Mon Jan 22 19:30:00 2018 +0100
ISIS-1846 test cases added for new utility classes
---
.../isis/applib/internal/base/_NullSafe.java | 2 +-
.../apache/isis/applib/internal/base/_Strings.java | 11 +-
.../isis/applib/internal/base/NullSafeTest.java | 116 ++++++++++++
.../isis/applib/internal/base/StringsTest.java | 207 +++++++++++++++++++++
.../applib/internal/compare/ComparatorsTest.java | 158 ++++++++++++++++
5 files changed, 488 insertions(+), 6 deletions(-)
diff --git a/core/applib/src/main/java/org/apache/isis/applib/internal/base/_NullSafe.java b/core/applib/src/main/java/org/apache/isis/applib/internal/base/_NullSafe.java
index a51c9ed..985c19b 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/internal/base/_NullSafe.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/internal/base/_NullSafe.java
@@ -100,7 +100,7 @@ public final class _NullSafe {
* @return whether {@code x} is null.
*/
public static boolean isAbsent(Object x) {
- return x!=null;
+ return x==null;
}
// -- EQUALS/COMPARE
diff --git a/core/applib/src/main/java/org/apache/isis/applib/internal/base/_Strings.java b/core/applib/src/main/java/org/apache/isis/applib/internal/base/_Strings.java
index 6c43230..ae4ad23 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/internal/base/_Strings.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/internal/base/_Strings.java
@@ -108,7 +108,8 @@ public final class _Strings {
// -- SPLITTING
/**
- * Splits the {@code input} into chunks separated by {@code separator}
+ * Splits the {@code input} into chunks separated by {@code separator},
+ * then puts all non-empty chunks on the stream.
* @param input
* @param separator
* @return empty stream if {@code input} is null
@@ -122,7 +123,8 @@ public final class _Strings {
if(!input.contains(separator))
return Stream.of(input);
- return Stream.of(input.split(Pattern.quote(separator)));
+ return Stream.of(input.split(Pattern.quote(separator)))
+ .filter(_Strings::isNotEmpty);
}
// -- REPLACEMENT OPERATORS
@@ -152,6 +154,7 @@ public final class _Strings {
private final UnaryOperator<String> operator;
private StringOperator(UnaryOperator<String> operator) {
+ Objects.requireNonNull(operator);
this.operator = operator;
}
@@ -160,8 +163,6 @@ public final class _Strings {
}
public StringOperator andThen(UnaryOperator<String> andThen) {
- if(operator==null)
- return new StringOperator(andThen::apply);
return new StringOperator(s->andThen.apply(operator.apply(s)));
}
@@ -172,7 +173,7 @@ public final class _Strings {
* @return
*/
public static StringOperator operator() {
- return new StringOperator(null);
+ return new StringOperator(UnaryOperator.identity());
}
// -- SPECIAL COMPOSITES
diff --git a/core/applib/src/test/java/org/apache/isis/applib/internal/base/NullSafeTest.java b/core/applib/src/test/java/org/apache/isis/applib/internal/base/NullSafeTest.java
new file mode 100644
index 0000000..2ff8422
--- /dev/null
+++ b/core/applib/src/test/java/org/apache/isis/applib/internal/base/NullSafeTest.java
@@ -0,0 +1,116 @@
+/*
+ * 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.applib.internal.base;
+
+import static org.hamcrest.CoreMatchers.is;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class NullSafeTest {
+
+ @Test
+ public 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));
+ }
+
+ @Test
+ public 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));
+ }
+
+ @Test
+ public void absence() throws Exception {
+ Assert.assertThat(_NullSafe.isAbsent(null), is(true));
+ Assert.assertThat(_NullSafe.isAbsent(""), is(false));
+ }
+
+ @Test
+ public void presence() throws Exception {
+ Assert.assertThat(_NullSafe.isPresent(null), is(false));
+ Assert.assertThat(_NullSafe.isPresent(""), is(true));
+ }
+
+
+ @Test
+ public void emptyStreamWithArray() throws Exception {
+
+ Assert.assertNotNull(_NullSafe.stream((String[])null));
+
+ Assert.assertNotNull(_NullSafe.stream(_Strings.emptyArray));
+ Assert.assertEquals(0L, _NullSafe.stream(_Strings.emptyArray).count());
+ }
+
+ @Test
+ public void streamWithArray() throws Exception {
+ Assert.assertThat(
+ _NullSafe.stream(new String[] {"foo", "bar"})
+ .collect(Collectors.joining("|")),
+ is("foo|bar"));
+ }
+
+ @Test
+ public void emptyStreamWithCollection() throws Exception {
+
+ Assert.assertNotNull(_NullSafe.stream((List<?>)null));
+
+ Assert.assertNotNull(_NullSafe.stream(Arrays.asList(_Strings.emptyArray)));
+ Assert.assertEquals(0L, _NullSafe.stream(Arrays.asList(_Strings.emptyArray)).count());
+ }
+
+ @Test
+ public void streamWithCollection() throws Exception {
+ Assert.assertThat(
+ _NullSafe.stream(Arrays.asList(new String[] {"foo", "bar"}))
+ .collect(Collectors.joining("|")),
+ is("foo|bar"));
+ }
+
+ @Test
+ public void emptyStreamWithIterator() throws Exception {
+
+ Assert.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());
+ }
+
+ @Test
+ public void streamWithIterator() throws Exception {
+ Assert.assertThat(
+ _NullSafe.stream(Arrays.asList(new String[] {"foo", "bar"}).iterator())
+ .collect(Collectors.joining("|")),
+ is("foo|bar"));
+ }
+
+
+
+}
diff --git a/core/applib/src/test/java/org/apache/isis/applib/internal/base/StringsTest.java b/core/applib/src/test/java/org/apache/isis/applib/internal/base/StringsTest.java
new file mode 100644
index 0000000..6765de9
--- /dev/null
+++ b/core/applib/src/test/java/org/apache/isis/applib/internal/base/StringsTest.java
@@ -0,0 +1,207 @@
+/*
+ * 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.applib.internal.base;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+
+import java.util.stream.Collectors;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public 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));
+ }
+
+ @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));
+ }
+
+
+ @Test
+ public void lowerWithNull() throws Exception {
+ Assert.assertThat(
+ _Strings.lower(null),
+ nullValue());
+ }
+
+ @Test
+ public void lowerMixed() throws Exception {
+ Assert.assertThat(
+ _Strings.lower("12aBc"),
+ is("12abc"));
+ }
+
+
+ @Test
+ public void upperWithNull() throws Exception {
+ Assert.assertThat(
+ _Strings.upper(null),
+ nullValue());
+ }
+
+ @Test
+ public void upperMixed() throws Exception {
+ Assert.assertThat(
+ _Strings.upper("12aBc"),
+ is("12ABC"));
+ }
+
+ @Test
+ public void trimWithNull() throws Exception {
+ Assert.assertThat(
+ _Strings.trim(null),
+ nullValue());
+ }
+
+ @Test
+ public void trimMixed() throws Exception {
+ Assert.assertThat(
+ _Strings.trim(" 12 aBc"),
+ is("12 aBc"));
+ }
+
+ @Test
+ public void splitThenStreamWithNull() throws Exception {
+ Assert.assertThat(
+ _Strings.splitThenStream(null, "$")
+ .collect(Collectors.joining("|")),
+ is(""));
+ }
+
+ @Test
+ public void splitThenStreamSingle() throws Exception {
+ Assert.assertThat(
+ _Strings.splitThenStream(" 12 aBc ", "$")
+ .collect(Collectors.joining("|")),
+ is(" 12 aBc "));
+ }
+
+ @Test
+ public void splitThenStreamMultipleWithSeparatorAtBegin() throws Exception {
+ Assert.assertThat(
+ _Strings.splitThenStream("$ 1$2 a$Bc ", "$")
+ .collect(Collectors.joining("|")),
+ is(" 1|2 a|Bc "));
+ }
+
+ @Test
+ public void splitThenStreamMultipleWithSeparatorAtEnd() throws Exception {
+ Assert.assertThat(
+ _Strings.splitThenStream(" 1$2 a$Bc $", "$")
+ .collect(Collectors.joining("|")),
+ is(" 1|2 a|Bc "));
+ }
+
+ @Test
+ public void condenseWhitespacesWithNull() throws Exception {
+ Assert.assertThat(
+ _Strings.condenseWhitespaces(null,"|"),
+ nullValue());
+ }
+
+ @Test
+ public void condenseWhitespaces() throws Exception {
+ Assert.assertThat(
+ _Strings.condenseWhitespaces(" 12 aBc","|"),
+ is("|12|aBc"));
+ }
+
+ // -- OPERATOR COMPOSITION
+
+ @Test
+ public void composeIdentityWithNull() throws Exception {
+ Assert.assertThat(
+ _Strings.operator().apply(null),
+ nullValue());
+ }
+
+ @Test
+ public void composeIdentity() throws Exception {
+ Assert.assertThat(
+ _Strings.operator().apply(" 12 aBc"),
+ is(" 12 aBc"));
+ }
+
+ @Test
+ public void compose2WithNull() throws Exception {
+ Assert.assertThat(
+ _Strings.operator()
+ .andThen(_Strings::lower)
+ .apply(null),
+ nullValue());
+ }
+
+ @Test
+ public void compose2() throws Exception {
+ Assert.assertThat(
+ _Strings.operator()
+ .andThen(_Strings::lower)
+ .apply(" 12 aBc"),
+ is(" 12 abc"));
+ }
+
+ @Test
+ public void composeOperatorSequency_LastShouldWin() throws Exception {
+ Assert.assertThat(
+ _Strings.operator()
+ .andThen(_Strings::lower)
+ .andThen(_Strings::upper)
+ .apply(" 12 aBc"),
+ is(" 12 ABC"));
+ }
+
+ // -- SPECIAL COMPOSITES
+
+ @Test
+ public void asLowerDashed() throws Exception {
+ Assert.assertThat(
+ _Strings.asLowerDashed
+ .apply(" 12 aBc"),
+ is("-12-abc"));
+ }
+
+ @Test
+ public void asNormalized() throws Exception {
+ Assert.assertThat(
+ _Strings.asNormalized
+ .apply(" 12 a B c"),
+ is(" 12 a B c"));
+ }
+
+ @Test
+ public void asNaturalName2() throws Exception {
+ Assert.assertThat(
+ _Strings.asNaturalName2
+ .apply("NextAvailableDate"),
+ is("Next Available Date"));
+ }
+
+
+}
diff --git a/core/applib/src/test/java/org/apache/isis/applib/internal/compare/ComparatorsTest.java b/core/applib/src/test/java/org/apache/isis/applib/internal/compare/ComparatorsTest.java
new file mode 100644
index 0000000..e29f771
--- /dev/null
+++ b/core/applib/src/test/java/org/apache/isis/applib/internal/compare/ComparatorsTest.java
@@ -0,0 +1,158 @@
+/*
+ * 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.applib.internal.compare;
+
+import static org.hamcrest.CoreMatchers.is;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import com.google.common.collect.Iterators;
+import com.google.common.collect.Lists;
+
+public class ComparatorsTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Test
+ public void nullArgumentLeftAndRigth() throws Exception {
+ Assert.assertThat(
+ _Comparators.deweyOrderCompare(null, null),
+ is(0));
+ }
+
+ @Test
+ public void nullArgumentLeft() throws Exception {
+ Assert.assertThat(
+ _Comparators.deweyOrderCompare(null, "any"),
+ is(1));
+ }
+
+ @Test
+ public void nullArgumentRight() throws Exception {
+ Assert.assertThat(
+ _Comparators.deweyOrderCompare("any", null),
+ is(-1));
+ }
+
+ @Test
+ public void inOrderMixed() throws Exception {
+ assertThatSorting(
+ ofS("1", "a"),
+ ofL("1", "a")
+ );
+ }
+
+ @Test
+ public void notInOrderMixed() throws Exception {
+ assertThatSorting(
+ ofS("b", "1"),
+ ofL("1", "b")
+ );
+ }
+
+
+ @Test
+ public void emptySet() throws Exception {
+ assertThatSorting(
+ ofS(),
+ ofL());
+ }
+
+ @Test
+ public void singleElement() throws Exception {
+ assertThatSorting(
+ ofS("1"),
+ ofL("1")
+ );
+ }
+
+ @Test
+ public void inOrder() throws Exception {
+ assertThatSorting(
+ ofS("1", "2"),
+ ofL("1", "2")
+ );
+ }
+
+ @Test
+ public void notInOrder() throws Exception {
+ assertThatSorting(
+ ofS("2", "1"),
+ ofL("1", "2")
+ );
+ }
+
+ @Test
+ public void notInOrderDepth2() throws Exception {
+ assertThatSorting(
+ ofS("1.2", "1.1"),
+ ofL("1.1", "1.2")
+ );
+ }
+
+ @Test
+ public void differentDepths() throws Exception {
+ assertThatSorting(
+ ofS("2", "1.3", "1.2", "1.2.2", "1.2.1", "1.1"),
+ ofL("1.1", "1.2", "1.2.1", "1.2.2", "1.3", "2")
+ );
+ }
+
+ @Test
+ public void mismatchedDepth3() throws Exception {
+ assertThatSorting(
+ ofS("1.2.2", "1.2.1", "1.1"),
+ ofL("1.1", "1.2.1", "1.2.2")
+ );
+ }
+
+ @Test
+ public void X() throws Exception {
+ assertThatSorting(
+ ofS("45.1", "10.10"),
+ ofL("10.10", "45.1")
+ );
+ }
+
+ private static Collection<String> ofS(String... str) {
+ return Arrays.asList(str);
+ }
+
+ private static List<String> ofL(String... str) {
+ return Lists.newArrayList(ofS(str));
+ }
+
+ private static void assertThatSorting(Collection<String> input, List<String> expected) {
+ final SortedSet<String> treeSet = new TreeSet<String>(_Comparators.deweyOrderComparator);
+ treeSet.addAll(input);
+ final List<String> strings = Arrays.asList(Iterators.toArray(treeSet.iterator(), String.class));
+ Assert.assertThat(strings, is(expected));
+ }
+}
--
To stop receiving notification emails like this one, please contact
ahuber@apache.org.