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