• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 org.junit.Test;
21 import org.yaml.snakeyaml.Yaml;
22 import org.yaml.snakeyaml.introspector.BeanAccess;
23 import org.yaml.snakeyaml.nodes.Node;
24 import org.yaml.snakeyaml.representer.Represent;
25 import org.yaml.snakeyaml.representer.Representer;
26 
27 public class PropertyWithPrivateCostructorTest {
28 
29     public static class OptionRepresenter extends Representer {
30 
OptionRepresenter()31         public OptionRepresenter() {
32             this.representers.put(Option.class, new RepresentOption());
33         }
34 
35         private class RepresentOption implements Represent {
representData(Object data)36             public Node representData(Object data) {
37                 Option<?> opt = (Option<?>) data;
38                 return represent(opt.getValue());
39             }
40         }
41 
42     }
43 
44     @Test
loadFromString()45     public void loadFromString() {
46 
47         String yamlStr = "id: 123\n" + "income: 123456.78\n" + "name: Neo Anderson";
48 
49         Person loadedPerson = yaml().loadAs(yamlStr, Person.class);
50 
51         assertEquals("id", loadedPerson.getId(), 123);
52         assertEquals("name", loadedPerson.getName(), "Neo Anderson");
53         assertEquals("income", loadedPerson.getIncome().getValue().doubleValue(), 123456.78, 0.);
54     }
55 
56     @Test
dumpNload()57     public void dumpNload() {
58 
59         Person person = new Person(123, "Neo Anderson", Option.valueOf(123456.78));
60 
61         String dump = yaml().dumpAsMap(person);
62 
63         Person loadedPerson = yaml().loadAs(dump, Person.class);
64 
65         assertEquals("id", loadedPerson.getId(), 123);
66         assertEquals("name", loadedPerson.getName(), "Neo Anderson");
67         assertEquals("income", loadedPerson.getIncome().getValue().doubleValue(), 123456.78, 0.);
68     }
69 
yaml()70     private Yaml yaml() {
71         Yaml _yaml = new Yaml(new OptionRepresenter());
72         _yaml.setBeanAccess(BeanAccess.FIELD);
73         return _yaml;
74     }
75 }
76