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 */
017package org.apache.commons.chain.impl;
018
019import static org.junit.jupiter.api.Assertions.assertEquals;
020import static org.junit.jupiter.api.Assertions.assertInstanceOf;
021import static org.junit.jupiter.api.Assertions.assertNotNull;
022import static org.junit.jupiter.api.Assertions.fail;
023
024import org.apache.commons.chain.ContextTestCase;
025import org.junit.jupiter.api.Test;
026
027/**
028 * Extension of {@code ContextBaseTestCase} to validate property
029 * delegation.
030 */
031public class TestContextTestCase extends ContextTestCase<TestContext> {
032
033    // ------------------------------------------------- Individual Test Methods
034
035    /**
036     * Test state of newly created instance
037     */
038    @Override
039    @Test
040    public void testPristine() {
041        super.testPristine();
042        assertEquals("readOnly", context.get("readOnly"));
043        assertEquals("readWrite", context.get("readWrite"));
044        assertEquals("writeOnly", context.returnWriteOnly());
045    }
046
047    /**
048     * Test a read only property on the Context implementation class
049     */
050    @Test
051    public void testReadOnly() {
052        Object readOnly = context.get("readOnly");
053        assertNotNull(readOnly, "readOnly found");
054        assertInstanceOf(String.class, readOnly,
055                         "readOnly String");
056        assertEquals("readOnly", readOnly, "readOnly value");
057
058        try {
059            context.put("readOnly", "new readOnly");
060            fail("Should have thrown UnsupportedOperationException");
061        } catch (UnsupportedOperationException e) {
062            ; // Expected result
063        }
064        assertEquals("readOnly", context.get("readOnly"),
065                     "readOnly unchanged");
066    }
067
068    /**
069     * Test a read write property on the Context implementation class
070     */
071    @Test
072    public void testReadWrite() {
073        Object readWrite = context.get("readWrite");
074        assertNotNull(readWrite, "readWrite found");
075        assertInstanceOf(String.class, readWrite,
076                         "readWrite String");
077        assertEquals("readWrite", readWrite, "readWrite value");
078
079        context.put("readWrite", "new readWrite");
080        readWrite = context.get("readWrite");
081        assertNotNull(readWrite, "readWrite found");
082        assertInstanceOf(String.class, readWrite,
083                         "readWrite String");
084        assertEquals("new readWrite", readWrite, "readWrite value");
085    }
086
087    /**
088     * Test a write only property on the Context implementation class
089     */
090    @Test
091    public void testWriteOnly() {
092        Object writeOnly = context.returnWriteOnly();
093        assertNotNull(writeOnly, "writeOnly found");
094        assertInstanceOf(String.class, writeOnly,
095                         "writeOnly String");
096        assertEquals("writeOnly", writeOnly, "writeOnly value");
097
098        context.put("writeOnly", "new writeOnly");
099        writeOnly = context.returnWriteOnly();
100        assertNotNull(writeOnly, "writeOnly found");
101        assertInstanceOf(String.class, writeOnly,
102                         "writeOnly String");
103        assertEquals("new writeOnly", writeOnly, "writeOnly value");
104    }
105
106    // ------------------------------------------------------- Protected Methods
107
108    /**
109     * Create a new instance of the appropriate Context type for this test case
110     */
111    @Override
112    protected TestContext createContext() {
113        return new TestContext();
114    }
115}