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.immutable.primitives;
18  
19  import junit.framework.TestCase;
20  
21  import org.yaml.snakeyaml.Yaml;
22  import org.yaml.snakeyaml.error.YAMLException;
23  
24  public class ImmutablePrimitivesTest extends TestCase {
25  
26      public void testPrimitives() {
27          Yaml yaml = new Yaml(new ImmutablePrimitivesRepresenter());
28          BunchOfPrimitives bunch = new BunchOfPrimitives(10, 40.0, true);
29          String dump = yaml.dump(bunch);
30          assertEquals("!!" + bunch.getClass().getCanonicalName() + " [10, 40.0, true]\n", dump);
31          Object loaded = yaml.load(dump);
32          assertEquals(loaded.toString(), bunch, loaded);
33      }
34  
35      public void testPrimitivesLong() {
36          Yaml yaml = new Yaml();
37          String dump = "!!org.yaml.snakeyaml.immutable.primitives.BunchOfPrimitives [10000000000, 40.0, true]";
38          BunchOfPrimitives bunch = (BunchOfPrimitives) yaml.load(dump);
39          assertEquals("Must be truncated.", new Long(10000000000L).intValue(),
40                  bunch.getPrimitiveInt());
41      }
42  
43      public void testPrimitivesException() {
44          Yaml yaml = new Yaml();
45          String dump = "!!org.yaml.snakeyaml.immutable.primitives.BunchOfPrimitives [10, 40, true]";
46          try {
47              yaml.load(dump);
48              fail();
49          } catch (YAMLException e) {
50              assertEquals(
51                      "null; Can't construct a java object for tag:yaml.org,2002:org.yaml.snakeyaml.immutable.primitives.BunchOfPrimitives; exception=No suitable constructor with 3 arguments found for class org.yaml.snakeyaml.immutable.primitives.BunchOfPrimitives",
52                      e.getMessage());
53          }
54      }
55  }