<commit_before>package kuleuven.group2.data.updating;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import kuleuven.group2.data.Test;
import kuleuven.group2.data.TestDatabase;

import org.junit.runner.Description;
import org.junit.runner.Request;
import org.junit.runner.Runner;

/**
 * A TestChangeUpdater detects additions or removal of test methods in a test
 * class and updates the database accordingly.
 * 
 * @author Group2
 * @version 14 November 2013
 */
public class TestChangeUpdater {

	protected final TestDatabase database;
	protected final ClassLoader testClassLoader;

	public TestChangeUpdater(TestDatabase database, ClassLoader testClassLoader) {
		this.database = checkNotNull(database);
		this.testClassLoader = checkNotNull(testClassLoader);
	}

	/**
	 * Update all given test classes.
	 * 
	 * @param testClassNames
	 *            The names of the test classes.
	 */
	public void updateTestClasses(Collection<String> testClassNames) {
		for (String testClassName : testClassNames) {
			try {
				updateTestClass(testClassLoader.loadClass(testClassName));
			} catch (ClassNotFoundException e) {
				// Assume invalid, delete from database
				removeTests(testClassName);
			}
		}
	}

	/**
	 * Update the given class.
	 * 
	 * @param testClassName
	 *            The name of the test class.
	 */
	public void updateTestClass(Class<?> testClass) {
		// Create a runner
		Runner runner = Request.aClass(testClass).getRunner();
		// TODO: when building fails it returns an errorreportingrunner
		// we should probably do something with this so this can be catched
		if (runner == null) return;
		// Collect test method names
		Set<String> testNames = new HashSet<String>();
		for (Description description : runner.getDescription().getChildren()) {<commit_msg>"Fixed" detection of invalid test classes.<commit_after>package kuleuven.group2.data.updating;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import kuleuven.group2.data.Test;
import kuleuven.group2.data.TestDatabase;

import org.junit.internal.runners.ErrorReportingRunner;
import org.junit.runner.Description;
import org.junit.runner.Request;
import org.junit.runner.Runner;

/**
 * A TestChangeUpdater detects additions or removal of test methods in a test
 * class and updates the database accordingly.
 * 
 * @author Group2
 * @version 14 November 2013
 */
public class TestChangeUpdater {

	protected final TestDatabase database;
	protected final ClassLoader testClassLoader;

	public TestChangeUpdater(TestDatabase database, ClassLoader testClassLoader) {
		this.database = checkNotNull(database);
		this.testClassLoader = checkNotNull(testClassLoader);
	}

	/**
	 * Update all given test classes.
	 * 
	 * @param testClassNames
	 *            The names of the test classes.
	 */
	public void updateTestClasses(Collection<String> testClassNames) {
		for (String testClassName : testClassNames) {
			try {
				updateTestClass(testClassLoader.loadClass(testClassName));
			} catch (ClassNotFoundException e) {
				// Assume invalid, delete from database
				removeTests(testClassName);
			}
		}
	}

	/**
	 * Update the given class.
	 * 
	 * @param testClassName
	 *            The name of the test class.
	 */
	public void updateTestClass(Class<?> testClass) {
		// Create a runner
		Runner runner = Request.aClass(testClass).getRunner();
		if (runner instanceof ErrorReportingRunner) {
			/*
			 * TODO This is a very dirty instanceof check, and we know it.
			 * 
			 * We need this because JUnit does not let us know about validation
			 * errors without actually running the test. Even the description of
			 * an ErrorReportingRunner does not give this away.
			 * 
			 * The alternative would be to add a field to the Description class
			 * indicating that it describes an invalid test. Such a change would
			 * break a lot of things, and it doesn't seem worthwhile to modify
			 * JUnit for this one small thing.
			 */
			return;
		}
		if (runner == null) return;
		// Collect test method names
		Set<String> testNames = new HashSet<String>();
		for (Description description : runner.getDescription().getChildren()) {
{'fraction_non_alphanumeric': 0.08685555039479796, 'fraction_numerical': 0.004644681839294009, 'mean_word_length': 3.8666666666666667, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '7250017', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1161, 'n_words': 384}