mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-06 21:27:40 +00:00
GUAC-340: Add filterValues() function for convenience.
This commit is contained in:
@@ -210,4 +210,25 @@ public class TokenFilter {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -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));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user