KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > context > test > ContextTestCase


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6  
7  Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
8  
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11  
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14  
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18  
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24  
25  4. The names "Jakarta", "Apache Avalon", "Avalon Excalibur", "Avalon
26     Framework" and "Apache Software Foundation" must not be used to endorse
27     or promote products derived from this software without prior written
28     permission. For written permission, please contact apache@apache.org.
29  
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33  
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation and was originally created by
47  Stefano Mazzocchi <stefano@apache.org>. For more information on the Apache
48  Software Foundation, please see <http://www.apache.org/>.
49  
50 */

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 /**
62  * TestCase for Context.
63  *
64  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
65  * @author <a HREF="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
66  */

67 public class ContextTestCase
68     extends TestCase
69 {
70     private static class ResolvableString implements Resolvable
71     {
72         private final String JavaDoc m_content;
73
74         public ResolvableString( final String JavaDoc 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 JavaDoc 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 JavaDoc buf = new StringBuffer JavaDoc( 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 JavaDoc name )
128     {
129         super( name );
130     }
131
132     public void testAddContext()
133         throws Exception JavaDoc
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 JavaDoc 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             // Supposed to be thrown.
207
}
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 JavaDoc ise)
217         {
218             // Supposed to be thrown.
219
}
220     }
221 }
222
Popular Tags