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;
}
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);
}
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());
}
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"));
}
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);
}
Aggregations