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