diff --git a/metafix-runner/build.gradle b/metafix-runner/build.gradle index fe2f8d9b..9ebdd3e7 100644 --- a/metafix-runner/build.gradle +++ b/metafix-runner/build.gradle @@ -49,3 +49,13 @@ application { ] } } + +tasks.withType(JavaExec) { + doFirst { + def prefix = project.group + '.' + + System.properties.each { k, v -> + if (k.startsWith(prefix)) systemProperties[k] = v + } + } +} diff --git a/metafix/integrationTest.sh b/metafix/integrationTest.sh index 82e5371b..7533c33c 100755 --- a/metafix/integrationTest.sh +++ b/metafix/integrationTest.sh @@ -74,7 +74,8 @@ function rm_temp() { } function run_metafix() { - $gradle_command --console=plain -p "$root_directory" :metafix-runner:run --args="$1" -P${noprofile}profile="${1%.*}" + local file=$1; shift + $gradle_command --console=plain -p "$root_directory" :metafix-runner:run --args="$file" -P${noprofile}profile="${file%.*}" $@ } function run_catmandu() { @@ -224,10 +225,11 @@ function run_tests() { metafix_command_output="$test_directory/metafix.out" metafix_command_error="$test_directory/metafix.err" + metafix_command_args="$test_directory/metafix.args" metafix_start_time=$(current_time) - run_metafix "$test_directory/$metafix_file" >"$metafix_command_output" 2>"$metafix_command_error" + run_metafix "$test_directory/$metafix_file" $(cat "$metafix_command_args" 2>/dev/null || true) >"$metafix_command_output" 2>"$metafix_command_error" metafix_exit_status=$? metafix_elapsed_time=$(elapsed_time "$metafix_start_time") diff --git a/metafix/src/main/java/org/metafacture/metafix/Metafix.java b/metafix/src/main/java/org/metafacture/metafix/Metafix.java index cbcfedcb..478d6268 100644 --- a/metafix/src/main/java/org/metafacture/metafix/Metafix.java +++ b/metafix/src/main/java/org/metafacture/metafix/Metafix.java @@ -76,6 +76,8 @@ public class Metafix implements StreamPipe, Maps { public static final Map NO_VARS = Collections.emptyMap(); + public static final int MAX_ENTITY_COUNT = Integer.getInteger("org.metafacture.metafix.maxEntityCount", -1); + private static final Logger LOG = LoggerFactory.getLogger(Metafix.class); private static final String ENTITIES_NOT_BALANCED = "Entity starts and ends are not balanced"; @@ -239,7 +241,7 @@ public void startRecord(final String identifier) { flattener.startRecord(identifier); entityCountStack.clear(); entityCount = 0; - entityCountStack.add(Integer.valueOf(entityCount)); + entityCountStack.add(entityCount); recordIdentifier = identifier; entities = new ArrayList<>(); } @@ -313,22 +315,36 @@ public void startEntity(final String name) { throw new IllegalArgumentException("Entity name must not be null."); } + ++entityCount; + if (maxEntityCountExceeded()) { + LOG.debug("Maximum number of entities exceeded: {}/{}", entityCount, MAX_ENTITY_COUNT); + return; + } + final Value value = isArrayName(name) ? Value.newArray() : Value.newHash(); addValue(name, value); entities.add(value); - entityCountStack.push(Integer.valueOf(++entityCount)); + entityCountStack.push(entityCount); flattener.startEntity(name); } @Override public void endEntity() { - entityCountStack.pop().intValue(); + if (maxEntityCountExceeded()) { + return; + } + + entityCountStack.pop(); flattener.endEntity(); } @Override public void literal(final String name, final String value) { + if (maxEntityCountExceeded()) { + return; + } + LOG.debug("Putting '{}': '{}'", name, value); flattener.literal(name, value); } @@ -438,6 +454,10 @@ public String getEntityMemberName() { return entityMemberName; } + private boolean maxEntityCountExceeded() { + return MAX_ENTITY_COUNT >= 0 && entityCount > MAX_ENTITY_COUNT; + } + public enum Strictness { /** diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/maxEntityCount/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/maxEntityCount/expected.json new file mode 100644 index 00000000..3a2407ce --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/maxEntityCount/expected.json @@ -0,0 +1,3 @@ +{"key1":"value1","key2":"value2","key3":"value3","key4":"value4"} +{"key1":"value1","key2":["v1","v2"]} +{"key1":"value1","key2":["v1","v2"],"key3":"value3","key4":"value4"} diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/maxEntityCount/input.json b/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/maxEntityCount/input.json new file mode 100644 index 00000000..42c84b5a --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/maxEntityCount/input.json @@ -0,0 +1,3 @@ +{"key1":"value1","key2":"value2","key3":"value3","key4":"value4"} +{"key1":"value1","key2":["v1","v2"],"key3":["v3"],"key4":"value4"} +{"key1":"value1","key2":["v1","v2"],"key3":"value3","key4":"value4"} diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/maxEntityCount/metafix.args b/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/maxEntityCount/metafix.args new file mode 100644 index 00000000..d59d5387 --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/maxEntityCount/metafix.args @@ -0,0 +1 @@ +-Dorg.metafacture.metafix.maxEntityCount=1 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/maxEntityCount/test.fix b/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/maxEntityCount/test.fix new file mode 100644 index 00000000..174b9a02 --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/maxEntityCount/test.fix @@ -0,0 +1 @@ +nothing() diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/maxEntityCount/test.flux b/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/maxEntityCount/test.flux new file mode 100644 index 00000000..ec507521 --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/script/fromJson/toJson/maxEntityCount/test.flux @@ -0,0 +1,8 @@ +FLUX_DIR + "input.json" +|open-file +|as-lines +|decode-json +|fix(FLUX_DIR + "test.fix") +|encode-json +|write(FLUX_DIR + "output-metafix.json") +;