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