KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > property > test > PropertyUtilTestCase


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.property.test;
9
10 import org.apache.avalon.framework.context.Context;
11 import org.apache.avalon.framework.context.DefaultContext;
12 import org.apache.avalon.framework.context.Resolvable;
13 import org.apache.avalon.excalibur.property.PropertyException;
14 import org.apache.avalon.excalibur.property.PropertyUtil;
15 import junit.framework.TestCase;
16
17 /**
18  *
19  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
20  */

21 public final class PropertyUtilTestCase
22     extends TestCase
23 {
24     public PropertyUtilTestCase( final String JavaDoc name )
25     {
26         super( name );
27     }
28
29     private static final class ResolveTest
30         implements Resolvable
31     {
32         int m_count;
33         int m_current;
34
35         public ResolveTest( final int count )
36         {
37             m_count = count;
38         }
39
40         public Object JavaDoc resolve( final Context context )
41         {
42             m_current++;
43
44             if( m_current >= m_count ) return new Integer JavaDoc( m_count );
45             else return this;
46         }
47     }
48
49     private final static Object JavaDoc OBJ1 = new Object JavaDoc();
50     private final static Object JavaDoc OBJ2 = new Object JavaDoc();
51
52     private DefaultContext m_context;
53
54
55     public void setUp()
56     {
57         m_context = new DefaultContext();
58         m_context.put( "obj1", OBJ1 );
59         m_context.put( "obj2", OBJ2 );
60         m_context.put( "res1", new ResolveTest( 1 ) );
61         m_context.put( "res2", new ResolveTest( 2 ) );
62         m_context.put( "res3", new ResolveTest( 3 ) );
63         m_context.put( "res4", new ResolveTest( 4 ) );
64         m_context.put( "key1", "obj1" );
65         m_context.put( "key2", "obj2" );
66         m_context.put( "key3", "2" );
67         m_context.put( "key4", "1" );
68         m_context.put( "key5", "obj" );
69         m_context.put( "key6", "key" );
70     }
71
72     public void testNoResolve()
73         throws PropertyException
74     {
75         final Object JavaDoc result =
76             PropertyUtil.resolveProperty( "blah", m_context, false );
77
78         assertEquals( result, "blah" );
79     }
80
81     public void testObjResolve()
82         throws PropertyException
83     {
84         final Object JavaDoc result =
85             PropertyUtil.resolveProperty( "${obj1}", m_context, false );
86
87         assertEquals( result, OBJ1 );
88     }
89
90     public void testObjResolveToText()
91         throws PropertyException
92     {
93         final Object JavaDoc result =
94             PropertyUtil.resolveProperty( "${obj1} ", m_context, false );
95
96         assertEquals( result, OBJ1 + " " );
97     }
98
99     public void testDualObjResolve()
100         throws PropertyException
101     {
102         final Object JavaDoc result =
103             PropertyUtil.resolveProperty( " ${obj1} ${obj2} ", m_context, false );
104
105         assertEquals( result, " " + OBJ1 + " " + OBJ2 + " " );
106     }
107
108     public void testRecurseObjResolve()
109         throws PropertyException
110     {
111         final Object JavaDoc result =
112             PropertyUtil.resolveProperty( "${res1}", m_context, false );
113
114         assertEquals( result, new Integer JavaDoc( 1 ) );
115     }
116
117     public void testRecurseObjResolve2()
118         throws PropertyException
119     {
120         final Object JavaDoc result =
121             PropertyUtil.resolveProperty( "${res2}", m_context, false );
122
123         assertEquals( result, new Integer JavaDoc( 2 ) );
124     }
125
126     public void testNullObjResolve()
127         throws PropertyException
128     {
129         final Object JavaDoc result =
130             PropertyUtil.resolveProperty( "${blahaaa}", m_context, true );
131
132         assertEquals( result, "" );
133     }
134
135     public void testNullObjResolveForException()
136         throws PropertyException
137     {
138         try
139         {
140             final Object JavaDoc result =
141                 PropertyUtil.resolveProperty( "${blahaaa}", m_context, false );
142         }
143         catch( final PropertyException pe )
144         {
145             return;
146         }
147         fail( "NUll resolve occured without exception" );
148     }
149
150 //////////////////////
151

152     public void testRecursiveNoResolve()
153         throws PropertyException
154     {
155         final Object JavaDoc result =
156             PropertyUtil.recursiveResolveProperty( "blah", m_context, false );
157
158         assertEquals( result, "blah" );
159     }
160
161     public void testRecursiveObjResolve()
162         throws PropertyException
163     {
164         final Object JavaDoc result =
165             PropertyUtil.recursiveResolveProperty( "${obj1}", m_context, false );
166
167         assertEquals( result, OBJ1 );
168     }
169
170     public void testRecursiveObjResolveToText()
171         throws PropertyException
172     {
173         final Object JavaDoc result =
174             PropertyUtil.recursiveResolveProperty( "${obj1} ", m_context, false );
175
176         assertEquals( result, OBJ1 + " " );
177     }
178
179     public void testRecursiveDualObjResolve()
180         throws PropertyException
181     {
182         final Object JavaDoc result =
183             PropertyUtil.recursiveResolveProperty( " ${obj1} ${obj2} ", m_context, false );
184
185         assertEquals( result, " " + OBJ1 + " " + OBJ2 + " " );
186     }
187
188     public void testRecursiveRecurseObjResolve()
189         throws PropertyException
190     {
191         final Object JavaDoc result =
192             PropertyUtil.recursiveResolveProperty( "${res1}", m_context, false );
193
194         assertEquals( result, new Integer JavaDoc( 1 ) );
195     }
196
197     public void testRecursiveRecurseObjResolve2()
198         throws PropertyException
199     {
200         final Object JavaDoc result =
201             PropertyUtil.recursiveResolveProperty( "${res2}", m_context, false );
202
203         assertEquals( result, new Integer JavaDoc( 2 ) );
204     }
205
206     public void testRecursiveNullObjResolve()
207         throws PropertyException
208     {
209         final Object JavaDoc result =
210             PropertyUtil.recursiveResolveProperty( "${blahaaa}", m_context, true );
211
212         assertEquals( result, "" );
213     }
214
215     public void testRecursiveNullObjResolveForException()
216         throws PropertyException
217     {
218         try
219         {
220             final Object JavaDoc result =
221                 PropertyUtil.recursiveResolveProperty( "${blahaaa}", m_context, false );
222         }
223         catch( final PropertyException pe )
224         {
225             return;
226         }
227         fail( "NUll resolve occured without exception" );
228     }
229     
230     public void testKey1()
231         throws PropertyException
232     {
233         final Object JavaDoc result =
234             PropertyUtil.recursiveResolveProperty( "${${key1}}", m_context, false );
235         assertEquals( result, OBJ1 );
236     }
237
238     public void testKey2AsString()
239         throws PropertyException
240     {
241         final Object JavaDoc result =
242             PropertyUtil.recursiveResolveProperty( "${${key2}} ", m_context, false );
243         assertEquals( result, OBJ2 + " " );
244     }
245
246     public void testKey54Peers()
247         throws PropertyException
248     {
249         final Object JavaDoc result =
250             PropertyUtil.recursiveResolveProperty( "${${key5}${key3}} ", m_context, false );
251         assertEquals( result, OBJ2 + " " );
252     }
253
254     public void testKey64Recursive()
255         throws PropertyException
256     {
257         final Object JavaDoc result =
258             PropertyUtil.recursiveResolveProperty( "${${${key6}${key3}}} ", m_context, false );
259         assertEquals( result, OBJ2 + " " );
260     }
261 /*
262   "key1", "obj1"
263   "key2", "obj2"
264   "key3", "2"
265   "key4", "1"
266   "key5", "obj"
267   "key6" "key"
268  */

269 }
270
Popular Tags