1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.chain.impl;
17  
18  
19  import junit.framework.Test;
20  import junit.framework.TestSuite;
21  import org.apache.commons.chain.Context;
22  
23  
24  /***
25   * Extension of <code>ContextBaseTestCase</code> to validate property
26   * delegation.
27   */
28  
29  public class TestContextTestCase extends ContextBaseTestCase {
30  
31  
32      // ------------------------------------------------------------ Constructors
33  
34      /***
35       * Construct a new instance of this test case.
36       *
37       * @param name Name of the test case
38       */
39      public TestContextTestCase(String name) {
40          super(name);
41      }
42  
43  
44      // ---------------------------------------------------- Overall Test Methods
45  
46  
47      /***
48       * Set up instance variables required by this test case.
49       */
50      public void setUp() {
51          context = createContext();
52      }
53  
54  
55      /***
56       * Return the tests included in this test suite.
57       */
58      public static Test suite() {
59          return (new TestSuite(TestContextTestCase.class));
60      }
61  
62  
63      // ------------------------------------------------- Individual Test Methods
64  
65  
66      // Test state of newly created instance
67      public void testPristine() {
68  
69          super.testPristine();
70          assertEquals("readOnly", (String) context.get("readOnly"));
71          assertEquals("readWrite", (String) context.get("readWrite"));
72          assertEquals("writeOnly", ((TestContext) context).returnWriteOnly());
73  
74      }
75  
76  
77      // Test a read only property on the Context implementation class
78      public void testReadOnly() {
79  
80          Object readOnly = context.get("readOnly");
81          assertNotNull("readOnly found", readOnly);
82          assertTrue("readOnly String",
83                     readOnly instanceof String);
84          assertEquals("readOnly value", "readOnly", readOnly);
85  
86          try {
87              context.put("readOnly", "new readOnly");
88              fail("Should have thrown UnsupportedOperationException");
89          } catch (UnsupportedOperationException e) {
90              ; // Expected result
91          }
92          assertEquals("readOnly unchanged", "readOnly",
93                       (String) context.get("readOnly"));
94  
95      }
96  
97  
98      // Test a read write property on the Context implementation class
99      public void testReadWrite() {
100 
101         Object readWrite = context.get("readWrite");
102         assertNotNull("readWrite found", readWrite);
103         assertTrue("readWrite String",
104                    readWrite instanceof String);
105         assertEquals("readWrite value", "readWrite", readWrite);
106 
107         context.put("readWrite", "new readWrite");
108         readWrite = context.get("readWrite");
109         assertNotNull("readWrite found", readWrite);
110         assertTrue("readWrite String",
111                    readWrite instanceof String);
112         assertEquals("readWrite value", "new readWrite", readWrite);
113 
114     }
115 
116 
117     // Test a write only property on the Context implementation class
118     public void testWriteOnly() {
119 
120         Object writeOnly = ((TestContext) context).returnWriteOnly();
121         assertNotNull("writeOnly found", writeOnly);
122         assertTrue("writeOnly String",
123                    writeOnly instanceof String);
124         assertEquals("writeOnly value", "writeOnly", writeOnly);
125 
126         context.put("writeOnly", "new writeOnly");
127         writeOnly = ((TestContext) context).returnWriteOnly();
128         assertNotNull("writeOnly found", writeOnly);
129         assertTrue("writeOnly String",
130                    writeOnly instanceof String);
131         assertEquals("writeOnly value", "new writeOnly", writeOnly);
132 
133     }
134 
135 
136     // ------------------------------------------------------- Protected Methods
137 
138 
139     // Create a new instance of the appropriate Context type for this test case
140     protected Context createContext() {
141         return (new TestContext());
142     }
143 
144 
145 }