001package org.apache.maven.doxia.module.xdoc;
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.Writer;
023
024import org.apache.maven.doxia.module.AbstractIdentityTest;
025import org.apache.maven.doxia.parser.Parser;
026import org.apache.maven.doxia.sink.Sink;
027import org.codehaus.plexus.util.StringUtils;
028
029/**
030 * Check that piping a full model through an XdocParser and an XdocSink
031 * leaves the model unchanged.
032 */
033public class XdocIdentityTest
034    extends AbstractIdentityTest
035{
036    /** {@inheritDoc} */
037    protected void setUp()
038        throws Exception
039    {
040        super.setUp();
041        assertIdentity( true );
042    }
043
044    /** {@inheritDoc} */
045    protected Sink createSink( Writer writer )
046    {
047        XdocSink sink = new XdocSink( writer );
048        sink.setInsertNewline( false );
049        return sink;
050    }
051
052    /** {@inheritDoc} */
053    protected Parser createParser()
054    {
055        return new XdocParser();
056    }
057
058    /** {@inheritDoc} */
059    protected String getExpected()
060    {
061        // DOXIA-177
062        String expected = super.getExpected();
063
064        String startCaption = "begin:tableCaption";
065        String endCaption = "end:tableCaption";
066
067        int iStartCaption = expected.indexOf( startCaption );
068        int iEndCaption = expected.indexOf( endCaption ) + endCaption.length();
069
070        String captionTag = expected.substring( iStartCaption, iEndCaption ) + EOL + EOL + EOL;
071        expected = StringUtils.replace( expected, captionTag, "" );
072
073        int iStartTableRows =
074            expected.substring( 0, iStartCaption ).lastIndexOf( "begin:tableRows" ) + "begin:tableRows".length();
075
076        StringBuilder text = new StringBuilder();
077        text.append( expected.substring( 0, iStartTableRows ) );
078        text.append( EOL + EOL + EOL );
079        text.append( captionTag.subSequence( 0, captionTag.indexOf( "end:tableCaption" )
080            + "end:tableCaption".length() ) );
081        text.append( expected.substring( iStartTableRows ) );
082
083        return text.toString();
084    }
085}