View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.doxia.parser;
20  
21  import java.io.IOException;
22  import java.io.Reader;
23  import java.io.Writer;
24  import java.util.Iterator;
25  
26  import org.apache.maven.doxia.AbstractModuleTest;
27  import org.apache.maven.doxia.sink.Sink;
28  import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
29  import org.apache.maven.doxia.sink.impl.SinkEventElement;
30  import org.apache.maven.doxia.sink.impl.TextSink;
31  import org.apache.maven.doxia.sink.impl.WellformednessCheckingSink;
32  import org.junit.jupiter.api.Assertions;
33  import org.junit.jupiter.api.Test;
34  
35  import static org.junit.jupiter.api.Assertions.assertTrue;
36  
37  /**
38   * Test the parsing of sample input files.
39   * <br>
40   * <b>Note</b>: you have to provide a sample "test." + outputExtension()
41   * file in the test resources directory if you extend this class.
42   * @since 1.0
43   */
44  public abstract class AbstractParserTest extends AbstractModuleTest {
45      /**
46       * Create a new instance of the parser to test.
47       *
48       * @return the parser to test.
49       */
50      protected abstract Parser createParser();
51  
52      /**
53       * Returns the directory where all parser test output will go.
54       *
55       * @return The test output directory.
56       */
57      protected String getOutputDir() {
58          return "parser/";
59      }
60  
61      /**
62       * Parse a test document '"test." + outputExtension()'
63       * with parser from {@link #createParser()}, and output to a new
64       * {@link WellformednessCheckingSink}. Asserts that output is well-formed.
65       *
66       * @throws IOException if the test document cannot be read.
67       * @throws ParseException if the test document cannot be parsed.
68       */
69      @Test
70      public final void testParser() throws IOException, ParseException {
71          WellformednessCheckingSink sink = new WellformednessCheckingSink();
72  
73          try (Reader reader = getTestReader("test", outputExtension())) {
74              createParser().parse(reader, sink);
75  
76              assertTrue(
77                      sink.isWellformed(),
78                      "Parser output not well-formed, last offending element: " + sink.getOffender());
79          }
80      }
81  
82      /**
83       * Parse a test document '"test." + outputExtension()'
84       * with parser from {@link #createParser()}, and output to a text file,
85       * using the {@link org.apache.maven.doxia.sink.impl.TextSink TextSink}.
86       *
87       * @throws IOException if the test document cannot be read.
88       * @throws ParseException if the test document cannot be parsed.
89       */
90      @Test
91      public final void testDocument() throws IOException, ParseException {
92          try (Writer writer = getTestWriter("test", "txt");
93                  Reader reader = getTestReader("test", outputExtension())) {
94              Sink sink = new TextSink(writer);
95              createParser().parse(reader, sink);
96          }
97      }
98  
99      protected static void assertSinkEquals(SinkEventElement element, String name, Object... args) {
100         Assertions.assertEquals(name, element.getName(), "Name of element doesn't match");
101         Assertions.assertArrayEquals(args, element.getArgs(), "Arguments don't match");
102     }
103 
104     protected static void assertSinkAttributeEquals(SinkEventElement element, String name, String attr, String value) {
105         Assertions.assertEquals(name, element.getName());
106         SinkEventAttributeSet atts = (SinkEventAttributeSet) element.getArgs()[0];
107         Assertions.assertEquals(value, atts.getAttribute(attr));
108     }
109 
110     protected static void assertSinkEquals(Iterator<SinkEventElement> it, String... names) {
111         StringBuilder expected = new StringBuilder();
112         StringBuilder actual = new StringBuilder();
113 
114         for (String name : names) {
115             expected.append(name).append('\n');
116         }
117 
118         while (it.hasNext()) {
119             actual.append(it.next().getName()).append('\n');
120         }
121 
122         Assertions.assertEquals(expected.toString(), actual.toString());
123     }
124 
125     protected static void assertSinkStartsWith(Iterator<SinkEventElement> it, String... names) {
126         StringBuilder expected = new StringBuilder();
127         StringBuilder actual = new StringBuilder();
128 
129         for (String name : names) {
130             expected.append(name).append('\n');
131             if (it.hasNext()) {
132                 actual.append(it.next().getName()).append('\n');
133             }
134         }
135         Assertions.assertEquals(expected.toString(), actual.toString());
136     }
137 }