1 /** 2 * Copyright (c) 2008, SnakeYAML 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package examples; 15 16 import java.io.ByteArrayInputStream; 17 import java.io.File; 18 import java.io.FileInputStream; 19 import java.io.IOException; 20 import java.io.InputStream; 21 import java.nio.charset.StandardCharsets; 22 import java.util.Iterator; 23 import java.util.List; 24 import java.util.Map; 25 import java.util.NoSuchElementException; 26 import junit.framework.TestCase; 27 import org.yaml.snakeyaml.Yaml; 28 29 public class LoadExampleTest extends TestCase { 30 31 @SuppressWarnings("unchecked") testLoad()32 public void testLoad() { 33 Yaml yaml = new Yaml(); 34 String document = "\n- Hesperiidae\n- Papilionidae\n- Apatelodidae\n- Epiplemidae"; 35 List<String> list = yaml.load(document); 36 assertEquals("[Hesperiidae, Papilionidae, Apatelodidae, Epiplemidae]", list.toString()); 37 } 38 testLoadFromString()39 public void testLoadFromString() { 40 Yaml yaml = new Yaml(); 41 String document = "hello: 25"; 42 @SuppressWarnings("unchecked") 43 Map<String, Integer> map = yaml.load(document); 44 assertEquals("{hello=25}", map.toString()); 45 assertEquals(Integer.valueOf(25), map.get("hello")); 46 } 47 testLoadFromStream()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(StandardCharsets.UTF_8))); 55 assertEquals("test2", data); 56 input.close(); 57 } 58 testLoadManyDocuments()59 public void testLoadManyDocuments() throws IOException { 60 InputStream input = 61 new FileInputStream(new File("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 testLoadManyDocumentsWithIterator()73 public void testLoadManyDocumentsWithIterator() throws IOException { 74 InputStream input = 75 new FileInputStream(new File("src/test/resources/specification/example2_28.yaml")); 76 Yaml yaml = new Yaml(); 77 int counter = 0; 78 Iterator<Object> iter = yaml.loadAll(input).iterator(); 79 while (iter.hasNext()) { 80 Object data = iter.next(); 81 assertNotNull(data); 82 assertTrue(data.toString().length() > 1); 83 counter++; 84 } 85 assertEquals(3, counter); 86 input.close(); 87 } 88 testLoadManyDocumentsWithIterator2()89 public void testLoadManyDocumentsWithIterator2() throws IOException { 90 InputStream input = 91 new FileInputStream(new File("src/test/resources/specification/example2_28.yaml")); 92 Yaml yaml = new Yaml(); 93 Iterator<Object> iter = yaml.loadAll(input).iterator(); 94 Object data = iter.next(); 95 assertNotNull(data); 96 data = iter.next(); 97 assertNotNull(data); 98 data = iter.next(); 99 assertNotNull(data); 100 try { 101 iter.next(); 102 fail("Expect NoSuchElementException"); 103 } catch (NoSuchElementException e) { 104 assertEquals("No document is available.", e.getMessage()); 105 } 106 input.close(); 107 } 108 } 109