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