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;
027import org.apache.maven.doxia.sink.Sink;
028
029/**
030 * Units tests for Lists
031 *
032 * @author Juan F. Codagnone
033 * @since Nov 1, 2005
034 */
035public class ListTest
036    extends AbstractBlockTestCase
037{
038
039    /**
040     * unit test for recurrent enumeration
041     *
042     * @throws ParseException on error
043     */
044    public final void testList()
045        throws ParseException
046    {
047        final BlockParser parser = listParser;
048
049        final String text =
050            "" + "      * item1.1 \n" + "   * item2\n" + "      * item2.1\n" + "   * item3\n"
051                + "      * item3.1\n" + "      * item3.2\n" + "         * item3.2.1\n" + "         * item3.2.2\n"
052                + "         * item3.2.3\n" + "      * item3.3\n" + "         * item3.3.1\n" + "   * item4";
053
054        final ByLineSource source = new ByLineReaderSource( new StringReader( text ) );
055        final Block b = parser.visit( source.getNextLine(), source );
056        final Block[] firstLevelBlocks = ( (UnorderedListBlock) b ).getBlocks();
057        final int numberOfChild = 4;
058        assertEquals( numberOfChild, firstLevelBlocks.length );
059
060        for ( int i = 0; i < firstLevelBlocks.length; i++ )
061        {
062            Block block = firstLevelBlocks[i];
063            assertEquals( ListItemBlock.class, block.getClass() );
064        }
065
066        ListBlock list;
067        ListItemBlock item;
068        Block[] blocks;
069
070        item = (ListItemBlock) firstLevelBlocks[1];
071        blocks = item.getBlocks();
072        assertEquals( 1, blocks.length );
073        assertEquals( "item2", ( (TextBlock) blocks[0] ).getText() );
074        list = item.getInnerList();
075        assertNotNull( list );
076        blocks = list.getBlocks();
077        assertEquals( blocks.length, 1 );
078        item = (ListItemBlock) blocks[0];
079        assertEquals( 1, item.getBlocks().length );
080        assertEquals( "item2.1", ( (TextBlock) item.getBlocks()[0] ).getText() );
081    }
082
083    /**
084     * @throws ParseException on error
085     */
086    public final void testNumeringDecimal()
087        throws ParseException
088    {
089        final String text = "" + "   1. item1\n" + "   1. item2\n" + "   1. item3";
090
091        final ByLineSource source = new ByLineReaderSource( new StringReader( text ) );
092        Block blocks, expected;
093        expected =
094            new NumeratedListBlock( Sink.NUMBERING_DECIMAL, new ListItemBlock[] {
095                new ListItemBlock( new Block[] { new TextBlock( "item1" ) } ),
096                new ListItemBlock( new Block[] { new TextBlock( "item2" ) } ),
097                new ListItemBlock( new Block[] { new TextBlock( "item3" ) } ) } );
098        blocks = listParser.visit( source.getNextLine(), source );
099        assertEquals( expected, blocks );
100    }
101
102    /**
103     * @throws ParseException on error
104     */
105    public final void testHetero()
106        throws ParseException
107    {
108        final String text =
109            "" + "   A. item1\n" + "      * item1.1\n" + "      * item1.2\n" + "   B. item2\n"
110                + "      i. item2.1\n" + "      i. item2.2\n" + "   C. item3";
111
112        final ByLineSource source = new ByLineReaderSource( new StringReader( text ) );
113        Block blocks, expected;
114        expected =
115            new NumeratedListBlock( Sink.NUMBERING_UPPER_ALPHA, new ListItemBlock[] {
116                new ListItemBlock( new Block[] { new TextBlock( "item1" ) },
117                                   new UnorderedListBlock( new ListItemBlock[] {
118                                       new ListItemBlock( new Block[] { new TextBlock( "item1.1" ) } ),
119                                       new ListItemBlock( new Block[] { new TextBlock( "item1.2" ) } ) } ) ),
120                new ListItemBlock( new Block[] { new TextBlock( "item2" ) },
121                                   new NumeratedListBlock( Sink.NUMBERING_LOWER_ROMAN, new ListItemBlock[] {
122                                       new ListItemBlock( new Block[] { new TextBlock( "item2.1" ) } ),
123                                       new ListItemBlock( new Block[] { new TextBlock( "item2.2" ) } ) } ) ),
124                new ListItemBlock( new Block[] { new TextBlock( "item3" ) } ) } );
125        blocks = listParser.visit( source.getNextLine(), source );
126        assertEquals( expected, blocks );
127    }
128}