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