<commit_before>/*
 * Copyright (c) 2006-2011 <NAME>
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.incremental.junit4;

import java.lang.reflect.*;
import java.lang.annotation.*;
import java.util.*;
import java.io.*;

import org.junit.internal.runners.*;
import org.junit.runner.Description;
import org.junit.runner.notification.*;
import org.junit.runners.*;
import org.junit.runners.model.*;
import org.junit.*;

import mockit.*;
import mockit.incremental.*;
import mockit.internal.util.*;

/**
 * JUnit 4 test runner which ignores tests not affected by current local changes in production code.
 * <p/>
 * This allows incremental execution of tests according to the changes made to production code,
 * instead of running the full suite of tests covering such code every time.
 */
@MockClass(realClass = ParentRunner.class, instantiation = Instantiation.PerMockSetup)
public final class IncrementalJUnit4Runner
{
   private static final Method shouldRunMethod;

   static
   {
      try {
         shouldRunMethod = ParentRunner.class.getDeclaredMethod("shouldRun", Object.class);
      }
      catch (NoSuchMethodException e) {
         throw new RuntimeException(e);
      }
   }

   private final Properties coverageMap;
   private final Map<Method, Boolean> testMethods;
   private RunNotifier runNotifier;
   private Method testMethod;
   public ParentRunner<?> it;

   public IncrementalJUnit4Runner()
   {
      File testRunFile = new File("testRun.properties");

      if (testRunFile.exists() && !testRunFile.canWrite()) {
         throw new IllegalStateException();
      }

      coverageMap = new Properties();
      testMethods = new HashMap<Method, Boolean>();
   }

   @Mock(reentrant = true)
   public void run(RunNotifier notifier)
   {
      runNotifier = notifier;

      CoverageInfoFile coverageFile = new CoverageInfoFile(coverageMap);
      it.run(notifier);
      coverageFile.saveToFile();
   }

   @Mock(reentrant = true)
   public boolean shouldRun(Object m)
   {
      testMethod = null;

      if (!coverageMap.isEmpty()) {
         if (m instanceof JUnit38ClassRunner) {
            boolean noTestsToRun = verifyTestMethodsInJUnit38TestClassThatShouldRun((JUnit38ClassRunner) m);

            if (noTestsToRun) {
               return false;
            }
         }
         else if (m instanceof FrameworkMethod) {
            testMethod = ((FrameworkMethod) m).getMethod();
            Boolean shouldRun = shouldRunTestInCurrentTestRun(Test.class, testMethod);

            if (shouldRun != null) {
               return shouldRun;
            }
         }
      }

      Boolean shouldRun = Utilities.invoke(it, shouldRunMethod, m);

      if (testMethod != null) {
         testMethods.put(testMethod, shouldRun);
      }

      return shouldRun;
   }

   private boolean verifyTestMethodsInJUnit38TestClassThatShouldRun(JUnit38ClassRunner runner)
   {
      Description testClassDescription = runner.getDescription();<commit_msg>Adapted implementation of "Incremental Runner" tool for JUnit 4.10.<commit_after>/*
 * Copyright (c) 2006-2011 <NAME>
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.incremental.junit4;

import java.lang.reflect.*;
import java.lang.annotation.*;
import java.util.*;
import java.io.*;

import org.junit.internal.runners.*;
import org.junit.runner.Description;
import org.junit.runner.manipulation.*;
import org.junit.runner.notification.*;
import org.junit.runners.*;
import org.junit.runners.model.*;
import org.junit.*;

import mockit.*;
import mockit.incremental.*;
import mockit.internal.util.*;

/**
 * JUnit 4 test runner which ignores tests not affected by current local changes in production code.
 * <p/>
 * This allows incremental execution of tests according to the changes made to production code,
 * instead of running the full suite of tests covering such code every time.
 */
@MockClass(realClass = ParentRunner.class, instantiation = Instantiation.PerMockSetup)
public final class IncrementalJUnit4Runner
{
   private static final Method shouldRunMethod;

   static
   {
      try {
         shouldRunMethod = ParentRunner.class.getDeclaredMethod("shouldRun", Filter.class, Object.class);
      }
      catch (NoSuchMethodException e) {
         throw new RuntimeException(e);
      }
   }

   private final Properties coverageMap;
   private final Map<Method, Boolean> testMethods;
   private RunNotifier runNotifier;
   private Method testMethod;
   public ParentRunner<?> it;

   public IncrementalJUnit4Runner()
   {
      File testRunFile = new File("testRun.properties");

      if (testRunFile.exists() && !testRunFile.canWrite()) {
         throw new IllegalStateException();
      }

      coverageMap = new Properties();
      testMethods = new HashMap<Method, Boolean>();
   }

   @Mock(reentrant = true)
   public void run(RunNotifier notifier)
   {
      runNotifier = notifier;

      CoverageInfoFile coverageFile = new CoverageInfoFile(coverageMap);
      it.run(notifier);
      coverageFile.saveToFile();
   }

   @Mock(reentrant = true)
   public boolean shouldRun(Filter filter, Object m)
   {
      testMethod = null;

      if (!coverageMap.isEmpty()) {
         if (m instanceof JUnit38ClassRunner) {
            boolean noTestsToRun = verifyTestMethodsInJUnit38TestClassThatShouldRun((JUnit38ClassRunner) m);

            if (noTestsToRun) {
               return false;
            }
         }
         else if (m instanceof FrameworkMethod) {
            testMethod = ((FrameworkMethod) m).getMethod();
            Boolean shouldRun = shouldRunTestInCurrentTestRun(Test.class, testMethod);

            if (shouldRun != null) {
               return shouldRun;
            }
         }
      }

      Boolean shouldRun = Utilities.invoke(it, shouldRunMethod, filter, m);

      if (testMethod != null) {
         testMethods.put(testMethod, shouldRun);
      }

      return shouldRun;
   }

   private boolean verifyTestMethodsInJUnit38TestClassThatShouldRun(JUnit38ClassRunner runner)
   {
      Description testClassDescription = runner.getDescription();
{'fraction_non_alphanumeric': 0.09482058775775289, 'fraction_numerical': 0.007631108946257509, 'mean_word_length': 3.063324538258575, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6988654', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1654, 'n_words': 459}