/*
Copyright 2011 Software Freedom Conservancy
Copyright 2011 WebDriver committers

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package org.openqa.selenium.testing;

import static org.openqa.selenium.testing.DevMode.isInDevMode;

import com.google.common.base.Throwables;

import org.junit.internal.runners.model.ReflectiveCallable;
import org.junit.internal.runners.statements.Fail;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.openqa.selenium.testing.drivers.Browser;
import org.openqa.selenium.testing.drivers.TestIgnorance;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class SeleniumTestRunner extends BlockJUnit4ClassRunner {

  private TestIgnorance ignorance;

  /**
   * Creates a BlockJUnit4ClassRunner to run {@code klass}
   *
   * @throws org.junit.runners.model.InitializationError
   *          if the test class is malformed.
   */
  public SeleniumTestRunner(Class<?> klass) throws InitializationError {
    super(klass);

    Browser browser = Browser.detect();
    if (browser == null && isInDevMode()) {
      browser = Browser.ff;
    }

    ignorance = new TestIgnorance(browser);
  }

  @Override
  protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
    Object test = null;
    Description description = describeChild(method);
    try {
      test = new ReflectiveCallable() {
        @Override
        protected Object runReflectiveCall() throws Throwable {
          return createTest();
        }
      }.run();
    } catch (Throwable e) {
      runLeaf(new Fail(e), description, notifier);
    }

    if (ignorance.isIgnored(method, test)) {
      notifier.fireTestIgnored(description);
    } else {
      runLeaf(methodBlock(method, test), description, notifier);
    }
  }

  private Statement methodBlock(FrameworkMethod method, Object test) {
    Statement statement = methodInvoker(method, test);
    statement = possiblyExpectingExceptions(method, test, statement);
    statement = withPotentialTimeout(method, test, statement);
    statement = withBefores(method, test, statement);
    statement = withAfters(method, test, statement);
    statement = withRules(method, test, statement);
    return statement;
  }

  private Statement withRules(FrameworkMethod method, Object target, Statement statement) {
    try {
      Method withRules = BlockJUnit4ClassRunner.class.getDeclaredMethod(
          "withRules", FrameworkMethod.class, Object.class, Statement.class);
      withRules.setAccessible(true);

      return (Statement) withRules.invoke(this, method, target, statement);
    } catch (NoSuchMethodException e) {
      throw Throwables.propagate(e);
    } catch (InvocationTargetException e) {
      throw Throwables.propagate(e);
    } catch (IllegalAccessException e) {
      throw Throwables.propagate(e);
    }
  }
}

@@ -1140,16 +1140,107 @@
 tement;%0A
+import org.openqa.selenium.NeedsFreshDriver;%0Aimport org.openqa.selenium.NoDriverAfterTest;%0A
 import o
@@ -1600,16 +1600,55 @@
 s%7D%0A   *%0A
+   * @param klass The class under test%0A
    * @th
@@ -2626,24 +2626,59 @@
 %0A    %7D%0A  %7D%0A%0A
+  @SuppressWarnings(%22deprecation%22)%0A
   private St
@@ -3091,29 +3091,1175 @@
     
-return statement;%0A  %7D
+if (test instanceof JUnit4TestBase) %7B%0A      JUnit4TestBase base = (JUnit4TestBase) test;%0A      statement = withFreshDriver(method, base, statement);%0A      statement = withNoDriverAfterTest(method, statement);%0A    %7D%0A    return statement;%0A  %7D%0A%0A  private Statement withFreshDriver(%0A      FrameworkMethod method, final JUnit4TestBase test, final Statement statement) %7B%0A%0A    NeedsFreshDriver annotation = method.getAnnotation(NeedsFreshDriver.class);%0A    if (annotation == null) %7B%0A      return statement;%0A    %7D%0A%0A    return new Statement() %7B%0A      @Override%0A      public void evaluate() throws Throwable %7B%0A        JUnit4TestBase.removeDriver();  // bletch%0A        test.createDriver();%0A        statement.evaluate();%0A      %7D%0A    %7D;%0A  %7D%0A%0A  private Statement withNoDriverAfterTest(%0A      FrameworkMethod method, final Statement statement) %7B%0A%0A    NoDriverAfterTest annotation = method.getAnnotation(NoDriverAfterTest.class);%0A    if (annotation == null) %7B%0A      return statement;%0A    %7D%0A%0A    return new Statement() %7B%0A      @Override%0A      public void evaluate() throws Throwable %7B%0A        JUnit4TestBase.removeDriver();  // bletch%0A        statement.evaluate();%0A      %7D%0A    %7D;%0A  %7D%0A
 %0A%0A  

Make the selenium test runner for junit 4 aware of our custom annotations for managing driver lifecycles