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; 023import java.util.Arrays; 024 025import org.apache.maven.doxia.util.ByLineReaderSource; 026import org.apache.maven.doxia.parser.ParseException; 027 028/** 029 * Tests the {@link org.apache.maven.doxia.module.twiki.parser.ParagraphBlockParser} 030 * 031 * @author Juan F. Codagnone 032 * @since Nov 1, 2005 033 */ 034public class ParagraphTest 035 extends AbstractBlockTestCase 036{ 037 038 /** 039 * @throws ParseException on error 040 */ 041 public final void testMultiLines() 042 throws ParseException 043 { 044 final String text = 045 "" + "\n\n\n" + "para1 -> text1\n" + "para1 -> text2\n" + "\n" + "para2 -> text1\n" 046 + "para2 -> text2\n" + " \n \n " + "para2 -> text1\n" + "para2 -> text2\n"; 047 048 final ByLineReaderSource source = new ByLineReaderSource( new StringReader( text ) ); 049 final ParagraphBlockParser parser = paraParser; 050 051 ParagraphBlock block; 052 053 block = (ParagraphBlock) parser.visit( source.getNextLine(), source ); 054 assertNotNull( block ); 055 assertEquals( 1, block.getBlocks().length ); 056 assertEquals( "para1 -> text1 para1 -> text2", ( (TextBlock) block.getBlocks()[0] ).getText() ); 057 058 block = (ParagraphBlock) parser.visit( source.getNextLine(), source ); 059 assertNotNull( block ); 060 assertEquals( 1, block.getBlocks().length ); 061 assertEquals( "para2 -> text1 para2 -> text2", ( (TextBlock) block.getBlocks()[0] ).getText() ); 062 } 063 064 /** 065 * @throws ParseException on error 066 */ 067 public final void testParagraphWithList() 068 throws ParseException 069 { 070 final String text = 071 "" + "Description text:\n" + " * item1\n" + " * item2\n" 072 + "This is more text in the same paragraph\n" + "\n" + "Another paragraph"; 073 074 final ByLineReaderSource source = new ByLineReaderSource( new StringReader( text ) ); 075 final ParagraphBlockParser parser = paraParser; 076 077 ParagraphBlock block; 078 079 block = (ParagraphBlock) parser.visit( source.getNextLine(), source ); 080 assertNotNull( block ); 081 final Block[] firstLevelChilds = block.getBlocks(); 082 final int numberOfChilds = 3; 083 assertEquals( numberOfChilds, firstLevelChilds.length ); 084 assertEquals( TextBlock.class, firstLevelChilds[0].getClass() ); 085 assertEquals( UnorderedListBlock.class, firstLevelChilds[1].getClass() ); 086 assertEquals( TextBlock.class, firstLevelChilds[2].getClass() ); 087 088 final Block[] listChilds = ( (UnorderedListBlock) firstLevelChilds[1] ).getBlocks(); 089 assertEquals( 2, listChilds.length ); 090 assertEquals( 1, ( (ListItemBlock) listChilds[0] ).getBlocks().length ); 091 assertEquals( "item1", ( (TextBlock) ( (ListItemBlock) listChilds[0] ).getBlocks()[0] ).getText() ); 092 assertEquals( "item2", ( (TextBlock) ( (ListItemBlock) listChilds[1] ).getBlocks()[0] ).getText() ); 093 } 094 095 /** 096 * tests some valid weired lists 097 * 098 * @throws ParseException on error 099 */ 100 public final void testParagraphWithStartingList() 101 throws ParseException 102 { 103 final String text = 104 "" + " * item1\n" + " * item2\n" + "This is more text in the same paragraph\n" + "\n" 105 + "Another paragraph"; 106 107 final ByLineReaderSource source = new ByLineReaderSource( new StringReader( text ) ); 108 final ParagraphBlockParser parser = paraParser; 109 110 ParagraphBlock block; 111 112 block = (ParagraphBlock) parser.visit( source.getNextLine(), source ); 113 assertNotNull( block ); 114 final Block[] firstLevelChilds = block.getBlocks(); 115 assertEquals( 2, firstLevelChilds.length ); 116 assertEquals( UnorderedListBlock.class, firstLevelChilds[0].getClass() ); 117 assertEquals( TextBlock.class, firstLevelChilds[1].getClass() ); 118 119 final Block[] listChilds = ( (UnorderedListBlock) firstLevelChilds[0] ).getBlocks(); 120 assertEquals( 2, listChilds.length ); 121 assertEquals( 1, ( (ListItemBlock) listChilds[0] ).getBlocks().length ); 122 assertEquals( "item1", ( (TextBlock) ( (ListItemBlock) listChilds[0] ).getBlocks()[0] ).getText() ); 123 assertEquals( "item2", ( (TextBlock) ( (ListItemBlock) listChilds[1] ).getBlocks()[0] ).getText() ); 124 } 125 126 /** 127 * @throws ParseException on error 128 */ 129 public final void testHorizontalRule() 130 throws ParseException 131 { 132 Block block, expected; 133 ByLineReaderSource source; 134 135 assertTrue( hruleParser.accept( "---" ) ); 136 assertFalse( hruleParser.accept( "---+ asdas" ) ); 137 138 source = new ByLineReaderSource( new StringReader( "" ) ); 139 expected = new HorizontalRuleBlock(); 140 block = hruleParser.visit( "---", source ); 141 assertNull( source.getNextLine() ); 142 assertEquals( expected, block ); 143 144 source = new ByLineReaderSource( new StringReader( "" ) ); 145 expected = new HorizontalRuleBlock(); 146 block = hruleParser.visit( "--- Some text ---- And some more", source ); 147 assertEquals( expected, block ); 148 expected = new ParagraphBlock( new Block[] { new TextBlock( "Some text ---- And some more" ) } ); 149 block = paraParser.visit( source.getNextLine(), source ); 150 assertEquals( expected, block ); 151 } 152 153 /** 154 * @throws ParseException on error 155 */ 156 public final void testHorizontalRuleAndParagraph() 157 throws ParseException 158 { 159 Block[] blocks, expected; 160 ByLineReaderSource source; 161 162 source = new ByLineReaderSource( new StringReader( "" + "Some text\n" + "-----------\n" + "More text" ) ); 163 expected = 164 new Block[] { new ParagraphBlock( new Block[] { new TextBlock( "Some text" ) } ), 165 new HorizontalRuleBlock(), new ParagraphBlock( new Block[] { new TextBlock( "More text" ) } ) }; 166 blocks = (Block[]) twikiParser.parse( source ).toArray( new Block[] {} ); 167 assertTrue( Arrays.equals( expected, blocks ) ); 168 } 169}