View Javadoc

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