View Javadoc

1   package org.apache.maven.plugin.coreit;
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.io.File;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.Properties;
26  
27  import junit.framework.TestCase;
28  
29  /**
30   * @author Benjamin Bentmann
31   * @version $Id$
32   */
33  public class PropertyUtilTest
34      extends TestCase
35  {
36  
37      public void testStoreScalar()
38      {
39          Properties props = new Properties();
40          PropertyUtil.store( props, "null", null );
41          PropertyUtil.store( props, "string", "str" );
42          PropertyUtil.store( props, "boolean", Boolean.TRUE );
43          PropertyUtil.store( props, "int", new Integer( 7 ) );
44          PropertyUtil.store( props, "file", new File( "pom.xml" ) );
45  
46          assertNull( props.get( "null" ) );
47          assertEquals( "str", props.get( "string" ) );
48          assertEquals( "true", props.get( "boolean" ) );
49          assertEquals( "7", props.get( "int" ) );
50          assertEquals( "pom.xml", props.get( "file" ) );
51          assertEquals( 4, props.size() );
52      }
53  
54      public void testStoreArray()
55      {
56          Properties props = new Properties();
57          PropertyUtil.store( props, "arr", new String[] { "one", "two" } );
58  
59          assertEquals( "2", props.get( "arr" ) );
60          assertEquals( "one", props.get( "arr.0" ) );
61          assertEquals( "two", props.get( "arr.1" ) );
62          assertEquals( 3, props.size() );
63      }
64  
65      public void testStoreList()
66      {
67          Properties props = new Properties();
68          PropertyUtil.store( props, "arr", Arrays.asList( new String[] { "one", "two" } ) );
69  
70          assertEquals( "2", props.get( "arr" ) );
71          assertEquals( "one", props.get( "arr.0" ) );
72          assertEquals( "two", props.get( "arr.1" ) );
73          assertEquals( 3, props.size() );
74      }
75  
76      public void testStoreMap()
77      {
78          Properties props = new Properties();
79          PropertyUtil.store( props, "map", Collections.singletonMap( "key", "value" ) );
80  
81          assertEquals( "1", props.get( "map" ) );
82          assertEquals( "value", props.get( "map.key" ) );
83          assertEquals( 2, props.size() );
84      }
85  
86      public void testStoreBean()
87      {
88          Properties props = new Properties();
89          PropertyUtil.store( props, "bean", new Bean() );
90  
91          assertEquals( "name", props.get( "bean.name" ) );
92          assertEquals( "false", props.get( "bean.enabled" ) );
93          assertEquals( 2, props.size() );
94      }
95  
96      public void testStoreCycle()
97      {
98          Object[] arr = { null };
99          arr[0] = Collections.singleton( Collections.singletonMap( "key", arr ) );
100 
101         Properties props = new Properties();
102         PropertyUtil.store( props, "cycle", arr );
103         assertTrue( "Should not die because of stack overflow", true );
104     }
105 
106     public void testGetPropertyName()
107     {
108         assertEquals( "name", PropertyUtil.getPropertyName( "getName" ) );
109         assertEquals( "enabled", PropertyUtil.getPropertyName( "isEnabled" ) );
110     }
111 
112     public static class Bean
113     {
114         public String getName()
115         {
116             return "name";
117         }
118 
119         public boolean isEnabled()
120         {
121             return false;
122         }
123 
124         public String toString()
125         {
126             return "excluded";
127         }
128 
129         public Object getUntypedReturnValue()
130         {
131             return "excluded";
132         }
133 
134         public Bean getCyclicReference()
135         {
136             return this;
137         }
138     }
139 
140 }