View Javadoc

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