View Javadoc
1   package org.apache.maven.plugin.surefire;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with 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,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import java.util.Enumeration;
22  import java.util.Iterator;
23  import java.util.Properties;
24  import java.util.SortedSet;
25  import java.util.TreeSet;
26  
27  import junit.framework.TestCase;
28  import org.apache.maven.surefire.booter.KeyValueSource;
29  
30  import static java.util.Collections.list;
31  
32  /**
33   * Tests the insertion-order preserving properties collection
34   */
35  public class SurefirePropertiesTest
36      extends TestCase
37  {
38  
39      public void testKeys()
40          throws Exception
41      {
42          SurefireProperties orderedProperties = new SurefireProperties( (KeyValueSource) null );
43          orderedProperties.setProperty( "abc", "1" );
44          orderedProperties.setProperty( "xyz", "1" );
45          orderedProperties.setProperty( "efg", "1" );
46  
47          Enumeration<Object> keys = orderedProperties.keys();
48          assertEquals( "abc", keys.nextElement() );
49          assertEquals( "xyz", keys.nextElement() );
50          assertEquals( "efg", keys.nextElement() );
51      }
52  
53      public void testKeysReinsert()
54          throws Exception
55      {
56          SurefireProperties orderedProperties = new SurefireProperties( (KeyValueSource) null );
57          orderedProperties.setProperty( "abc", "1" );
58          orderedProperties.setProperty( "xyz", "1" );
59          orderedProperties.setProperty( "efg", "1" );
60          orderedProperties.setProperty( "abc", "2" );
61          orderedProperties.remove( "xyz" );
62          orderedProperties.setProperty( "xyz", "1" );
63  
64          Enumeration<Object> keys = orderedProperties.keys();
65          assertEquals( "abc", keys.nextElement() );
66          assertEquals( "efg", keys.nextElement() );
67          assertEquals( "xyz", keys.nextElement() );
68      }
69  
70      public void testConstructWithOther()
71      {
72          Properties src = new Properties();
73          src.setProperty( "a", "1" );
74          src.setProperty( "b", "2" );
75          SurefireProperties orderedProperties = new SurefireProperties( src );
76          // Cannot make assumptions about insertion order
77          // keys() uses the items property, more reliable to test than size(),
78          // which is based on the Properties class
79          // see https://issues.apache.org/jira/browse/SUREFIRE-1445
80          assertEquals( src.size(), list( orderedProperties.keys() ).size() );
81          assertEquals( src.size(), size( orderedProperties.getStringKeySet().iterator() ) );
82          assertEquals( 2, orderedProperties.size() );
83  
84          assertTrue( list( orderedProperties.keys() ).contains( "a" ) );
85          assertTrue( list( orderedProperties.keys() ).contains( "b" ) );
86  
87          Iterator it = orderedProperties.getStringKeySet().iterator();
88          SortedSet<Object> keys = new TreeSet<Object>();
89          keys.add( it.next() );
90          keys.add( it.next() );
91          it = keys.iterator();
92          assertEquals( "a", it.next() );
93          assertEquals( "b", it.next() );
94      }
95  
96      public void testPutAll()
97      {
98          Properties src = new Properties();
99          src.setProperty( "a", "1" );
100         src.setProperty( "b", "2" );
101         SurefireProperties orderedProperties = new SurefireProperties();
102         orderedProperties.putAll( src );
103         // Cannot make assumptions about insertion order
104         // keys() uses the items property, more reliable to test than size(),
105         // which is based on the Properties class
106         // see https://issues.apache.org/jira/browse/SUREFIRE-1445
107         assertEquals( src.size(), list( orderedProperties.keys() ).size() );
108         assertEquals( src.size(), size( orderedProperties.getStringKeySet().iterator() ) );
109         assertEquals( 2, orderedProperties.size() );
110 
111         assertTrue( list( orderedProperties.keys() ).contains( "a" ) );
112         assertTrue( list( orderedProperties.keys() ).contains( "b" ) );
113 
114         Iterator it = orderedProperties.getStringKeySet().iterator();
115         SortedSet<Object> keys = new TreeSet<Object>();
116         keys.add( it.next() );
117         keys.add( it.next() );
118         it = keys.iterator();
119         assertEquals( "a", it.next() );
120         assertEquals( "b", it.next() );
121     }
122 
123     private static int size( Iterator<?> iterator )
124     {
125         int count = 0;
126         while ( iterator.hasNext() ) {
127             iterator.next();
128             count++;
129         }
130         return count;
131     }
132 
133 }