### Question:
EnumGuacamoleProperty implements GuacamoleProperty<T> { @Override public T parseValue(String value) throws GuacamoleException { if (value == null) return null; T parsedValue = valueMapping.get(value); if (parsedValue != null) return parsedValue; List<String> legalValues = new ArrayList<>(valueMapping.keySet()); Collections.sort(legalValues); throw new GuacamoleServerException(String.format("\"%s\" is not a " + "valid value for property \"%s\". Valid values are: \"%s\"", value, getName(), String.join("\", \"", legalValues))); } EnumGuacamoleProperty(Map<String, T> valueMapping);  EnumGuacamoleProperty(Class<T> enumClass);  EnumGuacamoleProperty(String key, T value, Object... additional); @Override T parseValue(String value);  }### Answer:
@Test public void testParseValue() throws GuacamoleException { assertEquals(Fish.SALMON, FAVORITE_FISH.parseValue("salmon")); assertEquals(Fish.TROUT, FAVORITE_FISH.parseValue("trout")); assertEquals(Fish.MACKEREL, FAVORITE_FISH.parseValue("mackerel")); assertEquals(Fish.TUNA, FAVORITE_FISH.parseValue("tuna")); assertEquals(Fish.SARDINE, FAVORITE_FISH.parseValue("sardine")); }
@Test public void testParseNullValue() throws GuacamoleException { assertNull(FAVORITE_FISH.parseValue(null)); }
@Test public void testParseInvalidValue() { try { FAVORITE_FISH.parseValue("anchovy"); fail("Invalid EnumGuacamoleProperty values should fail to parse with an exception."); } catch (GuacamoleException e) { String message = e.getMessage(); assertTrue(message.contains("\"mackerel\", \"salmon\", \"sardine\", \"trout\", \"tuna\"")); } }
@Test public void testUnannotatedEnum() throws GuacamoleException { EnumGuacamoleProperty<Vegetable> favoriteVegetable = new EnumGuacamoleProperty<Vegetable>( "potato", Vegetable.POTATO, "carrot", Vegetable.CARROT ) { @Override public String getName() { return "favorite-vegetable"; } }; assertEquals(Vegetable.POTATO, favoriteVegetable.parseValue("potato")); assertEquals(Vegetable.CARROT, favoriteVegetable.parseValue("carrot")); }
@Test public void testAnnotationPrecedence() throws GuacamoleException { EnumGuacamoleProperty<Fish> favoriteFish = new EnumGuacamoleProperty<Fish>( "chinook", Fish.SALMON, "rainbow", Fish.TROUT ) { @Override public String getName() { return "favorite-fish"; } }; assertEquals(Fish.SALMON, favoriteFish.parseValue("chinook")); assertEquals(Fish.TROUT, favoriteFish.parseValue("rainbow")); try { favoriteFish.parseValue("salmon"); fail("Explicit key/value mapping should take priority over annotations."); } catch (GuacamoleException e) { } try { favoriteFish.parseValue("trout"); fail("Explicit key/value mapping should take priority over annotations."); } catch (GuacamoleException e) { } try { favoriteFish.parseValue("tuna"); fail("Annotations should not have any effect if explicit key/value mapping is used."); } catch (GuacamoleException e) { } }