JsonConverter extends AbstractConfigurationConverter { public JsonValue convertChild(final @NotNull PlexusConfiguration child, final ExpressionEvaluator evaluator) throws ComponentConfigurationException { final Object value = fromExpression(child, evaluator); final String hint = child.getAttribute("hint"); if (hint != null) { switch (hint.toLowerCase()) { case HINT_MAP: return readJsonObjectFromConfig(child, evaluator); case HINT_LIST: return readJsonArrayFromConfig(child, evaluator); case HINT_STRING: return JavaxJson.wrap(String.valueOf(value)); case HINT_NUMBER: return JavaxJson.wrap(readNumberFromValue(child.getName(), value)); case HINT_BOOLEAN: return JavaxJson.wrap(readBooleanFromValue(child.getName(), value)); case HINT_VALUE_LITERAL: return readJsonValueFromValue(child.getName(), value); case HINT_OBJECT_LITERAL: return readJsonObjectFromValue(child.getName(), value); case HINT_ARRAY_LITERAL: return readJsonArrayFromValue(child.getName(), value); default: throw new ComponentConfigurationException("Unsupported JSON config hint type for key: " + child.getName() + ", hint=" + hint); } } else if (child.getChildCount() > 0) { if (mightBeList(child)) { return readJsonArrayFromConfig(child, evaluator); } else { return readJsonObjectFromConfig(child, evaluator); } } if (value instanceof String) { return JavaxJson.val(stringToValue((String) value)).get(); } return JavaxJson.val(value).get(); }  static String plexusPluralStem(final String key); @Override boolean canConvert(final Class<?> type); @Override Object fromConfiguration(final ConverterLookup lookup,
                                    final PlexusConfiguration configuration,
                                    final Class<?> type,
                                    final Class<?> enclosingType,
                                    final ClassLoader loader,
                                    final ExpressionEvaluator evaluator,
                                    final ConfigurationListener listener); JsonValue convertChild(final @NotNull PlexusConfiguration child, final ExpressionEvaluator evaluator); static Object stringToValue(String string);  }
@Test public void testConvertChild_stringToString() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); when(eval.evaluate(anyString())).thenAnswer(call -> call.getArgument(0, String.class)); assertEquals("expect json string", JavaxJson.wrap("bar"), converter().convertChild(xml("fooBar.xml"), eval)); }
@Test public void testConvertChild_expectNull() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); when(eval.evaluate(anyString())).thenAnswer(call -> null); assertSame("expect json null for null", JsonValue.NULL, converter().convertChild(xml("fooBar.xml"), eval)); }
@Test(expected = ComponentConfigurationException.class) public void testConvertChild_invalidHint() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); converter().convertChild(xml("invalidHint.xml"), eval); }
@Test public void testConvertChild_wrapNonStringValue() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); when(eval.evaluate(anyString())).thenAnswer(call -> call.getArgument(0, String.class).indexOf("t") == 0); assertEquals("expect true for empty string", JavaxJson.wrap(true), converter().convertChild(xml("topFoo.xml"), eval)); assertEquals("expect false for non-empty string", JavaxJson.wrap(false), converter().convertChild(xml("fooFoo.xml"), eval)); }
@Test public void testConvertChild_mightBeList() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); when(eval.evaluate(anyString())).thenAnswer(call -> call.getArgument(0, String.class)); assertEquals("expect json array for value", arr("one").get(), converter().convertChild(xml("foosFoo.xml"), eval)); assertEquals("expect json object for value", key("bar", "one").get(), converter().convertChild(xml("foosBar.xml"), eval)); }
@Test public void testConvertChild_hintMap() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); when(eval.evaluate(anyString())).thenAnswer(call -> call.getArgument(0, String.class)); assertEquals("expect json object for value", key("foo", "one").get(), converter().convertChild(xml("foosFoo.xml", "map"), eval)); assertEquals("expect json object for value", key("bar", "one").get(), converter().convertChild(xml("foosBar.xml", "map"), eval)); }
@Test public void testConvertChild_hintList() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); when(eval.evaluate(anyString())).thenAnswer(call -> call.getArgument(0, String.class)); assertEquals("expect json array for value", arr("one").get(), converter().convertChild(xml("foosFoo.xml", "list"), eval)); assertEquals("expect json array for value", arr("one").get(), converter().convertChild(xml("foosBar.xml", "list"), eval)); }
@Test public void testConvertChild_hintString() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); when(eval.evaluate(anyString())).thenAnswer(call -> call.getArgument(0, String.class)); assertSame("expect json true for value", JsonValue.TRUE, converter().convertChild(xml("fooTrue.xml"), eval)); assertEquals("expect json string 'true' for value", JavaxJson.wrap("true"), converter().convertChild(xml("fooTrue.xml", "string"), eval)); assertEquals("expect json number for value", JavaxJson.wrap(-500), converter().convertChild(xml("fooNumber.xml"), eval)); assertEquals("expect json string '-500' for value", JavaxJson.wrap("-500"), converter().convertChild(xml("fooNumber.xml", "string"), eval)); }
@Test public void testConvertChild_hintNumber() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); when(eval.evaluate(anyString())).thenAnswer(call -> call.getArgument(0, String.class)); assertSame("expect json true for value", JsonValue.TRUE, converter().convertChild(xml("fooTrue.xml"), eval)); assertEquals("expect json number for value", JavaxJson.wrap(-500), converter().convertChild(xml("fooNumber.xml"), eval)); assertEquals("expect json string '-500' for value", JavaxJson.wrap(-500), converter().convertChild(xml("fooNumber.xml", "number"), eval)); }
@Test(expected = ComponentConfigurationException.class) public void testConvertChild_hintNumber_NaN() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); when(eval.evaluate(anyString())).thenAnswer(call -> call.getArgument(0, String.class)); converter().convertChild(xml("fooTrue.xml", "number"), eval); }
@Test public void testConvertChild_hintBoolean() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); when(eval.evaluate(anyString())).thenAnswer(call -> call.getArgument(0, String.class)); assertSame("true is true", JsonValue.TRUE, converter().convertChild(xml("fooTrue.xml"), eval)); assertSame("true is true", JsonValue.TRUE, converter().convertChild(xml("fooTrue.xml", "boolean"), eval)); assertSame("non-boolean, empty string is false", JsonValue.FALSE, converter().convertChild(xml("emptyFoo.xml", "boolean"), eval)); assertSame("non-boolean, number is true", JsonValue.FALSE, converter().convertChild(xml("emptyFoo.xml", "boolean"), eval)); }
@Test(expected = ComponentConfigurationException.class) public void testConvertChild_hintBoolean_throws() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); when(eval.evaluate(anyString())).thenAnswer(call -> call.getArgument(0, String.class)); converter().convertChild(xml("fooBar.xml", "boolean"), eval); }
@Test public void testConvertChild_jsonValueLiteral() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); when(eval.evaluate(anyString())).thenAnswer(call -> call.getArgument(0, String.class)); assertSame("json true", JsonValue.TRUE, converter().convertChild(xml("fooTrue.xml", "json"), eval)); assertSame("json false", JsonValue.FALSE, converter().convertChild(xml("fooFalse.xml", "json"), eval)); assertSame("json null", JsonValue.NULL, converter().convertChild(xml("fooNull.xml", "json"), eval)); assertSame("json empty string", JsonValue.NULL, converter().convertChild(xml("emptyFoo.xml", "json"), eval)); assertSame("json blank string", JsonValue.NULL, converter().convertChild(xml("blankFoo.xml", "json"), eval)); assertEquals("json object", key("key", "value").get(), converter().convertChild(xml("fooObject.xml", "json"), eval)); assertEquals("json array", arr("one", "two").get(), converter().convertChild(xml("fooArray.xml", "json"), eval)); assertEquals("json integer", wrap(-500), converter().convertChild(xml("fooNumber.xml", "json"), eval)); }
@Test(expected = ComponentConfigurationException.class) public void testConvertChild_jsonValueLiteral_stringToken() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); when(eval.evaluate(anyString())).thenAnswer(call -> call.getArgument(0, String.class)); converter().convertChild(xml("fooBar.xml", "json"), eval); }
@Test public void testConvertChild_objectLiteral() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); when(eval.evaluate(anyString())).thenAnswer(call -> call.getArgument(0, String.class)); assertEquals("json object as object", key("key", "value").get(), converter().convertChild(xml("fooObject.xml", "jsonobject"), eval)); assertEquals("json object as string", wrap("{\"key\": \"value\"}"), converter().convertChild(xml("fooObject.xml"), eval)); }
@Test public void testConvertChild_arrayLiteral() throws Exception { ExpressionEvaluator eval = mock(ExpressionEvaluator.class); when(eval.evaluate(anyString())).thenAnswer(call -> call.getArgument(0, String.class)); assertEquals("json array as array", arr("one", "two").get(), converter().convertChild(xml("fooArray.xml", "jsonarray"), eval)); assertEquals("json array as string", wrap("[\"one\", \"two\"]"), converter().convertChild(xml("fooArray.xml"), eval)); }