<commit_before>/*
 * Copyright 2015-2019 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 v2.0 which
 * accompanies this distribution and is available at
 *
 * https://www.eclipse.org/legal/epl-v20.html
 */

package org.junit.jupiter.params.provider;

import static java.util.Collections.emptyList;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream;

import com.univocity.parsers.csv.CsvParser;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.support.AnnotationConsumer;
import org.junit.platform.commons.PreconditionViolationException;
import org.junit.platform.commons.util.BlacklistedExceptions;
import org.junit.platform.commons.util.Preconditions;

/**
 * @since 5.0
 */
class CsvArgumentsProvider implements ArgumentsProvider, AnnotationConsumer<CsvSource> {

	private static final String LINE_SEPARATOR = "\n";

	private CsvSource annotation;
	private List<String> nullValues;
	private CsvParser csvParser;

	@Override
	public void accept(CsvSource annotation) {
		this.annotation = annotation;
		this.nullValues = annotation.nullValues().length > 0 ? Arrays.asList(annotation.nullValues()) : emptyList();
		this.csvParser = CsvParserFactory.createParserFor(annotation);
	}

	@Override
	public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
		AtomicLong index = new AtomicLong(0);

		// @formatter:off
		return Arrays.stream(this.annotation.value())
				.map(line -> {
					String[] parsedLine = null;
					try {
						parsedLine = this.csvParser.parseLine(line + LINE_SEPARATOR);
						if (!this.nullValues.isEmpty()) {
							parsedLine = Arrays.stream(parsedLine)
									.map(value -> this.nullValues.contains(value) ? null : value)
									.toArray(String[]::new);
						}
					} catch (Throwable throwable) {
						handleCsvException(throwable, this.annotation);
					}
					Preconditions.notNull(parsedLine,
										  () -> "Line at index " + index.get() + " contains invalid CSV: \"" + line + "\"");
					return parsedLine;
				})
				.peek(values -> index.incrementAndGet())
				.map(Arguments::of);
		// @formatter:on
	}

	static void handleCsvException(Throwable throwable, Annotation annotation) {
		BlacklistedExceptions.rethrowIfBlacklisted(throwable);
		if (throwable instanceof PreconditionViolationException) {
			throw (PreconditionViolationException) throwable;
		}
		throw new CsvParsingException("Failed to parse CSV input configured via " + annotation, throwable);
	}

}<commit_msg>Polish CsvArgumentProvider<commit_after>/*
 * Copyright 2015-2019 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 v2.0 which
 * accompanies this distribution and is available at
 *
 * https://www.eclipse.org/legal/epl-v20.html
 */

package org.junit.jupiter.params.provider;

import static java.util.Collections.emptyList;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream;

import com.univocity.parsers.csv.CsvParser;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.support.AnnotationConsumer;
import org.junit.platform.commons.PreconditionViolationException;
import org.junit.platform.commons.util.BlacklistedExceptions;
import org.junit.platform.commons.util.Preconditions;

/**
 * @since 5.0
 */
class CsvArgumentsProvider implements ArgumentsProvider, AnnotationConsumer<CsvSource> {

	private static final String LINE_SEPARATOR = "\n";

	private CsvSource annotation;
	private List<String> nullValues;
	private CsvParser csvParser;

	@Override
	public void accept(CsvSource annotation) {
		this.annotation = annotation;
		this.nullValues = annotation.nullValues().length > 0 ? Arrays.asList(annotation.nullValues()) : emptyList();
		this.csvParser = CsvParserFactory.createParserFor(annotation);
	}

	@Override
	public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
		AtomicLong index = new AtomicLong(0);
		// @formatter:off
		return Arrays.stream(this.annotation.value())
				.map(line -> parseLine(index.getAndIncrement(), line))
				.map(Arguments::of);
		// @formatter:on
	}

	private String[] parseLine(long index, String line) {
		String[] parsedLine = null;
		try {
			parsedLine = this.csvParser.parseLine(line + LINE_SEPARATOR);
			if (parsedLine != null && !this.nullValues.isEmpty()) {
				for (int i = 0; i < parsedLine.length; i++) {
					if (this.nullValues.contains(parsedLine[i])) {
						parsedLine[i] = null;
					}
				}
			}
		}
		catch (Throwable throwable) {
			handleCsvException(throwable, this.annotation);
		}
		Preconditions.notNull(parsedLine, () -> "Line at index " + index + " contains invalid CSV: \"" + line + "\"");
		return parsedLine;
	}

	static void handleCsvException(Throwable throwable, Annotation annotation) {
		BlacklistedExceptions.rethrowIfBlacklisted(throwable);
		if (throwable instanceof PreconditionViolationException) {
			throw (PreconditionViolationException) throwable;
		}
		throw new CsvParsingException("Failed to parse CSV input configured via " + annotation, throwable);
	}

}
{'fraction_non_alphanumeric': 0.10966916468652897, 'fraction_numerical': 0.0060318040577590935, 'mean_word_length': 4.577981651376147, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 17, 'https://': 2, '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': '4337653', 'n_tokens_mistral': 1794, 'n_tokens_neox': 1591, 'n_words': 367}