View Javadoc
1   package org.apache.maven.doxia.sink.impl;
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.Enumeration;
23  
24  import javax.swing.text.AttributeSet;
25  
26  import org.apache.maven.doxia.sink.SinkEventAttributes;
27  import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
28  
29  import junit.framework.TestCase;
30  
31  /**
32   * Test SinkEventAttributeSet.
33   *
34   * @author ltheussl
35   */
36  public class SinkEventAttributeSetTest extends TestCase
37  {
38  
39      private SinkEventAttributeSet sinkEventAttributeSet;
40  
41      /**
42       * @throws java.lang.Exception if any.
43       */
44      @Override
45      protected void setUp()
46              throws Exception
47      {
48          super.setUp();
49          this.sinkEventAttributeSet = new SinkEventAttributeSet();
50      }
51  
52      /**
53       * Test of constructors, of class SinkEventAttributeSet.
54       */
55      public void testConstructor()
56      {
57          try
58          {
59              SinkEventAttributeSet aset = new SinkEventAttributeSet( new String[] {"key"} );
60              fail( "missing attribute value!" );
61          }
62          catch ( IllegalArgumentException e )
63          {
64              assertNotNull( e );
65          }
66      }
67  
68      /**
69       * Test of isEmpty method, of class SinkEventAttributeSet.
70       */
71      public void testIsEmpty()
72      {
73          assertTrue( sinkEventAttributeSet.isEmpty() );
74          sinkEventAttributeSet.addAttributes( SinkEventAttributeSet.BOLD );
75          assertFalse( sinkEventAttributeSet.isEmpty() );
76      }
77  
78      /**
79       * Test of getAttributeCount method, of class SinkEventAttributeSet.
80       */
81      public void testGetAttributeCount()
82      {
83          assertEquals( 0, sinkEventAttributeSet.getAttributeCount() );
84          sinkEventAttributeSet.addAttribute( "name1", "value1" );
85          assertEquals( 1, sinkEventAttributeSet.getAttributeCount() );
86          sinkEventAttributeSet.removeAttribute( "name2" );
87          assertEquals( 1, sinkEventAttributeSet.getAttributeCount() );
88          sinkEventAttributeSet.removeAttribute( "name1" );
89          assertEquals( 0, sinkEventAttributeSet.getAttributeCount() );
90  
91          sinkEventAttributeSet.addAttributes( SinkEventAttributeSet.BOLD );
92          assertEquals( 1, sinkEventAttributeSet.getAttributeCount() );
93          sinkEventAttributeSet.removeAttributes( SinkEventAttributeSet.BOXED );
94          assertEquals( 1, sinkEventAttributeSet.getAttributeCount() );
95          sinkEventAttributeSet.removeAttributes( SinkEventAttributeSet.BOLD );
96          assertEquals( 0, sinkEventAttributeSet.getAttributeCount() );
97      }
98  
99      /**
100      * Test of isDefined method, of class SinkEventAttributeSet.
101      */
102     public void testIsDefined()
103     {
104         assertFalse( sinkEventAttributeSet.isDefined( SinkEventAttributes.DECORATION ) );
105         sinkEventAttributeSet.addAttributes( SinkEventAttributeSet.BOXED );
106         assertTrue( sinkEventAttributeSet.isDefined( SinkEventAttributes.DECORATION ) );
107     }
108 
109     /**
110      * Test of isEqual method, of class SinkEventAttributeSet.
111      */
112     public void testIsEqual()
113     {
114         SinkEventAttributes instance = new SinkEventAttributeSet( SinkEventAttributeSet.BOLD );
115         sinkEventAttributeSet.addAttributes( SinkEventAttributeSet.BOLD );
116         assertTrue( instance.isEqual( sinkEventAttributeSet ) );
117         instance.addAttributes( SinkEventAttributeSet.BOXED );
118         assertFalse( instance.isEqual( sinkEventAttributeSet ) );
119     }
120 
121     /**
122      * Test of equals method, of class SinkEventAttributeSet.
123      */
124     public void testEquals()
125     {
126         assertFalse( sinkEventAttributeSet.equals( null ) );
127         assertTrue( sinkEventAttributeSet.equals( sinkEventAttributeSet ) );
128 
129         SinkEventAttributes instance = new SinkEventAttributeSet( SinkEventAttributeSet.BOLD );
130         sinkEventAttributeSet.addAttributes( SinkEventAttributeSet.BOLD );
131         assertTrue( instance.equals( sinkEventAttributeSet ) );
132         instance.addAttributes( SinkEventAttributeSet.BOXED );
133         assertFalse( instance.equals( sinkEventAttributeSet ) );
134     }
135 
136     /**
137      * Test of copyAttributes method, of class SinkEventAttributeSet.
138      */
139     public void testCopyAttributes()
140     {
141         sinkEventAttributeSet.addAttributes( SinkEventAttributeSet.ITALIC );
142         AttributeSet instance = sinkEventAttributeSet.copyAttributes();
143         assertTrue( instance.isEqual( sinkEventAttributeSet ) );
144     }
145 
146     /**
147      * Test of getAttributeNames method, of class SinkEventAttributeSet.
148      */
149     public void testGetAttributeNames()
150     {
151         sinkEventAttributeSet.addAttributes( SinkEventAttributeSet.UNDERLINE );
152         Enumeration<String> result = sinkEventAttributeSet.getAttributeNames();
153         assertEquals( "decoration", result.nextElement() );
154         assertFalse( result.hasMoreElements() );
155     }
156 
157     /**
158      * Test of getAttribute method, of class SinkEventAttributeSet.
159      */
160     public void testGetAttribute()
161     {
162         sinkEventAttributeSet.addAttribute( "key", "value" );
163         assertTrue( sinkEventAttributeSet.getAttribute( "key" ).equals( "value" ) );
164         assertNull( sinkEventAttributeSet.getAttribute( "bla" ) );
165     }
166 
167     /**
168      * Test of containsAttribute method, of class SinkEventAttributeSet.
169      */
170     public void testContainsAttribute()
171     {
172         sinkEventAttributeSet.addAttribute( "key", "value" );
173         assertTrue( sinkEventAttributeSet.containsAttribute( "key", "value" ) );
174         assertFalse( sinkEventAttributeSet.containsAttribute( "key", "valu" ) );
175     }
176 
177     /**
178      * Test of containsAttributes method, of class SinkEventAttributeSet.
179      */
180     public void testContainsAttributes()
181     {
182         sinkEventAttributeSet.addAttributes( SinkEventAttributeSet.JUSTIFY );
183         assertTrue( sinkEventAttributeSet.containsAttributes( SinkEventAttributeSet.JUSTIFY ) );
184         assertFalse( sinkEventAttributeSet.containsAttributes( SinkEventAttributeSet.BOXED ) );
185     }
186 
187     /**
188      * Test of addAttribute method, of class SinkEventAttributeSet.
189      */
190     public void testAddAttribute()
191     {
192         assertFalse( sinkEventAttributeSet.containsAttribute( "key", "value" ) );
193         sinkEventAttributeSet.addAttribute( "key", "value" );
194         assertTrue( sinkEventAttributeSet.containsAttribute( "key", "value" ) );
195         sinkEventAttributeSet.removeAttribute( "key" );
196         assertFalse( sinkEventAttributeSet.containsAttribute( "key", "value" ) );
197     }
198 
199     /**
200      * Test of add/removeAttributes methods, of class SinkEventAttributeSet.
201      */
202     public void testAddAttributes()
203     {
204         assertFalse( sinkEventAttributeSet.containsAttributes( SinkEventAttributeSet.JUSTIFY ) );
205         sinkEventAttributeSet.addAttributes( SinkEventAttributeSet.JUSTIFY );
206         assertTrue( sinkEventAttributeSet.containsAttributes( SinkEventAttributeSet.JUSTIFY ) );
207 
208         sinkEventAttributeSet.removeAttributes( SinkEventAttributeSet.JUSTIFY );
209         assertFalse( sinkEventAttributeSet.containsAttributes( SinkEventAttributeSet.JUSTIFY ) );
210 
211         sinkEventAttributeSet.addAttributes( SinkEventAttributeSet.JUSTIFY );
212         sinkEventAttributeSet.removeAttributes( SinkEventAttributeSet.JUSTIFY.getAttributeNames() );
213         assertFalse( sinkEventAttributeSet.containsAttributes( SinkEventAttributeSet.JUSTIFY ) );
214 
215         sinkEventAttributeSet.setResolveParent( SinkEventAttributeSet.JUSTIFY );
216         assertTrue( sinkEventAttributeSet.containsAttributes( SinkEventAttributeSet.JUSTIFY ) );
217 
218         sinkEventAttributeSet.removeAttributes( (AttributeSet) null ); // should do nothing
219     }
220 
221     /**
222      * Test of getResolveParent method, of class SinkEventAttributeSet.
223      */
224     public void testGetResolveParent()
225     {
226         assertNull( sinkEventAttributeSet.getResolveParent() );
227         sinkEventAttributeSet.setResolveParent( SinkEventAttributeSet.CENTER );
228         assertNotNull( sinkEventAttributeSet.getResolveParent() );
229     }
230 
231     /**
232      * Test of clone method, of class SinkEventAttributeSet.
233      */
234     public void testClone()
235     {
236         Object result = sinkEventAttributeSet.clone();
237         assertTrue( sinkEventAttributeSet.equals( result ) );
238 
239         sinkEventAttributeSet.addAttributes( SinkEventAttributeSet.MONOSPACED );
240         assertFalse( sinkEventAttributeSet.equals( result ) );
241 
242         result = sinkEventAttributeSet.clone();
243         assertTrue( sinkEventAttributeSet.equals( result ) );
244         sinkEventAttributeSet.setResolveParent( SinkEventAttributeSet.CENTER );
245         //assertFalse( sinkEventAttributeSet.equals( result ) );
246 
247         result = sinkEventAttributeSet.clone();
248         assertTrue( sinkEventAttributeSet.equals( result ) );
249         sinkEventAttributeSet.setResolveParent( SinkEventAttributeSet.BOXED );
250         //assertFalse( sinkEventAttributeSet.equals( result ) );
251     }
252 
253     /**
254      * Test of hashCode method, of class SinkEventAttributeSet.
255      */
256     public void testHashCode()
257     {
258         int oldValue = sinkEventAttributeSet.hashCode();
259         sinkEventAttributeSet.addAttributes( SinkEventAttributeSet.BOLD );
260         int newValue = sinkEventAttributeSet.hashCode();
261         assertFalse( oldValue == newValue );
262 
263         oldValue = newValue;
264         sinkEventAttributeSet.setResolveParent( SinkEventAttributeSet.CENTER );
265         newValue = sinkEventAttributeSet.hashCode();
266         assertFalse( oldValue == newValue );
267     }
268 
269     /**
270      * Test of toString method, of class SinkEventAttributeSet.
271      */
272     public void testToString()
273     {
274         String expected = "";
275         assertEquals( expected, sinkEventAttributeSet.toString() );
276 
277         sinkEventAttributeSet.addAttributes( SinkEventAttributeSet.BOXED );
278         expected = " decoration=boxed";
279         assertEquals( expected, sinkEventAttributeSet.toString() );
280 
281         sinkEventAttributeSet.addAttributes( SinkEventAttributeSet.CENTER );
282         expected = " decoration=boxed align=center";
283         assertEquals( expected, sinkEventAttributeSet.toString() );
284     }
285 }