Home | History | Annotate | Download | only in snakeyaml
      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  */
     16 package org.yaml.snakeyaml;
     17 
     18 import java.util.ArrayList;
     19 import java.util.Calendar;
     20 import java.util.Date;
     21 import java.util.LinkedHashMap;
     22 import java.util.List;
     23 import java.util.Map;
     24 import java.util.Set;
     25 import java.util.TimeZone;
     26 
     27 import junit.framework.TestCase;
     28 
     29 import org.yaml.snakeyaml.constructor.AbstractConstruct;
     30 import org.yaml.snakeyaml.constructor.Constructor;
     31 import org.yaml.snakeyaml.nodes.Node;
     32 import org.yaml.snakeyaml.nodes.ScalarNode;
     33 import org.yaml.snakeyaml.nodes.Tag;
     34 
     35 /**
     36  * Test Chapter 2.4 from the YAML specification
     37  *
     38  * @see <a href="http://yaml.org/spec/1.1/"></a>
     39  */
     40 public class Chapter2_4Test extends TestCase {
     41 
     42     @SuppressWarnings("unchecked")
     43     public void testExample_2_19() {
     44         YamlDocument document = new YamlDocument("example2_19.yaml");
     45         Map<String, Object> map = (Map<String, Object>) document.getNativeData();
     46         assertEquals(5, map.size());
     47         assertEquals("Expect 12345 to be an Integer.", Integer.class, map.get("canonical")
     48                 .getClass());
     49         assertEquals(new Integer(12345), map.get("canonical"));
     50         assertEquals(new Integer(12345), map.get("decimal"));
     51         assertEquals(new Integer(3 * 3600 + 25 * 60 + 45), map.get("sexagesimal"));
     52         assertEquals(new Integer(014), map.get("octal"));
     53         assertEquals(new Integer(0xC), map.get("hexadecimal"));
     54     }
     55 
     56     @SuppressWarnings("unchecked")
     57     public void testExample_2_20() {
     58         YamlDocument document = new YamlDocument("example2_20.yaml");
     59         Map<String, Object> map = (Map<String, Object>) document.getNativeData();
     60         assertEquals(6, map.size());
     61         assertEquals("Expect '1.23015e+3' to be a Double.", Double.class, map.get("canonical")
     62                 .getClass());
     63         assertEquals(new Double(1230.15), map.get("canonical"));
     64         assertEquals(new Double(12.3015e+02), map.get("exponential"));
     65         assertEquals(new Double(20 * 60 + 30.15), map.get("sexagesimal"));
     66         assertEquals(new Double(1230.15), map.get("fixed"));
     67         assertEquals(Double.NEGATIVE_INFINITY, map.get("negative infinity"));
     68         assertEquals(Double.NaN, map.get("not a number"));
     69     }
     70 
     71     @SuppressWarnings("unchecked")
     72     public void testExample_2_21() {
     73         YamlDocument document = new YamlDocument("example2_21.yaml");
     74         Map<String, Object> map = (Map<String, Object>) document.getNativeData();
     75         assertEquals(4, map.size());
     76         assertNull("'~' must be parsed as 'null': " + map.get(null), map.get(null));
     77         assertTrue((Boolean) map.get(Boolean.TRUE));
     78         assertFalse((Boolean) map.get(Boolean.FALSE));
     79         assertEquals("12345", map.get("string"));
     80     }
     81 
     82     @SuppressWarnings("unchecked")
     83     public void testExample_2_22() {
     84         YamlDocument document = new YamlDocument("example2_22.yaml");
     85         Map<String, Object> map = (Map<String, Object>) document.getNativeData();
     86         assertEquals(4, map.size());
     87         assertEquals("Expect '2001-12-15T02:59:43.1Z' to be a Date.", Date.class,
     88                 map.get("canonical").getClass());
     89         Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
     90         cal.clear();
     91         cal.set(Calendar.YEAR, 2001);
     92         cal.set(Calendar.MONTH, 11); // Java's months are zero-based...
     93         cal.set(Calendar.DAY_OF_MONTH, 15);
     94         cal.set(Calendar.HOUR_OF_DAY, 2);
     95         cal.set(Calendar.MINUTE, 59);
     96         cal.set(Calendar.SECOND, 43);
     97         cal.set(Calendar.MILLISECOND, 100);
     98         Date date = cal.getTime();
     99         assertEquals(date, map.get("canonical"));
    100         assertEquals("Expect '2001-12-14t21:59:43.10-05:00' to be a Date.", Date.class,
    101                 map.get("iso8601").getClass());
    102         assertEquals("Expect '2001-12-14 21:59:43.10 -5' to be a Date.", Date.class,
    103                 map.get("spaced").getClass());
    104         assertEquals("Expect '2002-12-14' to be a Date.", Date.class, map.get("date").getClass());
    105     }
    106 
    107     @SuppressWarnings("unchecked")
    108     public void testExample_2_23_non_date() {
    109         try {
    110             YamlDocument document = new YamlDocument("example2_23_non_date.yaml");
    111             Map<String, Object> map = (Map<String, Object>) document.getNativeData();
    112             assertEquals(1, map.size());
    113             assertEquals("2002-04-28", map.get("not-date"));
    114         } catch (RuntimeException e) {
    115             fail("Cannot parse '!!str': 'not-date: !!str 2002-04-28'");
    116         }
    117     }
    118 
    119     @SuppressWarnings("unchecked")
    120     public void testExample_2_23_picture() {
    121         YamlDocument document = new YamlDocument("example2_23_picture.yaml", false);
    122         Map<String, Object> map = (Map<String, Object>) document.getNativeData();
    123         assertEquals(1, map.size());
    124         byte[] picture = (byte[]) map.get("picture");
    125         assertEquals((byte) 'G', picture[0]);
    126         assertEquals((byte) 'I', picture[1]);
    127         assertEquals((byte) 'F', picture[2]);
    128     }
    129 
    130     class SomethingConstructor extends Constructor {
    131         public SomethingConstructor() {
    132             this.yamlConstructors.put(new Tag("!something"), new ConstructSomething());
    133         }
    134 
    135         private class ConstructSomething extends AbstractConstruct {
    136             public Object construct(Node node) {
    137                 // convert to upper case
    138                 String val = (String) constructScalar((ScalarNode) node);
    139                 return val.toUpperCase().replace('\n', ' ').trim();
    140             }
    141         }
    142     }
    143 
    144     @SuppressWarnings("unchecked")
    145     public void testExample_2_23() {
    146         YamlDocument document = new YamlDocument("example2_23.yaml", false,
    147                 new SomethingConstructor());
    148         Map<String, Object> map = (Map<String, Object>) document.getNativeData();
    149         assertEquals(3, map.size());
    150         String special = (String) map.get("application specific tag");
    151         assertEquals("THE SEMANTICS OF THE TAG ABOVE MAY BE DIFFERENT FOR DIFFERENT DOCUMENTS.",
    152                 special);
    153     }
    154 
    155     @SuppressWarnings("unchecked")
    156     public void testExample_2_25() {
    157         YamlDocument document = new YamlDocument("example2_25.yaml");
    158         Set<String> set = (Set<String>) document.getNativeData();
    159         assertEquals(3, set.size());
    160         assertTrue(set.contains("Mark McGwire"));
    161         assertTrue(set.contains("Sammy Sosa"));
    162         assertTrue(set.contains("Ken Griff"));
    163     }
    164 
    165     @SuppressWarnings("unchecked")
    166     public void testExample_2_26() {
    167         YamlDocument document = new YamlDocument("example2_26.yaml");
    168         Map<String, String> map = (Map<String, String>) document.getNativeData();
    169         assertEquals(3, map.size());
    170         assertTrue(map instanceof LinkedHashMap);
    171         assertEquals(new Integer(65), map.get("Mark McGwire"));
    172         assertEquals(new Integer(63), map.get("Sammy Sosa"));
    173         assertEquals(new Integer(58), map.get("Ken Griffy"));
    174         List<String> list = new ArrayList<String>();
    175         for (String key : map.keySet()) {
    176             list.add(key);
    177         }
    178         assertEquals("Mark McGwire", list.get(0));
    179         assertEquals("Sammy Sosa", list.get(1));
    180         assertEquals("Ken Griffy", list.get(2));
    181     }
    182 }
    183