Merge branch 'master' of https://github.com/sandy03934/tutorials into sandy03934-master
This commit is contained in:
commit
acccf855e5
1
libraries/.gitignore
vendored
1
libraries/.gitignore
vendored
@ -6,3 +6,4 @@
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
/bin/
|
||||
|
@ -771,6 +771,12 @@
|
||||
<version>${hamcrest-all.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
@ -909,6 +915,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>
|
||||
|
41
libraries/src/main/java/com/baeldung/snakeyaml/Address.java
Normal file
41
libraries/src/main/java/com/baeldung/snakeyaml/Address.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
25
libraries/src/main/java/com/baeldung/snakeyaml/Contact.java
Normal file
25
libraries/src/main/java/com/baeldung/snakeyaml/Contact.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.baeldung.snakeyaml;
|
||||
|
||||
public class Contact {
|
||||
|
||||
private String type;
|
||||
|
||||
private Integer number;
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Integer getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(Integer number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
}
|
53
libraries/src/main/java/com/baeldung/snakeyaml/Customer.java
Normal file
53
libraries/src/main/java/com/baeldung/snakeyaml/Customer.java
Normal file
@ -0,0 +1,53 @@
|
||||
package com.baeldung.snakeyaml;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Customer {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private Integer 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 Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer 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,56 @@
|
||||
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);
|
||||
System.out.println(writer.toString());
|
||||
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();
|
||||
System.out.println(yaml.dumpAs(customer, Tag.MAP, null));
|
||||
String expectedYaml = "{age: 45, contactDetails: null, firstName: Greg, homeAddress: null, lastName: McDowell}\n";
|
||||
assertEquals(expectedYaml, yaml.dumpAs(customer, Tag.MAP, null));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,132 @@
|
||||
package com.baeldung.snakeyaml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.yaml.snakeyaml.TypeDescription;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.constructor.Constructor;
|
||||
|
||||
import com.baeldung.snakeyaml.Contact;
|
||||
import com.baeldung.snakeyaml.Customer;
|
||||
|
||||
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);
|
||||
System.out.println(obj);
|
||||
}
|
||||
|
||||
@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);
|
||||
System.out.println(customer);
|
||||
}
|
||||
|
||||
@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);
|
||||
System.out.println(customer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadYAML_thenLoadCorrectImplicitTypes() {
|
||||
Yaml yaml = new Yaml();
|
||||
Map<Object, Object> document = yaml.load("3.0: 2018-07-22");
|
||||
assertNotNull(document);
|
||||
assertTrue(document.size() == 1);
|
||||
assertTrue(document.containsKey(Double.valueOf(3.0)));
|
||||
assertTrue(document.get(Double.valueOf(3.0)) 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());
|
||||
assertTrue(customer.getAge() == 31);
|
||||
assertNotNull(customer.getContactDetails());
|
||||
assertTrue(customer.getContactDetails()
|
||||
.size() == 2);
|
||||
assertEquals("mobile", customer.getContactDetails()
|
||||
.get(0)
|
||||
.getType());
|
||||
assertTrue(customer.getContactDetails()
|
||||
.get(0)
|
||||
.getNumber() == 123456789);
|
||||
assertEquals("landline", customer.getContactDetails()
|
||||
.get(1)
|
||||
.getType());
|
||||
assertTrue(customer.getContactDetails()
|
||||
.get(1)
|
||||
.getNumber() == 456786868);
|
||||
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());
|
||||
assertTrue(customer.getAge() == 31);
|
||||
assertNotNull(customer.getContactDetails());
|
||||
assertTrue(customer.getContactDetails()
|
||||
.size() == 2);
|
||||
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);
|
||||
}
|
||||
assertTrue(count == 2);
|
||||
}
|
||||
|
||||
}
|
3
libraries/src/test/resources/yaml/customer.yaml
Normal file
3
libraries/src/test/resources/yaml/customer.yaml
Normal file
@ -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
|
8
libraries/src/test/resources/yaml/customers.yaml
Normal file
8
libraries/src/test/resources/yaml/customers.yaml
Normal file
@ -0,0 +1,8 @@
|
||||
---
|
||||
firstName: "John"
|
||||
lastName: "Doe"
|
||||
age: 20
|
||||
---
|
||||
firstName: "Jack"
|
||||
lastName: "Jones"
|
||||
age: 25
|
Loading…
x
Reference in New Issue
Block a user