1 51 package org.apache.avalon.framework.context.test; 52 53 import junit.framework.AssertionFailedError; 54 import junit.framework.TestCase; 55 56 import org.apache.avalon.framework.context.Context; 57 import org.apache.avalon.framework.context.ContextException; 58 import org.apache.avalon.framework.context.DefaultContext; 59 import org.apache.avalon.framework.context.Resolvable; 60 61 67 public class ContextTestCase 68 extends TestCase 69 { 70 private static class ResolvableString implements Resolvable 71 { 72 private final String m_content; 73 74 public ResolvableString( final String content ) 75 { 76 this.m_content = content; 77 } 78 79 public ResolvableString() 80 { 81 this( "This is a ${test}." ); 82 } 83 84 public final Object resolve( final Context context ) 85 throws ContextException 86 { 87 int index = this.m_content.indexOf( "${" ); 88 89 if ( index < 0 ) 90 { 91 return this.m_content; 92 } 93 94 StringBuffer buf = new StringBuffer ( this.m_content.substring( 0, index ) ); 95 96 while ( index >= 0 && index <= this.m_content.length() ) 97 { 98 index += 2; 99 int end = this.m_content.indexOf( "}", index); 100 101 if ( end < 0 ) 102 { 103 end = this.m_content.length(); 104 } 105 106 buf.append( context.get( this.m_content.substring( index, end ) ) ); 107 end++; 108 109 index = this.m_content.indexOf( "${", end ) + 2; 110 111 if ( index < 2 ) 112 { 113 index = -1; 114 buf.append( this.m_content.substring( end, this.m_content.length() ) ); 115 } 116 117 if ( index >=0 && index <= this.m_content.length() ) 118 { 119 buf.append( this.m_content.substring( end, index ) ); 120 } 121 } 122 123 return buf.toString(); 124 } 125 } 126 127 public ContextTestCase( final String name ) 128 { 129 super( name ); 130 } 131 132 public void testAddContext() 133 throws Exception 134 { 135 final DefaultContext context = new DefaultContext(); 136 context.put( "key1", "value1" ); 137 assertTrue( "value1".equals( context.get( "key1" ) ) ); 138 context.put( "key1", "" ); 139 assertTrue( "".equals( context.get( "key1" ) ) ); 140 141 context.put( "key1", "value1" ); 142 context.makeReadOnly(); 143 144 try 145 { 146 context.put( "key1", "" ); 147 throw new AssertionFailedError( "You are not allowed to change a value after it has been made read only" ); 148 } 149 catch ( IllegalStateException ise ) 150 { 151 assertTrue( "Value is null", "value1".equals( context.get( "key1" ) ) ); 152 } 153 } 154 155 public void testResolveableObject() 156 throws ContextException 157 { 158 final DefaultContext context = new DefaultContext(); 159 context.put( "key1", new ResolvableString() ); 160 context.put( "test", "Cool Test" ); 161 context.makeReadOnly(); 162 163 final Context newContext = (Context) context; 164 assertTrue( "Cool Test".equals( newContext.get( "test" ) ) ); 165 assertTrue( ! "This is a ${test}.".equals( newContext.get( "key1" ) ) ); 166 assertTrue( "This is a Cool Test.".equals( newContext.get( "key1" ) ) ); 167 } 168 169 public void testCascadingContext() 170 throws ContextException 171 { 172 final DefaultContext parent = new DefaultContext(); 173 parent.put( "test", "ok test" ); 174 parent.makeReadOnly(); 175 final DefaultContext child = new DefaultContext( parent ); 176 child.put( "check", new ResolvableString("This is an ${test}.") ); 177 child.makeReadOnly(); 178 final Context context = (Context) child; 179 180 assertTrue ( "ok test".equals( context.get( "test" ) ) ); 181 assertTrue ( ! "This is an ${test}.".equals( context.get( "check" ) ) ); 182 assertTrue ( "This is an ok test.".equals( context.get( "check" ) ) ); 183 } 184 185 public void testHiddenItems() 186 throws ContextException 187 { 188 final DefaultContext parent = new DefaultContext(); 189 parent.put( "test", "test" ); 190 parent.makeReadOnly(); 191 final DefaultContext child = new DefaultContext( parent ); 192 child.put( "check", "check" ); 193 final Context context = (Context) child; 194 195 assertTrue ( "check".equals( context.get( "check" ) ) ); 196 assertTrue ( "test".equals( context.get( "test" ) ) ); 197 198 child.hide( "test" ); 199 try 200 { 201 context.get( "test" ); 202 fail( "The item \"test\" was hidden in the child context, but could still be retrieved via get()." ); 203 } 204 catch (ContextException ce) 205 { 206 } 208 209 child.makeReadOnly(); 210 211 try 212 { 213 child.hide( "test" ); 214 fail( "hide() did not throw an exception, even though the context is supposed to be read-only." ); 215 } 216 catch (IllegalStateException ise) 217 { 218 } 220 } 221 } 222 | Popular Tags |