001package org.apache.maven.doxia.sink; 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 java.io.File; 023import java.io.FileWriter; 024import java.io.InputStream; 025import java.io.InputStreamReader; 026import java.io.Reader; 027import java.io.Writer; 028 029import org.apache.maven.doxia.parser.Parser; 030import org.codehaus.plexus.PlexusTestCase; 031 032/** 033 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a> 034 * @version $Id$ 035 * @since 1.0 036 */ 037public abstract class AbstractSinkTestCase 038 extends PlexusTestCase 039{ 040 private Writer testWriter; 041 042 // --------------------------------------------------------------------- 043 // Test case 044 // ---------------------------------------------------------------------- 045 046 /** 047 * Parses the test apt document (obtained via {@link #getTestReader()}) with the Parser returned 048 * by {@link #createParser()} into the Sink returned by {@link #createSink()}. 049 * 050 * @throws java.lang.Exception if anything goes wrong. 051 */ 052 public void testApt() 053 throws Exception 054 { 055 Parser parser = createParser(); 056 057 parser.parse( getTestReader(), createSink() ); 058 } 059 060 // ---------------------------------------------------------------------- 061 // Abstract methods the individual SinkTests must provide 062 // ---------------------------------------------------------------------- 063 064 /** 065 * Return the default extension of files created by the test Sink. 066 * 067 * @return the extension of files created by the test Sink. 068 * @see #createSink() 069 */ 070 protected abstract String outputExtension(); 071 072 /** 073 * Return a Parser for testing. 074 * 075 * @return a test Parser. 076 */ 077 protected abstract Parser createParser(); 078 079 /** 080 * Return a Sink for testing. 081 * 082 * @return a test Sink. 083 * @throws java.lang.Exception if the Sink cannot be constructed. 084 */ 085 protected abstract Sink createSink() 086 throws Exception; 087 088 // ---------------------------------------------------------------------- 089 // Methods for creating the test reader and writer 090 // ---------------------------------------------------------------------- 091 092 /** 093 * Returns a Writer to write a test output result. The Writer writes to a File 094 * <code>"target/output/test. + extension"</code>, where extension is returned by 095 * {@link #outputExtension()}, in the current base directory. 096 * 097 * @return a Writer to write a test output result. 098 * @throws java.lang.Exception if the Writer cannot be constructed. 099 */ 100 protected Writer getTestWriter() 101 throws Exception 102 { 103 if ( testWriter == null ) 104 { 105 File outputDirectory = new File( getBasedirFile(), "target/output" ); 106 107 if ( !outputDirectory.exists() ) 108 { 109 outputDirectory.mkdirs(); 110 } 111 112 testWriter = new FileWriter( new File( outputDirectory, "test." + outputExtension() ) ); 113 } 114 115 return testWriter; 116 } 117 118 /** 119 * Returns a Reader that gives access to a common test apt file. 120 * 121 * @return a Reader to access the test apt resource file. 122 * @throws java.lang.Exception if the Reader cannot be constructed. 123 */ 124 protected Reader getTestReader() 125 throws Exception 126 { 127 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( "test.apt" ); 128 129 InputStreamReader reader = new InputStreamReader( is ); 130 131 return reader; 132 } 133 134 // ---------------------------------------------------------------------- 135 // Utility methods 136 // ---------------------------------------------------------------------- 137 138 /** 139 * Return the current base diretory as a File. 140 * 141 * @return the current base diretory as a File. 142 */ 143 public File getBasedirFile() 144 { 145 return new File( getBasedir() ); 146 } 147}