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.issue311;
17 
18 import org.junit.Test;
19 import org.yaml.snakeyaml.DumperOptions;
20 import org.yaml.snakeyaml.Yaml;
21 import org.yaml.snakeyaml.constructor.Constructor;
22 import org.yaml.snakeyaml.nodes.Node;
23 import org.yaml.snakeyaml.nodes.NodeId;
24 import org.yaml.snakeyaml.nodes.ScalarNode;
25 import org.yaml.snakeyaml.nodes.Tag;
26 import org.yaml.snakeyaml.representer.Represent;
27 import org.yaml.snakeyaml.representer.Representer;
28 
29 import static org.junit.Assert.assertEquals;
30 
31 public class BooleanEnumTest {
32 
33     @Test
loadEnum()34     public void loadEnum() {
35 
36         Yaml yaml = new Yaml(new MyConstructor(), new MyRepresenter());
37         BeanWithEnum parsed = yaml.loadAs("{boolField: true, enumField: true, name: '10'}", BeanWithEnum.class);
38         //System.out.println(parsed.getEnumField());
39         assertEquals(BooleanEnum.TRUE, parsed.getEnumField());
40         assertEquals("10", parsed.getName());
41     }
42 
43     @Test
loadEnumUndefined()44     public void loadEnumUndefined() {
45 
46         Yaml yaml = new Yaml(new MyConstructor(), new MyRepresenter());
47         BeanWithEnum parsed = yaml.loadAs("{boolField: true, enumField: nonsense, name: bar}", BeanWithEnum.class);
48         //System.out.println(parsed.getEnumField());
49         assertEquals(BooleanEnum.UNKNOWN, parsed.getEnumField());
50         assertEquals("bar", parsed.getName());
51     }
52 
53     @Test
dumpEnum()54     public void dumpEnum() {
55 
56         BeanWithEnum bean = new BeanWithEnum(true, "10", BooleanEnum.TRUE);
57         Yaml yaml = new Yaml(new MyConstructor(), new MyRepresenter());
58         String output = yaml.dumpAs(bean, Tag.MAP, DumperOptions.FlowStyle.FLOW);
59         assertEquals("{boolField: true, enumField: 'true', name: '10'}\n", output);
60     }
61 
62     class MyRepresenter extends Representer {
MyRepresenter()63         public MyRepresenter() {
64             this.representers.put(BooleanEnum.class, new RepresentEnum());
65         }
66 
67         private class RepresentEnum implements Represent {
representData(Object data)68             public Node representData(Object data) {
69                 BooleanEnum myEnum = (BooleanEnum) data;
70                 String value;
71                 switch (myEnum) {
72                     case TRUE:
73                         value = "true";
74                         break;
75 
76                     case FALSE:
77                         value = "false";
78                         break;
79 
80                     case UNKNOWN:
81                         value = "unknown";
82                         break;
83 
84                     default:
85                         throw new IllegalArgumentException();
86                 }
87                 return representScalar(Tag.STR, value);
88             }
89         }
90     }
91 
92     class MyConstructor extends Constructor {
MyConstructor()93         public MyConstructor() {
94             this.yamlClassConstructors.put(NodeId.scalar, new ConstructEnum());
95         }
96 
97         private class ConstructEnum extends ConstructScalar {
construct(Node node)98             public Object construct(Node node) {
99                 if (node.getType().equals(BooleanEnum.class)) {
100                     String val = (String) constructScalar((ScalarNode) node);
101                     if ("true".equals(val)) {
102                         return BooleanEnum.TRUE;
103                     } else if ("false".equals(val)) {
104                         return BooleanEnum.FALSE;
105                     } else
106                         return BooleanEnum.UNKNOWN;
107                 }
108                 return super.construct(node);
109             }
110         }
111     }
112 }
113