Skip to content

Commit d7bceaa

Browse files
committed
WIP
1 parent f7661dd commit d7bceaa

3 files changed

Lines changed: 62 additions & 2 deletions

File tree

metafix/src/main/java/org/metafacture/metafix/Metafix.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,25 @@ public boolean getStrictnessHandlesProcessExceptions() {
602602
return strictnessHandlesProcessExceptions;
603603
}
604604

605+
/**
606+
* Sets the global error limit.
607+
* The default is {@link Strictness#DEFAULT_ERROR_LIMIT}.
608+
*
609+
* @param errorLimit the limit of allowed errors
610+
*/
611+
public void setErrorLimit(final int errorLimit) {
612+
this.strictness.setErrorLimit(errorLimit);
613+
}
614+
615+
/**
616+
* Gets the error limit.
617+
*
618+
* @return the error limit
619+
*/
620+
public int getErrorLimit() {
621+
return this.strictness.errorLimit;
622+
}
623+
605624
/**
606625
* Flags whether repeated fields should always be emitted as array entities.
607626
*
@@ -660,7 +679,7 @@ protected void handleInternal(final MetafactureException exception, final Record
660679
*/
661680
RECORD {
662681
@Override
663-
protected void handleInternal(final MetafactureException exception, final Record record) {
682+
protected void handleInternal(final MetafactureException exception, final Record record) {;
664683
log(exception, LOG::error);
665684
record.setReject(true); // TODO: Skip remaining expressions?
666685
}
@@ -676,18 +695,28 @@ protected void handleInternal(final MetafactureException exception, final Record
676695
}
677696
};
678697

698+
public static final int DEFAULT_ERROR_LIMIT = Integer.MAX_VALUE;
699+
private static int errorLimit = DEFAULT_ERROR_LIMIT;
700+
private int errorCnt = 0;
679701
/**
680702
* Handles the exception based on the selected strictness level.
681703
*
682704
* @param exception the exception to be handled
683705
* @param record the current record
684706
*/
685707
public void handle(final MetafactureException exception, final Record record) {
708+
errorCnt++;
709+
if ( errorCnt > errorLimit){
710+
throw new MetafactureException("Too many errors (as defined in errorLimit) - exiting");
711+
}
686712
LOG.info("Current record: {}", record);
687713
handleInternal(exception, record);
688714
}
689715

690716
protected abstract void handleInternal(MetafactureException exception, Record record);
717+
public void setErrorLimit(int errorLimit){
718+
this.errorLimit=errorLimit;
719+
}
691720

692721
protected void log(final MetafactureException exception, final BiConsumer<String, Throwable> logger) {
693722
logger.accept(exception.getMessage(), exception.getCause());

metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
*/
4747
@ExtendWith(MockitoExtension.class)
4848
public class MetafixLookupTest {
49-
private static final String CSV_MAP = "src/test/resources/org/metafacture/metafix/maps/test.csv";
49+
private static final String CSV_MAP = "FUTSCHsrc/test/resources/org/metafacture/metafix/maps/test.csv";
5050
private static final String CSV_PATH = "/maps/test.csv";
5151
private static final String CSV_URL = "%s" + CSV_PATH;
5252
private static final String RDF_MAP = "src/test/resources/org/metafacture/metafix/maps/test.ttl";
@@ -469,6 +469,14 @@ public void shouldLookupInSeparateExternalFileMap() {
469469
);
470470
}
471471

472+
@Test
473+
public void shouldLogWhenExternalFileMapMissing() {
474+
assertMap(
475+
"put_filemap('" + CSV_MAP + "')",
476+
LOOKUP + " '" + CSV_MAP + "')"
477+
);
478+
}
479+
472480
@Test
473481
public void shouldNotLookupInRelativeExternalFileMapFromInlineScript() {
474482
final String mapFile = "../maps/test.csv";

metafix/src/test/java/org/metafacture/metafix/MetafixScriptTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,29 @@ public void shouldOptionallySkipExpressionOnProcessException() {
584584
});
585585
}
586586

587+
@Test
588+
public void shouldExitingOnProcessExceptionWhenErrorLimitIsOne() {
589+
assertStrictness(Metafix.Strictness.EXPRESSION, "upcase()", true, i -> {i.setStrictnessHandlesProcessExceptions(true); i.setErrorLimit(1);}, o -> {
590+
o.get().startRecord("1");
591+
o.get().literal("data", "foo");
592+
o.get().literal("before", "");
593+
o.get().literal("after", "");
594+
o.get().endRecord();
595+
596+
o.get().startRecord("2");
597+
o.get().literal("data", "foo");
598+
o.get().literal("data", "bar");
599+
o.get().literal("before", "");
600+
o.get().literal("after", "");
601+
o.get().endRecord();
602+
603+
o.get().startRecord("3");
604+
o.get().literal("data", "bar");
605+
o.get().literal("before", "");
606+
o.get().literal("after", "");
607+
o.get().endRecord();
608+
});
609+
}
587610
private void assertVar(final String fixDef, final Map<String, String> vars, final Map<String, String> result) {
588611
assertFix(fixDef, vars, f -> result.forEach((k, v) -> Assertions.assertEquals(v, f.getVars().get(k))));
589612
}

0 commit comments

Comments
 (0)