Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear cache if validator instance changes #16

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import ch.interlis.iox_j.validator.Value;

public abstract class BaseInterlisFunction implements InterlisFunction {
private static Validator lastValidator = null;

protected LogEventFactory logger;
protected TransferDescription td;
Expand All @@ -26,6 +27,11 @@ public final void init(TransferDescription td, Settings settings, IoxValidationC
this.settings = settings;
this.validator = (Validator) settings.getTransientObject(IOX_VALIDATOR);
this.objectPool = objectPool;

if (lastValidator != this.validator) {
resetCaches();
lastValidator = this.validator;
}
}

@Override
Expand All @@ -43,4 +49,9 @@ public final Value evaluate(String validationKind, String usageScope, IomObject
}

protected abstract Value evaluateInternal(String validationKind, String usageScope, IomObject mainObj, Value[] actualArguments);

/**
* A change was detected that requires the caches to be reset.
*/
protected void resetCaches() { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public String getQualifiedIliName() {
return "NGK_SO_FunctionsExt.IsInsideAreaByCode";
}

@Override
protected void resetCaches() {
OBJECTS_CACHE.clear();
}

@Override
protected Value evaluateInternal(String validationKind, String usageScope, IomObject contextObject, Value[] arguments) {
Value argObjects = arguments[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ch.geowerkstatt.ilivalidator.extensions.functions.ngk;

import ch.interlis.iox.IoxLogEvent;
import com.vividsolutions.jts.util.Assert;

import java.util.List;
import java.util.regex.Pattern;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

public final class AssertionHelper {
Expand All @@ -23,7 +23,7 @@ public static void assertConstraintErrors(LogCollector logger, int expectedCount
}
}

Assert.equals(expectedCount, errorsFound,
assertEquals(expectedCount, errorsFound,
String.format("Expected %d but found %d errors with OID <%s> and Source <%s>.", expectedCount, errorsFound, oid, constraintName));
}

Expand All @@ -39,7 +39,7 @@ public static void assertConstraintErrors(LogCollector logger, int expectedCount
}
}

Assert.equals(expectedCount, errorsFound,
assertEquals(expectedCount, errorsFound,
String.format("Expected %s errors with Source <%s> but found %d.", expectedCount, constraintName, errorsFound));
}

Expand All @@ -51,7 +51,7 @@ public static void assertNoConstraintError(LogCollector logger, String constrain
}
}

Assert.equals(0, errorsFound,
assertEquals(0, errorsFound,
String.format("Expected No errors with Source <%s> but found %d.", constraintName, errorsFound));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import ch.interlis.iom.IomObject;
import ch.interlis.iom_j.Iom_jObject;
import ch.interlis.iox.IoxException;
import com.vividsolutions.jts.util.Assert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -18,6 +17,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

public final class IsInsideAreaByCodeIoxPluginTest {
private static final String ILI_FILE = "IsInsideAreaByCode/SetConstraints.ili";
private static final String TEST_DATA_OK = "IsInsideAreaByCode/TestData_Ok.xtf";
Expand All @@ -44,15 +45,15 @@ public void tearDown() throws NoSuchFieldException, IllegalAccessException {
@Test
public void setConstraintOk() throws Ili2cFailure, IoxException {
LogCollector logger = vh.runValidation(new String[]{TEST_DATA_OK}, new String[]{ILI_FILE});
Assert.equals(0, logger.getErrs().size());
assertEquals(0, logger.getErrs().size());
AssertionHelper.assertNoConstraintError(logger, "insideAreaConstraintEnum");
AssertionHelper.assertNoConstraintError(logger, "insideAreaConstraintNumeric");
}

@Test
public void setConstraintFail() throws Ili2cFailure, IoxException {
LogCollector logger = vh.runValidation(new String[]{TEST_DATA_FAIL}, new String[]{ILI_FILE});
Assert.equals(9, logger.getErrs().size());
assertEquals(9, logger.getErrs().size());

AssertionHelper.assertLogEventsMessages(logger.getErrs(), "^IsInsideAreaByCode found an invalid overlap or topological error \\(missing support point\\) between code 'code_2' and 'code_3'", 1);
AssertionHelper.assertLogEventsMessages(logger.getErrs(), "^IsInsideAreaByCode found an invalid overlap or topological error \\(missing support point\\) between code 'code_3' and 'code_4'", 1);
Expand Down Expand Up @@ -82,7 +83,7 @@ public void isInsideAreaByCode() throws Ili2cFailure {
});

LogCollector logger = vh.runValidation(new String[]{ILI_FILE}, TOPIC, objects.stream().map(Supplier::get).toArray(IomObject[]::new));
Assert.equals(0, logger.getErrs().size());
assertEquals(0, logger.getErrs().size());
}

@Test
Expand Down Expand Up @@ -140,7 +141,7 @@ public void multiplePolygonsPerCode() throws Ili2cFailure {
});

LogCollector logger = vh.runValidation(new String[]{ILI_FILE}, TOPIC, objects.stream().map(Supplier::get).toArray(IomObject[]::new));
Assert.equals(0, logger.getErrs().size());
assertEquals(0, logger.getErrs().size());
}

@Test
Expand Down Expand Up @@ -188,7 +189,7 @@ public void allCodesStacked() throws Ili2cFailure {
.collect(Collectors.toList());

LogCollector logger = vh.runValidation(new String[]{ILI_FILE}, TOPIC, objects.stream().map(Supplier::get).toArray(IomObject[]::new));
Assert.equals(0, logger.getErrs().size());
assertEquals(0, logger.getErrs().size());
}

@Test
Expand Down Expand Up @@ -235,7 +236,7 @@ public void enumWithoutNumber() throws Ili2cFailure {
});

LogCollector logger = vh.runValidation(new String[]{ILI_FILE}, TOPIC, objects.stream().map(Supplier::get).toArray(IomObject[]::new));
Assert.equals(0, logger.getErrs().size());
assertEquals(0, logger.getErrs().size());
}

@Test
Expand All @@ -259,7 +260,7 @@ public void sharedSegment() throws Ili2cFailure {
});

LogCollector logger = vh.runValidation(new String[]{ILI_FILE}, TOPIC, objects.stream().map(Supplier::get).toArray(IomObject[]::new));
Assert.equals(0, logger.getErrs().size());
assertEquals(0, logger.getErrs().size());
}

@Test
Expand All @@ -277,7 +278,7 @@ public void collinearSegment() throws Ili2cFailure {
});

LogCollector logger = vh.runValidation(new String[]{ILI_FILE}, TOPIC, objects.stream().map(Supplier::get).toArray(IomObject[]::new));
Assert.equals(0, logger.getErrs().size());
assertEquals(0, logger.getErrs().size());
}

@Test
Expand Down Expand Up @@ -325,7 +326,7 @@ public void sharedArcSegment() throws Ili2cFailure {
});

LogCollector logger = vh.runValidation(new String[]{ILI_FILE}, TOPIC, objects.stream().map(Supplier::get).toArray(IomObject[]::new));
Assert.equals(0, logger.getErrs().size());
assertEquals(0, logger.getErrs().size());
}

@Test
Expand Down Expand Up @@ -354,7 +355,7 @@ public void sharedArcSegmentDifferentMidPoint() throws Ili2cFailure {

LogCollector logger = vh.runValidation(new String[]{ILI_FILE}, TOPIC, objects.stream().map(Supplier::get).toArray(IomObject[]::new));
// Because the arcs are stroked differently, thin overlaps occur
Assert.equals(2, logger.getErrs().size());
assertEquals(2, logger.getErrs().size());

AssertionHelper.assertLogEventsMessages(logger.getErrs(), "^IsInsideAreaByCode found an invalid overlap or topological error \\(missing support point\\) between code 'code_10' and 'code_30'. The offending geometry is near: POINT \\(15.8\\d+ 59.5\\d+\\)$", 1);
AssertionHelper.assertLogEventsMessages(logger.getErrs(), "^Set Constraint TestSuite.FunctionTestTopic.TestClass.insideAreaConstraint is not true.$", 1);
Expand Down Expand Up @@ -390,4 +391,43 @@ public void almostCollinearSegment() throws Ili2cFailure {
"IsInsideAreaByCode found a topological error (probably missing support point) between code 'code_10' and 'code_30'. The offending geometry is inside the envelope: POLYGON ((2610067.640033932 1252503.9000373208, 2610067.640033932 1252503.9794528584, 2610067.67 1252503.9794528584, 2610067.67 1252503.9000373208, 2610067.640033932 1252503.9000373208))",
"Set Constraint TestSuite.FunctionTestTopic.TestClass.insideAreaConstraint is not true.");
}

@Test
public void clearCacheBetweenValidations() throws Ili2cFailure {
// The oids are in both validations the same to get incorrect cache hits if the cache is not cleared in between validations
final String oid1 = "o1";
final String oid2 = "o2";

List<Supplier<IomObject>> objectsError = Arrays.asList(() -> {
IomObject object = new Iom_jObject(TEST_CLASS, oid1);
object.setattrvalue("code", "code_10");
object.addattrobj("surface", IomObjectHelper.createRectangleGeometry("20", "20", "60", "60"));
return object;
}, () -> {
IomObject object = new Iom_jObject(TEST_CLASS, oid2);
object.setattrvalue("code", "code_40");
object.addattrobj("surface", IomObjectHelper.createRectangleGeometry("0", "0", "40", "40"));
return object;
});

LogCollector logger = vh.runValidation(new String[]{ILI_FILE}, TOPIC, objectsError.stream().map(Supplier::get).toArray(IomObject[]::new));
AssertionHelper.assertEventMessagesAreEqual(logger.getErrs(),
"IsInsideAreaByCode found an invalid overlap or topological error (missing support point) between code 'code_10' and 'code_40'. The offending geometry is near: POINT (40 50)",
"Set Constraint TestSuite.FunctionTestTopic.TestClass.insideAreaConstraint is not true.");

List<Supplier<IomObject>> objectsValid = Arrays.asList(() -> {
IomObject object = new Iom_jObject(TEST_CLASS, oid1);
object.setattrvalue("code", "code_10");
object.addattrobj("surface", IomObjectHelper.createRectangleGeometry("20", "20", "40", "40"));
return object;
}, () -> {
IomObject object = new Iom_jObject(TEST_CLASS, oid2);
object.setattrvalue("code", "code_40");
object.addattrobj("surface", IomObjectHelper.createRectangleGeometry("0", "0", "60", "60"));
return object;
});

logger = vh.runValidation(new String[]{ILI_FILE}, TOPIC, objectsValid.stream().map(Supplier::get).toArray(IomObject[]::new));
assertEquals(0, logger.getErrs().size());
}
}
Loading