DumpExampleTest.java revision 7f67d9bf9c5368f1a9db91fa853db67f76870751
1/* 2 * See LICENSE file in distribution for copyright and licensing information. 3 */ 4package examples; 5 6import java.io.StringWriter; 7import java.util.HashMap; 8import java.util.LinkedList; 9import java.util.List; 10import java.util.Map; 11 12import junit.framework.TestCase; 13 14import org.yaml.snakeyaml.DumperOptions; 15import org.yaml.snakeyaml.Yaml; 16 17public class DumpExampleTest extends TestCase { 18 public void testDump() { 19 Map<String, Object> data = new HashMap<String, Object>(); 20 data.put("name", "Silenthand Olleander"); 21 data.put("race", "Human"); 22 data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" }); 23 Yaml yaml = new Yaml(); 24 String output = yaml.dump(data); 25 assertTrue(output.contains("name: Silenthand Olleander")); 26 assertTrue(output.contains("race: Human")); 27 assertTrue(output.contains("traits: [ONE_HAND, ONE_EYE]")); 28 } 29 30 public void testDumpWriter() { 31 Map<String, Object> data = new HashMap<String, Object>(); 32 data.put("name", "Silenthand Olleander"); 33 data.put("race", "Human"); 34 data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" }); 35 Yaml yaml = new Yaml(); 36 StringWriter writer = new StringWriter(); 37 yaml.dump(data, writer); 38 assertTrue(writer.toString().contains("name: Silenthand Olleander")); 39 assertTrue(writer.toString().contains("race: Human")); 40 assertTrue(writer.toString().contains("traits: [ONE_HAND, ONE_EYE]")); 41 } 42 43 public void testDumpMany() { 44 List<Integer> docs = new LinkedList<Integer>(); 45 for (int i = 1; i < 4; i++) { 46 docs.add(i); 47 } 48 DumperOptions options = new DumperOptions(); 49 options.setExplicitStart(true); 50 Yaml yaml = new Yaml(options); 51 String result = yaml.dumpAll(docs.iterator()); 52 assertNotNull(result); 53 assertTrue(result.contains("--- 2")); 54 } 55 56 public void testDumpCustomJavaClass() { 57 Hero hero = new Hero("Galain Ysseleg", -3, 2); 58 Yaml yaml = new Yaml(); 59 String output = yaml.dump(hero); 60 assertEquals("!!examples.Hero {hp: -3, name: Galain Ysseleg, sp: 2}\n", output); 61 } 62 63 public void testDumperOptions() { 64 List<Integer> data = new LinkedList<Integer>(); 65 for (int i = 0; i < 50; i++) { 66 data.add(i); 67 } 68 Yaml yaml = new Yaml(); 69 String output = yaml.dump(data); 70 assertTrue(output.contains("[0, 1, 2, 3, 4, 5, 6, 7, 8")); 71 // 72 DumperOptions options = new DumperOptions(); 73 options.setWidth(50); 74 options.setIndent(4); 75 yaml = new Yaml(options); 76 output = yaml.dump(data); 77 assertTrue(output.contains("1, 2")); 78 } 79 80 public void testDumperOptionsCanonical() { 81 List<Integer> data = new LinkedList<Integer>(); 82 for (int i = 0; i < 5; i++) { 83 data.add(i); 84 } 85 DumperOptions options = new DumperOptions(); 86 options.setCanonical(true); 87 Yaml yaml = new Yaml(options); 88 String output = yaml.dump(data); 89 assertTrue(output.contains("---")); 90 assertTrue(output.contains("!!seq [")); 91 assertTrue(output.contains("!!int \"3\",")); 92 } 93 94 public void testDumperOptionsFlowStyle() { 95 List<Integer> data = new LinkedList<Integer>(); 96 for (int i = 0; i < 5; i++) { 97 data.add(i); 98 } 99 DumperOptions options = new DumperOptions(); 100 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); 101 Yaml yaml = new Yaml(options); 102 String output = yaml.dump(data); 103 assertTrue(output.contains("- 0\n")); 104 assertTrue(output.contains("- 1\n")); 105 assertTrue(output.contains("- 4\n")); 106 } 107 108 public void testDumperOptionsStyle() { 109 List<Integer> data = new LinkedList<Integer>(); 110 for (int i = 0; i < 5; i++) { 111 data.add(i); 112 } 113 DumperOptions options = new DumperOptions(); 114 options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED); 115 Yaml yaml = new Yaml(options); 116 String output = yaml.dump(data); 117 assertTrue(output.contains("- !!int \"0\"")); 118 assertTrue(output.contains("- !!int \"1\"")); 119 assertTrue(output.contains("- !!int \"4\"")); 120 } 121 122} 123