Skip to content

Commit 9837cdf

Browse files
committed
updated TestDataGenerator to create lists with strings instead of files
1 parent 9d3372e commit 9837cdf

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

src/main/java/me/steffenjacobs/jsonmatcher/TestFileGenerator.java renamed to src/main/java/me/steffenjacobs/jsonmatcher/TestDataGenerator.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package me.steffenjacobs.jsonmatcher;
22

3-
import java.io.File;
4-
import java.io.FileNotFoundException;
5-
import java.io.PrintWriter;
63
import java.util.Collection;
74
import java.util.LinkedList;
5+
import java.util.List;
86

97
/** @author Steffen Jacobs */
10-
public class TestFileGenerator {
8+
public class TestDataGenerator {
119
private static final String[] KEYS_TEMPERATURE = new String[] { "temperature", "temp", "t", "temperatura", "Temperatur", "temperatur" };
1210
private static final String[] VALUES_TEMPERATURE = new String[] { "23", "\"23\"", "23.1", "\"23.1\"", "23°F", "23°C", "23.1°F" };
1311

@@ -25,18 +23,14 @@ public class TestFileGenerator {
2523

2624
private static final double RANDOM_SAMPLING_RATE = .00008;
2725

28-
public static void main(String[] args) throws FileNotFoundException {
29-
new TestFileGenerator().generateFull("all.lst");
30-
}
31-
32-
public void generateFull(String filename) throws FileNotFoundException {
26+
public List<String> generateSampled() {
3327
Collection<String> temperature = getAllPermutations(KEYS_TEMPERATURE, VALUES_TEMPERATURE);
3428
Collection<String> humidity = getAllPermutations(KEYS_HUMIDITY, VALUES_HUMIDITY);
3529
Collection<String> pressure = getAllPermutations(KEYS_PRESSURE, VALUES_PRESSURE);
3630
Collection<String> co2 = getAllPermutations(KEYS_CO2, VALUES_CO2);
3731
Collection<String> light = getAllPermutations(KEYS_LIGHT, VALUES_LIGHT);
3832

39-
final PrintWriter printWriter = new PrintWriter(new File(filename));
33+
final List<String> result = new LinkedList<>();
4034

4135
for (String temp : temperature) {
4236
StringBuilder sbT = new StringBuilder("{");
@@ -59,15 +53,15 @@ public void generateFull(String filename) throws FileNotFoundException {
5953
StringBuilder sb = new StringBuilder(sbC);
6054
sb.append(l);
6155
sb.append("}");
62-
printWriter.println(sb.toString());
56+
result.add(sb.toString());
6357
}
6458
}
6559
}
6660
}
6761
}
6862
}
6963

70-
printWriter.close();
64+
return result;
7165
}
7266

7367
private Collection<String> getAllPermutations(String[] keys, String[] values) {

src/test/java/me/steffenjacobs/jsonmatcher/TestMapperWithBigList.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import org.slf4j.Logger;
2020
import org.slf4j.LoggerFactory;
2121

22-
import me.steffenjacobs.jsonmatcher.Mapper;
23-
import me.steffenjacobs.jsonmatcher.TestFileGenerator;
2422
import me.steffenjacobs.jsonmatcher.domain.MappingDTO;
2523
import me.steffenjacobs.jsonmatcher.service.PrintingService;
2624

@@ -70,23 +68,22 @@ public static void initAllowedMappings() {
7068

7169
@Test
7270
public void testMapperWithBigList() throws IOException, URISyntaxException {
73-
File f = new File("all.list");
74-
new TestFileGenerator().generateFull("all.lst");
75-
testForList("all.lst");
76-
77-
f.delete();
71+
testForList(new TestDataGenerator().generateSampled());
7872
}
7973

8074
private boolean isMappingAllowed(MappingDTO<Object, Object> mapping) {
8175
return allowedMappings.get(mapping.getKeySource()).contains(mapping.getKeyTarget());
8276
}
8377

84-
public void testForList(String listFile) throws IOException, URISyntaxException {
78+
public void testForFile(String listFile) throws IOException, URISyntaxException {
79+
testForList(Files.readAllLines(new File(listFile).toPath()));
80+
}
81+
82+
public void testForList(List<String> lines) {
8583

8684
long countTotalMappings = 0;
8785
long countErrors = 0;
8886

89-
List<String> lines = Files.readAllLines(new File(listFile).toPath());
9087
lines = lines.stream().filter(l -> !"".equals(l)).collect(Collectors.toList());
9188
List<String> lines2 = new ArrayList<>(lines);
9289
LOG.info("Checking mapper for {} permutations...", lines.size() * lines2.size());
@@ -106,7 +103,7 @@ public void testForList(String listFile) throws IOException, URISyntaxException
106103
} catch (AssertionError error) {
107104
countErrors++;
108105
if (PRINT_ERRORS) {
109-
System.out.println("Bad mapping: " + mapping.getKeySource() + " -> " + mapping.getKeyTarget() + " (" + listFile + ")");
106+
System.out.println("Bad mapping: " + mapping.getKeySource() + " -> " + mapping.getKeyTarget());
110107
for (MappingDTO<Object, Object> m : mappings) {
111108
System.out.println(printingService.mappingToString(line, line2, m, SHOW_JSON));
112109
}

0 commit comments

Comments
 (0)