View Javadoc

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