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