001    /*
002     * $Id: SmokeBuilder.java,v 1.2 2011/12/31 11:58:44 oboehm Exp $
003     *
004     * Copyright (c) 2012 by Oliver Boehm
005     *
006     * Licensed under the Apache License, Version 2.0 (the "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     * 
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     * 
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     * 
018     * (c)reated 30.12.2011 by oliver (ob@oasd.de)
019     */
020    
021    package patterntesting.runtime.junit.internal;
022    
023    import org.junit.internal.runners.JUnit38ClassRunner;
024    import org.junit.runner.Runner;
025    import org.junit.runners.BlockJUnit4ClassRunner;
026    import org.junit.runners.model.RunnerBuilder;
027    
028    import patterntesting.runtime.junit.SmokeRunner;
029    
030    /**
031     * This builder returns a SmokeRunner for the test class. But only if the
032     * test class is not annotated by another Runner.
033     * 
034     * @author oliver
035     * @since 1.2 (30.12.2011)
036     */
037    public class SmokeBuilder extends RunnerBuilder {
038        
039        private final RunnerBuilder fallback;
040        
041        /**
042         * Instantiates a new smoke builder.
043         *
044         * @param builder the builder
045         */
046        public SmokeBuilder(final RunnerBuilder builder) {
047            this.fallback = builder;
048        }
049    
050        /**
051         * Returs a Runner for class. But only if the given testClass is not
052         * annotated with another Runner.
053         *
054         * @param testClass the test class
055         * @return a SmokeRunner
056         * @throws Throwable the throwable
057         * @see org.junit.runners.model.RunnerBuilder#runnerForClass(java.lang.Class)
058         */
059        @Override
060        public Runner runnerForClass(final Class<?> testClass) throws Throwable {
061            Runner runner = this.fallback.runnerForClass(testClass);
062            if (isDefaultRunner(runner)) {
063                return new SmokeRunner(testClass);
064            }
065            return runner;
066        }
067    
068        private static boolean isDefaultRunner(Runner runner) {
069            Class<? extends Runner> runnerClass = runner.getClass();
070            return runnerClass.equals(BlockJUnit4ClassRunner.class)
071                    || runnerClass.equals(JUnit38ClassRunner.class);
072        }
073    
074    }
075