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 items = new ArrayList();
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      private final Classpath classpathWithTwoElements = createClasspathWithTwoElements();
66  
67      public void testReadFromProperties()
68          throws Exception
69      {
70          properties.put( DUMMY_PREFIX + "0", FIRST_ELEMENT );
71          properties.put( DUMMY_PREFIX + "1", SECOND_ELEMENT );
72          Classpath recreatedClasspath = readClasspathFromProperties();
73          assertEquals( classpathWithTwoElements, recreatedClasspath );
74      }
75  
76      public void testReadFromPropertiesWithEmptyProperties()
77          throws Exception
78      {
79          Classpath recreatedClasspath = readClasspathFromProperties();
80          assertTrue( recreatedClasspath.getClassPath().isEmpty() );
81      }
82  
83      public void testWriteToProperties()
84          throws Exception
85      {
86          mapper.setClasspath( DUMMY_PREFIX,  classpathWithTwoElements);
87          assertEquals( FIRST_ELEMENT, mapper.getProperty( DUMMY_PREFIX + "0" ) );
88          assertEquals( SECOND_ELEMENT, mapper.getProperty( DUMMY_PREFIX + "1" ) );
89      }
90  
91      public void testRoundtrip()
92          throws Exception
93      {
94          mapper.setClasspath( DUMMY_PREFIX,  classpathWithTwoElements);
95          Classpath recreatedClasspath = readClasspathFromProperties();
96          assertEquals( classpathWithTwoElements, recreatedClasspath );
97      }
98  
99      private Classpath createClasspathWithTwoElements()
100     {
101         Classpath classpath = new Classpath();
102         classpath.addClassPathElementUrl( FIRST_ELEMENT );
103         classpath.addClassPathElementUrl( SECOND_ELEMENT );
104         return classpath;
105     }
106 
107     private Classpath readClasspathFromProperties()
108     {
109         return mapper.getClasspath( DUMMY_PREFIX);
110     }
111 }