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