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 junit.framework.TestCase;
    20
    21  import org.yaml.snakeyaml.DumperOptions;
    22  import org.yaml.snakeyaml.Yaml;
    23  import org.yaml.snakeyaml.nodes.Tag;
    24  import org.yaml.snakeyaml.representer.Representer;
    25
    26  public class LongTest extends TestCase {
    27      public void testLongFail() {
    28          DumperOptions options = new DumperOptions();
    29          options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
    30          Yaml yaml = new Yaml(options);
    31          Foo foo = new Foo();
    32          String output = yaml.dump(foo);
    33          // System.out.println(output);
    34          try {
    35              yaml.load(output);
    36          } catch (Exception e) {
    37              assertTrue(e.getMessage(), e.getMessage().contains("argument type mismatch"));
    38          }
    39      }
    40
    41      public static class Foo {
    42          private Long bar = Long.valueOf(42L);
    43
    44          public Long getBar() {
    45              return bar;
    46          }
    47
    48          public void setBar(Long bar) {
    49              this.bar = bar;
    50          }
    51      }
    52
    53      public void testLongRepresenter() {
    54          DumperOptions options = new DumperOptions();
    55          options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
    56          Representer repr = new Representer();
    57          repr.addClassTag(Long.class, new Tag("!!java.lang.Long"));
    58          Yaml yaml = new Yaml(repr, options);
    59
    60          Foo foo = new Foo();
    61          String output = yaml.dump(foo);
    62          // System.out.println(output);
    63          Foo foo2 = (Foo) yaml.load(output);
    64          assertEquals(new Long(42L), foo2.getBar());
    65      }
    66
    67      public void testLongConstructor() {
    68          String doc = "!!org.yaml.snakeyaml.javabeans.LongTest$Foo\n\"bar\": !!int \"42\"";
    69          // System.out.println(doc);
    70          Yaml yaml = new Yaml();
    71          Foo foo2 = (Foo) yaml.load(doc);
    72          assertEquals(new Long(42L), foo2.getBar());
    73      }
    74  }