001// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.ioc.test;
016
017import org.apache.tapestry5.ioc.annotations.Inject;
018import org.apache.tapestry5.ioc.def.ModuleDef;
019import org.junit.runner.Description;
020import org.junit.runner.Result;
021import org.junit.runner.notification.Failure;
022import org.junit.runner.notification.RunListener;
023import org.junit.runner.notification.RunNotifier;
024import org.junit.runner.notification.StoppedByUserException;
025import org.junit.runners.BlockJUnit4ClassRunner;
026import org.junit.runners.model.InitializationError;
027import org.junit.runners.model.Statement;
028
029/**
030 * <p>
031 * A JUnit4ClassRunner to help with Tapestry IOC integration tests. The test
032 * runner requires a registry configuration to be defined in a {@link Registry}
033 * annotation. A {@link RegistryShutdownType} can be specified to configure the
034 * lifecycle of the test registry and it's services
035 * </p>
036 * 
037 * <p>
038 * {@link ModuleDef}s can be added to the
039 * {@link org.apache.tapestry5.ioc.Registry} by annotating a factory method(s)
040 * with {@link org.apache.tapestry5.ioc.test.ModuleDef}. These {@link ModuleDef}
041 * factory methods must be
042 * <ul>
043 * <li>public</li>
044 * <li>static</li>
045 * <li>take zero arguments</li>
046 * <li>return a subclass of {@link ModuleDef}</li>
047 * </ul>
048 * </p>
049 * 
050 * <p>
051 * Any services defined in the registry can be {@link Inject}ed into the test
052 * class to be used during testing.
053 * </p>
054 */
055public class TapestryIOCJUnit4ClassRunner extends BlockJUnit4ClassRunner {
056        private final TestRegistryManager registryManager;
057
058        public TapestryIOCJUnit4ClassRunner(Class<?> type) throws InitializationError {
059                super(type);
060                this.registryManager = new TestRegistryManager(type);
061        }
062
063        @Override
064        public void run(RunNotifier notifier) {
065                RunNotifier wrapper = new RegistryManagerRunNotifier(registryManager, notifier);
066                super.run(wrapper);
067        }
068
069        @Override
070        protected Statement withAfterClasses(Statement statement) {
071                final Statement superStatement = super.withAfterClasses(statement);
072                return new Statement() {
073                        public void evaluate() throws Throwable {
074                                superStatement.evaluate();
075                                registryManager.afterTestClass();
076                        }
077                };
078        }
079
080        @Override
081        protected Object createTest() throws Exception {
082                org.apache.tapestry5.ioc.Registry registry = registryManager.getOrCreateRegistry();
083                return registry.autobuild(getTestClass().getJavaClass());
084        }
085
086        public static class RegistryManagerRunNotifier extends RunNotifier {
087                private final RunNotifier delegate;
088                private final TestRegistryManager registryManager;
089
090                public RegistryManagerRunNotifier(TestRegistryManager registryManager, RunNotifier delegate) {
091                        super();
092                        this.delegate = delegate;
093                        this.registryManager = registryManager;
094                }
095                
096                public void addListener(RunListener listener) {
097                        delegate.addListener(listener);
098                }
099
100                public void removeListener(RunListener listener) {
101                        delegate.removeListener(listener);
102                }
103
104                public void fireTestRunStarted(Description description) {
105                        delegate.fireTestRunStarted(description);
106                }
107
108                public void fireTestRunFinished(Result result) {
109                        delegate.fireTestRunFinished(result);
110                }
111
112                public void fireTestStarted(Description description) throws StoppedByUserException {
113                        delegate.fireTestStarted(description);
114                }
115
116                public void fireTestFailure(Failure failure) {
117                        delegate.fireTestFailure(failure);
118                }
119
120                public void fireTestAssumptionFailed(Failure failure) {
121                        delegate.fireTestAssumptionFailed(failure);
122                }
123
124                public void fireTestIgnored(Description description) {
125                        delegate.fireTestIgnored(description);
126                }
127
128                public void fireTestFinished(Description description) {
129                        registryManager.afterTestMethod();
130                        delegate.fireTestFinished(description);
131                }
132
133                public void pleaseStop() {
134                        delegate.pleaseStop();
135                }
136
137                public void addFirstListener(RunListener listener) {
138                        delegate.addFirstListener(listener);
139                }
140        }
141}