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