aboutsummaryrefslogtreecommitdiff
path: root/src/test/java8/org/yaml/snakeyaml/issues/issue310/OptionalTesting.java
blob: 962303345e5b14f7098f3bc5f74c25d341d9f242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
 * Copyright (c) 2008, SnakeYAML
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.yaml.snakeyaml.issues.issue310;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.junit.Test;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Represent;
import org.yaml.snakeyaml.representer.Representer;

public class OptionalTesting {

    public static class Salary {

        private Optional<Double> income = Optional.empty();

        public Optional<Double> getIncome() {
            return income;
        }

        public void setIncome(Double income) {
            this.income = Optional.of(income);
        }

        public void setIncome(Optional<Double> income) {
            this.income = income;
        }

        @Override
        public String toString() {
            return "Salary{" + "income=" + income + '}';
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((income == null) ? 0 : income.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Salary other = (Salary) obj;
            if (income == null) {
                if (other.income != null)
                    return false;
            } else if (!income.equals(other.income))
                return false;
            return true;
        }
    }

    public static class Person {

        private String name;

        private Optional<Salary> salary = Optional.empty();

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Optional<Salary> getSalary() {
            return salary;
        }

        public void setSalary(Optional<Salary> salary) {
            this.salary = salary;
        }

        @Override
        public String toString() {
            return "Person{" + "name='" + name + '\'' + ", salary=" + salary + '}';
        }
    }

    public static class OptionalRepresenter extends Representer {
        public OptionalRepresenter() {
            this.representers.put(Optional.class, new RepresentOptional());
        }

        private class RepresentOptional implements Represent {

            public Node representData(Object data) {
                Optional<?> opt = (Optional<?>) data;
                List<Object> seq = new ArrayList<>(1);
                seq.add(opt.get());
                return representSequence(Tag.SEQ, seq, DumperOptions.FlowStyle.FLOW);
            }
        }
    }

    static boolean reflectiveAccessDenied = false;

    protected void loadOptionalString() {
        final String yamlStr = "name: Neo Anderson\nsalary: [{income: [123456.78]}]\n";
        final Yaml yamlParser = new Yaml(new OptionalRepresenter());
        Person expectedPerson = new Person();
        Salary s = new Salary();
        s.setIncome(123456.78);
        expectedPerson.setName("Neo Anderson");
        expectedPerson.setSalary(Optional.of(s));

        final Person pFromStr = yamlParser.loadAs(yamlStr, Person.class);

        assertEquals(expectedPerson.getName(), pFromStr.getName());
        assertEquals(expectedPerson.getSalary(), pFromStr.getSalary());
    }

    protected void dumpLoadOptional() {
        final Yaml yamlParser = new Yaml(new OptionalRepresenter());
        Person expectedPerson = new Person();
        Salary s = new Salary();
        s.setIncome(123456.78);
        expectedPerson.setName("Neo Anderson");
        expectedPerson.setSalary(Optional.of(s));

        String pDump = yamlParser.dump(expectedPerson);
        // System.out.println(pDump);
        final Person pFromDump = yamlParser.loadAs(pDump, Person.class);

        assertEquals(expectedPerson.getName(), pFromDump.getName());
        assertEquals(expectedPerson.getSalary(), pFromDump.getSalary());
    }
}