1 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 21 public final class PropertyUtilTestCase 22 extends TestCase 23 { 24 public PropertyUtilTestCase( final String 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 resolve( final Context context ) 41 { 42 m_current++; 43 44 if( m_current >= m_count ) return new Integer ( m_count ); 45 else return this; 46 } 47 } 48 49 private final static Object OBJ1 = new Object (); 50 private final static Object OBJ2 = new Object (); 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 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 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 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 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 result = 112 PropertyUtil.resolveProperty( "${res1}", m_context, false ); 113 114 assertEquals( result, new Integer ( 1 ) ); 115 } 116 117 public void testRecurseObjResolve2() 118 throws PropertyException 119 { 120 final Object result = 121 PropertyUtil.resolveProperty( "${res2}", m_context, false ); 122 123 assertEquals( result, new Integer ( 2 ) ); 124 } 125 126 public void testNullObjResolve() 127 throws PropertyException 128 { 129 final Object 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 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 152 public void testRecursiveNoResolve() 153 throws PropertyException 154 { 155 final Object 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 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 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 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 result = 192 PropertyUtil.recursiveResolveProperty( "${res1}", m_context, false ); 193 194 assertEquals( result, new Integer ( 1 ) ); 195 } 196 197 public void testRecursiveRecurseObjResolve2() 198 throws PropertyException 199 { 200 final Object result = 201 PropertyUtil.recursiveResolveProperty( "${res2}", m_context, false ); 202 203 assertEquals( result, new Integer ( 2 ) ); 204 } 205 206 public void testRecursiveNullObjResolve() 207 throws PropertyException 208 { 209 final Object 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 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 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 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 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 result = 258 PropertyUtil.recursiveResolveProperty( "${${${key6}${key3}}} ", m_context, false ); 259 assertEquals( result, OBJ2 + " " ); 260 } 261 269 } 270 | Popular Tags |