BAEL-707 Introduction to JiBX (#1340)
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.xml.jibx;
|
||||
|
||||
import org.jibx.runtime.BindingDirectory;
|
||||
import org.jibx.runtime.IBindingFactory;
|
||||
import org.jibx.runtime.IUnmarshallingContext;
|
||||
import org.jibx.runtime.JiBXException;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class CustomerTest {
|
||||
|
||||
@Test
|
||||
public void whenUnmarshalXML_ThenFieldsAreMapped() throws JiBXException, FileNotFoundException {
|
||||
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
|
||||
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
|
||||
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
|
||||
|
||||
assertEquals("Stefan", customer.getPerson().getFirstName());
|
||||
assertEquals("Jaeger", customer.getPerson().getLastName());
|
||||
assertEquals("Davos Dorf", customer.getCity());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WhenUnmarshal_ThenMappingInherited() throws JiBXException, FileNotFoundException {
|
||||
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
|
||||
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
|
||||
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
|
||||
|
||||
assertEquals(12345, customer.getPerson().getCustomerId());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WhenUnmarshal_ThenPhoneMappingRead() throws JiBXException, FileNotFoundException {
|
||||
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
|
||||
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
|
||||
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
|
||||
|
||||
assertEquals("1", customer.getHomePhone().getCountryCode());
|
||||
assertEquals("234", customer.getHomePhone().getNetworkPrefix());
|
||||
assertEquals("234678", customer.getHomePhone().getNumber());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<customer>
|
||||
<person>
|
||||
<customer-id>12345</customer-id>
|
||||
<first-name>Stefan</first-name>
|
||||
<last-name>Jaeger</last-name>
|
||||
</person>
|
||||
<home-phone>
|
||||
<country-code>1</country-code>
|
||||
<network-prefix>234</network-prefix>
|
||||
<number>234678</number>
|
||||
</home-phone>
|
||||
|
||||
<office-phone>
|
||||
<country-code>1</country-code>
|
||||
<network-prefix>234</network-prefix>
|
||||
<number>234678</number>
|
||||
</office-phone>
|
||||
<city>Davos Dorf</city>
|
||||
|
||||
</customer>
|
||||
Reference in New Issue
Block a user