You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by le...@apache.org on 2002/05/12 15:30:08 UTC

cvs commit: jakarta-avalon/src/test/org/apache/avalon/framework/context/test ContextTestCase.java

leosutic    02/05/12 06:30:08

  Modified:    src/java/org/apache/avalon/framework/context
                        DefaultContext.java
               src/test/org/apache/avalon/framework/context/test
                        ContextTestCase.java
  Log:
  Added methods for hiding values in parent context.
  
  Revision  Changes    Path
  1.11      +29 -1     jakarta-avalon/src/java/org/apache/avalon/framework/context/DefaultContext.java
  
  Index: DefaultContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/context/DefaultContext.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- DefaultContext.java	12 May 2002 11:32:13 -0000	1.10
  +++ DefaultContext.java	12 May 2002 13:30:08 -0000	1.11
  @@ -7,6 +7,7 @@
    */
   package org.apache.avalon.framework.context;
   
  +import java.io.Serializable;
   import java.util.Hashtable;
   import java.util.Map;
   
  @@ -18,10 +19,15 @@
    * @author <a href="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
    * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  + * @author <a href="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
    */
   public class DefaultContext
  -    implements Context
  +    implements Context, Serializable
   {
  +    private final static class Hidden implements Serializable {}
  +    
  +    private final static Hidden m_hiddenMarker = new Hidden ();
  +    
       private final Map m_contextData;
       private final Context m_parent;
       private boolean m_readOnly;
  @@ -81,6 +87,12 @@
   
           if( null != data )
           {
  +            if( data instanceof Hidden )
  +            {
  +                // Always fail.
  +                throw new ContextException( "Unable to locate " + key );
  +            }
  +            
               if( data instanceof Resolvable )
               {
                   return ( (Resolvable)data ).resolve( this );
  @@ -120,6 +132,22 @@
           }
       }
   
  +    /**
  +     * Hides the item in the context.
  +     * After remove(key) has been called, a get(key)
  +     * will always fail, even if the parent context
  +     * has such a mapping.
  +     *
  +     * @param key the items key
  +     * @throws IllegalStateException if context is read only
  +     */
  +    public void hide( final Object key ) 
  +        throws IllegalStateException
  +    {
  +        checkWriteable();
  +        m_contextData.put( key, m_hiddenMarker );
  +    }
  +    
       /**
        * Utility method to retrieve context data.
        *
  
  
  
  1.3       +38 -0     jakarta-avalon/src/test/org/apache/avalon/framework/context/test/ContextTestCase.java
  
  Index: ContextTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon/src/test/org/apache/avalon/framework/context/test/ContextTestCase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ContextTestCase.java	11 Dec 2001 09:00:48 -0000	1.2
  +++ ContextTestCase.java	12 May 2002 13:30:08 -0000	1.3
  @@ -21,6 +21,7 @@
    * TestCase for Context.
    *
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
  + * @author <a href="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
    */
   public class ContextTestCase
       extends TestCase
  @@ -138,5 +139,42 @@
           assertTrue ( "ok test".equals( context.get( "test" ) ) );
           assertTrue ( ! "This is an ${test}.".equals( context.get( "check" ) ) );
           assertTrue ( "This is an ok test.".equals( context.get( "check" ) ) );
  +    }
  +    
  +    public void testHiddenItems()
  +        throws ContextException
  +    {
  +        final DefaultContext parent = new DefaultContext();
  +        parent.put( "test", "test" );
  +        parent.makeReadOnly();
  +        final DefaultContext child = new DefaultContext( parent );
  +        child.put( "check", "check" );
  +        final Context context = (Context) child;
  +        
  +        assertTrue ( "check".equals( context.get( "check" ) ) );
  +        assertTrue ( "test".equals( context.get( "test" ) ) );
  +                
  +        child.hide( "test" );
  +        try 
  +        {
  +            context.get( "test" );
  +            fail( "The item \"test\" was hidden in the child context, but could still be retrieved via get()." );
  +        }
  +        catch (ContextException ce)
  +        {
  +            // Supposed to be thrown.
  +        }
  +        
  +        child.makeReadOnly();
  +        
  +        try 
  +        {
  +            child.hide( "test" );
  +            fail( "hide() did not throw an exception, even though the context is supposed to be read-only." );
  +        }
  +        catch (IllegalStateException ise)
  +        {
  +            // Supposed to be thrown.
  +        }
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>