001package org.apache.maven.doxia.module.itext;
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.FileInputStream;
024import java.io.FileOutputStream;
025import java.io.Writer;
026
027import java.net.URL;
028import java.net.URLClassLoader;
029
030import org.apache.maven.doxia.AbstractModuleTest;
031import org.apache.maven.doxia.sink.Sink;
032import org.apache.maven.doxia.sink.impl.SinkTestDocument;
033
034/**
035 * <code>iText Sink</code> Test case.
036 *
037 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
038 * @version $Id$
039 */
040public class ITextSinkTestCase
041    extends AbstractModuleTest
042{
043    /** {@inheritDoc} */
044    protected String outputExtension()
045    {
046        return "xml";
047    }
048
049    /** {@inheritDoc} */
050    protected String getOutputDir()
051    {
052        return "sink/";
053    }
054
055    /**
056     * Convenience method
057     *
058     * @param prefix
059     * @param suffix
060     * @return the input file
061     */
062    protected File getGeneratedFile( String prefix, String suffix )
063    {
064        File outputDirectory = new File( getBasedir(), outputBaseDir() + getOutputDir() );
065        if ( !outputDirectory.exists() )
066        {
067            outputDirectory.mkdirs();
068        }
069
070        return new File( outputDirectory, prefix + "." + suffix );
071    }
072
073    protected Sink createSink( Writer writer )
074    {
075        ITextSink sink = new ITextSink( writer );
076
077        sink.setClassLoader( new URLClassLoader(
078            new URL[] { ITextSinkTestCase.class.getResource( "/images/" ) } ) );
079
080        return sink;
081    }
082
083    /**
084     * Test PDF generation
085     *
086     * @throws Exception
087     */
088    public void testGeneratingPDFFromITextXml()
089        throws Exception
090    {
091        File f = new File( getBasedir(), "src/test/resources/itext/itext.xml" );
092
093        ITextUtil.writePdf( new FileInputStream( f ),
094                            new FileOutputStream( getGeneratedFile( "test_itext", "pdf" ) ) );
095    }
096
097    /**
098     * Generate a pdf and a rtf from the standart test model.
099     *
100     * @throws Exception if any
101     */
102    public void testModel()
103        throws Exception
104    {
105        Sink sink = createSink( getXmlTestWriter( "test_model", "xml" ) );
106
107        SinkTestDocument.generate( sink );
108
109        sink.close();
110
111        ITextUtil.writePdf( new FileInputStream( getGeneratedFile( "test_model", "xml" ) ),
112                            new FileOutputStream( getGeneratedFile( "test_model", "pdf" ) ) );
113        ITextUtil.writeRtf( new FileInputStream( getGeneratedFile( "test_model", "xml" ) ),
114                            new FileOutputStream( getGeneratedFile( "test_model", "rtf" ) ) );
115    }
116
117    /**
118     * Test empty anchor DOXIA-329
119     * @throws Exception if any
120     */
121    public void testEmptyAnchor()
122        throws Exception
123    {
124        Sink sink = createSink( getXmlTestWriter( "empty_anchor", "xml" ) );
125
126        SinkTestDocument.generateHead( sink );
127
128        sink.body();
129
130        sink.anchor( "empty_local_anchor" );
131        sink.anchor_();
132        sink.lineBreak();
133        sink.link( "#empty_local_anchor" );
134        sink.text( "link to empty local anchor" );
135        sink.link_();
136
137        sink.lineBreak();
138
139        sink.anchor( "defined_local_anchor" );
140        sink.text( "defined local anchor" );
141        sink.anchor_();
142        sink.lineBreak();
143        sink.link( "#defined_local_anchor" );
144        sink.text( "link to defined local anchor" );
145        sink.link_();
146
147        sink.body_();
148
149        sink.flush();
150        sink.close();
151
152        ITextUtil.writePdf( new FileInputStream( getGeneratedFile( "empty_anchor", "xml" ) ),
153                            new FileOutputStream( getGeneratedFile( "empty_anchor", "pdf" ) ) );
154    }
155}