View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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.issues.issue29;
17  
18  import java.util.LinkedHashMap;
19  import java.util.Map;
20  
21  import junit.framework.TestCase;
22  
23  import org.yaml.snakeyaml.DumperOptions;
24  import org.yaml.snakeyaml.DumperOptions.ScalarStyle;
25  import org.yaml.snakeyaml.Util;
26  import org.yaml.snakeyaml.Yaml;
27  import org.yaml.snakeyaml.nodes.Node;
28  import org.yaml.snakeyaml.nodes.ScalarNode;
29  import org.yaml.snakeyaml.representer.Representer;
30  
31  /**
32   * to test http://code.google.com/p/snakeyaml/issues/detail?id=29
33   */
34  public class FlexibleScalarStyleTest extends TestCase {
35      public void testLong() {
36          DumperOptions options = new DumperOptions();
37          options.setDefaultScalarStyle(ScalarStyle.FOLDED);
38          Yaml yaml = new Yaml(options);
39          String result = yaml
40                  .dump("qqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq "
41                          + "qqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq "
42                          + "qqqqqqqqqqqqqqqqqqqqqqqqq 111111111111111111111111\n "
43                          + "qqqqqqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqqqq\n");
44          // System.out.println(result);
45          assertTrue(result.startsWith(">\n"));
46          assertEquals(
47                  ">\n  qqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq\n  qqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqq 111111111111111111111111\n   qqqqqqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqqqq\n",
48                  result);
49      }
50  
51      public void testNoFoldedScalar() {
52          DumperOptions options = new DumperOptions();
53          options.setWidth(30);
54          Yaml yaml = new Yaml(options);
55          String output = yaml.dump(getData());
56          // System.out.println(output);
57          String etalon = Util.getLocalResource("representer/scalar-style1.yaml");
58          assertEquals(etalon, output);
59      }
60  
61      public void testCustomScalarStyle() {
62          DumperOptions options = new DumperOptions();
63          options.setWidth(30);
64          Yaml yaml = new Yaml(new MyRepresenter(), options);
65          String output = yaml.dump(getData());
66          // System.out.println(output);
67          String etalon = Util.getLocalResource("representer/scalar-style2.yaml");
68          assertEquals(etalon, output);
69      }
70  
71      private Map<String, String> getData() {
72          Map<String, String> map = new LinkedHashMap<String, String>();
73          map.put("name", "Steve Jobs");
74          map.put("address", "Name\nStreet Number\nCountry");
75          map.put("description",
76                  "1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
77          return map;
78      }
79  
80      private class MyRepresenter extends Representer {
81  
82          public MyRepresenter() {
83              super();
84              this.representers.put(String.class, new FlexibleRepresent());
85          }
86  
87          private class FlexibleRepresent extends RepresentString {
88              public Node representData(Object data) {
89                  ScalarNode node = (ScalarNode) super.representData(data);
90                  if (node.getStyle() == null) {
91                      // if Plain scalar style
92                      if (node.getValue().length() < 25) {
93                          return node;
94                      } else {
95                          // Folded scalar style
96                          return new ScalarNode(node.getTag(), node.getValue(), node.getStartMark(),
97                                  node.getEndMark(), '>');
98                      }
99                  } else {
100                     return node;
101                 }
102             }
103         }
104     }
105 }