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.javabeans;
17  
18  import java.util.Arrays;
19  
20  import junit.framework.TestCase;
21  
22  import org.yaml.snakeyaml.DumperOptions;
23  import org.yaml.snakeyaml.Yaml;
24  
25  public class StringArrayTest extends TestCase {
26      public void testStrings() {
27          A a = new A();
28          a.setNames(new String[] { "aaa", "bbb", "ccc" });
29          Yaml yaml = new Yaml();
30          String output = yaml.dump(a);
31          assertEquals("!!org.yaml.snakeyaml.javabeans.StringArrayTest$A\nnames: [aaa, bbb, ccc]\n",
32                  output);
33          A b = (A) yaml.load(output);
34          assertTrue(Arrays.equals(a.getNames(), b.getNames()));
35      }
36  
37      public void testStringsPretty() {
38          A a = new A();
39          a.setNames(new String[] { "aaa", "bbb", "ccc" });
40          DumperOptions options = new DumperOptions();
41          options.setPrettyFlow(true);
42          Yaml yaml = new Yaml(options);
43          String output = yaml.dump(a);
44          assertEquals(
45                  "!!org.yaml.snakeyaml.javabeans.StringArrayTest$A\nnames: [\n  aaa,\n  bbb,\n  ccc]\n",
46                  output);
47          A b = (A) yaml.load(output);
48          assertTrue(Arrays.equals(a.getNames(), b.getNames()));
49      }
50  
51      public static class A {
52          String[] names;
53  
54          public String[] getNames() {
55              return names;
56          }
57  
58          public void setNames(String[] names) {
59              this.names = names;
60          }
61  
62          public String getName(int index) {
63              return names[index];
64          }
65  
66          public void setName(int index, String name) {
67              this.names[index] = name;
68          }
69      }
70  }