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.representer;
18  
19  import java.math.BigInteger;
20  import java.util.ArrayList;
21  import java.util.Date;
22  import java.util.HashMap;
23  import java.util.LinkedHashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.regex.Pattern;
27  
28  import junit.framework.TestCase;
29  
30  import org.yaml.snakeyaml.DumperOptions;
31  import org.yaml.snakeyaml.Yaml;
32  
33  public class SafeRepresenterTest extends TestCase {
34  
35      public void testBinaryPattern() {
36          Pattern pattern = SafeRepresenter.BINARY_PATTERN;
37          assertFalse(pattern.matcher("\tAndrey\r\n").find());
38          assertTrue(pattern.matcher("\u0005Andrey").find());
39      }
40  
41      public void testFloat() {
42          assertEquals("1.0E12", String.valueOf(new Double("1e12")));
43      }
44  
45      public void testNumber() {
46          List<Number> list = new ArrayList<Number>();
47          list.add(new Byte((byte) 3));
48          list.add(new Short((short) 4));
49          list.add(new Integer(5));
50          list.add(new BigInteger("6"));
51          list.add(new Long(7L));
52          list.add(Double.POSITIVE_INFINITY);
53          list.add(Double.NEGATIVE_INFINITY);
54          list.add(Double.NaN);
55          Yaml yaml = new Yaml();
56          String output = yaml.dump(list);
57          assertEquals("[3, 4, 5, 6, 7, .inf, -.inf, .NaN]\n", output);
58      }
59  
60      public void testDate() {
61          List<Date> list = new ArrayList<Date>();
62          list.add(new Date(1229684761159L));
63          list.add(new Date(1229684761059L));
64          list.add(new Date(1229684761009L));
65          list.add(new Date(1229684761150L));
66          list.add(new Date(1229684761100L));
67          list.add(new Date(1229684761000L));
68          list.add(new Date(1229684760000L));
69          DumperOptions options = new DumperOptions();
70          options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
71          Yaml yaml = new Yaml(options);
72          String output = yaml.dump(list);
73          assertEquals(
74                  "- 2008-12-19T11:06:01.159Z\n- 2008-12-19T11:06:01.059Z\n- 2008-12-19T11:06:01.009Z\n- 2008-12-19T11:06:01.150Z\n- 2008-12-19T11:06:01.100Z\n- 2008-12-19T11:06:01Z\n- 2008-12-19T11:06:00Z\n",
75                  output);
76      }
77  
78      public void testEmptyArray() {
79          Yaml yaml = new Yaml();
80          String output = yaml.dump(new String[0]);
81          assertEquals("[]\n", output);
82      }
83  
84      public void testStyle() {
85          List<Integer> list = new ArrayList<Integer>();
86          list.add(new Integer(1));
87          list.add(new Integer(1));
88          Map<String, Object> map = new HashMap<String, Object>();
89          map.put("list", list);
90          map.put("name", "Ubuntu");
91          map.put("age", 5);
92          DumperOptions options = new DumperOptions();
93          options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
94          options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
95          Yaml yaml = new Yaml(options);
96          String output = yaml.dump(map);
97          assertTrue(output.contains("\"age\": !!int \"5\""));
98          assertTrue(output.contains("\"name\": \"Ubuntu\""));
99          assertTrue(output.contains("- !!int \"1\""));
100     }
101 
102     public void testStyle2() {
103         List<Integer> list = new ArrayList<Integer>();
104         list.add(new Integer(1));
105         list.add(new Integer(1));
106         Map<String, Object> map = new LinkedHashMap<String, Object>();
107         map.put("age", 5);
108         map.put("name", "Ubuntu");
109         map.put("list", list);
110         DumperOptions options = new DumperOptions();
111         options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
112         options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
113         Yaml yaml = new Yaml(options);
114         String output = yaml.dump(map);
115         assertEquals("{'age': !!int '5', 'name': 'Ubuntu', 'list': [!!int '1', !!int '1']}\n",
116                 output);
117     }
118 
119     public void testStyle2Pretty() {
120         List<Integer> list = new ArrayList<Integer>();
121         list.add(new Integer(1));
122         list.add(new Integer(1));
123         Map<String, Object> map = new LinkedHashMap<String, Object>();
124         map.put("age", 5);
125         map.put("name", "Ubuntu");
126         map.put("list", list);
127         DumperOptions options = new DumperOptions();
128         options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
129         options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
130         options.setPrettyFlow(true);
131         Yaml yaml = new Yaml(options);
132         String output = yaml.dump(map);
133         assertEquals(
134                 "{\n  'age': !!int '5',\n  'name': 'Ubuntu',\n  'list': [\n    !!int '1',\n    !!int '1']\n  \n}\n",
135                 output);
136     }
137 }