StringArgumentsHandler { public Map<String, String> createStringsMap() throws BaleenException { if (argumentPairs.length % 2 != 0) { LOGGER.error("String arguments must be key value pairs"); throw new BaleenException("Invalid number of arguments. Arguments must be key value pairs"); } Map<String, String> map = new HashMap<>(); for (int i = 0; i < argumentPairs.length - 1; i += 2) { map.put(argumentPairs[i], argumentPairs[i + 1]); } return map; } StringArgumentsHandler(String[] argumentPairs); Map<String, String> createStringsMap();  }
@Test public void testArgumentsArePutInMap() throws Exception { Map<String, String> keyValuePairs = new StringArgumentsHandler(new String[] {"key1", "value1", "key2", "value2"}) .createStringsMap(); assertEquals("key1 should map to value1", "value1", keyValuePairs.get("key1")); assertEquals("key2 should map to value2", "value2", keyValuePairs.get("key2")); }
@Test(expected = Exception.class) public void testExceptionIsThrownIfInvalidNumberOfArgumentsAreProvided() throws Exception { new StringArgumentsHandler(new String[] {"invalid", "number of", "arguments"}) .createStringsMap(); }