sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/03: Verify also the ComparisonFunction name during tests, and verify at least one of the two expression operands. Recycle some previously created objects, and add comments about what is being tested in TemporalFunctionTest.
Date Mon, 26 Aug 2019 17:28:17 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 5961fbcdefbcceea661fc73f470d4b7da17e753e
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Mon Aug 26 15:47:21 2019 +0200

    Verify also the ComparisonFunction name during tests, and verify at least one of the two
expression operands.
    Recycle some previously created objects, and add comments about what is being tested in
TemporalFunctionTest.
---
 .../apache/sis/filter/ComparisonFunctionTest.java  | 101 +++++++++++++++------
 .../apache/sis/filter/TemporalFunctionTest.java    |  58 +++++++++++-
 2 files changed, 128 insertions(+), 31 deletions(-)

diff --git a/core/sis-feature/src/test/java/org/apache/sis/filter/ComparisonFunctionTest.java
b/core/sis-feature/src/test/java/org/apache/sis/filter/ComparisonFunctionTest.java
index 183ae06..cfe4dff 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/filter/ComparisonFunctionTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/filter/ComparisonFunctionTest.java
@@ -16,11 +16,13 @@
  */
 package org.apache.sis.filter;
 
-import static org.apache.sis.test.Assert.*;
+import org.opengis.filter.FilterFactory;
+import org.opengis.filter.expression.Literal;
+import org.opengis.filter.BinaryComparisonOperator;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
-import org.opengis.filter.Filter;
-import org.opengis.filter.FilterFactory2;
+
+import static org.apache.sis.test.Assert.*;
 
 
 /**
@@ -35,16 +37,56 @@ public final strictfp class ComparisonFunctionTest extends TestCase {
     /**
      * The factory to use for creating the objects to test.
      */
-    private final FilterFactory2 factory = new DefaultFilterFactory();
+    private final FilterFactory factory;
+
+    /**
+     * Expressions used as constant for the tests.
+     */
+    private final Literal c05, c10, c20;
+
+    /**
+     * Expected name of the filter to be evaluated. The {@link #evaluate(BinaryComparisonOperator)}
method
+     * will compare {@link ComparisonFunction#getName()} against this value.
+     */
+    private String expectedName;
+
+    /**
+     * The filter tested by last call to {@link #evaluate(BinaryComparisonOperator)}.
+     */
+    private BinaryComparisonOperator filter;
+
+    /**
+     * Creates a new test case.
+     */
+    public ComparisonFunctionTest() {
+        factory = new DefaultFilterFactory();
+        c05 = factory.literal(5);
+        c10 = factory.literal(10);
+        c20 = factory.literal(20);
+    }
+
+    /**
+     * Evaluates the given filter. The {@link #expectedName} field must be set before this
method is invoked.
+     * This method assumes that the first expression of all filters is {@link #c10}.
+     */
+    private boolean evaluate(final BinaryComparisonOperator filter) {
+        this.filter = filter;
+        assertInstanceOf("Expected SIS implementation.", ComparisonFunction.class, filter);
+        assertEquals("name", expectedName, ((ComparisonFunction) filter).getName());
+        assertSame("expression1", c10, filter.getExpression1());
+        return filter.evaluate(null);
+    }
 
     /**
      * Tests "LessThan" (construction, evaluation, serialization, equality).
      */
     @Test
     public void testLess() {
-        assertFilter(true,  factory.less(factory.literal(10), factory.literal(20)));
-        assertFilter(false, factory.less(factory.literal(10), factory.literal(10)));
-        assertFilter(false, factory.less(factory.literal(10), factory.literal(5)));
+        expectedName = "LessThan";
+        assertTrue (evaluate(factory.less(c10, c20)));
+        assertFalse(evaluate(factory.less(c10, c10)));
+        assertFalse(evaluate(factory.less(c10, c05)));
+        assertSerializedEquals(filter);
     }
 
     /**
@@ -52,9 +94,11 @@ public final strictfp class ComparisonFunctionTest extends TestCase {
      */
     @Test
     public void testLessOrEqual() {
-        assertFilter(true,  factory.lessOrEqual(factory.literal(10), factory.literal(20)));
-        assertFilter(true,  factory.lessOrEqual(factory.literal(10), factory.literal(10)));
-        assertFilter(false, factory.lessOrEqual(factory.literal(10), factory.literal(5)));
+        expectedName = "LessThanOrEqualTo";
+        assertTrue (evaluate(factory.lessOrEqual(c10, c20)));
+        assertTrue (evaluate(factory.lessOrEqual(c10, c10)));
+        assertFalse(evaluate(factory.lessOrEqual(c10, c05)));
+        assertSerializedEquals(filter);
     }
 
     /**
@@ -62,9 +106,11 @@ public final strictfp class ComparisonFunctionTest extends TestCase {
      */
     @Test
     public void testGreater() {
-        assertFilter(false, factory.greater(factory.literal(10), factory.literal(20)));
-        assertFilter(false, factory.greater(factory.literal(10), factory.literal(10)));
-        assertFilter(true,  factory.greater(factory.literal(10), factory.literal(5)));
+        expectedName = "GreaterThan";
+        assertFalse(evaluate(factory.greater(c10, c20)));
+        assertFalse(evaluate(factory.greater(c10, c10)));
+        assertTrue (evaluate(factory.greater(c10, c05)));
+        assertSerializedEquals(filter);
     }
 
     /**
@@ -72,9 +118,11 @@ public final strictfp class ComparisonFunctionTest extends TestCase {
      */
     @Test
     public void testGreaterOrEqual() {
-        assertFilter(false, factory.greaterOrEqual(factory.literal(10), factory.literal(20)));
-        assertFilter(true,  factory.greaterOrEqual(factory.literal(10), factory.literal(10)));
-        assertFilter(true,  factory.greaterOrEqual(factory.literal(10), factory.literal(5)));
+        expectedName = "GreaterThanOrEqualTo";
+        assertFalse(evaluate(factory.greaterOrEqual(c10, c20)));
+        assertTrue (evaluate(factory.greaterOrEqual(c10, c10)));
+        assertTrue (evaluate(factory.greaterOrEqual(c10, c05)));
+        assertSerializedEquals(filter);
     }
 
     /**
@@ -82,9 +130,11 @@ public final strictfp class ComparisonFunctionTest extends TestCase {
      */
     @Test
     public void testEqual() {
-        assertFilter(false, factory.equals(factory.literal(10), factory.literal(20)));
-        assertFilter(true,  factory.equals(factory.literal(10), factory.literal(10)));
-        assertFilter(false, factory.equals(factory.literal(10), factory.literal(5)));
+        expectedName = "EqualTo";
+        assertFalse(evaluate(factory.equals(c10, c20)));
+        assertTrue (evaluate(factory.equals(c10, c10)));
+        assertFalse(evaluate(factory.equals(c10, c05)));
+        assertSerializedEquals(filter);
     }
 
     /**
@@ -92,13 +142,10 @@ public final strictfp class ComparisonFunctionTest extends TestCase {
      */
     @Test
     public void testNotEqual() {
-        assertFilter(true,  factory.notEqual(factory.literal(10), factory.literal(20)));
-        assertFilter(false, factory.notEqual(factory.literal(10), factory.literal(10)));
-        assertFilter(true,  factory.notEqual(factory.literal(10), factory.literal(5)));
-    }
-
-    private static void assertFilter(boolean expected, Filter op) {
-        assertEquals(expected, op.evaluate(null));
-        assertSerializedEquals(op);
+        expectedName = "NotEqualTo";
+        assertTrue (evaluate(factory.notEqual(c10, c20)));
+        assertFalse(evaluate(factory.notEqual(c10, c10)));
+        assertTrue (evaluate(factory.notEqual(c10, c05)));
+        assertSerializedEquals(filter);
     }
 }
diff --git a/core/sis-feature/src/test/java/org/apache/sis/filter/TemporalFunctionTest.java
b/core/sis-feature/src/test/java/org/apache/sis/filter/TemporalFunctionTest.java
index c46c512..507d33d 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/filter/TemporalFunctionTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/filter/TemporalFunctionTest.java
@@ -66,15 +66,15 @@ public final strictfp class TemporalFunctionTest extends TestCase {
 
     /**
      * Performs some validation on newly created filter.
+     * The {@link #filter} field must be initialized before this method is invoked.
      *
      * @param  name  expected filter name.
      */
     private void validate(final String name) {
         assertInstanceOf("Expected SIS implementation.", TemporalFunction.class, filter);
-        final TemporalFunction f = ((TemporalFunction) filter);
-        assertEquals("name", name, f.getName());
-        assertSame("expression1", expression1, f.expression1);
-        assertSame("expression2", expression2, f.expression2);
+        assertEquals("name", name, ((TemporalFunction) filter).getName());
+        assertSame("expression1", expression1, filter.getExpression1());
+        assertSame("expression2", expression2, filter.getExpression2());
         assertSerializedEquals(filter);
     }
 
@@ -93,6 +93,8 @@ public final strictfp class TemporalFunctionTest extends TestCase {
         filter = factory.tequals(expression1, expression2);
         validate("TEquals");
         assertTrue(evaluate());
+
+        // Break the "self.end = other.end" condition.
         expression1.end++;
         assertFalse(evaluate());
     }
@@ -105,9 +107,13 @@ public final strictfp class TemporalFunctionTest extends TestCase {
         filter = factory.before(expression1, expression2);
         validate("Before");
         assertFalse(evaluate());
+
+        // Move before expression 2.
         expression1.begin -= 10 * MILLISECONDS_PER_DAY;
         expression1.end   -= 10 * MILLISECONDS_PER_DAY;
         assertTrue(evaluate());
+
+        // Break the "self.end < other.begin" condition.
         expression1.end = expression2.begin;
         assertFalse(evaluate());
     }
@@ -120,9 +126,13 @@ public final strictfp class TemporalFunctionTest extends TestCase {
         filter = factory.after(expression1, expression2);
         validate("After");
         assertFalse(evaluate());
+
+        // Move after expression 2.
         expression1.begin += 10 * MILLISECONDS_PER_DAY;
         expression1.end   += 10 * MILLISECONDS_PER_DAY;
         assertTrue(evaluate());
+
+        // Break the "self.begin > other.end" condition.
         expression1.begin = expression2.end;
         assertFalse(evaluate());
     }
@@ -135,8 +145,12 @@ public final strictfp class TemporalFunctionTest extends TestCase {
         filter = factory.begins(expression1, expression2);
         validate("Begins");
         assertFalse(evaluate());
+
+        // End before ending of expression 2.
         expression1.end--;
         assertTrue(evaluate());
+
+        // Break the "self.begin = other.begin" condition.
         expression1.begin++;
         assertFalse(evaluate());
     }
@@ -149,8 +163,12 @@ public final strictfp class TemporalFunctionTest extends TestCase {
         filter = factory.ends(expression1, expression2);
         validate("Ends");
         assertFalse(evaluate());
+
+        // Begin after beginning of expression 2.
         expression1.begin++;
         assertTrue(evaluate());
+
+        // Break the "self.end = other.end" condition.
         expression1.end--;
         assertFalse(evaluate());
     }
@@ -163,8 +181,12 @@ public final strictfp class TemporalFunctionTest extends TestCase {
         filter = factory.begunBy(expression1, expression2);
         validate("BegunBy");
         assertFalse(evaluate());
+
+        // End after ending of expression 2.
         expression1.end++;
         assertTrue(evaluate());
+
+        // Break the "self.begin = other.begin" condition.
         expression1.begin--;
         assertFalse(evaluate());
     }
@@ -177,8 +199,12 @@ public final strictfp class TemporalFunctionTest extends TestCase {
         filter = factory.endedBy(expression1, expression2);
         validate("EndedBy");
         assertFalse(evaluate());
+
+        // Begin before beginning of expression 2.
         expression1.begin--;
         assertTrue(evaluate());
+
+        // Break the "self.end = other.end" condition.
         expression1.end++;
         assertFalse(evaluate());
     }
@@ -191,9 +217,13 @@ public final strictfp class TemporalFunctionTest extends TestCase {
         filter = factory.meets(expression1, expression2);
         validate("Meets");
         assertFalse(evaluate());
+
+        // Move before expression 2.
         expression1.begin -= 10 * MILLISECONDS_PER_DAY;
         expression1.end   -= 10 * MILLISECONDS_PER_DAY;
         assertFalse(evaluate());
+
+        // Met the "self.end = other.begin" condition.
         expression1.end = expression2.begin;
         assertTrue(evaluate());
     }
@@ -206,9 +236,13 @@ public final strictfp class TemporalFunctionTest extends TestCase {
         filter = factory.metBy(expression1, expression2);
         validate("MetBy");
         assertFalse(evaluate());
+
+        // Move after expression 2.
         expression1.begin += 10 * MILLISECONDS_PER_DAY;
         expression1.end   += 10 * MILLISECONDS_PER_DAY;
         assertFalse(evaluate());
+
+        // Met the "self.begin = other.end" condition.
         expression1.begin = expression2.end;
         assertTrue(evaluate());
     }
@@ -221,9 +255,13 @@ public final strictfp class TemporalFunctionTest extends TestCase {
         filter = factory.during(expression1, expression2);
         validate("During");
         assertFalse(evaluate());
+
+        // Shrink inside expression 2.
         expression1.begin++;
         expression1.end--;
         assertTrue(evaluate());
+
+        // Break the "self.end < other.end" condition.
         expression1.end += 2;
         assertFalse(evaluate());
     }
@@ -236,9 +274,13 @@ public final strictfp class TemporalFunctionTest extends TestCase {
         filter = factory.tcontains(expression1, expression2);
         validate("TContains");
         assertFalse(evaluate());
+
+        // Expand to encompass expression 2.
         expression1.begin--;
         expression1.end++;
         assertTrue(evaluate());
+
+        // Break the "self.end > other.end" condition.
         expression1.end -= 2;
         assertFalse(evaluate());
     }
@@ -251,9 +293,13 @@ public final strictfp class TemporalFunctionTest extends TestCase {
         filter = factory.toverlaps(expression1, expression2);
         validate("TOverlaps");
         assertFalse(evaluate());
+
+        // Translate to overlap left part of expression 2.
         expression1.begin--;
         expression1.end--;
         assertTrue(evaluate());
+
+        // Break the "self.end < other.end" condition.
         expression1.end += 2;
         assertFalse(evaluate());
     }
@@ -266,9 +312,13 @@ public final strictfp class TemporalFunctionTest extends TestCase {
         filter = factory.overlappedBy(expression1, expression2);
         validate("OverlappedBy");
         assertFalse(evaluate());
+
+        // Translate to overlap right part of expression 2.
         expression1.begin++;
         expression1.end++;
         assertTrue(evaluate());
+
+        // Break the "self.end > other.end" condition.
         expression1.end -= 2;
         assertFalse(evaluate());
     }


Mime
View raw message