001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.runtime.test.runner;
020
021import org.junit.runners.model.FrameworkMethod;
022
023import com.google.inject.Binder;
024
025/**
026 * These are the states the runner goes through when using runtime feature:
027 *
028 * <pre>
029 * CREATE FRAMEWORK
030 * new feature()        --&gt; constructor
031 * COLLECT DEFINED DEPLOYMENTS
032 * feature.initialize() --&gt; can be used to configure nuxeo home or register JNDI objects
033 * START FRAMEWORK
034 * feature.start()
035 * feature.beforeRun()
036 * feature.configure() --&gt; can be used to add guice bindings and to dynamically deploy components using the harness
037 * for each test method:
038 *   feature.testCreated()
039 *   feature.beforeSetup
040 *   feature.beforeMethodRun()  --&gt; test method interceptor
041 *   testMethod()
042 *   feature.afterMethodRun()   --&gt; test method interceptor
043 *   feature.afterTeardown()
044 * feature.afterRun() --&gt; cleanup that require framework to be started
045 * STOP FRAMEWORK
046 * feature.stop()  --&gt; destructor
047 * </pre>
048 *
049 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
050 */
051public interface RunnerFeature {
052
053    /**
054     * Called when preparing to run the test class. Framework is not started at this point. Here is time for the feature
055     * to configure the runner from annotations on the test class.
056     */
057    default void initialize(FeaturesRunner runner) throws Exception {
058    }
059
060    /**
061     * Configures Guice bindings if any is required by the feature. This is called after the framework is started and
062     * before Guice module is built. The tests are launched after guice module is built.
063     */
064    default void configure(FeaturesRunner runner, Binder binder) {
065    }
066
067    /**
068     * Before running tests. At this point Guice modules are registered and injector created.
069     */
070    default void beforeRun(FeaturesRunner runner) throws Exception {
071    }
072
073    /**
074     * After tests were run.
075     */
076    default void afterRun(FeaturesRunner runner) throws Exception {
077    }
078
079    /**
080     * Features are initialized. Runner is ready to create the injector.
081     */
082    default void start(FeaturesRunner runner) throws Exception {
083    }
084
085    /**
086     * Notification that a test instance was created. Can be used by features to make custom injection or other
087     * preparation of the test instance.
088     */
089    default void testCreated(Object test) throws Exception {
090    }
091
092    /**
093     * Before exiting the test.
094     */
095    default void stop(FeaturesRunner runner) throws Exception {
096    }
097
098    /**
099     * Before entering in the @Before methods
100     *
101     * @deprecated since 11.1, use {@link #beforeSetup(FeaturesRunner, FrameworkMethod, Object)} instead
102     */
103    @Deprecated
104    default void beforeSetup(FeaturesRunner runner) throws Exception {
105    }
106
107    /**
108     * Before entering in the @Before methods
109     */
110    default void beforeSetup(FeaturesRunner runner, FrameworkMethod method, Object test) throws Exception {
111        beforeSetup(runner); // forward calls for backward compatibility
112    }
113
114    /**
115     * After the call of the @After methods
116     *
117     * @deprecated since 11.1, use {@link #afterTeardown(FeaturesRunner, FrameworkMethod, Object)} instead
118     */
119    @Deprecated
120    default void afterTeardown(FeaturesRunner runner) throws Exception {
121    }
122
123    /**
124     * After the call of the @After methods
125     */
126    default void afterTeardown(FeaturesRunner runner, FrameworkMethod method, Object test) throws Exception {
127        afterTeardown(runner); // forward calls for backward compatibility
128    }
129
130    /**
131     * Before a test method is invoked.
132     */
133    default void beforeMethodRun(FeaturesRunner runner, FrameworkMethod method, Object test) throws Exception {
134    }
135
136    /**
137     * After a test method was ran.
138     */
139    default void afterMethodRun(FeaturesRunner runner, FrameworkMethod method, Object test) throws Exception {
140    }
141
142}