1 package org.apache.maven.plugin.coreit;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
31
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 }