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