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