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: $Header:$
018: */
019: package org.apache.beehive.controls.test.junit;
020:
021: import junit.framework.TestCase;
022:
023: import org.apache.beehive.controls.api.bean.ControlBean;
024: import org.apache.beehive.controls.api.context.ControlContainerContext;
025: import org.apache.beehive.controls.api.context.ControlThreadContext;
026: import org.apache.beehive.controls.test.container.ControlTestContainerContext;
027: import org.apache.beehive.controls.test.util.ControlContainerContextManager;
028: import org.apache.beehive.controls.test.util.ControlContainerContextManagerFactory;
029: import org.apache.beehive.controls.test.ControlTestException;
030:
031: /**
032: * Base control test case.
033: */
034: public abstract class ControlTestCase extends TestCase {
035:
036: /* todo: push strings into a .properties file */
037:
038: private ControlContainerContextManager _controlContainerContextManager = null;
039:
040: public void setUp() throws Exception {
041:
042: super .setUp();
043:
044: beginContext();
045: initializeControls();
046: }
047:
048: public void tearDown() throws Exception {
049:
050: super .tearDown();
051:
052: endContext();
053: }
054:
055: protected ControlContainerContext initializeControlContainerContext() {
056: return new ControlTestContainerContext();
057: }
058:
059: protected ControlContainerContext getControlContainerContext() {
060: return getControlContainerContextManager()
061: .getControlContainerContext();
062: }
063:
064: protected void initializeControls() {
065: getControlContainerContextManager().instantiateControls(this );
066: }
067:
068: protected void beginContext() {
069: getControlContainerContextManager().beginContext();
070: }
071:
072: protected void endContext() {
073: getControlContainerContextManager().endContext();
074: }
075:
076: protected ControlContainerContextManager getControlContainerContextManager() {
077: if (_controlContainerContextManager == null) {
078: ControlContainerContext ccc = initializeControlContainerContext();
079:
080: if (ccc == null)
081: throw new ControlTestException(
082: "Could not instantiate a ControlContainerContextManager as the control container context was null");
083:
084: _controlContainerContextManager = ControlContainerContextManagerFactory
085: .getInstance(ccc);
086: }
087:
088: return _controlContainerContextManager;
089: }
090:
091: protected ControlBean instantiateControl(String className) {
092: ClassLoader classLoader = Thread.currentThread()
093: .getContextClassLoader();
094: try {
095: Object controlBean = java.beans.Beans.instantiate(
096: classLoader, className, ControlThreadContext
097: .getContext());
098: assert controlBean instanceof ControlBean;
099: return (ControlBean) controlBean;
100: } catch (Exception e) {
101: throw new ControlTestException(
102: "Could not instantiate control with class \""
103: + className + "\". Cause: "
104: + e.getMessage(), e);
105: }
106: }
107: }
|