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.pyyaml; 17 18 import java.io.File; 19 import java.io.FilenameFilter; 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.util.ArrayList; 23 import java.util.List; 24 25 import junit.framework.TestCase; 26 27 import org.yaml.snakeyaml.Util; 28 import org.yaml.snakeyaml.Yaml; 29 import org.yaml.snakeyaml.constructor.Constructor; 30 import org.yaml.snakeyaml.events.Event; 31 import org.yaml.snakeyaml.parser.Parser; 32 import org.yaml.snakeyaml.parser.ParserImpl; 33 import org.yaml.snakeyaml.reader.StreamReader; 34 import org.yaml.snakeyaml.reader.UnicodeReader; 35 36 public abstract class PyImportTest extends TestCase { 37 public static final String PATH = "pyyaml"; 38 load(String data)39 protected Object load(String data) { 40 Yaml yaml = new Yaml(); 41 return yaml.load(data); 42 } 43 load(Constructor loader, String data)44 protected Object load(Constructor loader, String data) { 45 Yaml yaml = new Yaml(loader); 46 return yaml.load(data); 47 } 48 loadAll(InputStream data)49 protected Iterable<Object> loadAll(InputStream data) { 50 Yaml yaml = new Yaml(); 51 return yaml.loadAll(data); 52 } 53 loadAll(String data)54 protected Iterable<Object> loadAll(String data) { 55 Yaml yaml = new Yaml(); 56 return yaml.loadAll(data); 57 } 58 loadAll(Constructor loader, String data)59 protected Iterable<Object> loadAll(Constructor loader, String data) { 60 Yaml yaml = new Yaml(loader); 61 return yaml.loadAll(data); 62 } 63 getResource(String theName)64 protected String getResource(String theName) { 65 String content; 66 content = Util.getLocalResource(PATH + File.separator + theName); 67 return content; 68 } 69 getStreamsByExtension(String extention)70 protected File[] getStreamsByExtension(String extention) { 71 return getStreamsByExtension(extention, false); 72 } 73 getStreamsByExtension(String extention, boolean onlyIfCanonicalPresent)74 protected File[] getStreamsByExtension(String extention, boolean onlyIfCanonicalPresent) { 75 File file = new File("src/test/resources/pyyaml"); 76 assertTrue("Folder not found: " + file.getAbsolutePath(), file.exists()); 77 assertTrue(file.isDirectory()); 78 return file.listFiles(new PyFilenameFilter(extention, onlyIfCanonicalPresent)); 79 } 80 getFileByName(String name)81 protected File getFileByName(String name) { 82 File file = new File("src/test/resources/pyyaml/" + name); 83 assertTrue("Folder not found: " + file.getAbsolutePath(), file.exists()); 84 assertTrue(file.isFile()); 85 return file; 86 } 87 canonicalParse(InputStream input2)88 protected List<Event> canonicalParse(InputStream input2) throws IOException { 89 StreamReader reader = new StreamReader(new UnicodeReader(input2)); 90 StringBuilder buffer = new StringBuilder(); 91 while (reader.peek() != '\0') { 92 buffer.append(reader.peek()); 93 reader.forward(); 94 } 95 CanonicalParser parser = new CanonicalParser(buffer.toString()); 96 List<Event> result = new ArrayList<Event>(); 97 while (parser.peekEvent() != null) { 98 result.add(parser.getEvent()); 99 } 100 input2.close(); 101 return result; 102 } 103 parse(InputStream input)104 protected List<Event> parse(InputStream input) throws IOException { 105 StreamReader reader = new StreamReader(new UnicodeReader(input)); 106 Parser parser = new ParserImpl(reader); 107 List<Event> result = new ArrayList<Event>(); 108 while (parser.peekEvent() != null) { 109 result.add(parser.getEvent()); 110 } 111 input.close(); 112 return result; 113 } 114 115 private class PyFilenameFilter implements FilenameFilter { 116 private String extension; 117 private boolean onlyIfCanonicalPresent; 118 PyFilenameFilter(String extension, boolean onlyIfCanonicalPresent)119 public PyFilenameFilter(String extension, boolean onlyIfCanonicalPresent) { 120 this.extension = extension; 121 this.onlyIfCanonicalPresent = onlyIfCanonicalPresent; 122 } 123 accept(File dir, String name)124 public boolean accept(File dir, String name) { 125 int position = name.lastIndexOf('.'); 126 String canonicalFileName = name.substring(0, position) + ".canonical"; 127 File canonicalFile = new File(dir, canonicalFileName); 128 if (onlyIfCanonicalPresent && !canonicalFile.exists()) { 129 return false; 130 } else { 131 return name.endsWith(extension); 132 } 133 } 134 } 135 } 136