<commit_before>package io.probedock.client.junit;

import io.probedock.client.annotations.ProbeTest;
import io.probedock.client.annotations.ProbeTestClass;
import io.probedock.client.common.config.Configuration;
import io.probedock.client.common.model.v1.*;
import io.probedock.client.common.utils.FingerprintGenerator;
import io.probedock.client.common.utils.Inflector;
import io.probedock.client.utils.CollectionHelper;

import java.util.*;
import java.util.logging.Logger;

import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

/**
 * Shared code to create Junit Probe Dock listeners
 *
 * @author <NAME> <<EMAIL>>
 */
public abstract class AbstractProbeListener extends RunListener {
	private static final Logger LOGGER = Logger.getLogger(AbstractProbeListener.class.getCanonicalName());
	
	/**
	 * Probe Dock configuration
	 */
	protected static final Configuration configuration = Configuration.getInstance();

	/**
	 * Default category when none is specified
	 */
	private static final String DEFAULT_CATEGORY = "unit";

	/**
	 * Store the date when the run started
	 */
	protected Long runStartedDate;

	/**
	 * Store the start date of a test to measure the approximative execution
	 * time of a each test.
	 */
	protected Map<String, Long> testStartDates = new HashMap<>();

	/**
	 * Define if all the stack traces must be printed/written
	 */
	private boolean fullStackTraces = true;
	
	/**
	 * Define the category of the test that are running. Will
	 * be use donly if no other is specified.
	 */
	private String category;
	
	/**
	 * Constructor
	 */
	public AbstractProbeListener() {
	}
	
	/**
	 * Constructor
	 * 
	 * @param category The test category
	 */
	public AbstractProbeListener(String category) {
		this();
		this.category = category;
	}

	@Override
	public void testStarted(Description description) throws Exception {
		super.testStarted(description);
	}

	@Override
	public void testFinished(Description description) throws Exception {
		super.testFinished(description);
	}
	
	/**
	 * @param fullStackTraces The full stack trace mode
	 */
	public void setFullStackTraces(Boolean fullStackTraces) {
		this.fullStackTraces = fullStackTraces;
	}
	
	@Override
	public void testRunStarted(Description description) throws Exception {
		super.testRunStarted(description);
		runStartedDate = System.currentTimeMillis();
	}
	
	/**
	 * Try to retrieve the {@link ProbeTest} annotation of the test method
	 * 
	 * @param description The representation of the test
	 * @return The annotation found, or null if not found
	 * @throws NoSuchMethodException 
	 */
	protected ProbeTest getMethodAnnotation(Description description) throws NoSuchMethodException {
		String methodName = description.getMethodName();

		if (methodName.contains("[")) {
			methodName = methodName.replaceAll("\\[.*\\]", "");
		}

		return description.getTestClass().getMethod(methodName).getAnnotation(ProbeTest.class);
	}
	
	/**
	 * Try to retrieve the {@link ProbeTestClass} annotation of the test class
	 * 
	 * @param description The representation of the test
	 * @return The annotation found, or null if not found
	 */
	protected ProbeTestClass getClassAnnotation(Description description) {
		return description.getTestClass().getAnnotation(ProbeTestClass.class);
	}

	/**
	 * Create a test based on the different information gathered from class, method and description
	 *
	 * @param fingerprint The fingerprint of the test
	 * @param description jUnit test description
	 * @param mAnnotation Method annotation
	 * @param cAnnotation Class annotation
	 * @param passed Test passing or not
	 * @param message Message associated to the test result
	 * @return The test created from all the data available
	 */
	protected TestResult createTestResult(String fingerprint, Description description, ProbeTest mAnnotation, ProbeTestClass cAnnotation, boolean passed, String message) {
		TestResult result = ModelFactory.createTestResult(
			getKey(mAnnotation),
			fingerprint,
			getName(description, mAnnotation),
			getCategory(cAnnotation, mAnnotation),
			System.currentTimeMillis() - testStartDates.get(fingerprint),
			message,
			passed,
			isActive(mAnnotation),
			getContributors(mAnnotation, cAnnotation),
			getTags(mAnnotation, cAnnotation),
			getTickets(mAnnotation, cAnnotation),
			null
		);

		ModelFactory.enrichTestResult(result, description.getTestClass().getPackage().getName(), description.getTestClass().getName(), description.getMethodName());

		return result;
	}

	/**
	 * Retrieve the tags from the annotations
	 *
	 * @param annotation The annotation
	 * @return The key or null if there is no key
	 */
	private String getKey(ProbeTest annotation) {
		return annotation != null && annotation.key() != null && !annotation.key().isEmpty() ? annotation.key() : null;
	}

	private Boolean isActive(ProbeTest annotation) {
		return annotation != null ? annotation.active() : null;
	}
	
	/**
	 * Retrieve a name from a test
	 * 
	 * @param description The description of the test
	 * @param mAnnotation The method annotation
	 * @return The name retrieved
	 */
	private String getName(Description description, ProbeTest mAnnotation) {
		if (mAnnotation == null || mAnnotation.name() == null || mAnnotation.name().isEmpty()) {
			return Inflector.getHumanName(description.getTestClass().getSimpleName() + ": " + description.getMethodName());
		}
		else {
			return mAnnotation.name();
		}
	} 
	
	/**
	 * Retrieve the category to apply to the test
	 * @param classAnnotation The roxable class annotation to get the override category
	 * @param methodAnnotation The roxable annotation to get the override category
	 * @return The category found
	 */
	protected String getCategory(ProbeTestClass classAnnotation, ProbeTest methodAnnotation) {
		if (methodAnnotation != null && methodAnnotation.category() != null && !methodAnnotation.category().isEmpty()) {
			return methodAnnotation.category();
		}<commit_msg>Updated the name construction to use the generic way offered by the lib<commit_after>package io.probedock.client.junit;

import io.probedock.client.annotations.ProbeTest;
import io.probedock.client.annotations.ProbeTestClass;
import io.probedock.client.common.config.Configuration;
import io.probedock.client.common.model.v1.*;
import io.probedock.client.common.utils.FingerprintGenerator;
import io.probedock.client.common.utils.Inflector;
import io.probedock.client.utils.CollectionHelper;

import java.util.*;
import java.util.logging.Logger;

import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

/**
 * Shared code to create Junit Probe Dock listeners
 *
 * @author <NAME> <<EMAIL>>
 */
public abstract class AbstractProbeListener extends RunListener {
	private static final Logger LOGGER = Logger.getLogger(AbstractProbeListener.class.getCanonicalName());
	
	/**
	 * Probe Dock configuration
	 */
	protected static final Configuration configuration = Configuration.getInstance();

	/**
	 * Default category when none is specified
	 */
	private static final String DEFAULT_CATEGORY = "unit";

	/**
	 * Store the date when the run started
	 */
	protected Long runStartedDate;

	/**
	 * Store the start date of a test to measure the approximative execution
	 * time of a each test.
	 */
	protected Map<String, Long> testStartDates = new HashMap<>();

	/**
	 * Define if all the stack traces must be printed/written
	 */
	private boolean fullStackTraces = true;
	
	/**
	 * Define the category of the test that are running. Will
	 * be use donly if no other is specified.
	 */
	private String category;
	
	/**
	 * Constructor
	 */
	public AbstractProbeListener() {
	}
	
	/**
	 * Constructor
	 * 
	 * @param category The test category
	 */
	public AbstractProbeListener(String category) {
		this();
		this.category = category;
	}

	@Override
	public void testStarted(Description description) throws Exception {
		super.testStarted(description);
	}

	@Override
	public void testFinished(Description description) throws Exception {
		super.testFinished(description);
	}
	
	/**
	 * @param fullStackTraces The full stack trace mode
	 */
	public void setFullStackTraces(Boolean fullStackTraces) {
		this.fullStackTraces = fullStackTraces;
	}
	
	@Override
	public void testRunStarted(Description description) throws Exception {
		super.testRunStarted(description);
		runStartedDate = System.currentTimeMillis();
	}
	
	/**
	 * Try to retrieve the {@link ProbeTest} annotation of the test method
	 * 
	 * @param description The representation of the test
	 * @return The annotation found, or null if not found
	 * @throws NoSuchMethodException 
	 */
	protected ProbeTest getMethodAnnotation(Description description) throws NoSuchMethodException {
		String methodName = description.getMethodName();

		if (methodName.contains("[")) {
			methodName = methodName.replaceAll("\\[.*\\]", "");
		}

		return description.getTestClass().getMethod(methodName).getAnnotation(ProbeTest.class);
	}
	
	/**
	 * Try to retrieve the {@link ProbeTestClass} annotation of the test class
	 * 
	 * @param description The representation of the test
	 * @return The annotation found, or null if not found
	 */
	protected ProbeTestClass getClassAnnotation(Description description) {
		return description.getTestClass().getAnnotation(ProbeTestClass.class);
	}

	/**
	 * Create a test based on the different information gathered from class, method and description
	 *
	 * @param fingerprint The fingerprint of the test
	 * @param description jUnit test description
	 * @param mAnnotation Method annotation
	 * @param cAnnotation Class annotation
	 * @param passed Test passing or not
	 * @param message Message associated to the test result
	 * @return The test created from all the data available
	 */
	protected TestResult createTestResult(String fingerprint, Description description, ProbeTest mAnnotation, ProbeTestClass cAnnotation, boolean passed, String message) {
		TestResult result = ModelFactory.createTestResult(
			getKey(mAnnotation),
			fingerprint,
			Inflector.forgeName(description.getTestClass(), description.getMethodName(), mAnnotation),
			getCategory(cAnnotation, mAnnotation),
			System.currentTimeMillis() - testStartDates.get(fingerprint),
			message,
			passed,
			isActive(mAnnotation),
			getContributors(mAnnotation, cAnnotation),
			getTags(mAnnotation, cAnnotation),
			getTickets(mAnnotation, cAnnotation),
			null
		);

		ModelFactory.enrichTestResult(result, description.getTestClass().getPackage().getName(), description.getTestClass().getName(), description.getMethodName());

		return result;
	}

	/**
	 * Retrieve the tags from the annotations
	 *
	 * @param annotation The annotation
	 * @return The key or null if there is no key
	 */
	private String getKey(ProbeTest annotation) {
		return annotation != null && annotation.key() != null && !annotation.key().isEmpty() ? annotation.key() : null;
	}

	private Boolean isActive(ProbeTest annotation) {
		return annotation != null ? annotation.active() : null;
	}
	
	/**
	 * Retrieve the category to apply to the test
	 * @param classAnnotation The roxable class annotation to get the override category
	 * @param methodAnnotation The roxable annotation to get the override category
	 * @return The category found
	 */
	protected String getCategory(ProbeTestClass classAnnotation, ProbeTest methodAnnotation) {
		if (methodAnnotation != null && methodAnnotation.category() != null && !methodAnnotation.category().isEmpty()) {
			return methodAnnotation.category();
		}
{'fraction_non_alphanumeric': 0.09543926200534529, 'fraction_numerical': 0.00017242865764290026, 'mean_word_length': 4.526441162458314, 'pattern_counts': {'":': 1, '<': 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': '5629080', 'n_tokens_mistral': 3346, 'n_tokens_neox': 3009, 'n_words': 961}