View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.EnumMap;
21  import java.util.LinkedHashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import junit.framework.TestCase;
26  
27  import org.yaml.snakeyaml.constructor.Constructor;
28  
29  public class EnumTest extends TestCase {
30  
31      // Dumping
32      public void testDumpEnum() {
33          Yaml yaml = new Yaml();
34          String output = yaml.dump(Suit.CLUBS);
35          assertEquals("!!org.yaml.snakeyaml.Suit 'CLUBS'\n", output);
36      }
37  
38      public void testDumpOverriddenToString() {
39          Yaml yaml = new Yaml();
40          String output = yaml.dump(DumperOptions.FlowStyle.BLOCK);
41          assertEquals("!!org.yaml.snakeyaml.DumperOptions$FlowStyle 'BLOCK'\n", output);
42      }
43  
44      public void testDumpEnumArray() {
45          DumperOptions options = new DumperOptions();
46          options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
47          Yaml yaml = new Yaml(options);
48          String output = yaml.dump(Suit.values());
49          assertEquals(
50                  "- !!org.yaml.snakeyaml.Suit 'CLUBS'\n- !!org.yaml.snakeyaml.Suit 'DIAMONDS'\n- !!org.yaml.snakeyaml.Suit 'HEARTS'\n- !!org.yaml.snakeyaml.Suit 'SPADES'\n",
51                  output);
52      }
53  
54      public void testDumpEnumList() {
55          DumperOptions options = new DumperOptions();
56          options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
57          Yaml yaml = new Yaml(options);
58          List<Suit> list = Arrays.asList(Suit.values());
59          String output = yaml.dump(list);
60          assertEquals(
61                  "- !!org.yaml.snakeyaml.Suit 'CLUBS'\n- !!org.yaml.snakeyaml.Suit 'DIAMONDS'\n- !!org.yaml.snakeyaml.Suit 'HEARTS'\n- !!org.yaml.snakeyaml.Suit 'SPADES'\n",
62                  output);
63      }
64  
65      public void testDumpEnumListNoAnchor() {
66          DumperOptions options = new DumperOptions();
67          options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
68          Yaml yaml = new Yaml(options);
69          List<Suit> list = new ArrayList<Suit>(3);
70          list.add(Suit.CLUBS);
71          list.add(Suit.DIAMONDS);
72          list.add(Suit.CLUBS);
73          String output = yaml.dump(list);
74          assertEquals(
75                  "- !!org.yaml.snakeyaml.Suit 'CLUBS'\n- !!org.yaml.snakeyaml.Suit 'DIAMONDS'\n- !!org.yaml.snakeyaml.Suit 'CLUBS'\n",
76                  output);
77      }
78  
79      public void testDumpEnumMap() {
80          DumperOptions options = new DumperOptions();
81          options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
82          Yaml yaml = new Yaml(options);
83          Map<String, Suit> map = new LinkedHashMap<String, Suit>();
84          map.put("c", Suit.CLUBS);
85          map.put("d", Suit.DIAMONDS);
86          String output = yaml.dump(map);
87          assertEquals(
88                  "c: !!org.yaml.snakeyaml.Suit 'CLUBS'\nd: !!org.yaml.snakeyaml.Suit 'DIAMONDS'\n",
89                  output);
90      }
91  
92      public void testDumpEnumMap2() {
93          DumperOptions options = new DumperOptions();
94          options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
95          Yaml yaml = new Yaml(options);
96          Map<Suit, Integer> map = new EnumMap<Suit, Integer>(Suit.class);
97          map.put(Suit.CLUBS, 0);
98          map.put(Suit.DIAMONDS, 123);
99          String output = yaml.dump(map);
100         assertEquals(
101                 "!!org.yaml.snakeyaml.Suit 'CLUBS': 0\n!!org.yaml.snakeyaml.Suit 'DIAMONDS': 123\n",
102                 output);
103     }
104 
105     public void testDumpEnumBean() {
106         DumperOptions options = new DumperOptions();
107         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
108         Yaml yaml = new Yaml(options);
109         EnumBean bean = new EnumBean();
110         bean.setId(17);
111         bean.setSuit(Suit.SPADES);
112         LinkedHashMap<Suit, Integer> map = new LinkedHashMap<Suit, Integer>();
113         map.put(Suit.CLUBS, 1);
114         map.put(Suit.DIAMONDS, 2);
115         bean.setMap(map);
116         String output = yaml.dump(bean);
117         assertEquals(
118                 "!!org.yaml.snakeyaml.EnumBean\nid: 17\nmap:\n  CLUBS: 1\n  DIAMONDS: 2\nsuit: SPADES\n",
119                 output);
120     }
121 
122     // Loading
123     public void testLoadEnum() {
124         Yaml yaml = new Yaml();
125         Suit suit = (Suit) yaml.load("!!org.yaml.snakeyaml.Suit 'CLUBS'\n");
126         assertEquals(Suit.CLUBS, suit);
127     }
128 
129     public void testLoadOverridenToString() {
130         Yaml yaml = new Yaml();
131         assertEquals(DumperOptions.FlowStyle.BLOCK,
132                 yaml.load("!!org.yaml.snakeyaml.DumperOptions$FlowStyle 'BLOCK'\n"));
133     }
134 
135     @SuppressWarnings("unchecked")
136     public void testLoadEnumList() {
137         Yaml yaml = new Yaml();
138         List<Suit> list = (List<Suit>) yaml
139                 .load("- !!org.yaml.snakeyaml.Suit 'CLUBS'\n- !!org.yaml.snakeyaml.Suit 'DIAMONDS'\n- !!org.yaml.snakeyaml.Suit 'HEARTS'\n- !!org.yaml.snakeyaml.Suit 'SPADES'");
140         assertEquals(4, list.size());
141         assertEquals(Suit.CLUBS, list.get(0));
142         assertEquals(Suit.DIAMONDS, list.get(1));
143         assertEquals(Suit.HEARTS, list.get(2));
144         assertEquals(Suit.SPADES, list.get(3));
145     }
146 
147     @SuppressWarnings("unchecked")
148     public void testLoadEnumMap() {
149         Yaml yaml = new Yaml();
150         Map<Integer, Suit> map = (Map<Integer, Suit>) yaml
151                 .load("1: !!org.yaml.snakeyaml.Suit 'HEARTS'\n2: !!org.yaml.snakeyaml.Suit 'DIAMONDS'");
152         assertEquals(2, map.size());
153         assertEquals(Suit.HEARTS, map.get(1));
154         assertEquals(Suit.DIAMONDS, map.get(2));
155     }
156 
157     public void testLoadEnumBean() {
158         Yaml yaml = new Yaml();
159         EnumBean bean = (EnumBean) yaml
160                 .load("!!org.yaml.snakeyaml.EnumBean\nid: 174\nmap:\n  !!org.yaml.snakeyaml.Suit 'CLUBS': 1\n  !!org.yaml.snakeyaml.Suit 'DIAMONDS': 2\nsuit: CLUBS");
161 
162         LinkedHashMap<Suit, Integer> map = new LinkedHashMap<Suit, Integer>();
163         map.put(Suit.CLUBS, 1);
164         map.put(Suit.DIAMONDS, 2);
165 
166         assertEquals(Suit.CLUBS, bean.getSuit());
167         assertEquals(174, bean.getId());
168         assertEquals(map, bean.getMap());
169     }
170 
171     public void testLoadEnumBean2() {
172         Constructor c = new Constructor();
173         TypeDescription td = new TypeDescription(EnumBean.class);
174         td.putMapPropertyType("map", Suit.class, Object.class);
175         c.addTypeDescription(td);
176         Yaml yaml = new Yaml(c);
177         EnumBean bean = (EnumBean) yaml
178                 .load("!!org.yaml.snakeyaml.EnumBean\nid: 174\nmap:\n  CLUBS: 1\n  DIAMONDS: 2\nsuit: CLUBS");
179 
180         LinkedHashMap<Suit, Integer> map = new LinkedHashMap<Suit, Integer>();
181         map.put(Suit.CLUBS, 1);
182         map.put(Suit.DIAMONDS, 2);
183 
184         assertEquals(Suit.CLUBS, bean.getSuit());
185         assertEquals(174, bean.getId());
186         assertEquals(map, bean.getMap());
187     }
188 
189     public void testLoadWrongEnum() {
190         Yaml yaml = new Yaml();
191         try {
192             yaml.load("1: !!org.yaml.snakeyaml.Suit 'HEARTS'\n2: !!org.yaml.snakeyaml.Suit 'KOSYR'");
193             fail("KOSYR is not Suit");
194         } catch (Exception e) {
195             assertTrue("KOSYR must be reported",
196                     e.getMessage().contains("Unable to find enum value 'KOSYR' for enum"));
197         }
198     }
199 }