1   package org.apache.maven.doxia.module.twiki.parser;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Arrays;
23  
24  import junit.framework.TestCase;
25  
26  /**
27   * Generic unit tests for
28   * {@link Block}s
29   *
30   * @author Juan F. Codagnone
31   * @since Nov 2, 2005
32   */
33  public class BlockTest
34      extends TestCase
35  {
36  
37      /**
38       * @see TextBlock#equals(Object)
39       */
40      public final void testTextBlockEquals()
41      {
42          testEquals( new TextBlock( "bar" ), new TextBlock( "bar" ), new TextBlock( "foo" ) );
43      }
44  
45      /**
46       * @see WikiWordBlock#equals(Object)
47       */
48      public final void testWikiWordBlockEquals()
49      {
50          final WikiWordLinkResolver resolver = new XHTMLWikiWordLinkResolver();
51          testEquals( new WikiWordBlock( "bar", resolver ), new WikiWordBlock( "bar", resolver ),
52                      new WikiWordBlock( "foo", resolver ) );
53  
54          testEquals( new WikiWordBlock( "bar", new TextBlock( "text" ), resolver ),
55                      new WikiWordBlock( "bar", new TextBlock( "text" ), resolver ), new WikiWordBlock( "bar",
56                                                                                                        resolver ) );
57  
58          testEquals( new WikiWordBlock( "bar", new TextBlock( "text" ), resolver ),
59                      new WikiWordBlock( "bar", new TextBlock( "text" ), resolver ),
60                      new WikiWordBlock( "text", new TextBlock( "bar" ), resolver ) );
61  
62      }
63  
64      /**
65       * @see LinkBlock#equals(Object)
66       */
67      public final void testLinkBlockEquals()
68      {
69          testEquals( new LinkBlock( "foo", new TextBlock( "bar" ) ),
70                      new LinkBlock( "foo", new TextBlock( "bar" ) ), new LinkBlock( "bar", new TextBlock( "foo" ) ) );
71      }
72  
73      /**
74       * @see ListItemBlock#equals(Object)
75       */
76      public final void testListBlockEquals()
77      {
78          final Block[] blocks = new Block[] { new TextBlock( "hello" ) };
79  
80          testEquals( new ListItemBlock( blocks ), new ListItemBlock( blocks ), new ListItemBlock( new Block[] {} ) );
81      }
82  
83      /**
84       * @see ListItemBlock#equals(Object)
85       */
86      public final void testNestedBlockEquals()
87      {
88  
89          testEquals( new ParagraphBlock( new Block[] { new BoldBlock( new Block[] { new TextBlock( "foo" ) } ) } ),
90                      new ParagraphBlock( new Block[] { new BoldBlock( new Block[] { new TextBlock( "foo" ) } ) } ),
91                      new ParagraphBlock( new Block[] { new BoldBlock( new Block[] { new TextBlock( "bar" ) } ) } ) );
92      }
93  
94      /**
95       * @see AbstractFatherBlock#equals(Object)
96       */
97      public final void testAbstractFatherBlockEquals()
98      {
99          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 }