1 /** 2 * Copyright (c) 2008, http://www.snakeyaml.org 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.yaml.snakeyaml.issues.issue310; 17 18 import static org.junit.Assert.assertEquals; 19 20 import java.util.ArrayList; 21 import java.util.List; 22 import java.util.Optional; 23 24 import org.junit.Test; 25 import org.yaml.snakeyaml.Yaml; 26 import org.yaml.snakeyaml.nodes.Node; 27 import org.yaml.snakeyaml.nodes.Tag; 28 import org.yaml.snakeyaml.representer.Represent; 29 import org.yaml.snakeyaml.representer.Representer; 30 31 public class OptionalTest { 32 33 public static class Salary { 34 35 private Optional<Double> income = Optional.empty(); 36 getIncome()37 public Optional<Double> getIncome() { 38 return income; 39 } 40 setIncome(Double income)41 public void setIncome(Double income) { 42 this.income = Optional.of(income); 43 } 44 setIncome(Optional<Double> income)45 public void setIncome(Optional<Double> income) { 46 this.income = income; 47 } 48 49 @Override toString()50 public String toString() { 51 return "Salary{" + "income=" + income + '}'; 52 } 53 54 @Override hashCode()55 public int hashCode() { 56 final int prime = 31; 57 int result = 1; 58 result = prime * result 59 + ((income == null) ? 0 : income.hashCode()); 60 return result; 61 } 62 63 @Override equals(Object obj)64 public boolean equals(Object obj) { 65 if (this == obj) 66 return true; 67 if (obj == null) 68 return false; 69 if (getClass() != obj.getClass()) 70 return false; 71 Salary other = (Salary) obj; 72 if (income == null) { 73 if (other.income != null) 74 return false; 75 } else if (!income.equals(other.income)) 76 return false; 77 return true; 78 } 79 } 80 81 public static class Person { 82 83 private String name; 84 85 private Optional<Salary> salary = Optional.empty(); 86 getName()87 public String getName() { 88 return name; 89 } 90 setName(String name)91 public void setName(String name) { 92 this.name = name; 93 } 94 getSalary()95 public Optional<Salary> getSalary() { 96 return salary; 97 } 98 setSalary(Optional<Salary> salary)99 public void setSalary(Optional<Salary> salary) { 100 this.salary = salary; 101 } 102 103 @Override toString()104 public String toString() { 105 return "Person{" + "name='" + name + '\'' + ", salary=" + salary 106 + '}'; 107 } 108 } 109 110 public static class OptionalRepresenter extends Representer { OptionalRepresenter()111 public OptionalRepresenter() { 112 this.representers.put(Optional.class, new RepresentOptional()); 113 } 114 115 private class RepresentOptional implements Represent { 116 representData(Object data)117 public Node representData(Object data) { 118 Optional<?> opt = (Optional<?>) data; 119 List<Object> seq = new ArrayList<>(1); 120 seq.add(opt.get()); 121 return representSequence(Tag.SEQ, seq, true); 122 } 123 } 124 } 125 126 @Test testOptionaStringLoad()127 public void testOptionaStringLoad() { 128 final String yamlStr = "name: Neo Anderson\nsalary: [{income: [123456.78]}]\n"; 129 final Yaml yamlParser = new Yaml(new OptionalRepresenter()); 130 Person expectedPerson = new Person(); 131 Salary s = new Salary(); 132 s.setIncome(123456.78); 133 expectedPerson.setName("Neo Anderson"); 134 expectedPerson.setSalary(Optional.of(s)); 135 136 final Person pFromStr = yamlParser.loadAs(yamlStr, Person.class); 137 138 assertEquals(expectedPerson.getName(), pFromStr.getName()); 139 assertEquals(expectedPerson.getSalary(), pFromStr.getSalary()); 140 } 141 142 @Test testOptionalDumpLoad()143 public void testOptionalDumpLoad() { 144 final Yaml yamlParser = new Yaml(new OptionalRepresenter()); 145 Person expectedPerson = new Person(); 146 Salary s = new Salary(); 147 s.setIncome(123456.78); 148 expectedPerson.setName("Neo Anderson"); 149 expectedPerson.setSalary(Optional.of(s)); 150 151 String pDump = yamlParser.dump(expectedPerson); 152 // System.out.println(pDump); 153 final Person pFromDump = yamlParser.loadAs(pDump, Person.class); 154 155 assertEquals(expectedPerson.getName(), pFromDump.getName()); 156 assertEquals(expectedPerson.getSalary(), pFromDump.getSalary()); 157 } 158 } 159