Skip to content

Commit

Permalink
Reduced test case for metafacture#366.
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed May 21, 2021
1 parent 6f3a197 commit 5d02bbe
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
5 changes: 5 additions & 0 deletions metamorph/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -45,4 +48,6 @@ test {
showStandardStreams = true
exceptionFormat = 'full'
}

systemProperties System.properties
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.metafacture.metamorph;

import org.metafacture.io.LineReader;
import org.metafacture.io.ObjectJavaIoWriter;
import org.metafacture.json.JsonDecoder;
import org.metafacture.json.JsonEncoder;
import org.metafacture.strings.StringReader;

import org.junit.Assert;
import org.junit.Test;

import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.LongAdder;
import java.util.stream.IntStream;

public final class Issue366Test {

private static final String INPUT = "{\"10012\":{\"a\":\"V\"}}";

private static final String[] OUTPUT = new String[]{
"{\"r\":{\"v\":\"V\",\"k\":\"K\"}}\n",
"{\"r\":{\"k\":\"K\",\"v\":\"V\"}}\n",
"{\"r\":{\"k\":\"K\",\"v\":\"V\"}}\n"
};

private static final String MORPH = "" +
"<metamorph xmlns=\"http://www.culturegraph.org/metamorph\" version=\"1\">\n" +
" <rules>\n" +
" <entity name=\"r\" flushWith=\"100??\">\n" +
" <choose flushWith=\"100%3$s%1$s\">\n" +
" <data name=\"k\" source=\"100%2$s.a\">\n" +
" <constant value=\"K\"/>\n" +
" </data>\n" +
" </choose>\n" +
" <data name=\"v\" source=\"100%3$s.a\" />\n" +
" </entity>\n" +
" </rules>\n" +
"</metamorph>";

private static final int OPTION = Integer.valueOf(System.getProperty("issue366.option", "0"));

private static final int ITER = Integer.valueOf(System.getProperty("issue366.iter", "10"));

private final Map<String, LongAdder> outcome = new HashMap<>();

private final Object[] args = new String[]{".a", "?2", "??"};
// Either one of these replacements avoids the issue:
private final Object[] opts = new String[]{"", "??", "?2"};

@Test
public void testTransform() {
if (OPTION > args.length) {
throw new RuntimeException("Invalid option: " + OPTION);
}
else if (OPTION > 0) {
args[OPTION - 1] = opts[OPTION - 1];
}

final String morph = String.format(MORPH, args);
System.out.println(morph);

IntStream.range(0, ITER).forEach(i -> transform(morph));
outcome.forEach((k, v) -> System.err.print(v.sum() + "/" + ITER + ": " + k));

Assert.assertEquals(1, outcome.size());
Assert.assertEquals(OUTPUT[OPTION - 1], outcome.keySet().iterator().next());
}

private void transform(final String morph) {
final StringReader reader = new StringReader();
final Writer writer = new StringWriter();

reader
.setReceiver(new LineReader())
.setReceiver(new JsonDecoder())
.setReceiver(new Metamorph(new java.io.StringReader(morph)))
.setReceiver(new JsonEncoder())
// ObjectJavaIoWriter in org.metafacture.io has been deprecated
.setReceiver(new ObjectJavaIoWriter<>(writer));

try {
reader.process(INPUT);
reader.closeStream();

outcome.computeIfAbsent(writer.toString(), k -> new LongAdder()).increment();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}

}

0 comments on commit 5d02bbe

Please sign in to comment.