View Javadoc
1   package org.codehaus.plexus.context;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import junit.framework.AssertionFailedError;
20  import junit.framework.TestCase;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  /**
26   * TestCase for Context.
27   *
28   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
29   * @author <a href="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
30   */
31  public class DefaultContextTest
32      extends TestCase
33  {
34  
35      public DefaultContextTest( String name )
36      {
37          super( name );
38      }
39  
40      public void testContextCreationWithMap()
41          throws Exception
42      {
43          Map map = new HashMap();
44  
45          map.put( "name", "jason" );
46  
47          DefaultContext context = new DefaultContext( map );
48  
49          assertEquals( "jason", (String) context.get( "name" ) );
50  
51          assertEquals( map, context.getContextData() );
52  
53          // Test removal
54          context.put( "name", null );
55  
56          // There is no data and no parent containerContext.
57          try
58          {
59              context.get( "name" );
60          }
61          catch ( ContextException e )
62          {
63              // do nothing
64          }
65  
66  
67      }
68  
69      public void testAddContext()
70          throws Exception
71      {
72          DefaultContext context = new DefaultContext();
73          context.put( "key1", "value1" );
74          assertTrue( "value1".equals( context.get( "key1" ) ) );
75          context.put( "key1", "" );
76          assertTrue( "".equals( context.get( "key1" ) ) );
77  
78          context.put( "key1", "value1" );
79          context.makeReadOnly();
80  
81          try
82          {
83              context.put( "key1", "" );
84              throw new AssertionFailedError( "You are not allowed to change a value after it has been made read only" );
85          }
86          catch ( IllegalStateException ise )
87          {
88              assertTrue( "Value is null", "value1".equals( context.get( "key1" ) ) );
89          }
90      }
91  
92      public void testHiddenItems()
93          throws ContextException
94      {
95          // initalize
96          DefaultContext context = new DefaultContext();
97          context.put( "test", "test" );
98  
99          // verify inital state
100         assertTrue( "test".equals( context.get( "test" ) ) );
101 
102         // hide value and verify
103         context.hide( "test" );
104         try
105         {
106             context.get( "test" );
107             fail( "The item \"test\" was hidden in the child containerContext, but could still be retrieved via get()." );
108         }
109         catch ( ContextException ce )
110         {
111             assertTrue( true );
112         }
113 
114         // reset to inital state and verify
115         context.put( "test", "test" );
116         assertTrue( "test".equals( context.get( "test" ) ) );
117 
118         // mark context read-only and verify that item can not be hidden
119         context.makeReadOnly();
120         try
121         {
122             context.hide( "test" );
123             fail( "hide() did not throw an exception, even though the containerContext is supposed to be read-only." );
124         }
125         catch ( IllegalStateException ise )
126         {
127             assertTrue( true );
128         }
129 
130         // verify state did not change in failed hide() invocation
131         assertTrue( "test".equals( context.get( "test" ) ) );
132     }
133 }