Skip to content

Commit 9d10a70

Browse files
committed
Fix "setreplace" using a FileMap (#381)
"AbstractReadOnlyMap" didn't allow access to the full contents of Maps. This broke the "setreplace" function where the whole Map is loaded first to be able to replace (parts of) the input string.
1 parent 27780be commit 9d10a70

File tree

4 files changed

+48
-24
lines changed

4 files changed

+48
-24
lines changed

metafacture-commons/src/main/java/org/metafacture/commons/tries/SetReplacer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Collections;
2222
import java.util.Comparator;
2323
import java.util.List;
24-
import java.util.Map.Entry;
2524
import java.util.Map;
2625

2726
/**
@@ -54,8 +53,8 @@ public void addReplacement(final String toReplace, final String replacement) {
5453
* @param replacements the Map of Strings to be replaced
5554
*/
5655
public void addReplacements(final Map<String, String> replacements) {
57-
for (final Entry<String, String> entry : replacements.entrySet()) {
58-
addReplacement(entry.getKey(), entry.getValue());
56+
for (final String k : replacements.keySet()) {
57+
addReplacement(k, replacements.get(k));
5958
}
6059
}
6160

metamorph-api/src/main/java/org/metafacture/metamorph/api/helpers/AbstractReadOnlyMap.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
import java.util.Set;
2222

2323
/**
24-
* Base class for maps which are read only and do not allow access to their
25-
* full contents.
24+
* Base class for maps which are read only. Allows access to the key set. It's
25+
* up to the extending class when overriding {@link #keySet()} to return an
26+
* {@link java.util.Collections#unmodifiableSet(Set)}.
2627
*
2728
* @param <K> type of keys
2829
* @param <V> type of values
@@ -71,8 +72,12 @@ public final void clear() {
7172
throw new UnsupportedOperationException();
7273
}
7374

75+
/**
76+
* It's up to the extending class to return an
77+
* {@link java.util.Collections#unmodifiableSet(Set)} when overriding.
78+
*/
7479
@Override
75-
public final Set<K> keySet() {
80+
public Set<K> keySet() {
7681
throw new UnsupportedOperationException();
7782
}
7883

metamorph/src/main/java/org/metafacture/metamorph/maps/FileMap.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
import java.net.MalformedURLException;
3030
import java.net.URL;
3131
import java.nio.charset.StandardCharsets;
32+
import java.util.Collections;
3233
import java.util.HashMap;
3334
import java.util.Map;
3435
import java.util.Optional;
36+
import java.util.Set;
3537
import java.util.regex.Pattern;
3638

3739
/**
@@ -148,4 +150,9 @@ public String get(final Object key) {
148150
return map.get(key);
149151
}
150152

153+
@Override
154+
public Set<String> keySet() {
155+
return Collections.unmodifiableSet(map.keySet());
156+
}
157+
151158
}

metamorph/src/test/java/org/metafacture/metamorph/maps/FileMapTest.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,20 @@ public final class FileMapTest {
3939
@Mock
4040
private StreamReceiver receiver;
4141

42+
private static String MORPH =
43+
"<rules>" +
44+
" <data source='1'>" +
45+
" <%s='map1' />" +
46+
" </data>" +
47+
"</rules>" +
48+
"<maps>" +
49+
" <filemap name='map1' files='org/metafacture/metamorph/maps/" +
50+
"file-map-test.txt' />" +
51+
"</maps>";
52+
4253
@Test
4354
public void shouldLookupValuesInFileBasedMap() {
44-
assertMorph(receiver,
45-
"<rules>" +
46-
" <data source='1'>" +
47-
" <lookup in='map1' />" +
48-
" </data>" +
49-
"</rules>" +
50-
"<maps>" +
51-
" <filemap name='map1' files='org/metafacture/metamorph/maps/file-map-test.txt' />" +
52-
"</maps>",
55+
assertMorph(receiver, String.format(MORPH, "lookup in"),
5356
i -> {
5457
i.startRecord("1");
5558
i.literal("1", "gw");
@@ -67,15 +70,7 @@ public void shouldLookupValuesInFileBasedMap() {
6770

6871
@Test
6972
public void shouldWhitelistValuesInFileBasedMap() {
70-
assertMorph(receiver,
71-
"<rules>" +
72-
" <data source='1'>" +
73-
" <whitelist map='map1' />" +
74-
" </data>" +
75-
"</rules>" +
76-
"<maps>" +
77-
" <filemap name='map1' files='org/metafacture/metamorph/maps/file-map-test.txt' />" +
78-
"</maps>",
73+
assertMorph(receiver, String.format(MORPH, "whitelist map"),
7974
i -> {
8075
i.startRecord("1");
8176
i.literal("1", "gw");
@@ -92,4 +87,22 @@ public void shouldWhitelistValuesInFileBasedMap() {
9287
);
9388
}
9489

90+
@Test
91+
public void shouldReplaceValuesUsingFileBasedMap() {
92+
assertMorph(receiver, String.format(MORPH, "setreplace map"),
93+
i -> {
94+
i.startRecord("1");
95+
i.literal("1", "gw-fj: 1:1");
96+
i.literal("1", "fj-gw: 0:0");
97+
i.endRecord();
98+
},
99+
o -> {
100+
o.get().startRecord("1");
101+
o.get().literal("1", "Germany-Fiji: 1:1");
102+
o.get().literal("1", "Fiji-Germany: 0:0");
103+
o.get().endRecord();
104+
}
105+
);
106+
}
107+
95108
}

0 commit comments

Comments
 (0)