Skip to content

Commit de29f6a

Browse files
baljeet20maibin
authored andcommitted
BALE-707 Refactoring changes (eugenp#1418)
* BAEL-707 Add the changes as per review comment * BAEL-707 Refactored the code as per review comments
1 parent d1233d0 commit de29f6a

6 files changed

Lines changed: 9 additions & 71 deletions

File tree

‎xml/src/main/java/com/baeldung/xml/jibx/Person.java‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,14 @@
99
import org.apache.commons.lang.builder.ToStringBuilder;
1010

1111
public class Person extends Identity {
12-
private String firstName;
13-
private String lastName;
12+
private String name;
1413

15-
public String getFirstName() {
16-
return firstName;
14+
public String getName() {
15+
return name;
1716
}
1817

19-
public void setFirstName(String firstName) {
20-
this.firstName = firstName;
21-
}
22-
23-
public String getLastName() {
24-
return lastName;
25-
}
26-
27-
public void setLastName(String lastName) {
28-
this.lastName = lastName;
18+
public void setName(String name) {
19+
this.name = name;
2920
}
3021

3122
public String toString() {

‎xml/src/main/java/com/baeldung/xml/jibx/Phone.java‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,8 @@
22

33
public class Phone {
44

5-
private String countryCode;
6-
private String networkPrefix;
75
private String number;
86

9-
public String getCountryCode() {
10-
return countryCode;
11-
}
12-
13-
public void setCountryCode(String countryCode) {
14-
this.countryCode = countryCode;
15-
}
16-
17-
public String getNetworkPrefix() {
18-
return networkPrefix;
19-
}
20-
21-
public void setNetworkPrefix(String networkPrefix) {
22-
this.networkPrefix = networkPrefix;
23-
}
24-
257
public String getNumber() {
268
return number;
279
}

‎xml/src/main/resources/Order.xsd‎

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,6 @@
1313
<xs:attribute type="xs:string" name="Type" use="optional" />
1414
</xs:complexType>
1515
</xs:element>
16-
<xs:element type="xs:string" name="DeliveryNotes" />
17-
<xs:element name="Items">
18-
<xs:complexType>
19-
<xs:sequence>
20-
<xs:element name="Item" maxOccurs="unbounded"
21-
minOccurs="0">
22-
<xs:complexType>
23-
<xs:sequence>
24-
<xs:element type="xs:string" name="ProductName" />
25-
<xs:element type="xs:byte" name="Quantity" />
26-
<xs:element type="xs:float" name="USPrice" />
27-
<xs:element type="xs:string" name="Comment"
28-
minOccurs="0" />
29-
<xs:element type="xs:date" name="ShipDate"
30-
minOccurs="0" />
31-
</xs:sequence>
32-
<xs:attribute type="xs:string" name="PartNumber"
33-
use="optional" />
34-
</xs:complexType>
35-
</xs:element>
36-
</xs:sequence>
37-
</xs:complexType>
38-
</xs:element>
3916
</xs:sequence>
4017
<xs:attribute type="xs:int" name="OrderNumber" />
4118
<xs:attribute type="xs:date" name="OrderDate" />

‎xml/src/main/resources/customer-binding.xml‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414
<mapping name="person" class="com.baeldung.xml.jibx.Person"
1515
extends="com.baeldung.xml.jibx.Identity">
1616
<structure map-as="com.baeldung.xml.jibx.Identity" />
17-
<value name="first-name" field="firstName" />
18-
<value name="last-name" field="lastName" />
17+
<value name="name" field="name" />
1918

2019
</mapping>
2120

2221
<mapping class="com.baeldung.xml.jibx.Phone" abstract="true">
23-
<value name="country-code" field="countryCode" />
24-
<value name="network-prefix" field="networkPrefix" />
2522
<value name="number" field="number" />
2623
</mapping>
2724

‎xml/src/test/java/com/baeldung/xml/jibx/CustomerTest.java‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.jibx.runtime.JiBXException;
77
import org.junit.Test;
88

9-
import java.io.FileInputStream;
109
import java.io.FileNotFoundException;
1110
import java.io.InputStream;
1211

@@ -22,8 +21,7 @@ public void whenUnmarshalXML_ThenFieldsAreMapped() throws JiBXException, FileNot
2221
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
2322
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
2423

25-
assertEquals("Stefan", customer.getPerson().getFirstName());
26-
assertEquals("Jaeger", customer.getPerson().getLastName());
24+
assertEquals("Stefan Jaegar", customer.getPerson().getName());
2725
assertEquals("Davos Dorf", customer.getCity());
2826

2927
}
@@ -47,9 +45,7 @@ public void WhenUnmarshal_ThenPhoneMappingRead() throws JiBXException, FileNotFo
4745
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
4846
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
4947
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
50-
51-
assertEquals("1", customer.getHomePhone().getCountryCode());
52-
assertEquals("234", customer.getHomePhone().getNetworkPrefix());
48+
5349
assertEquals("234678", customer.getHomePhone().getNumber());
5450

5551
}

‎xml/src/test/resources/Customer1.xml‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@
22
<customer>
33
<person>
44
<customer-id>12345</customer-id>
5-
<first-name>Stefan</first-name>
6-
<last-name>Jaeger</last-name>
5+
<name>Stefan Jaeger</name>
76
</person>
87
<home-phone>
9-
<country-code>1</country-code>
10-
<network-prefix>234</network-prefix>
118
<number>234678</number>
129
</home-phone>
1310

1411
<office-phone>
15-
<country-code>1</country-code>
16-
<network-prefix>234</network-prefix>
1712
<number>234678</number>
1813
</office-phone>
1914
<city>Davos Dorf</city>

0 commit comments

Comments
 (0)