001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.doxia.module.twiki.parser;
020
021import java.io.StringReader;
022
023import org.apache.maven.doxia.module.twiki.TWikiParser;
024import org.apache.maven.doxia.parser.ParseException;
025import org.apache.maven.doxia.util.ByLineReaderSource;
026import org.apache.maven.doxia.util.ByLineSource;
027
028/**
029 * Tests for {@link TWikiParser#getTitle(java.util.List)}
030 *
031 *
032 * @author Juan F. Codagnone
033 * @since Nov 10, 2007
034 */
035public class TitleTest
036    extends AbstractBlockTestCase
037{
038
039    public void testSectionTitle()
040        throws Exception
041    {
042        final ByLineSource source = new ByLineReaderSource( new StringReader( "---++ Test\n hello world" ) );
043
044        final TWikiParser parser = new TWikiParser();
045
046        assertEquals( "Test", parser.getTitle( parser.parse( source ), source ) );
047    }
048
049    public void testNoSectionTitle()
050        throws Exception
051    {
052        final ByLineSource source =
053            new NamedByLineSource( new ByLineReaderSource( new StringReader( "hello world" ) ), "testpage" );
054
055        final TWikiParser parser = new TWikiParser();
056
057        assertEquals( "testpage", parser.getTitle( parser.parse( source ), source ) );
058    }
059
060    public void testNoSectionTwikiExtensionTitle()
061        throws Exception
062    {
063        final ByLineSource source =
064            new NamedByLineSource( new ByLineReaderSource( new StringReader( "hello world" ) ), "testpage.twiki" );
065
066        final TWikiParser parser = new TWikiParser();
067
068        assertEquals( "testpage", parser.getTitle( parser.parse( source ), source ) );
069    }
070
071}
072
073class NamedByLineSource
074    implements ByLineSource
075{
076    /** reader */
077    private final ByLineReaderSource reader;
078
079    /** reader's name */
080    private final String name;
081
082    public NamedByLineSource( final ByLineReaderSource reader, final String name )
083    {
084        if ( reader == null || name == null )
085        {
086            throw new IllegalArgumentException( "null arguments are not allowed" );
087        }
088
089        this.reader = reader;
090        this.name = name;
091    }
092
093    /** @see ByLineReaderSource#close() */
094    public final void close()
095    {
096        reader.close();
097    }
098
099    /** @see ByLineReaderSource#getLineNumber() */
100    public final int getLineNumber()
101    {
102        return reader.getLineNumber();
103    }
104
105    /** @see ByLineReaderSource#getName() */
106    public final String getName()
107    {
108        return name;
109    }
110
111    /** @see ByLineReaderSource#getNextLine() */
112    public final String getNextLine()
113        throws ParseException
114    {
115        return reader.getNextLine();
116    }
117
118    /** @see ByLineReaderSource#unget(java.lang.String) */
119    public final void unget( final String s )
120        throws IllegalStateException
121    {
122        reader.unget( s );
123    }
124
125    /** @see ByLineReaderSource#ungetLine() */
126    public final void ungetLine()
127        throws IllegalStateException
128    {
129        reader.ungetLine();
130    }
131}