Skip to content

Commit

Permalink
Merge metafacture#419 from '381-setreplaceUsingFilemapKeyset' of http…
Browse files Browse the repository at this point in the history
  • Loading branch information
dr0i committed Nov 8, 2021
2 parents 99e88eb + d753411 commit 73bedb1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public void addReplacement(final String toReplace, final String replacement) {
* @param replacements the Map of Strings to be replaced
*/
public void addReplacements(final Map<String, String> replacements) {
for (final Entry<String, String> entry : replacements.entrySet()) {
addReplacement(entry.getKey(), entry.getValue());
for (final String k : replacements.keySet()) {
addReplacement(k, replacements.get(k));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import java.util.Set;

/**
* Base class for maps which are read only and do not allow access to their
* full contents.
* Base class for maps which are read only. Allows access to the key set. It's
* up to the extending class when overriding {@link #keySet()} to return an
* {@link java.util.Collections#unmodifiableSet(Set)}.
*
* @param <K> type of keys
* @param <V> type of values
Expand All @@ -41,7 +42,7 @@ public final boolean isEmpty() {
}

@Override
public final boolean containsKey(final Object key) {
public boolean containsKey(final Object key) {
return get(key) != null;
}

Expand Down Expand Up @@ -71,8 +72,12 @@ public final void clear() {
throw new UnsupportedOperationException();
}

/**
* It's up to the extending class to return an
* {@link java.util.Collections#unmodifiableSet(Set)} when overriding.
*/
@Override
public final Set<K> keySet() {
public Set<K> keySet() {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -148,4 +150,9 @@ public String get(final Object key) {
return map.get(key);
}

@Override
public Set<String> keySet() {
return Collections.unmodifiableSet(map.keySet());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,20 @@ public final class FileMapTest {
@Mock
private StreamReceiver receiver;

private static String MORPH =
"<rules>" +
" <data source='1'>" +
" <%s='map1' />" +
" </data>" +
"</rules>" +
"<maps>" +
" <filemap name='map1' files='org/metafacture/metamorph/maps/" +
"file-map-test.txt' />" +
"</maps>";

@Test
public void shouldLookupValuesInFileBasedMap() {
assertMorph(receiver,
"<rules>" +
" <data source='1'>" +
" <lookup in='map1' />" +
" </data>" +
"</rules>" +
"<maps>" +
" <filemap name='map1' files='org/metafacture/metamorph/maps/file-map-test.txt' />" +
"</maps>",
assertMorph(receiver, String.format(MORPH, "lookup in"),
i -> {
i.startRecord("1");
i.literal("1", "gw");
Expand All @@ -67,15 +70,7 @@ public void shouldLookupValuesInFileBasedMap() {

@Test
public void shouldWhitelistValuesInFileBasedMap() {
assertMorph(receiver,
"<rules>" +
" <data source='1'>" +
" <whitelist map='map1' />" +
" </data>" +
"</rules>" +
"<maps>" +
" <filemap name='map1' files='org/metafacture/metamorph/maps/file-map-test.txt' />" +
"</maps>",
assertMorph(receiver, String.format(MORPH, "whitelist map"),
i -> {
i.startRecord("1");
i.literal("1", "gw");
Expand All @@ -92,4 +87,22 @@ public void shouldWhitelistValuesInFileBasedMap() {
);
}

@Test
public void shouldReplaceValuesUsingFileBasedMap() {
assertMorph(receiver, String.format(MORPH, "setreplace map"),
i -> {
i.startRecord("1");
i.literal("1", "gw-fj: 1:1");
i.literal("1", "fj-gw: 0:0");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().literal("1", "Germany-Fiji: 1:1");
o.get().literal("1", "Fiji-Germany: 0:0");
o.get().endRecord();
}
);
}

}

0 comments on commit 73bedb1

Please sign in to comment.