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.util.Arrays;
023
024import junit.framework.TestCase;
025
026/**
027 * Generic unit tests for
028 * {@link Block}s
029 *
030 * @author Juan F. Codagnone
031 * @since Nov 2, 2005
032 */
033public class BlockTest
034    extends TestCase
035{
036
037    /**
038     * @see TextBlock#equals(Object)
039     */
040    public final void testTextBlockEquals()
041    {
042        testEquals( new TextBlock( "bar" ), new TextBlock( "bar" ), new TextBlock( "foo" ) );
043    }
044
045    /**
046     * @see WikiWordBlock#equals(Object)
047     */
048    public final void testWikiWordBlockEquals()
049    {
050        final WikiWordLinkResolver resolver = new XHTMLWikiWordLinkResolver();
051        testEquals( new WikiWordBlock( "bar", resolver ), new WikiWordBlock( "bar", resolver ),
052                    new WikiWordBlock( "foo", resolver ) );
053
054        testEquals( new WikiWordBlock( "bar", new TextBlock( "text" ), resolver ),
055                    new WikiWordBlock( "bar", new TextBlock( "text" ), resolver ), new WikiWordBlock( "bar",
056                                                                                                      resolver ) );
057
058        testEquals( new WikiWordBlock( "bar", new TextBlock( "text" ), resolver ),
059                    new WikiWordBlock( "bar", new TextBlock( "text" ), resolver ),
060                    new WikiWordBlock( "text", new TextBlock( "bar" ), resolver ) );
061
062    }
063
064    /**
065     * @see LinkBlock#equals(Object)
066     */
067    public final void testLinkBlockEquals()
068    {
069        testEquals( new LinkBlock( "foo", new TextBlock( "bar" ) ),
070                    new LinkBlock( "foo", new TextBlock( "bar" ) ), new LinkBlock( "bar", new TextBlock( "foo" ) ) );
071    }
072
073    /**
074     * @see ListItemBlock#equals(Object)
075     */
076    public final void testListBlockEquals()
077    {
078        final Block[] blocks = new Block[] { new TextBlock( "hello" ) };
079
080        testEquals( new ListItemBlock( blocks ), new ListItemBlock( blocks ), new ListItemBlock( new Block[] {} ) );
081    }
082
083    /**
084     * @see ListItemBlock#equals(Object)
085     */
086    public final void testNestedBlockEquals()
087    {
088
089        testEquals( new ParagraphBlock( new Block[] { new BoldBlock( new Block[] { new TextBlock( "foo" ) } ) } ),
090                    new ParagraphBlock( new Block[] { new BoldBlock( new Block[] { new TextBlock( "foo" ) } ) } ),
091                    new ParagraphBlock( new Block[] { new BoldBlock( new Block[] { new TextBlock( "bar" ) } ) } ) );
092    }
093
094    /**
095     * @see AbstractFatherBlock#equals(Object)
096     */
097    public final void testAbstractFatherBlockEquals()
098    {
099        assertFalse( Arrays
100                           .equals(
101                                    new Block[] {
102                                        new TextBlock( "mary " ),
103                                        new ItalicBlock(
104                                                         new Block[] { new MonospaceBlock(
105                                                                                           new Block[] { new TextBlock(
106                                                                                                                        "has" ) } ) } ) },
107                                    new Block[] {
108                                        new TextBlock( "mary " ),
109                                        new BoldBlock(
110                                                       new Block[] { new MonospaceBlock(
111                                                                                         new Block[] { new TextBlock(
112                                                                                                                      "has" ) } ) } ) } ) );
113    }
114
115    /**
116     * @see AnchorBlock#equals(Object)
117     */
118    public final void testAnchorBlockEquals()
119    {
120        testEquals( new AnchorBlock( "anchor" ), new AnchorBlock( "anchor" ), new AnchorBlock( "anch" ) );
121    }
122
123    /**
124     * @see HorizontalRuleBlock#equals(Object)
125     */
126    public final void testHorizontalEquals()
127    {
128        testEquals( new HorizontalRuleBlock(), new HorizontalRuleBlock(), "foo" );
129    }
130
131    /**
132     * @param a an object
133     * @param b an object that is equals to a
134     * @param c a diferent object
135     */
136    public final void testEquals( final Object a, final Object b, final Object c )
137    {
138        assertFalse( a.equals( null ) );
139        assertFalse( b.equals( null ) );
140        assertFalse( c.equals( null ) );
141
142        assertNotSame( a, b );
143
144        assertEquals( a, a );
145        assertEquals( b, b );
146        assertEquals( c, c );
147
148        assertEquals( a, b );
149        assertEquals( b, a );
150        assertFalse( a.equals( c ) );
151
152        assertEquals( a.hashCode(), b.hashCode() );
153    }
154}