View Javadoc

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