001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.chain.impl;
018
019
020 import junit.framework.Test;
021 import junit.framework.TestSuite;
022 import org.apache.commons.chain.Context;
023
024
025 /**
026 * Extension of <code>ContextBaseTestCase</code> to validate property
027 * delegation.
028 */
029
030 public class TestContextTestCase extends ContextBaseTestCase {
031
032
033 // ------------------------------------------------------------ Constructors
034
035 /**
036 * Construct a new instance of this test case.
037 *
038 * @param name Name of the test case
039 */
040 public TestContextTestCase(String name) {
041 super(name);
042 }
043
044
045 // ---------------------------------------------------- Overall Test Methods
046
047
048 /**
049 * Set up instance variables required by this test case.
050 */
051 public void setUp() {
052 context = createContext();
053 }
054
055
056 /**
057 * Return the tests included in this test suite.
058 */
059 public static Test suite() {
060 return (new TestSuite(TestContextTestCase.class));
061 }
062
063
064 // ------------------------------------------------- Individual Test Methods
065
066
067 // Test state of newly created instance
068 public void testPristine() {
069
070 super.testPristine();
071 assertEquals("readOnly", (String) context.get("readOnly"));
072 assertEquals("readWrite", (String) context.get("readWrite"));
073 assertEquals("writeOnly", ((TestContext) context).returnWriteOnly());
074
075 }
076
077
078 // Test a read only property on the Context implementation class
079 public void testReadOnly() {
080
081 Object readOnly = context.get("readOnly");
082 assertNotNull("readOnly found", readOnly);
083 assertTrue("readOnly String",
084 readOnly instanceof String);
085 assertEquals("readOnly value", "readOnly", readOnly);
086
087 try {
088 context.put("readOnly", "new readOnly");
089 fail("Should have thrown UnsupportedOperationException");
090 } catch (UnsupportedOperationException e) {
091 ; // Expected result
092 }
093 assertEquals("readOnly unchanged", "readOnly",
094 (String) context.get("readOnly"));
095
096 }
097
098
099 // 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 }