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.surefire.booter;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import junit.framework.TestCase;
27  
28  /**
29   * @author Kristian Rosenvold
30   */
31  public class PropertiesWrapperTest extends TestCase {
32      public void testAddList() {
33          PropertiesWrapper propertiesWrapper = new PropertiesWrapper(new HashMap<String, String>());
34          List<String> items = new ArrayList<>();
35          items.add("String1");
36          items.add("String2,String3");
37          items.add("String4");
38          items.add("String5,");
39          propertiesWrapper.addList(items, "Test");
40  
41          final List test = propertiesWrapper.getStringList("Test");
42          assertEquals(5, test.size());
43          assertEquals("String5", test.get(4));
44          assertEquals("String3", test.get(2));
45          assertEquals("String2", test.get(1));
46      }
47  
48      private static final String DUMMY_PREFIX = "dummyPrefix";
49  
50      private static final String FIRST_ELEMENT = "foo0";
51  
52      private static final String SECOND_ELEMENT = "foo1";
53  
54      private final Map<String, String> properties = new HashMap<>();
55  
56      private final PropertiesWrapper mapper = new PropertiesWrapper(properties);
57  
58      private final Classpath classpathWithTwoElements = createClasspathWithTwoElements();
59  
60      public void testReadFromProperties() {
61          properties.put(DUMMY_PREFIX + "0", FIRST_ELEMENT);
62          properties.put(DUMMY_PREFIX + "1", SECOND_ELEMENT);
63          Classpath recreatedClasspath = readClasspathFromProperties();
64          assertEquals(classpathWithTwoElements, recreatedClasspath);
65      }
66  
67      public void testReadFromPropertiesWithEmptyProperties() {
68          Classpath recreatedClasspath = readClasspathFromProperties();
69          assertTrue(recreatedClasspath.getClassPath().isEmpty());
70      }
71  
72      public void testWriteToProperties() {
73          mapper.setClasspath(DUMMY_PREFIX, classpathWithTwoElements);
74          assertEquals(FIRST_ELEMENT, mapper.getProperty(DUMMY_PREFIX + "0"));
75          assertEquals(SECOND_ELEMENT, mapper.getProperty(DUMMY_PREFIX + "1"));
76      }
77  
78      public void testRoundtrip() {
79          mapper.setClasspath(DUMMY_PREFIX, classpathWithTwoElements);
80          Classpath recreatedClasspath = readClasspathFromProperties();
81          assertEquals(classpathWithTwoElements, recreatedClasspath);
82      }
83  
84      private Classpath createClasspathWithTwoElements() {
85          return Classpath.emptyClasspath().addClassPathElementUrl(FIRST_ELEMENT).addClassPathElementUrl(SECOND_ELEMENT);
86      }
87  
88      private Classpath readClasspathFromProperties() {
89          return mapper.getClasspath(DUMMY_PREFIX);
90      }
91  }