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