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 java.util.HashMap;
20  import java.util.Map;
21  
22  import junit.framework.AssertionFailedError;
23  import junit.framework.TestCase;
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 extends TestCase {
32  
33      public DefaultContextTest(String name) {
34          super(name);
35      }
36  
37      public void testContextCreationWithMap() throws Exception {
38          Map map = new HashMap();
39  
40          map.put("name", "jason");
41  
42          DefaultContext context = new DefaultContext(map);
43  
44          assertEquals("jason", (String) context.get("name"));
45  
46          assertEquals(map, context.getContextData());
47  
48          // Test removal
49          context.put("name", null);
50  
51          // There is no data and no parent containerContext.
52          try {
53              context.get("name");
54          } catch (ContextException e) {
55              // do nothing
56          }
57      }
58  
59      public void testAddContext() throws Exception {
60          DefaultContext context = new DefaultContext();
61          context.put("key1", "value1");
62          assertTrue("value1".equals(context.get("key1")));
63          context.put("key1", "");
64          assertTrue("".equals(context.get("key1")));
65  
66          context.put("key1", "value1");
67          context.makeReadOnly();
68  
69          try {
70              context.put("key1", "");
71              throw new AssertionFailedError("You are not allowed to change a value after it has been made read only");
72          } catch (IllegalStateException ise) {
73              assertTrue("Value is null", "value1".equals(context.get("key1")));
74          }
75      }
76  
77      public void testHiddenItems() throws ContextException {
78          // initalize
79          DefaultContext context = new DefaultContext();
80          context.put("test", "test");
81  
82          // verify inital state
83          assertTrue("test".equals(context.get("test")));
84  
85          // hide value and verify
86          context.hide("test");
87          try {
88              context.get("test");
89              fail("The item \"test\" was hidden in the child containerContext, but could still be retrieved via get().");
90          } catch (ContextException ce) {
91              assertTrue(true);
92          }
93  
94          // reset to inital state and verify
95          context.put("test", "test");
96          assertTrue("test".equals(context.get("test")));
97  
98          // mark context read-only and verify that item can not be hidden
99          context.makeReadOnly();
100         try {
101             context.hide("test");
102             fail("hide() did not throw an exception, even though the containerContext is supposed to be read-only.");
103         } catch (IllegalStateException ise) {
104             assertTrue(true);
105         }
106 
107         // verify state did not change in failed hide() invocation
108         assertTrue("test".equals(context.get("test")));
109     }
110 }