001package org.apache.maven.doxia.module.twiki.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 java.io.StringReader;
023
024import org.apache.maven.doxia.util.ByLineReaderSource;
025import org.apache.maven.doxia.util.ByLineSource;
026import org.apache.maven.doxia.parser.ParseException;
027
028/**
029 * Tests the {@link org.apache.maven.doxia.module.twiki.parser.TableBlockParser}
030 *
031 * @author Juan F. Codagnone
032 * @since Nov 9, 2005
033 */
034public class TableTest
035    extends AbstractBlockTestCase
036{
037
038    /**
039     * unit test the regex
040     */
041    public final void testRegex()
042    {
043        assertTrue( tableParser.accept( "  | cell1 | cell2|   " ) );
044        assertFalse( tableParser.accept( "  | cell1 | cell" ) );
045    }
046
047    /**
048     * @throws ParseException on error
049     */
050    public final void testTable()
051        throws ParseException
052    {
053        final StringReader sw = new StringReader( "" + "  |cell1|cell2|  \n" + "|cell3|cell4|\n" );
054
055        final ByLineSource source = new ByLineReaderSource( sw );
056
057        Block block, expected;
058        expected =
059            new TableBlock( new Block[] {
060                new TableRowBlock( new Block[] { new TableCellBlock( new Block[] { new TextBlock( "cell1" ) } ),
061                    new TableCellBlock( new Block[] { new TextBlock( "cell2" ) } ) } ),
062                new TableRowBlock( new Block[] { new TableCellBlock( new Block[] { new TextBlock( "cell3" ) } ),
063                    new TableCellBlock( new Block[] { new TextBlock( "cell4" ) } ) } ) } );
064
065        block = tableParser.visit( source.getNextLine(), source );
066        assertEquals( block, expected );
067    }
068
069    /**
070     * @throws ParseException on error
071     */
072    public final void testTableHeader()
073        throws ParseException
074    {
075        final StringReader sw = new StringReader( "|*cell1*|*cell2*|\n" );
076
077        final ByLineSource source = new ByLineReaderSource( sw );
078
079        Block block, expected;
080        expected =
081            new TableBlock( new Block[] { new TableRowBlock( new Block[] {
082                new TableCellHeaderBlock( new Block[] { new TextBlock( "cell1" ) } ),
083                new TableCellHeaderBlock( new Block[] { new TextBlock( "cell2" ) } ) } ) } );
084
085        block = tableParser.visit( source.getNextLine(), source );
086        assertEquals( block, expected );
087    }
088}