<commit_before>/*
 * Copyright 2015 the original author or authors.
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution and is available at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.junit.gen5.engine.junit5;

import static java.util.stream.Collectors.toList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.gen5.engine.ClassNameSpecification;
import org.junit.gen5.engine.EngineExecutionContext;
import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.TestEngine;
import org.junit.gen5.engine.TestPlanSpecification;
import org.junit.gen5.engine.TestPlanSpecificationElement;
import org.junit.gen5.engine.UniqueIdSpecification;
import org.junit.gen5.engine.junit5.descriptor.ClassNameTestDescriptorResolver;
import org.junit.gen5.engine.junit5.descriptor.TestDescriptorResolver;
import org.junit.gen5.engine.junit5.descriptor.TestDescriptorResolverRegistry;
import org.junit.gen5.engine.junit5.descriptor.UniqueIdTestDescriptorResolver;
import org.junit.gen5.engine.junit5.execution.TestExecutionNode;
import org.junit.gen5.engine.junit5.execution.TestExecutionNodeResolver;

public class JUnit5TestEngine implements TestEngine {

	// TODO Consider using class names for engine IDs.
	private static final String ENGINE_ID = "junit5";

	@Override
	public String getId() {
		return ENGINE_ID;
	}

	@Override
	public List<TestDescriptor> discoverTests(TestPlanSpecification specification, TestDescriptor engineDescriptor) {
		TestDescriptorResolverRegistry testDescriptorResolverRegistry = createResolverRegistry();

		// TODO Avoid redundant creation of TestDescriptors during this phase
		Set<TestDescriptor> testDescriptors = new LinkedHashSet<>();

		for (TestPlanSpecificationElement element : specification) {
			testDescriptors.addAll(resolveElement(testDescriptorResolverRegistry, engineDescriptor, element));
		}
		return new ArrayList<>(testDescriptors);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private Set<TestDescriptor> resolveElement(TestDescriptorResolverRegistry testDescriptorResolverRegistry,
			TestDescriptor root, TestPlanSpecificationElement element) {
		Set<TestDescriptor> testDescriptors = new LinkedHashSet<>();
		TestDescriptorResolver testDescriptorResolver = testDescriptorResolverRegistry.forType(element.getClass());
		TestDescriptor descriptor = testDescriptorResolver.resolve(root, element);
		//Get rid of null check
		if (descriptor != null) {
			testDescriptors.add(descriptor);
			testDescriptors.addAll(testDescriptorResolver.resolveChildren(descriptor, element));
		}
		return testDescriptors;
	}

	private TestDescriptorResolverRegistry createResolverRegistry() {
		// TODO Look up TestDescriptorResolverRegistry within the
		// ApplicationExecutionContext
		TestDescriptorResolverRegistry testDescriptorResolverRegistry = new TestDescriptorResolverRegistry();
		testDescriptorResolverRegistry.addResolver(ClassNameSpecification.class, new ClassNameTestDescriptorResolver());<commit_msg>Extracted specification resolution into method to allow easier experimentation<commit_after>/*
 * Copyright 2015 the original author or authors.
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution and is available at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.junit.gen5.engine.junit5;

import static java.util.stream.Collectors.toList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.gen5.engine.ClassNameSpecification;
import org.junit.gen5.engine.EngineExecutionContext;
import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.TestEngine;
import org.junit.gen5.engine.TestPlanSpecification;
import org.junit.gen5.engine.TestPlanSpecificationElement;
import org.junit.gen5.engine.UniqueIdSpecification;
import org.junit.gen5.engine.junit5.descriptor.ClassNameTestDescriptorResolver;
import org.junit.gen5.engine.junit5.descriptor.TestDescriptorResolver;
import org.junit.gen5.engine.junit5.descriptor.TestDescriptorResolverRegistry;
import org.junit.gen5.engine.junit5.descriptor.UniqueIdTestDescriptorResolver;
import org.junit.gen5.engine.junit5.execution.TestExecutionNode;
import org.junit.gen5.engine.junit5.execution.TestExecutionNodeResolver;

public class JUnit5TestEngine implements TestEngine {

	// TODO Consider using class names for engine IDs.
	private static final String ENGINE_ID = "junit5";

	@Override
	public String getId() {
		return ENGINE_ID;
	}

	@Override
	public List<TestDescriptor> discoverTests(TestPlanSpecification specification, TestDescriptor engineDescriptor) {
		// TODO Avoid redundant creation of TestDescriptors during this phase
		Set<TestDescriptor> testDescriptors = new LinkedHashSet<>();
		resolveSpecification(specification, engineDescriptor, testDescriptors);
		return new ArrayList<>(testDescriptors);
	}

	// Isolated this step to allow easier experimentation / branching / pull requesting
	private void resolveSpecification(TestPlanSpecification specification, TestDescriptor engineDescriptor,
			Set<TestDescriptor> testDescriptors) {
		TestDescriptorResolverRegistry testDescriptorResolverRegistry = createResolverRegistry();

		for (TestPlanSpecificationElement element : specification) {
			testDescriptors.addAll(resolveElement(testDescriptorResolverRegistry, engineDescriptor, element));
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private Set<TestDescriptor> resolveElement(TestDescriptorResolverRegistry testDescriptorResolverRegistry,
			TestDescriptor root, TestPlanSpecificationElement element) {
		Set<TestDescriptor> testDescriptors = new LinkedHashSet<>();
		TestDescriptorResolver testDescriptorResolver = testDescriptorResolverRegistry.forType(element.getClass());
		TestDescriptor descriptor = testDescriptorResolver.resolve(root, element);
		//Get rid of null check
		if (descriptor != null) {
			testDescriptors.add(descriptor);
			testDescriptors.addAll(testDescriptorResolver.resolveChildren(descriptor, element));
		}
		return testDescriptors;
	}

	private TestDescriptorResolverRegistry createResolverRegistry() {
		// TODO Look up TestDescriptorResolverRegistry within the
		// ApplicationExecutionContext
		TestDescriptorResolverRegistry testDescriptorResolverRegistry = new TestDescriptorResolverRegistry();
		testDescriptorResolverRegistry.addResolver(ClassNameSpecification.class, new ClassNameTestDescriptorResolver());
{'fraction_non_alphanumeric': 0.08019141531322506, 'fraction_numerical': 0.008990719257540603, 'mean_word_length': 6.688963210702341, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3319101', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1755, 'n_words': 414}