KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jvyamlb > YAMLLoadTest


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2007 Ola Bini <ola@ologix.com>
15  *
16  * Alternatively, the contents of this file may be used under the terms of
17  * either of the GNU General Public License Version 2 or later (the "GPL"),
18  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19  * in which case the provisions of the GPL or the LGPL are applicable instead
20  * of those above. If you wish to allow use of your version of this file only
21  * under the terms of either the GPL or the LGPL, and not to allow others to
22  * use your version of this file under the terms of the CPL, indicate your
23  * decision by deleting the provisions above and replace them with the notice
24  * and other provisions required by the GPL or the LGPL. If you do not delete
25  * the provisions above, a recipient may use your version of this file under
26  * the terms of any one of the CPL, the GPL or the LGPL.
27  ***** END LICENSE BLOCK *****/

28 package org.jvyamlb;
29
30 import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32
33 import junit.framework.TestCase;
34
35 import org.jruby.util.ByteList;
36
37 /**
38  * @author <a HREF="mailto:ola.bini@ki.se">Ola Bini</a>
39  */

40 public class YAMLLoadTest extends TestCase {
41     public YAMLLoadTest(final String JavaDoc name) {
42         super(name);
43     }
44
45     private static ByteList s(String JavaDoc st) {
46         return new ByteList(st.getBytes());
47     }
48
49     public void testBasicStringScalarLoad() throws Exception JavaDoc {
50         ByteList str = s("str");
51         assertEquals(str,YAML.load(s("--- str")));
52         assertEquals(str,YAML.load(s("---\nstr")));
53         assertEquals(str,YAML.load(s("--- \nstr")));
54         assertEquals(str,YAML.load(s("--- \n str")));
55         assertEquals(str,YAML.load(s("str")));
56         assertEquals(str,YAML.load(s(" str")));
57         assertEquals(str,YAML.load(s("\nstr")));
58         assertEquals(str,YAML.load(s("\n str")));
59         assertEquals(str,YAML.load(s("\"str\"")));
60         assertEquals(str,YAML.load(s("'str'")));
61         assertEquals(s("ΓΌ"),YAML.load(s("---\n\"\\xC3\\xBC\"")));
62     }
63     
64     public void testBasicIntegerScalarLoad() {
65         assertEquals(new Long JavaDoc(47),YAML.load(s("47")));
66         assertEquals(new Long JavaDoc(0),YAML.load(s("0")));
67         assertEquals(new Long JavaDoc(-1),YAML.load(s("-1")));
68     }
69
70     public void testBlockMappingLoad() {
71         Map JavaDoc expected = new HashMap JavaDoc();
72         expected.put(s("a"),s("b"));
73         expected.put(s("c"),s("d"));
74         assertEquals(expected,YAML.load(s("a: b\nc: d")));
75         assertEquals(expected,YAML.load(s("c: d\na: b\n")));
76     }
77
78     public void testFlowMappingLoad() {
79         Map JavaDoc expected = new HashMap JavaDoc();
80         expected.put(s("a"),s("b"));
81         expected.put(s("c"),s("d"));
82         assertEquals(expected,YAML.load(s("{a: b, c: d}")));
83         assertEquals(expected,YAML.load(s("{c: d,\na: b}")));
84     }
85
86     public void testInternalChar() {
87         Map JavaDoc expected = new HashMap JavaDoc();
88         expected.put(s("bad_sample"),s("something:("));
89         assertEquals(expected,YAML.load(s("--- \nbad_sample: something:(\n")));
90     }
91
92     public void testBuiltinTag() {
93         assertEquals(s("str"),YAML.load(s("!!str str")));
94         assertEquals(s("str"),YAML.load(s("%YAML 1.1\n---\n!!str str")));
95         assertEquals(s("str"),YAML.load(s("%YAML 1.0\n---\n!str str")));
96         assertEquals(s("str"),YAML.load(s("---\n!str str"),YAML.config().version("1.0")));
97         assertEquals(new Long JavaDoc(123),YAML.load(s("---\n!int 123"),YAML.config().version("1.0")));
98         assertEquals(new Long JavaDoc(123),YAML.load(s("%YAML 1.1\n---\n!!int 123"),YAML.config().version("1.0")));
99     }
100
101     public void testDirectives() {
102         assertEquals(s("str"),YAML.load(s("%YAML 1.1\n--- !!str str")));
103         assertEquals(s("str"),YAML.load(s("%YAML 1.1\n%TAG !yaml! tag:yaml.org,2002:\n--- !yaml!str str")));
104         try {
105             YAML.load(s("%YAML 1.1\n%YAML 1.1\n--- !!str str"));
106             fail("should throw exception when repeating directive");
107         } catch(final ParserException e) {
108             assertTrue(true);
109         }
110     }
111
112     public void testJavaBeanLoad() {
113         final java.util.Calendar JavaDoc cal = java.util.Calendar.getInstance();
114         cal.clear();
115         cal.set(1982,5-1,3); // Java's months are zero-based...
116

117         final TestBean expected = new TestBean(s("Ola Bini"), 24, cal.getTime());
118         assertEquals(expected, YAML.load(s("--- !java/object:org.jvyamlb.TestBean\nname: Ola Bini\nage: 24\nborn: 1982-05-03\n")));
119     }
120 }// YAMLLoadTest
121
Popular Tags