From 92a035cde2c9a96d91d7c4babac2ee37cc13cfe9 Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Fri, 21 May 2021 11:12:50 +0200 Subject: [PATCH] Reduced test case for #366. --- metamorph/build.gradle | 5 + .../metafacture/metamorph/Issue366Test.java | 111 ++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 metamorph/src/test/java/org/metafacture/metamorph/Issue366Test.java diff --git a/metamorph/build.gradle b/metamorph/build.gradle index ed537f1a5..5fd1c5121 100644 --- a/metamorph/build.gradle +++ b/metamorph/build.gradle @@ -25,6 +25,9 @@ dependencies { implementation project(':metafacture-mangling') implementation project(':metafacture-javaintegration') implementation 'org.slf4j:slf4j-api:1.7.21' + testImplementation project(':metafacture-io') + testImplementation project(':metafacture-json') + testImplementation project(':metafacture-strings') testImplementation 'junit:junit:4.12' testImplementation 'org.mockito:mockito-core:2.5.5' testRuntimeOnly 'org.slf4j:slf4j-simple:1.7.21' @@ -45,4 +48,6 @@ test { showStandardStreams = true exceptionFormat = 'full' } + + systemProperties System.properties } diff --git a/metamorph/src/test/java/org/metafacture/metamorph/Issue366Test.java b/metamorph/src/test/java/org/metafacture/metamorph/Issue366Test.java new file mode 100644 index 000000000..ba2272972 --- /dev/null +++ b/metamorph/src/test/java/org/metafacture/metamorph/Issue366Test.java @@ -0,0 +1,111 @@ +package org.metafacture.metamorph; + +import org.metafacture.io.LineReader; +import org.metafacture.json.JsonDecoder; +import org.metafacture.json.JsonEncoder; +import org.metafacture.strings.StringReader; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.io.StringWriter; +import java.io.Writer; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.LongAdder; +import java.util.stream.IntStream; + +@RunWith(Parameterized.class) +public final class Issue366Test { + + @Parameterized.Parameters + public static Object[][] data() { + return new Object[][]{ + {0, null, "{\"r\":{\"k\":\"K\",\"v\":\"V\"}}\n"}, + {1, "", "{\"r\":{\"v\":\"V\",\"k\":\"K\"}}\n"}, + {2, "??", "{\"r\":{\"k\":\"K\",\"v\":\"V\"}}\n"}, + {3, "?2", "{\"r\":{\"k\":\"K\",\"v\":\"V\"}}\n"} + }; + } + + @Parameterized.Parameter(0) + public int option; + + @Parameterized.Parameter(1) + public String replacement; + + @Parameterized.Parameter(2) + public String output; + + private static final String INPUT = "{\"10012\":{\"a\":\"V\"}}"; + + private static final String MORPH = "" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + + private static final Object[] ARGS = new String[]{".a", "?2", "??"}; + + private static final boolean VERBOSE = System.getProperty("issue366.verbose", "false").equals("true"); + private static final int ITER = Integer.valueOf(System.getProperty("issue366.iter", "10")); + + private final Map outcome = new HashMap<>(); + private final Object[] args = Arrays.copyOf(ARGS, ARGS.length); + + @Test + public void testTransform() { + if (option > 0) { + args[option - 1] = replacement; + } + + final String morph = String.format(MORPH, args); + IntStream.range(0, ITER).forEach(i -> transform(morph)); + + if (VERBOSE) { + System.out.println(morph); + outcome.forEach((k, v) -> System.err.print(v.sum() + "/" + ITER + ": " + k)); + } + + Assert.assertEquals(1, outcome.size()); + Assert.assertEquals(output, outcome.keySet().iterator().next()); + } + + private void transform(final String morph) { + final StringReader reader = new StringReader(); + final Writer writer = new StringWriter(); + + @SuppressWarnings("deprecation") + final org.metafacture.io.ObjectJavaIoWriter ojiw = + new org.metafacture.io.ObjectJavaIoWriter<>(writer); + + reader + .setReceiver(new LineReader()) + .setReceiver(new JsonDecoder()) + .setReceiver(new Metamorph(new java.io.StringReader(morph))) + .setReceiver(new JsonEncoder()) + .setReceiver(ojiw); + + try { + reader.process(INPUT); + reader.closeStream(); + + outcome.computeIfAbsent(writer.toString(), k -> new LongAdder()).increment(); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + +}