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