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 examples; 17 18 import java.io.ByteArrayInputStream; 19 import java.io.File; 20 import java.io.FileInputStream; 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.util.List; 24 import java.util.Map; 25 26 import junit.framework.TestCase; 27 28 import org.yaml.snakeyaml.Yaml; 29 30 public class LoadExampleTest extends TestCase { 31 @SuppressWarnings("unchecked") 32 public void testLoad() { 33 Yaml yaml = new Yaml(); 34 String document = "\n- Hesperiidae\n- Papilionidae\n- Apatelodidae\n- Epiplemidae"; 35 List<String> list = (List<String>) yaml.load(document); 36 assertEquals("[Hesperiidae, Papilionidae, Apatelodidae, Epiplemidae]", list.toString()); 37 } 38 39 public void testLoadFromString() { 40 Yaml yaml = new Yaml(); 41 String document = "hello: 25"; 42 @SuppressWarnings("unchecked") 43 Map<String, Integer> map = (Map<String, Integer>) yaml.load(document); 44 assertEquals("{hello=25}", map.toString()); 45 assertEquals(new Integer(25), map.get("hello")); 46 } 47 48 public void testLoadFromStream() throws IOException { 49 InputStream input = new FileInputStream(new File("src/test/resources/reader/utf-8.txt")); 50 Yaml yaml = new Yaml(); 51 Object data = yaml.load(input); 52 assertEquals("test", data); 53 // 54 data = yaml.load(new ByteArrayInputStream("test2".getBytes("UTF-8"))); 55 assertEquals("test2", data); 56 input.close(); 57 } 58 59 public void testLoadManyDocuments() throws IOException { 60 InputStream input = new FileInputStream(new File( 61 "src/test/resources/specification/example2_28.yaml")); 62 Yaml yaml = new Yaml(); 63 int counter = 0; 64 for (Object data : yaml.loadAll(input)) { 65 assertNotNull(data); 66 assertTrue(data.toString().length() > 1); 67 counter++; 68 } 69 assertEquals(3, counter); 70 input.close(); 71 } 72 } 73