Ingredient { protected void setCompoundOptional(String name, boolean repeatable, Object... keyValuePairs) { if (keyValuePairs.length % 2 != 0) { throw new IllegalArgumentException( "must have an even number of key-value pairs for compound optional"); } for (int i = 0; i < keyValuePairs.length; i += 2) { if (!(keyValuePairs[i] instanceof String)) { throw new IllegalArgumentException("key in compound optional is not a string"); } } if (!repeatable) { Map map = (Map) getProperties().getOrDefault(name, new HashMap()); for (int i = 0; i < keyValuePairs.length; i += 2) { map.put(keyValuePairs[i], keyValuePairs[i + 1]); } setProperty(name, map); } else { List<Map> list = (List) getProperties().getOrDefault(name, new ArrayList<>()); Map map = new HashMap(); for (int i = 0; i < keyValuePairs.length; i += 2) { map.put(keyValuePairs[i], keyValuePairs[i + 1]); } list.add(map); setProperty(name, list); } } Ingredient(String ingredientType, String domain);  Ingredient(String ingredientType); protected  Ingredient(Ingredient other); String getDomain(); String getIngredientType();  }
@Test(expected = IllegalArgumentException.class) public void testSetCompoundOptional_throwsOnNonEvenNumberOfKeyValuePairs() { Ingredient ingredient = new Ingredient("TestIngredient", "A") {}; ingredient.setCompoundOptional("optional", false, "a", 1, "b", true, "c"); }
@Test(expected = IllegalArgumentException.class) public void testSetCompoundOptional_throwsOnNonKeyNotAString() { Ingredient ingredient = new Ingredient("TestIngredient", "A") {}; ingredient.setCompoundOptional("optional", false, "a", 1, 2, "foo"); }