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.FileInputStream;
    21  import java.io.IOException;
    22  import java.io.InputStream;
    23  import java.io.StringWriter;
    24  import java.util.ArrayList;
    25  import java.util.Arrays;
    26  import java.util.Iterator;
    27  import java.util.List;
    28
    29  import org.yaml.snakeyaml.DumperOptions;
    30  import org.yaml.snakeyaml.emitter.Emitter;
    31  import org.yaml.snakeyaml.emitter.EventConstructor;
    32  import org.yaml.snakeyaml.events.CollectionStartEvent;
    33  import org.yaml.snakeyaml.events.Event;
    34  import org.yaml.snakeyaml.events.MappingStartEvent;
    35  import org.yaml.snakeyaml.events.NodeEvent;
    36  import org.yaml.snakeyaml.events.ScalarEvent;
    37  import org.yaml.snakeyaml.events.SequenceStartEvent;
    38  import org.yaml.snakeyaml.parser.Parser;
    39  import org.yaml.snakeyaml.parser.ParserImpl;
    40  import org.yaml.snakeyaml.reader.StreamReader;
    41  import org.yaml.snakeyaml.reader.UnicodeReader;
    42
    43  /**
    44   * @see imported from PyYAML
    45   */
    46  public class PyEmitterTest extends PyImportTest {
    47      public void testEmitterOnData() throws IOException {
    48          _testEmitter(".data", false);
    49      }
    50
    51      public void testEmitterOnCanonicalNormally() throws IOException {
    52          _testEmitter(".canonical", false);
    53      }
    54
    55      public void testEmitterOnCanonicalCanonically() throws IOException {
    56          _testEmitter(".canonical", true);
    57      }
    58
    59      private void _testEmitter(String mask, boolean canonical) throws IOException {
    60          File[] files = getStreamsByExtension(mask, true);
    61          assertTrue("No test files found.", files.length > 0);
    62          for (File file : files) {
    63              // if (!file.getName().contains("spec-06-01.canonical")) {
    64              // continue;
    65              // }
    66              try {
    67                  InputStream input = new FileInputStream(file);
    68                  List<Event> events = parse(input);
    69                  input.close();
    70                  //
    71                  StringWriter stream = new StringWriter();
    72                  DumperOptions options = new DumperOptions();
    73                  options.setCanonical(canonical);
    74                  Emitter emitter = new Emitter(stream, options);
    75                  for (Event event : events) {
    76                      emitter.emit(event);
    77                  }
    78                  //
    79                  String data = stream.toString();
    80                  List<Event> newEvents = new ArrayList<Event>();
    81                  StreamReader reader = new StreamReader(data);
    82                  Parser parser = new ParserImpl(reader);
    83                  while (parser.peekEvent() != null) {
    84                      Event event = parser.getEvent();
    85                      newEvents.add(event);
    86                  }
    87                  // check
    88                  assertEquals(events.size(), newEvents.size());
    89                  Iterator<Event> iter1 = events.iterator();
    90                  Iterator<Event> iter2 = newEvents.iterator();
    91                  while (iter1.hasNext()) {
    92                      Event event = iter1.next();
    93                      Event newEvent = iter2.next();
    94                      assertEquals(event.getClass().getName(), newEvent.getClass().getName());
    95                      if (event instanceof NodeEvent) {
    96                          NodeEvent e1 = (NodeEvent) event;
    97                          NodeEvent e2 = (NodeEvent) newEvent;
    98                          assertEquals(e1.getAnchor(), e2.getAnchor());
    99                      }
    100                     if (event instanceof CollectionStartEvent) {
    101                         CollectionStartEvent e1 = (CollectionStartEvent) event;
    102                         CollectionStartEvent e2 = (CollectionStartEvent) newEvent;
    103                         assertEquals(e1.getTag(), e2.getTag());
    104                     }
    105                     if (event instanceof ScalarEvent) {
    106                         ScalarEvent e1 = (ScalarEvent) event;
    107                         ScalarEvent e2 = (ScalarEvent) newEvent;
    108                         if (e1.getImplicit().bothFalse() && e2.getImplicit().bothFalse()) {
    109                             assertEquals(e1.getTag(), e2.getTag());
    110                         }
    111                         assertEquals(e1.getValue(), e2.getValue());
    112                     }
    113                 }
    114             } catch (Exception e) {
    115                 System.out.println("Failed File: " + file);
    116                 // fail("Failed File: " + file + "; " + e.getMessage());
    117                 throw new RuntimeException(e);
    118             }
    119         }
    120     }
    121
    122     public void testEmitterStyles() throws IOException {
    123         File[] canonicalFiles = getStreamsByExtension(".canonical", false);
    124         assertTrue("No test files found.", canonicalFiles.length > 0);
    125         File[] dataFiles = getStreamsByExtension(".data", true);
    126         assertTrue("No test files found.", dataFiles.length > 0);
    127         List<File> allFiles = new ArrayList<File>(Arrays.asList(canonicalFiles));
    128         allFiles.addAll(Arrays.asList(dataFiles));
    129         for (File file : allFiles) {
    130             try {
    131                 List<Event> events = new ArrayList<Event>();
    132                 InputStream input = new FileInputStream(file);
    133                 StreamReader reader = new StreamReader(new UnicodeReader(input));
    134                 Parser parser = new ParserImpl(reader);
    135                 while (parser.peekEvent() != null) {
    136                     Event event = parser.getEvent();
    137                     events.add(event);
    138                 }
    139                 input.close();
    140                 //
    141                 for (Boolean flowStyle : new Boolean[] { Boolean.FALSE, Boolean.TRUE }) {
    142                     for (DumperOptions.ScalarStyle style : DumperOptions.ScalarStyle.values()) {
    143                         List<Event> styledEvents = new ArrayList<Event>();
    144                         for (Event event : events) {
    145                             if (event instanceof ScalarEvent) {
    146                                 ScalarEvent scalar = (ScalarEvent) event;
    147                                 event = new ScalarEvent(scalar.getAnchor(), scalar.getTag(),
    148                                         scalar.getImplicit(), scalar.getValue(),
    149                                         scalar.getStartMark(), scalar.getEndMark(), style.getChar());
    150                             } else if (event instanceof SequenceStartEvent) {
    151                                 SequenceStartEvent seqStart = (SequenceStartEvent) event;
    152                                 event = new SequenceStartEvent(seqStart.getAnchor(),
    153                                         seqStart.getTag(), seqStart.getImplicit(),
    154                                         seqStart.getStartMark(), seqStart.getEndMark(), flowStyle);
    155                             } else if (event instanceof MappingStartEvent) {
    156                                 MappingStartEvent mapStart = (MappingStartEvent) event;
    157                                 event = new MappingStartEvent(mapStart.getAnchor(),
    158                                         mapStart.getTag(), mapStart.getImplicit(),
    159                                         mapStart.getStartMark(), mapStart.getEndMark(), flowStyle);
    160                             }
    161                             styledEvents.add(event);
    162                         }
    163                         // emit
    164                         String data = emit(styledEvents);
    165                         List<Event> newEvents = parse(data);
    166                         assertEquals("Events must not change. File: " + file, events.size(),
    167                                 newEvents.size());
    168                         Iterator<Event> oldIter = events.iterator();
    169                         Iterator<Event> newIter = newEvents.iterator();
    170                         while (oldIter.hasNext()) {
    171                             Event event = oldIter.next();
    172                             Event newEvent = newIter.next();
    173                             assertEquals(event.getClass(), newEvent.getClass());
    174                             if (event instanceof NodeEvent) {
    175                                 assertEquals(((NodeEvent) event).getAnchor(),
    176                                         ((NodeEvent) newEvent).getAnchor());
    177                             }
    178                             if (event instanceof CollectionStartEvent) {
    179                                 assertEquals(((CollectionStartEvent) event).getTag(),
    180                                         ((CollectionStartEvent) newEvent).getTag());
    181                             }
    182                             if (event instanceof ScalarEvent) {
    183                                 ScalarEvent scalarOld = (ScalarEvent) event;
    184                                 ScalarEvent scalarNew = (ScalarEvent) newEvent;
    185                                 if (scalarOld.getImplicit().bothFalse()
    186                                         && scalarNew.getImplicit().bothFalse()) {
    187                                     assertEquals(scalarOld.getTag(), scalarNew.getTag());
    188                                 }
    189                                 assertEquals(scalarOld.getValue(), scalarNew.getValue());
    190                             }
    191                         }
    192                     }
    193                 }
    194
    195             } catch (Exception e) {
    196                 System.out.println("Failed File: " + file);
    197                 // fail("Failed File: " + file + "; " + e.getMessage());
    198                 throw new RuntimeException(e);
    199             }
    200         }
    201     }
    202
    203     private String emit(List<Event> events) throws IOException {
    204         StringWriter writer = new StringWriter();
    205         Emitter emitter = new Emitter(writer, new DumperOptions());
    206         for (Event event : events) {
    207             emitter.emit(event);
    208         }
    209         return writer.toString();
    210     }
    211
    212     private List<Event> parse(String data) {
    213         ParserImpl parser = new ParserImpl(new StreamReader(data));
    214         List<Event> newEvents = new ArrayList<Event>();
    215         while (parser.peekEvent() != null) {
    216             newEvents.add(parser.getEvent());
    217         }
    218         return newEvents;
    219     }
    220
    221     @SuppressWarnings("unchecked")
    222     public void testEmitterEvents() throws IOException {
    223         File[] files = getStreamsByExtension(".events", false);
    224         assertTrue("No test files found.", files.length > 0);
    225         for (File file : files) {
    226             // if (!file.getName().contains("spec-06-01.canonical")) {
    227             // continue;
    228             // }
    229             try {
    230                 List<Event> events = new ArrayList<Event>();
    231                 String content = getResource(file.getName());
    232                 events = (List<Event>) load(new EventConstructor(), content);
    233                 //
    234                 StringWriter stream = new StringWriter();
    235                 Emitter emitter = new Emitter(stream, new DumperOptions());
    236                 for (Event event : events) {
    237                     emitter.emit(event);
    238                 }
    239                 //
    240                 String data = stream.toString();
    241                 List<Event> newEvents = new ArrayList<Event>();
    242                 StreamReader reader = new StreamReader(data);
    243                 Parser parser = new ParserImpl(reader);
    244                 while (parser.peekEvent() != null) {
    245                     Event event = parser.getEvent();
    246                     newEvents.add(event);
    247                 }
    248                 // check
    249                 assertEquals(events.size(), newEvents.size());
    250                 Iterator<Event> iter1 = events.iterator();
    251                 Iterator<Event> iter2 = newEvents.iterator();
    252                 while (iter1.hasNext()) {
    253                     Event event = iter1.next();
    254                     Event newEvent = iter2.next();
    255                     assertEquals(event.getClass().getName(), newEvent.getClass().getName());
    256                     if (event instanceof NodeEvent) {
    257                         NodeEvent e1 = (NodeEvent) event;
    258                         NodeEvent e2 = (NodeEvent) newEvent;
    259                         assertEquals(e1.getAnchor(), e2.getAnchor());
    260                     }
    261                     if (event instanceof CollectionStartEvent) {
    262                         CollectionStartEvent e1 = (CollectionStartEvent) event;
    263                         CollectionStartEvent e2 = (CollectionStartEvent) newEvent;
    264                         assertEquals(e1.getTag(), e2.getTag());
    265                     }
    266                     if (event instanceof ScalarEvent) {
    267                         ScalarEvent e1 = (ScalarEvent) event;
    268                         ScalarEvent e2 = (ScalarEvent) newEvent;
    269                         if (e1.getImplicit().canOmitTagInPlainScalar() == e2.getImplicit().canOmitTagInPlainScalar()
    270                                 && e1.getImplicit().canOmitTagInNonPlainScalar() == e2.getImplicit().canOmitTagInNonPlainScalar()) {
    271
    272                         } else {
    273                             if ((e1.getTag() == null || e2.getTag() == null)
    274                                     || e1.getTag().equals(e2.getTag())) {
    275                             } else {
    276                                 System.out.println("tag1: " + e1.getTag());
    277                                 System.out.println("tag2: " + e2.getTag());
    278                                 fail("in file: " + file);
    279                             }
    280                         }
    281                         assertEquals(e1.getValue(), e2.getValue());
    282                     }
    283                 }
    284             } catch (Exception e) {
    285                 System.out.println("Failed File: " + file);
    286                 // fail("Failed File: " + file + "; " + e.getMessage());
    287                 throw new RuntimeException(e);
    288             }
    289         }
    290     }
    291 }