Search in sources :

Example 1 with DefaultAutoConverter

use of com.dtflys.forest.converter.auto.DefaultAutoConverter in project forest by dromara.

the class TestAutoConverter method getConverter.

private DefaultAutoConverter getConverter() {
    ForestConfiguration configuration = ForestConfiguration.createConfiguration();
    DefaultAutoConverter autoConverter = (DefaultAutoConverter) configuration.getConverterMap().get(ForestDataType.AUTO);
    assertNotNull(autoConverter);
    return autoConverter;
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) DefaultAutoConverter(com.dtflys.forest.converter.auto.DefaultAutoConverter)

Example 2 with DefaultAutoConverter

use of com.dtflys.forest.converter.auto.DefaultAutoConverter in project forest by dromara.

the class TestAutoConverter method testAutoText.

@Test
public void testAutoText() {
    DefaultAutoConverter autoConverter = getConverter();
    String expect = "xxxxx";
    String text = autoConverter.convertToJavaObject(expect, String.class);
    assertEquals(expect, text);
    expect = "{{{{jljfelUF*(";
    text = autoConverter.convertToJavaObject(expect, String.class);
    assertEquals(expect, text);
    expect = "<div>xxx</div>";
    text = autoConverter.convertToJavaObject(expect, String.class);
    assertEquals(expect, text);
    expect = "[{\"username\": \"foo\", \"password\": \"bar\"}, {\"username\": \"xxx\", \"password\": \"yyy\"}]";
    text = autoConverter.convertToJavaObject(expect, String.class);
    assertEquals(expect, text);
    expect = "{\"username\": \"foo\", \"password\": \"bar\"}";
    text = autoConverter.convertToJavaObject(expect, String.class);
    assertEquals(expect, text);
}
Also used : DefaultAutoConverter(com.dtflys.forest.converter.auto.DefaultAutoConverter) Test(org.junit.Test)

Example 3 with DefaultAutoConverter

use of com.dtflys.forest.converter.auto.DefaultAutoConverter in project forest by dromara.

the class TestAutoConverter method testAutoXml.

@Test
public void testAutoXml() {
    DefaultAutoConverter autoConverter = getConverter();
    String xmlText = "  <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<user>\n" + "<name>Peter</name>\n" + "<age>32</age>\n" + "</user>";
    TestJaxbConverter.User user = autoConverter.convertToJavaObject(xmlText, TestJaxbConverter.User.class);
    assertNotNull(user);
    assertEquals("Peter", user.getName());
    assertEquals(Integer.valueOf(32), user.getAge());
    user = autoConverter.convertToJavaObject(xmlText, new TypeReference<TestJaxbConverter.User>() {
    }.getType());
    assertNotNull(user);
    assertEquals("Peter", user.getName());
    assertEquals(Integer.valueOf(32), user.getAge());
}
Also used : DefaultAutoConverter(com.dtflys.forest.converter.auto.DefaultAutoConverter) Test(org.junit.Test)

Example 4 with DefaultAutoConverter

use of com.dtflys.forest.converter.auto.DefaultAutoConverter in project forest by dromara.

the class TestAutoConverter method testAutoJsonArray.

@Test
public void testAutoJsonArray() {
    DefaultAutoConverter autoConverter = getConverter();
    String text = "    [{\"username\": \"foo\", \"password\": \"bar\"}, {\"username\": \"xxx\", \"password\": \"yyy\"}] ";
    List<Map<String, Object>> list = autoConverter.convertToJavaObject(text, new TypeReference<List<Map<String, Object>>>() {
    }.getType());
    assertNotNull(list);
    assertEquals(2, list.size());
    assertEquals("foo", list.get(0).get("username"));
    assertEquals("bar", list.get(0).get("password"));
    assertEquals("xxx", list.get(1).get("username"));
    assertEquals("yyy", list.get(1).get("password"));
}
Also used : DefaultAutoConverter(com.dtflys.forest.converter.auto.DefaultAutoConverter) TypeReference(com.alibaba.fastjson.TypeReference) Map(java.util.Map) Test(org.junit.Test)

Example 5 with DefaultAutoConverter

use of com.dtflys.forest.converter.auto.DefaultAutoConverter in project forest by dromara.

the class TestExceptions method testConvertException.

@Test
public void testConvertException() {
    Throwable th = new Exception("xxx");
    ForestConverter<?> converter = new ForestFastjsonConverter();
    ForestConvertException exception = new ForestConvertException(converter, th);
    assertThat(exception.getMessage()).isEqualTo("[Forest] json converter: 'ForestFastjsonConverter' error: xxx");
    assertThat(exception.getConverterClass()).isEqualTo(ForestFastjsonConverter.class);
    converter = new ForestJacksonConverter();
    exception = new ForestConvertException(converter, th);
    assertThat(exception.getMessage()).isEqualTo("[Forest] json converter: 'ForestJacksonConverter' error: xxx");
    assertThat(exception.getConverterClass()).isEqualTo(ForestJacksonConverter.class);
    converter = new DefaultAutoConverter(ForestConfiguration.configuration());
    exception = new ForestConvertException(converter, th);
    assertThat(exception.getMessage()).isEqualTo("[Forest] auto converter: 'DefaultAutoConverter' error: xxx");
    assertThat(exception.getConverterClass()).isEqualTo(DefaultAutoConverter.class);
}
Also used : ForestFastjsonConverter(com.dtflys.forest.converter.json.ForestFastjsonConverter) DefaultAutoConverter(com.dtflys.forest.converter.auto.DefaultAutoConverter) ForestJacksonConverter(com.dtflys.forest.converter.json.ForestJacksonConverter) ForestConvertException(com.dtflys.forest.exceptions.ForestConvertException) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestUnsupportException(com.dtflys.forest.exceptions.ForestUnsupportException) ForestInterceptorDefineException(com.dtflys.forest.exceptions.ForestInterceptorDefineException) ForestHandlerException(com.dtflys.forest.exceptions.ForestHandlerException) ForestVariableUndefinedException(com.dtflys.forest.exceptions.ForestVariableUndefinedException) ForestRetryException(com.dtflys.forest.exceptions.ForestRetryException) ForestNetworkException(com.dtflys.forest.exceptions.ForestNetworkException) ForestNoFileNameException(com.dtflys.forest.exceptions.ForestNoFileNameException) ForestFileNotFoundException(com.dtflys.forest.exceptions.ForestFileNotFoundException) ForestConvertException(com.dtflys.forest.exceptions.ForestConvertException) Test(org.junit.Test)

Aggregations

DefaultAutoConverter (com.dtflys.forest.converter.auto.DefaultAutoConverter)8 Test (org.junit.Test)6 TypeReference (com.alibaba.fastjson.TypeReference)1 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)1 DefaultBinaryConverter (com.dtflys.forest.converter.binary.DefaultBinaryConverter)1 DefaultFormConvertor (com.dtflys.forest.converter.form.DefaultFormConvertor)1 ForestFastjsonConverter (com.dtflys.forest.converter.json.ForestFastjsonConverter)1 ForestJacksonConverter (com.dtflys.forest.converter.json.ForestJacksonConverter)1 JSONConverterSelector (com.dtflys.forest.converter.json.JSONConverterSelector)1 ForestProtobufConverterManager (com.dtflys.forest.converter.protobuf.ForestProtobufConverterManager)1 DefaultTextConverter (com.dtflys.forest.converter.text.DefaultTextConverter)1 ForestJaxbConverter (com.dtflys.forest.converter.xml.ForestJaxbConverter)1 ForestConvertException (com.dtflys.forest.exceptions.ForestConvertException)1 ForestFileNotFoundException (com.dtflys.forest.exceptions.ForestFileNotFoundException)1 ForestHandlerException (com.dtflys.forest.exceptions.ForestHandlerException)1 ForestInterceptorDefineException (com.dtflys.forest.exceptions.ForestInterceptorDefineException)1 ForestNetworkException (com.dtflys.forest.exceptions.ForestNetworkException)1 ForestNoFileNameException (com.dtflys.forest.exceptions.ForestNoFileNameException)1 ForestRetryException (com.dtflys.forest.exceptions.ForestRetryException)1 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)1