Search in sources :

Example 1 with ForestJaxbConverter

use of com.dtflys.forest.converter.xml.ForestJaxbConverter in project forest by dromara.

the class TestJaxbConverter method convertToJavaObject.

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

Example 2 with ForestJaxbConverter

use of com.dtflys.forest.converter.xml.ForestJaxbConverter in project forest by dromara.

the class TestJaxbConverter method testConvertToXml.

@Test
public void testConvertToXml() {
    User user = new User();
    user.setName("Peter");
    user.setAge(32);
    ForestJaxbConverter forestJaxbConverter = new ForestJaxbConverter();
    String xml = forestJaxbConverter.encodeToString(user);
    assertNotNull(xml);
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + "<user>\n" + "    <name>Peter</name>\n" + "    <age>32</age>\n" + "</user>\n", xml);
}
Also used : ForestJaxbConverter(com.dtflys.forest.converter.xml.ForestJaxbConverter) Test(org.junit.Test)

Example 3 with ForestJaxbConverter

use of com.dtflys.forest.converter.xml.ForestJaxbConverter in project forest by dromara.

the class ForestConfiguration method createConfiguration.

public static ForestConfiguration createConfiguration() {
    ForestConfiguration configuration = new ForestConfiguration();
    configuration.setId("forestConfiguration" + configuration.hashCode());
    configuration.setJsonConverterSelector(new JSONConverterSelector());
    ForestProtobufConverterManager protobufConverterFactory = ForestProtobufConverterManager.getInstance();
    configuration.setProtobufConverter(protobufConverterFactory.getForestProtobufConverter());
    configuration.setXmlConverter(new ForestJaxbConverter());
    configuration.setTextConverter(new DefaultTextConverter());
    DefaultAutoConverter autoConverter = new DefaultAutoConverter(configuration);
    configuration.getConverterMap().put(ForestDataType.AUTO, autoConverter);
    configuration.getConverterMap().put(ForestDataType.BINARY, new DefaultBinaryConverter(autoConverter));
    configuration.getConverterMap().put(ForestDataType.FORM, new DefaultFormConvertor(configuration));
    setupJSONConverter(configuration);
    configuration.setTimeout(3000);
    configuration.setMaxConnections(500);
    configuration.setMaxRouteConnections(500);
    configuration.setRetryer(BackOffRetryer.class);
    configuration.setMaxRetryCount(0);
    configuration.setMaxRetryInterval(0);
    configuration.registerFilter("json", JSONFilter.class);
    configuration.registerFilter("xml", XmlFilter.class);
    configuration.setLogHandler(new DefaultLogHandler());
    return configuration;
}
Also used : ForestJaxbConverter(com.dtflys.forest.converter.xml.ForestJaxbConverter) DefaultTextConverter(com.dtflys.forest.converter.text.DefaultTextConverter) DefaultAutoConverter(com.dtflys.forest.converter.auto.DefaultAutoConverter) JSONConverterSelector(com.dtflys.forest.converter.json.JSONConverterSelector) DefaultBinaryConverter(com.dtflys.forest.converter.binary.DefaultBinaryConverter) DefaultLogHandler(com.dtflys.forest.logging.DefaultLogHandler) ForestProtobufConverterManager(com.dtflys.forest.converter.protobuf.ForestProtobufConverterManager) DefaultFormConvertor(com.dtflys.forest.converter.form.DefaultFormConvertor)

Example 4 with ForestJaxbConverter

use of com.dtflys.forest.converter.xml.ForestJaxbConverter in project forest by dromara.

the class TestJaxbConverter method testConvertToXmlError.

@Test
public void testConvertToXmlError() {
    BadUser user = new BadUser();
    user.setName("Peter");
    user.setAge(32);
    ForestJaxbConverter forestJaxbConverter = new ForestJaxbConverter();
    boolean error = false;
    try {
        forestJaxbConverter.encodeToString(user);
    } catch (ForestRuntimeException e) {
        error = true;
    }
    assertTrue(error);
}
Also used : ForestJaxbConverter(com.dtflys.forest.converter.xml.ForestJaxbConverter) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) Test(org.junit.Test)

Example 5 with ForestJaxbConverter

use of com.dtflys.forest.converter.xml.ForestJaxbConverter in project forest by dromara.

the class TestJaxbConverter method convertToJavaObjectError.

@Test
public void convertToJavaObjectError() {
    ForestJaxbConverter forestJaxbConverter = new ForestJaxbConverter();
    String xmlText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<user>\n" + "<name>Peter</name>\n" + "<age>32</age>\n" + "</user";
    boolean error = false;
    try {
        forestJaxbConverter.convertToJavaObject(xmlText, User.class);
    } catch (ForestRuntimeException e) {
        error = true;
        assertNotNull(e.getCause());
    }
    assertTrue(error);
}
Also used : ForestJaxbConverter(com.dtflys.forest.converter.xml.ForestJaxbConverter) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) Test(org.junit.Test)

Aggregations

ForestJaxbConverter (com.dtflys.forest.converter.xml.ForestJaxbConverter)5 Test (org.junit.Test)4 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)2 DefaultAutoConverter (com.dtflys.forest.converter.auto.DefaultAutoConverter)1 DefaultBinaryConverter (com.dtflys.forest.converter.binary.DefaultBinaryConverter)1 DefaultFormConvertor (com.dtflys.forest.converter.form.DefaultFormConvertor)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 DefaultLogHandler (com.dtflys.forest.logging.DefaultLogHandler)1