001package org.apache.maven.doxia.parser; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.doxia.AbstractModuleTest; 023import org.apache.maven.doxia.sink.WellformednessCheckingSink; 024 025import org.apache.maven.doxia.sink.Sink; 026import org.apache.maven.doxia.sink.SinkEventAttributeSet; 027import org.apache.maven.doxia.sink.SinkEventElement; 028import org.apache.maven.doxia.sink.TextSink; 029import org.codehaus.plexus.util.IOUtil; 030import org.junit.Assert; 031 032import java.io.IOException; 033import java.io.Reader; 034import java.io.Writer; 035import java.util.Iterator; 036 037/** 038 * Test the parsing of sample input files. 039 * <br/> 040 * <b>Note</b>: you have to provide a sample "test." + outputExtension() 041 * file in the test resources directory if you extend this class. 042 * 043 * @version $Id$ 044 * @since 1.0 045 */ 046public abstract class AbstractParserTest 047 extends AbstractModuleTest 048{ 049 /** 050 * Create a new instance of the parser to test. 051 * 052 * @return the parser to test. 053 */ 054 protected abstract Parser createParser(); 055 056 /** 057 * Returns the directory where all parser test output will go. 058 * 059 * @return The test output directory. 060 */ 061 protected String getOutputDir() 062 { 063 return "parser/"; 064 } 065 066 /** 067 * Parse a test document '"test." + outputExtension()' 068 * with parser from {@link #createParser()}, and output to a new 069 * {@link WellformednessCheckingSink}. Asserts that output is well-formed. 070 * 071 * @throws IOException if the test document cannot be read. 072 * @throws ParseException if the test document cannot be parsed. 073 */ 074 public final void testParser() 075 throws IOException, ParseException 076 { 077 WellformednessCheckingSink sink = new WellformednessCheckingSink(); 078 079 Reader reader = null; 080 try 081 { 082 reader = getTestReader( "test", outputExtension() ); 083 084 createParser().parse( reader, sink ); 085 086 assertTrue( "Parser output not well-formed, last offending element: " 087 + sink.getOffender(), sink.isWellformed() ); 088 } 089 finally 090 { 091 IOUtil.close( reader ); 092 } 093 } 094 095 /** 096 * Parse a test document '"test." + outputExtension()' 097 * with parser from {@link #createParser()}, and output to a text file, 098 * using the {@link org.apache.maven.doxia.sink.TextSink TextSink}. 099 * 100 * @throws IOException if the test document cannot be read. 101 * @throws ParseException if the test document cannot be parsed. 102 */ 103 public final void testDocument() 104 throws IOException, ParseException 105 { 106 Writer writer = null; 107 Reader reader = null; 108 109 try 110 { 111 writer = getTestWriter( "test", "txt" ); 112 113 reader = getTestReader( "test", outputExtension() ); 114 115 Sink sink = new TextSink( writer ); 116 117 createParser().parse( reader, sink ); 118 } 119 finally 120 { 121 IOUtil.close( reader ); 122 IOUtil.close( writer ); 123 } 124 } 125 126 protected void assertEquals( SinkEventElement element, String name, Object... args ) 127 { 128 assertEquals( "Name of element doesn't match", name, element.getName() ); 129 Assert.assertArrayEquals( "Arguments don't match", args, element.getArgs() ); 130 } 131 132 protected void assertAttributeEquals( SinkEventElement element, String name, String attr, String value ) 133 { 134 assertEquals( name, element.getName() ); 135 SinkEventAttributeSet atts = (SinkEventAttributeSet) element.getArgs()[0]; 136 assertEquals( value, atts.getAttribute( attr ) ); 137 } 138 139 protected void assertEquals( Iterator<SinkEventElement> it, String... names ) 140 { 141 StringBuilder expected = new StringBuilder(); 142 StringBuilder actual = new StringBuilder(); 143 144 for ( String name : names ) 145 { 146 expected.append( name ).append( '\n' ); 147 if ( it.hasNext() ) 148 { 149 actual.append( it.next().getName() ).append( '\n' ); 150 } 151 } 152 assertEquals( expected.toString(), actual.toString() ); 153 } 154 155 156}