View Javadoc
1   package org.apache.maven.plugin.surefire;
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  import java.util.Iterator;
24  import java.util.Properties;
25  import java.util.SortedSet;
26  import java.util.TreeSet;
27  
28  import junit.framework.TestCase;
29  import org.apache.maven.surefire.booter.KeyValueSource;
30  
31  import static java.util.Collections.list;
32  
33  /**
34   * Tests the insertion-order preserving properties collection
35   */
36  public class SurefirePropertiesTest
37      extends TestCase
38  {
39  
40      public void testKeys()
41          throws Exception
42      {
43          SurefireProperties orderedProperties = new SurefireProperties( (KeyValueSource) null );
44          orderedProperties.setProperty( "abc", "1" );
45          orderedProperties.setProperty( "xyz", "1" );
46          orderedProperties.setProperty( "efg", "1" );
47  
48          Enumeration<Object> keys = orderedProperties.keys();
49          assertEquals( "abc", keys.nextElement() );
50          assertEquals( "xyz", keys.nextElement() );
51          assertEquals( "efg", keys.nextElement() );
52      }
53  
54      public void testKeysReinsert()
55          throws Exception
56      {
57          SurefireProperties orderedProperties = new SurefireProperties( (KeyValueSource) null );
58          orderedProperties.setProperty( "abc", "1" );
59          orderedProperties.setProperty( "xyz", "1" );
60          orderedProperties.setProperty( "efg", "1" );
61          orderedProperties.setProperty( "abc", "2" );
62          orderedProperties.remove( "xyz" );
63          orderedProperties.setProperty( "xyz", "1" );
64  
65          Enumeration<Object> keys = orderedProperties.keys();
66          assertEquals( "abc", keys.nextElement() );
67          assertEquals( "efg", keys.nextElement() );
68          assertEquals( "xyz", keys.nextElement() );
69      }
70  
71      public void testConstructWithOther()
72      {
73          Properties src = new Properties();
74          src.setProperty( "a", "1" );
75          src.setProperty( "b", "2" );
76          SurefireProperties orderedProperties = new SurefireProperties( src );
77          // Cannot make assumptions about insertion order
78          // keys() uses the items property, more reliable to test than size(),
79          // which is based on the Properties class
80          // see https://issues.apache.org/jira/browse/SUREFIRE-1445
81          assertEquals( src.size(), list( orderedProperties.keys() ).size() );
82          assertEquals( src.size(), size( orderedProperties.getStringKeySet().iterator() ) );
83          assertEquals( 2, orderedProperties.size() );
84  
85          assertTrue( list( orderedProperties.keys() ).contains( "a" ) );
86          assertTrue( list( orderedProperties.keys() ).contains( "b" ) );
87  
88          Iterator it = orderedProperties.getStringKeySet().iterator();
89          SortedSet<Object> keys = new TreeSet<>();
90          keys.add( it.next() );
91          keys.add( it.next() );
92          it = keys.iterator();
93          assertEquals( "a", it.next() );
94          assertEquals( "b", it.next() );
95      }
96  
97      public void testPutAll()
98      {
99          Properties src = new Properties();
100         src.setProperty( "a", "1" );
101         src.setProperty( "b", "2" );
102         SurefireProperties orderedProperties = new SurefireProperties();
103         orderedProperties.putAll( src );
104         // Cannot make assumptions about insertion order
105         // keys() uses the items property, more reliable to test than size(),
106         // which is based on the Properties class
107         // see https://issues.apache.org/jira/browse/SUREFIRE-1445
108         assertEquals( src.size(), list( orderedProperties.keys() ).size() );
109         assertEquals( src.size(), size( orderedProperties.getStringKeySet().iterator() ) );
110         assertEquals( 2, orderedProperties.size() );
111 
112         assertTrue( list( orderedProperties.keys() ).contains( "a" ) );
113         assertTrue( list( orderedProperties.keys() ).contains( "b" ) );
114 
115         Iterator it = orderedProperties.getStringKeySet().iterator();
116         SortedSet<Object> keys = new TreeSet<>();
117         keys.add( it.next() );
118         keys.add( it.next() );
119         it = keys.iterator();
120         assertEquals( "a", it.next() );
121         assertEquals( "b", it.next() );
122     }
123 
124     private static int size( Iterator<?> iterator )
125     {
126         int count = 0;
127         while ( iterator.hasNext() )
128         {
129             iterator.next();
130             count++;
131         }
132         return count;
133     }
134 
135 }