1 /** 2 * Copyright (c) 2008, 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.emitter; 17 18 import java.util.ArrayList; 19 import java.util.List; 20 import java.util.Map; 21 22 import junit.framework.TestCase; 23 24 import org.yaml.snakeyaml.DumperOptions; 25 import org.yaml.snakeyaml.DumperOptions.FlowStyle; 26 import org.yaml.snakeyaml.Yaml; 27 28 public class EmitterMultiLineTest extends TestCase { 29 testWriteMultiLineLiteral()30 public void testWriteMultiLineLiteral() { 31 String plain = "mama\nmila\nramu"; 32 Yaml yaml = new Yaml(); 33 String output = yaml.dump(plain); 34 // System.out.println(output); 35 assertEquals("|-\n mama\n mila\n ramu\n", output); 36 String parsed = (String) yaml.load(output); 37 // System.out.println(parsed); 38 assertEquals(plain, parsed); 39 } 40 testWriteMultiLineList()41 public void testWriteMultiLineList() { 42 String one = "first\nsecond\nthird"; 43 String two = "one\ntwo\nthree\n"; 44 byte[] binary = { 8, 14, 15, 10, 126, 32, 65, 65, 65 }; 45 List<Object> list = new ArrayList<Object>(2); 46 list.add(one); 47 list.add(two); 48 list.add(binary); 49 DumperOptions options = new DumperOptions(); 50 options.setDefaultFlowStyle(FlowStyle.BLOCK); 51 Yaml yaml = new Yaml(options); 52 String output = yaml.dump(list); 53 // System.out.println(output); 54 String etalon = "- |-\n first\n second\n third\n- |\n one\n two\n three\n- !!binary |-\n CA4PCn4gQUFB\n"; 55 assertEquals(etalon, output); 56 @SuppressWarnings("unchecked") 57 List<Object> parsed = (List<Object>) yaml.load(etalon); 58 assertEquals(3, parsed.size()); 59 assertEquals(one, parsed.get(0)); 60 assertEquals(two, parsed.get(1)); 61 assertEquals(new String(binary), new String((byte[]) parsed.get(2))); 62 } 63 testWriteMultiLineLiteralWithClipChomping()64 public void testWriteMultiLineLiteralWithClipChomping() { 65 String source = "a: 1\nb: |\n mama\n mila\n ramu\n"; 66 // System.out.println("Source:\n" + source); 67 Yaml yaml = new Yaml(); 68 @SuppressWarnings("unchecked") 69 Map<String, Object> parsed = (Map<String, Object>) yaml.load(source); 70 String value = (String) parsed.get("b"); 71 // System.out.println(value); 72 assertEquals("mama\nmila\nramu\n", value); 73 String dumped = yaml.dump(parsed); 74 // System.out.println(dumped); 75 assertEquals("a: 1\nb: |\n mama\n mila\n ramu\n", dumped); 76 } 77 testWriteMultiLineQuotedInFlowContext()78 public void testWriteMultiLineQuotedInFlowContext() { 79 String source = "{a: 1, b: 'mama\n\n mila\n\n ramu'}\n"; 80 // System.out.println("Source:\n" + source); 81 DumperOptions options = new DumperOptions(); 82 options.setDefaultFlowStyle(FlowStyle.FLOW); 83 Yaml yaml = new Yaml(options); 84 @SuppressWarnings("unchecked") 85 Map<String, Object> parsed = (Map<String, Object>) yaml.load(source); 86 String value = (String) parsed.get("b"); 87 // System.out.println(value); 88 assertEquals("mama\nmila\nramu", value); 89 String dumped = yaml.dump(parsed); 90 // System.out.println(dumped); 91 assertEquals("{a: 1, b: \"mama\\nmila\\nramu\"}\n", dumped); 92 } 93 testWriteMultiLineLiteralWithStripChomping()94 public void testWriteMultiLineLiteralWithStripChomping() { 95 String source = "a: 1\nb: |-\n mama\n mila\n ramu\n"; 96 // System.out.println("Source:\n" + source); 97 DumperOptions options = new DumperOptions(); 98 options.setDefaultFlowStyle(FlowStyle.BLOCK); 99 Yaml yaml = new Yaml(options); 100 @SuppressWarnings("unchecked") 101 Map<String, Object> parsed = (Map<String, Object>) yaml.load(source); 102 String value = (String) parsed.get("b"); 103 // System.out.println(value); 104 assertEquals("mama\nmila\nramu", value); 105 String dumped = yaml.dump(parsed); 106 // System.out.println(dumped); 107 assertEquals(source, dumped); 108 } 109 } 110