GUAC-340: Add filterValues() function for convenience.

This commit is contained in:
Michael Jumper
2015-01-06 03:59:07 -08:00
parent 3b3b6b6955
commit 49ba38b20a
2 changed files with 65 additions and 1 deletions

View File

@@ -209,5 +209,26 @@ public class TokenFilter {
return output.toString(); return output.toString();
} }
/**
* Given an arbitrary map containing String values, replace each non-null
* value with the corresponding filtered value.
*
* @param map
* The map whose values should be filtered.
*/
public void filterValues(Map<?, String> map) {
// For each map entry
for (Map.Entry<?, String> entry : map.entrySet()) {
// If value is non-null, filter value through this TokenFilter
String value = entry.getValue();
if (value != null)
entry.setValue(filter(value));
}
}
} }

View File

@@ -22,6 +22,8 @@
package org.glyptodon.guacamole.token; package org.glyptodon.guacamole.token;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@@ -57,4 +59,45 @@ public class TokenFilterTest {
} }
/**
* Verifies that token replacement via filterValues() functions as
* specified.
*/
@Test
public void testFilterValues() {
// Create token filter
TokenFilter tokenFilter = new TokenFilter();
tokenFilter.setToken("TOKEN_A", "value-of-a");
tokenFilter.setToken("TOKEN_B", "value-of-b");
// Create test map
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "$$${NOPE}hello${TOKEN_A}world${TOKEN_B}$${NOT_A_TOKEN}");
map.put(2, "${NOPE}hello${TOKEN_A}world${TOKEN_C}");
map.put(3, null);
// Filter map values
tokenFilter.filterValues(map);
// Filter should not affect size of map
assertEquals(3, map.size());
// Filtered value 1
assertEquals(
"$${NOPE}hellovalue-of-aworldvalue-of-b${NOT_A_TOKEN}",
map.get(1)
);
// Filtered value 2
assertEquals(
"${NOPE}hellovalue-of-aworld${TOKEN_C}",
map.get(2)
);
// Null values are not filtered
assertNull(map.get(3));
}
} }