BAEL-1979 Added examples for SnakeYAML Library (#4802)
* BAEL-1979 Added examples for SnakeYAML Library * BAEL-1979 Moved the snakeyaml related code to libraries module * BAEL-1979 Removed the System.out.println() statements and converted the assertTrue to assertEquals wherever possible. * BAEL-1979 Removed println statements, small formatting fix in pom.xml
This commit is contained in:
parent
ca1908a351
commit
1bae07cc5c
|
@ -6,3 +6,4 @@
|
|||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
/bin/
|
||||
|
|
|
@ -771,12 +771,20 @@
|
|||
<version>${hamcrest-all.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
<version>1.2.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
|
@ -914,6 +922,7 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<snakeyaml.version>1.21</snakeyaml.version>
|
||||
<googleclient.version>1.23.0</googleclient.version>
|
||||
<crdt.version>0.1.0</crdt.version>
|
||||
<multiverse.version>0.7.0</multiverse.version>
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.snakeyaml;
|
||||
|
||||
public class Address {
|
||||
private String line;
|
||||
private String city;
|
||||
private String state;
|
||||
private Integer zip;
|
||||
|
||||
public String getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
public void setLine(String line) {
|
||||
this.line = line;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(String state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public Integer getZip() {
|
||||
return zip;
|
||||
}
|
||||
|
||||
public void setZip(Integer zip) {
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.snakeyaml;
|
||||
|
||||
public class Contact {
|
||||
|
||||
private String type;
|
||||
|
||||
private int number;
|
||||
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public int getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung.snakeyaml;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Customer {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private int age;
|
||||
private List<Contact> contactDetails;
|
||||
private Address homeAddress;
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public List<Contact> getContactDetails() {
|
||||
return contactDetails;
|
||||
}
|
||||
|
||||
public void setContactDetails(List<Contact> contactDetails) {
|
||||
this.contactDetails = contactDetails;
|
||||
}
|
||||
|
||||
public Address getHomeAddress() {
|
||||
return homeAddress;
|
||||
}
|
||||
|
||||
public void setHomeAddress(Address homeAddress) {
|
||||
this.homeAddress = homeAddress;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.snakeyaml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.nodes.Tag;
|
||||
|
||||
import com.baeldung.snakeyaml.Customer;
|
||||
|
||||
public class JavaToYAMLSerializationUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenDumpMap_thenGenerateCorrectYAML() {
|
||||
Map<String, Object> data = new LinkedHashMap<String, Object>();
|
||||
data.put("name", "Silenthand Olleander");
|
||||
data.put("race", "Human");
|
||||
data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
|
||||
Yaml yaml = new Yaml();
|
||||
StringWriter writer = new StringWriter();
|
||||
yaml.dump(data, writer);
|
||||
String expectedYaml = "name: Silenthand Olleander\nrace: Human\ntraits: [ONE_HAND, ONE_EYE]\n";
|
||||
assertEquals(expectedYaml, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDumpACustomType_thenGenerateCorrectYAML() {
|
||||
Customer customer = new Customer();
|
||||
customer.setAge(45);
|
||||
customer.setFirstName("Greg");
|
||||
customer.setLastName("McDowell");
|
||||
Yaml yaml = new Yaml();
|
||||
StringWriter writer = new StringWriter();
|
||||
yaml.dump(customer, writer);
|
||||
String expectedYaml = "!!com.baeldung.snakeyaml.Customer {age: 45, contactDetails: null, firstName: Greg,\n homeAddress: null, lastName: McDowell}\n";
|
||||
assertEquals(expectedYaml, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDumpAsCustomType_thenGenerateCorrectYAML() {
|
||||
Customer customer = new Customer();
|
||||
customer.setAge(45);
|
||||
customer.setFirstName("Greg");
|
||||
customer.setLastName("McDowell");
|
||||
Yaml yaml = new Yaml();
|
||||
String expectedYaml = "{age: 45, contactDetails: null, firstName: Greg, homeAddress: null, lastName: McDowell}\n";
|
||||
assertEquals(expectedYaml, yaml.dumpAs(customer, Tag.MAP, null));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
package com.baeldung.snakeyaml;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.yaml.snakeyaml.TypeDescription;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.constructor.Constructor;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class YAMLToJavaDeserialisationUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenLoadYAMLDocument_thenLoadCorrectMap() {
|
||||
Yaml yaml = new Yaml();
|
||||
InputStream inputStream = this.getClass()
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("yaml/customer.yaml");
|
||||
Map<String, Object> obj = yaml.load(inputStream);
|
||||
assertEquals("John", obj.get("firstName"));
|
||||
assertEquals("Doe", obj.get("lastName"));
|
||||
assertEquals(20, obj.get("age"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadYAMLDocumentWithTopLevelClass_thenLoadCorrectJavaObject() {
|
||||
Yaml yaml = new Yaml(new Constructor(Customer.class));
|
||||
InputStream inputStream = this.getClass()
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("yaml/customer.yaml");
|
||||
Customer customer = yaml.load(inputStream);
|
||||
assertEquals("John", customer.getFirstName());
|
||||
assertEquals("Doe", customer.getLastName());
|
||||
assertEquals(20, customer.getAge());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadYAMLDocumentWithAssumedClass_thenLoadCorrectJavaObject() {
|
||||
Yaml yaml = new Yaml();
|
||||
InputStream inputStream = this.getClass()
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("yaml/customer_with_type.yaml");
|
||||
Customer customer = yaml.load(inputStream);
|
||||
assertEquals("John", customer.getFirstName());
|
||||
assertEquals("Doe", customer.getLastName());
|
||||
assertEquals(20, customer.getAge());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadYAML_thenLoadCorrectImplicitTypes() {
|
||||
Yaml yaml = new Yaml();
|
||||
Map<Object, Object> document = yaml.load("3.0: 2018-07-22");
|
||||
assertNotNull(document);
|
||||
assertEquals(1, document.size());
|
||||
assertTrue(document.containsKey(3.0d));
|
||||
assertTrue(document.get(3.0d) instanceof Date);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadYAMLDocumentWithTopLevelClass_thenLoadCorrectJavaObjectWithNestedObjects() {
|
||||
Yaml yaml = new Yaml(new Constructor(Customer.class));
|
||||
InputStream inputStream = this.getClass()
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("yaml/customer_with_contact_details_and_address.yaml");
|
||||
Customer customer = yaml.load(inputStream);
|
||||
assertNotNull(customer);
|
||||
assertEquals("John", customer.getFirstName());
|
||||
assertEquals("Doe", customer.getLastName());
|
||||
assertEquals(31, customer.getAge());
|
||||
assertNotNull(customer.getContactDetails());
|
||||
assertEquals(2, customer.getContactDetails().size());
|
||||
assertEquals("mobile", customer.getContactDetails()
|
||||
.get(0)
|
||||
.getType());
|
||||
assertEquals(123456789,customer.getContactDetails()
|
||||
.get(0)
|
||||
.getNumber());
|
||||
assertEquals("landline", customer.getContactDetails()
|
||||
.get(1)
|
||||
.getType());
|
||||
assertEquals(456786868, customer.getContactDetails()
|
||||
.get(1)
|
||||
.getNumber());
|
||||
assertNotNull(customer.getHomeAddress());
|
||||
assertEquals("Xyz, DEF Street", customer.getHomeAddress()
|
||||
.getLine());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadYAMLDocumentWithTypeDescription_thenLoadCorrectJavaObjectWithCorrectGenericType() {
|
||||
Constructor constructor = new Constructor(Customer.class);
|
||||
TypeDescription customTypeDescription = new TypeDescription(Customer.class);
|
||||
customTypeDescription.addPropertyParameters("contactDetails", Contact.class);
|
||||
constructor.addTypeDescription(customTypeDescription);
|
||||
Yaml yaml = new Yaml(constructor);
|
||||
InputStream inputStream = this.getClass()
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("yaml/customer_with_contact_details.yaml");
|
||||
Customer customer = yaml.load(inputStream);
|
||||
assertNotNull(customer);
|
||||
assertEquals("John", customer.getFirstName());
|
||||
assertEquals("Doe", customer.getLastName());
|
||||
assertEquals(31, customer.getAge());
|
||||
assertNotNull(customer.getContactDetails());
|
||||
assertEquals(2, customer.getContactDetails().size());
|
||||
assertEquals("mobile", customer.getContactDetails()
|
||||
.get(0)
|
||||
.getType());
|
||||
assertEquals("landline", customer.getContactDetails()
|
||||
.get(1)
|
||||
.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadMultipleYAMLDocuments_thenLoadCorrectJavaObjects() {
|
||||
Yaml yaml = new Yaml(new Constructor(Customer.class));
|
||||
InputStream inputStream = this.getClass()
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("yaml/customers.yaml");
|
||||
int count = 0;
|
||||
for (Object object : yaml.loadAll(inputStream)) {
|
||||
count++;
|
||||
assertTrue(object instanceof Customer);
|
||||
}
|
||||
assertEquals(2, count);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 20
|
|
@ -0,0 +1,7 @@
|
|||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 31
|
||||
contactDetails:
|
||||
- { type: "mobile", number: 123456789}
|
||||
- { type: "landline", number: 456786868}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 31
|
||||
contactDetails:
|
||||
- type: "mobile"
|
||||
number: 123456789
|
||||
- type: "landline"
|
||||
number: 456786868
|
||||
homeAddress:
|
||||
line: "Xyz, DEF Street"
|
||||
city: "City Y"
|
||||
state: "State Y"
|
||||
zip: 345657
|
|
@ -0,0 +1,6 @@
|
|||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 31
|
||||
contactDetails:
|
||||
- !contact { type: "mobile", number: 123456789}
|
||||
- !contact { type: "landline", number: 456786868}
|
|
@ -0,0 +1,4 @@
|
|||
!!com.baeldung.snakeyaml.Customer
|
||||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 20
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 20
|
||||
---
|
||||
firstName: "Jack"
|
||||
lastName: "Jones"
|
||||
age: 25
|
6
pom.xml
6
pom.xml
|
@ -550,7 +550,7 @@
|
|||
<module>apache-meecrowave</module>
|
||||
<module>spring-reactive-kotlin</module>
|
||||
<module>jnosql</module>
|
||||
<module>testing-modules/junit-abstract</module>
|
||||
<module>testing-modules/junit-abstract</module>
|
||||
</modules>
|
||||
|
||||
</profile>
|
||||
|
@ -670,7 +670,7 @@
|
|||
<module>spring-amqp-simple</module>
|
||||
<module>spring-apache-camel</module>
|
||||
<module>spring-batch</module>
|
||||
<module>testing-modules/junit-abstract</module>
|
||||
<module>testing-modules/junit-abstract</module>
|
||||
|
||||
|
||||
<!-- group 2 - Pass, 11-16 min, 42 test failures, 4,020 KB -->
|
||||
|
@ -1076,7 +1076,7 @@
|
|||
<module>antlr</module>
|
||||
<module>maven-archetype</module>
|
||||
<module>apache-meecrowave</module>
|
||||
<module>testing-modules/junit-abstract</module>
|
||||
<module>testing-modules/junit-abstract</module>
|
||||
|
||||
<module>spring-hibernate4</module>
|
||||
<module>xml</module>
|
||||
|
|
Loading…
Reference in New Issue