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