• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2008, SnakeYAML
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package examples.enumset;
15 
16 import java.util.EnumSet;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.yaml.snakeyaml.LoaderOptions;
20 import org.yaml.snakeyaml.TypeDescription;
21 import org.yaml.snakeyaml.Yaml;
22 import org.yaml.snakeyaml.error.YAMLException;
23 import org.yaml.snakeyaml.nodes.Node;
24 
25 public class YamlEnumSetTest {
26 
27   Day day;
28   EnumSet<Day> setOfDays;
29 
getDay()30   public Day getDay() {
31     return this.day;
32   }
33 
setDay(Day day)34   public void setDay(Day day) {
35     this.day = day;
36   }
37 
getSetOfDays()38   public EnumSet<Day> getSetOfDays() {
39     return this.setOfDays;
40   }
41 
setSetOfDays(EnumSet<Day> setOfDays)42   public void setSetOfDays(EnumSet<Day> setOfDays) {
43     this.setOfDays = setOfDays;
44   }
45 
46   @Test
enumSetDumpLoad()47   public void enumSetDumpLoad() {
48 
49     YamlEnumSetTest yEST = new YamlEnumSetTest();
50     yEST.day = Day.SUNDAY;
51     yEST.setOfDays = EnumSet.of(Day.MONDAY, Day.WEDNESDAY, Day.FRIDAY);
52 
53     String output = createYaml().dump(yEST);
54     YamlEnumSetTest loaded = createYaml().loadAs(output, YamlEnumSetTest.class);
55     Assert.assertSame(loaded.day, Day.SUNDAY);
56 
57     Object[] expected = yEST.setOfDays.toArray();
58     Object[] actual = loaded.setOfDays.toArray();
59 
60     Assert.assertArrayEquals(expected, actual);
61     Assert.assertEquals(yEST.setOfDays, loaded.setOfDays);
62   }
63 
64   @Test
enumSetLoadWithoutTags()65   public void enumSetLoadWithoutTags() {
66 
67     YamlEnumSetTest yEST = new YamlEnumSetTest();
68     yEST.day = Day.SUNDAY;
69     yEST.setOfDays = EnumSet.of(Day.MONDAY, Day.WEDNESDAY, Day.FRIDAY);
70 
71     String yamlStr = "day: SUNDAY\nsetOfDays: { MONDAY, WEDNESDAY, FRIDAY }\n";
72     YamlEnumSetTest loaded = createYaml().loadAs(yamlStr, YamlEnumSetTest.class);
73     Assert.assertSame(loaded.day, Day.SUNDAY);
74 
75     Object[] expected = yEST.setOfDays.toArray();
76     Object[] actual = loaded.setOfDays.toArray();
77 
78     Assert.assertArrayEquals(expected, actual);
79     Assert.assertEquals(yEST.setOfDays, loaded.setOfDays);
80   }
81 
82   @Test
enumSetLoadWithoutCaseSensitive()83   public void enumSetLoadWithoutCaseSensitive() {
84     // given
85     LoaderOptions loaderOptions = new LoaderOptions();
86     loaderOptions.setEnumCaseSensitive(false);
87 
88     YamlEnumSetTest yEST = new YamlEnumSetTest();
89     yEST.day = Day.SUNDAY;
90     yEST.setOfDays = EnumSet.of(Day.MONDAY, Day.WEDNESDAY, Day.FRIDAY);
91 
92     String yamlStr = "day: SUNDAY\nsetOfDays: { MONDAY, wednesday, friDay }\n";
93 
94     // when
95     YamlEnumSetTest loaded = createYaml(loaderOptions).loadAs(yamlStr, YamlEnumSetTest.class);
96 
97     // then
98     Assert.assertSame(loaded.day, Day.SUNDAY);
99 
100     Object[] expected = yEST.setOfDays.toArray();
101     Object[] actual = loaded.setOfDays.toArray();
102 
103     Assert.assertArrayEquals(expected, actual);
104     Assert.assertEquals(yEST.setOfDays, loaded.setOfDays);
105   }
106 
107   @Test(expected = YAMLException.class)
enumSetLoadWithCaseSensitive()108   public void enumSetLoadWithCaseSensitive() {
109     YamlEnumSetTest yEST = new YamlEnumSetTest();
110     yEST.day = Day.SUNDAY;
111     yEST.setOfDays = EnumSet.of(Day.MONDAY, Day.WEDNESDAY, Day.FRIDAY);
112 
113     String yamlStr = "day: SUNDAY\nsetOfDays: { MONDAY, wednesday, friDay }\n";
114 
115     // when
116     createYaml().loadAs(yamlStr, YamlEnumSetTest.class);
117   }
118 
createYaml(LoaderOptions loaderOptions)119   private Yaml createYaml(LoaderOptions loaderOptions) {
120     Yaml yaml = loaderOptions != null ? new Yaml(loaderOptions) : new Yaml();
121 
122     TypeDescription yamlEnumSetTD = new TypeDescription(YamlEnumSetTest.class) {
123 
124       @Override
125       public Object newInstance(String propertyName, Node node) {
126         if ("setOfDays".equals(propertyName)) {
127           node.setTwoStepsConstruction(true);
128           return EnumSet.noneOf(Day.class);
129         }
130         return super.newInstance(propertyName, node);
131       }
132     };
133 
134     yaml.addTypeDescription(yamlEnumSetTD);
135 
136     return yaml;
137   }
138 
createYaml()139   private Yaml createYaml() {
140     return createYaml(null);
141   }
142 
143 
144 }
145