You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2013/06/06 17:34:20 UTC
git commit: ISIS-426: ComparableContractTest_compareTo
Updated Branches:
refs/heads/master 395b4a47d -> 51586b75a
ISIS-426: ComparableContractTest_compareTo
... to simplify the writing of tests of Comparable objects (domain objects or otherwise).
Project: http://git-wip-us.apache.org/repos/asf/isis/repo
Commit: http://git-wip-us.apache.org/repos/asf/isis/commit/51586b75
Tree: http://git-wip-us.apache.org/repos/asf/isis/tree/51586b75
Diff: http://git-wip-us.apache.org/repos/asf/isis/diff/51586b75
Branch: refs/heads/master
Commit: 51586b75a29d476b3a7a5eb3959e7eea561b809e
Parents: 395b4a4
Author: Dan Haywood <da...@apache.org>
Authored: Thu Jun 6 16:31:16 2013 +0100
Committer: Dan Haywood <da...@apache.org>
Committed: Thu Jun 6 16:31:16 2013 +0100
----------------------------------------------------------------------
.../ComparableContractTest_compareTo.java | 32 +++++++
.../comparable/ComparableContractTester.java | 54 +++++++++++
.../comparable/CategorizedDomainObject.java | 70 +++++++++++++++
...mainObjectComparableContractTest_compareTo.java | 49 ++++++++++
4 files changed, 205 insertions(+), 0 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/isis/blob/51586b75/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/comparable/ComparableContractTest_compareTo.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/comparable/ComparableContractTest_compareTo.java b/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/comparable/ComparableContractTest_compareTo.java
new file mode 100644
index 0000000..f7e1322
--- /dev/null
+++ b/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/comparable/ComparableContractTest_compareTo.java
@@ -0,0 +1,32 @@
+package org.apache.isis.core.unittestsupport.comparable;
+
+import java.util.List;
+
+import com.google.common.collect.Lists;
+
+import org.junit.Test;
+
+public abstract class ComparableContractTest_compareTo<T extends Comparable<T>> {
+
+ /**
+ * Return an array of tuples; each tuple should consist of 4 elements, whereby
+ * item0 < item1 = item2 < item3
+ *
+ * Typically item0 should be null valued (if supported by the impl).
+ */
+ protected abstract List<List<T>> orderedTuples();
+
+ @Test
+ public void compareAllOrderedTuples() {
+
+ new ComparableContractTester<T>(orderedTuples()).test();
+ }
+
+ /**
+ * Syntax sugar to remove boilerplate from subclasses.
+ */
+ protected <E> List<E> listOf(E... elements) {
+ return Lists.newArrayList(elements);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/51586b75/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/comparable/ComparableContractTester.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/comparable/ComparableContractTester.java b/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/comparable/ComparableContractTester.java
new file mode 100644
index 0000000..cbfdbd5
--- /dev/null
+++ b/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/comparable/ComparableContractTester.java
@@ -0,0 +1,54 @@
+package org.apache.isis.core.unittestsupport.comparable;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+import java.util.List;
+
+import com.google.common.collect.Lists;
+
+import org.hamcrest.Matchers;
+
+public class ComparableContractTester<T extends Comparable<T>> {
+
+
+ private final List<List<T>> orderedTuples;
+
+ /**
+ * Provide an array of tuples; each tuple should consist of 4 elements, whereby
+ * item0 < item1 = item2 < item3
+ *
+ * Typically item0 should be null valued (if supported by the impl).
+ */
+ public ComparableContractTester(List<List<T>> orderedTuples) {
+ this.orderedTuples = orderedTuples;
+ }
+
+ public void test() {
+
+ for(List<T> orderedTuple: orderedTuples) {
+
+ T item1 = orderedTuple.get(0);
+ T item2 = orderedTuple.get(1);
+ T item3 = orderedTuple.get(2);
+ T item4 = orderedTuple.get(3);
+
+ assertThat(item1.compareTo(item2), is(Matchers.lessThan(0)));
+ assertThat(item2.compareTo(item1), is(Matchers.greaterThan(0)));
+
+ assertThat(item2.compareTo(item3), is(0));
+
+ assertThat(item3.compareTo(item4), is(Matchers.lessThan(0)));
+ assertThat(item4.compareTo(item3), is(Matchers.greaterThan(0)));
+ }
+ }
+
+ /**
+ * Syntax sugar to remove boilerplate from subclasses.
+ */
+ public static <E> List<E> listOf(E... elements) {
+ return Lists.newArrayList(elements);
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/51586b75/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/comparable/CategorizedDomainObject.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/comparable/CategorizedDomainObject.java b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/comparable/CategorizedDomainObject.java
new file mode 100644
index 0000000..b3de810
--- /dev/null
+++ b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/comparable/CategorizedDomainObject.java
@@ -0,0 +1,70 @@
+/**
+ * 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.core.unittestsupport.comparable;
+
+import com.google.common.collect.Ordering;
+
+
+public class CategorizedDomainObject implements Comparable<CategorizedDomainObject> {
+
+ private Integer category;
+
+ public Integer getCategory() {
+ return category;
+ }
+
+ public void setCategory(final Integer category) {
+ this.category = category;
+ }
+
+
+
+ private Integer subcategory;
+
+ public Integer getSubcategory() {
+ return subcategory;
+ }
+
+ public void setSubcategory(final Integer subcategory) {
+ this.subcategory = subcategory;
+ }
+
+
+
+ @Override
+ public int compareTo(CategorizedDomainObject other) {
+ return ORDER_BY_CATEGORY.compound(ORDER_BY_SUBCATEGORY).compare(this, other);
+ }
+
+ private static Ordering<CategorizedDomainObject> ORDER_BY_CATEGORY = new Ordering<CategorizedDomainObject>() {
+ @Override
+ public int compare(CategorizedDomainObject left, CategorizedDomainObject right) {
+ return Ordering.natural().nullsFirst().compare(left.getCategory(), right.getCategory());
+ }
+ };
+
+ private static Ordering<CategorizedDomainObject> ORDER_BY_SUBCATEGORY = new Ordering<CategorizedDomainObject>() {
+ @Override
+ public int compare(CategorizedDomainObject left, CategorizedDomainObject right) {
+ return Ordering.natural().nullsFirst().compare(left.getSubcategory(), right.getSubcategory());
+ }
+
+ };
+
+
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/51586b75/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/comparable/CategorizedDomainObjectComparableContractTest_compareTo.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/comparable/CategorizedDomainObjectComparableContractTest_compareTo.java b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/comparable/CategorizedDomainObjectComparableContractTest_compareTo.java
new file mode 100644
index 0000000..8bce302
--- /dev/null
+++ b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/comparable/CategorizedDomainObjectComparableContractTest_compareTo.java
@@ -0,0 +1,49 @@
+/**
+ * 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.core.unittestsupport.comparable;
+
+import java.util.List;
+
+public class CategorizedDomainObjectComparableContractTest_compareTo extends ComparableContractTest_compareTo<CategorizedDomainObject> {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ protected List<List<CategorizedDomainObject>> orderedTuples() {
+ return listOf(
+ listOf(
+ newObject(null, null),
+ newObject(1, null),
+ newObject(1, null),
+ newObject(2, null)
+ ),
+ listOf(
+ newObject(1, null),
+ newObject(1, 1),
+ newObject(1, 1),
+ newObject(1, 2)
+ )
+ );
+ }
+
+ private CategorizedDomainObject newObject(Integer category, Integer subcategory) {
+ final CategorizedDomainObject obj = new CategorizedDomainObject();
+ obj.setCategory(category);
+ obj.setSubcategory(subcategory);
+ return obj;
+ }
+
+}