1   package org.apache.maven.util;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.util.Arrays;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Set;
26  
27  import junit.framework.TestCase;
28  
29  /**
30   * Test InsertionOrderedSet.
31   *
32   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
33   * @version $Id: InsertionOrderedSetTest.java 517014 2007-03-11 21:15:50Z ltheussl $
34   */
35  public class InsertionOrderedSetTest
36      extends TestCase
37  {
38      private Set set = new InsertionOrderedSet();
39  
40      private static final String STRING_1 = "zzz";
41  
42      private static final String STRING_2 = "qqq";
43  
44      private static final String STRING_3 = "www";
45  
46      private static final HashSet REMOVED_ELEMENT_SET = new HashSet( Arrays.asList( new String[] { STRING_1, STRING_3 } ) );
47  
48      private static final HashSet OUTPUT_SET = new HashSet( Arrays
49          .asList( new String[] { STRING_1, STRING_2, STRING_3 } ) );
50  
51      private static final List INPUT_SET = Arrays.asList( new String[] { STRING_1, STRING_2, STRING_1, STRING_3 } );
52  
53      public void setUp()
54          throws Exception
55      {
56      }
57  
58      public void testAdd()
59      {
60          set.add( STRING_1 );
61          set.add( STRING_2 );
62          set.add( STRING_1 );
63          set.add( STRING_3 );
64  
65          assertEquals( OUTPUT_SET, set );
66      }
67  
68      public void testAddAll()
69      {
70          set.addAll( INPUT_SET );
71          assertEquals( OUTPUT_SET, set );
72      }
73  
74      public void testConstructor()
75      {
76          Set set = new InsertionOrderedSet( INPUT_SET );
77          assertEquals( OUTPUT_SET, set );
78      }
79  
80      public void testRemove()
81      {
82          set.addAll( INPUT_SET );
83          set.remove( STRING_2 );
84          assertEquals( REMOVED_ELEMENT_SET, set );
85      }
86  
87      public void testIterator()
88      {
89          set.addAll( INPUT_SET );
90  
91          Iterator i = set.iterator();
92          assertEquals( STRING_1, i.next() );
93          assertEquals( STRING_2, i.next() );
94          assertEquals( STRING_3, i.next() );
95      }
96  
97      public void testIteratorRemove()
98      {
99          set.addAll( INPUT_SET );
100 
101         Iterator i = set.iterator();
102         assertEquals( STRING_1, i.next() );
103         assertEquals( STRING_2, i.next() );
104         i.remove();
105         assertEquals( STRING_3, i.next() );
106 
107         assertEquals( REMOVED_ELEMENT_SET, set );
108     }
109 
110 }