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.issue102;
17  
18  import java.io.StringReader;
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.Yaml;
27  
28  public class BigDataLoadTest extends TestCase {
29      private static final int SIZE = 5000;
30  
31      public void testBigStringData() {
32          Yaml yaml = new Yaml();
33          List<?> loaded = (List<?>) yaml.load(getLongYamlDocument(SIZE));
34          assertEquals(SIZE, loaded.size());
35      }
36  
37      public void testBigStreamData() {
38          Yaml yaml = new Yaml();
39          StringReader buffer = new StringReader(getLongYamlDocument(SIZE));
40          List<?> loaded = (List<?>) yaml.load(buffer);
41          assertEquals(SIZE, loaded.size());
42      }
43  
44      private String getLongYamlDocument(int size) {
45          List<DataBean> beans = new ArrayList<DataBean>();
46          Yaml yaml = new Yaml();
47          StringBuilder builder = new StringBuilder();
48          for (int i = 0; i < size; i++) {
49              List<String> list = new ArrayList<String>();
50              for (int j = 0; j < 10; j++) {
51                  list.add(String.valueOf(i + j));
52              }
53              Map<String, Integer> map = new HashMap<String, Integer>();
54              for (int j = 0; j < 10; j++) {
55                  map.put(String.valueOf(j), i + j);
56              }
57              DataBean bean = new DataBean();
58              bean.setId("id" + i);
59              bean.setList(list);
60              bean.setMap(map);
61              beans.add(bean);
62              String ooo = yaml.dumpAsMap(bean);
63              String[] lines = ooo.split("\n");
64              builder.append("-\n");
65              for (int j = 0; j < lines.length; j++) {
66                  builder.append("  ");
67                  builder.append(lines[j]);
68                  builder.append("\n");
69              }
70          }
71          String data = builder.toString();
72          System.out.println("Long data size: " + data.length() / 1024 + " kBytes.");
73          return data;
74      }
75  }