001    package org.apache.fulcrum.testcontainer;
002    /*
003     * Licensed to the Apache Software Foundation (ASF) under one
004     * or more contributor license agreements.  See the NOTICE file
005     * distributed with this work for additional information
006     * regarding copyright ownership.  The ASF licenses this file
007     * to you under the Apache License, Version 2.0 (the
008     * "License"); you may not use this file except in compliance
009     * with the License.  You may obtain a copy of the License at
010     *
011     *   http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing,
014     * software distributed under the License is distributed on an
015     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016     * KIND, either express or implied.  See the License for the
017     * specific language governing permissions and limitations
018     * under the License.
019     */
020    import junit.framework.TestCase;
021    
022    import org.apache.avalon.framework.component.ComponentException;
023    /**
024     * Base class for unit tests for components. This version doesn't load the container until the
025     * first request for a component. This allows the tester to populate the configurationFileName and
026     * roleFileName, possible one per test.
027     *
028     * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
029     * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
030     * @version $Id: BaseUnitTest.java 535465 2007-05-05 06:58:06Z tv $
031     */
032    public class BaseUnitTest extends TestCase
033    {
034    
035        public static final String CONTAINER_ECM="CONTAINER_ECM";
036        public static final String CONTAINER_YAAFI="CONTAINER_YAAFI";
037        /** Key used in the context for defining the application root */
038        public static String COMPONENT_APP_ROOT = Container.COMPONENT_APP_ROOT;
039    
040        /** Pick the default container to be Yaafi **/
041        public static String containerType = CONTAINER_YAAFI;
042        /** Container for the components */
043        private Container container;
044        /** Setup our default configurationFileName */
045        private String configurationFileName = "src/test/TestComponentConfig.xml";
046        /** Setup our default roleFileName */
047        private String roleFileName = "src/test/TestRoleConfig.xml";
048        /** Setup our default parameterFileName */
049        private String parameterFileName = null;
050    
051        /**
052             * Gets the configuration file name for the container should use for this test. By default it
053             * is src/test/TestComponentConfig.
054             *
055             * @param configurationFileName
056             */
057        protected void setConfigurationFileName(String configurationFileName)
058        {
059            this.configurationFileName = configurationFileName;
060        }
061    
062        /**
063             * Override the role file name for the container should use for this test. By default it is
064             * src/test/TestRoleConfig.
065             *
066             * @param roleFileName
067             */
068        protected void setRoleFileName(String roleFileName)
069        {
070            this.roleFileName = roleFileName;
071        }
072    
073        /**
074             * Constructor for test.
075             *
076             * @param testName name of the test being executed
077             */
078        public BaseUnitTest(String testName)
079        {
080            super(testName);
081        }
082    
083        /**
084             * Clean up after each test is run.
085             */
086        protected void tearDown()
087        {
088            if (container != null)
089            {
090                container.dispose();
091            }
092            container = null;
093        }
094        /**
095             * Gets the configuration file name for the container should use for this test.
096             *
097             * @return The filename of the configuration file
098             */
099        protected String getConfigurationFileName()
100        {
101            return configurationFileName;
102        }
103        /**
104             * Gets the role file name for the container should use for this test.
105             *
106             * @return The filename of the role configuration file
107             */
108        protected String getRoleFileName()
109        {
110            return roleFileName;
111        }
112        /**
113         * Gets the parameter file name for the container should use for this test.
114         *
115         * @return The filename of the role configuration file
116         */
117        protected String getParameterFileName()
118        {
119            return parameterFileName;
120        }
121        /**
122             * Returns an instance of the named component. Starts the container if it hasn't been started.
123             *
124             * @param roleName Name of the role the component fills.
125             * @throws ComponentException generic exception
126             */
127        protected Object lookup(String roleName) throws ComponentException
128        {
129            if (container == null)
130            {
131                if(containerType.equals(CONTAINER_ECM)){
132                    container = new ECMContainer();
133                }
134                else {
135                    container = new YAAFIContainer();
136                }
137                container.startup(getConfigurationFileName(), getRoleFileName(),getParameterFileName());
138            }
139            return container.lookup(roleName);
140        }
141    
142        /**
143         * Helper method for converting to and from Merlin Unit TestCase.
144         * @param roleName
145         * @return
146         * @throws ComponentException
147         */
148        protected Object resolve(String roleName) throws ComponentException
149        {
150            return lookup(roleName);
151        }
152        /**
153             * Releases the component
154             *
155             * @param component
156             */
157        protected void release(Object component)
158        {
159            if (container != null)
160            {
161                container.release(component);
162            }
163        }
164    }