<commit_before>package com.google.code.maven_replacer_plugin;


import static java.util.Arrays.asList;
import static junit.framework.Assert.assertSame;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.google.code.maven_replacer_plugin.file.FileUtils;
import com.google.code.maven_replacer_plugin.include.FileSelector;

@RunWith(MockitoJUnitRunner.class)
public class ReplacerMojoTest {

	private static final String XPATH = "xpath";
	private static final String REGEX_FLAG = "regex flag";
	private static final String FILE = "file";
	private static final boolean REGEX = true;
	private static final String OUTPUT_FILE = "output file";
	private static final int REGEX_PATTERN_FLAGS = 999;
	private static final String BASE_DIR = "base dir";
	private static final String TOKEN_VALUE_MAP = "token value map";
	private static final String TOKEN_FILE = "token file";
	private static final String VALUE_FILE = "value file";
	private static final String TOKEN = "token";
	private static final String VALUE = "value";

	@Mock
	private FileUtils fileUtils;
	@Mock
	private ReplacementProcessor processor;
	@Mock
	private ReplacerFactory replacerFactory;
	@Mock
	private TokenValueMapFactory tokenValueMapFactory;
	@Mock
	private FileSelector fileSelector;
	@Mock
	private PatternFlagsFactory patternFlagsFactory;
	@Mock
	private Log log;
	@Mock
	private OutputFilenameBuilder outputFilenameBuilder;
	@Mock
	private SummaryBuilder summaryBuilder;
	
	private List<String> regexFlags;
	private ReplacerMojo mojo;

	@Before
	public void setUp() throws Exception {
		regexFlags = asList(REGEX_FLAG);
		when(patternFlagsFactory.buildFlags(regexFlags)).thenReturn(REGEX_PATTERN_FLAGS);

		mojo = new ReplacerMojo(fileUtils, processor, replacerFactory, tokenValueMapFactory,
				fileSelector, patternFlagsFactory, outputFilenameBuilder, summaryBuilder) {
			@Override
			public Log getLog() {
				return log;
			}
		};
		when(outputFilenameBuilder.buildFrom(FILE, mojo)).thenReturn(OUTPUT_FILE);
	}

	@Test
	public void shouldReplaceContentsInReplacements() throws Exception {
		Replacement replacement = mock(Replacement.class);
		List<Replacement> replacements = asList(replacement);

		mojo.setRegexFlags(regexFlags);
		mojo.setRegex(REGEX);
		mojo.setReplacements(replacements);
		mojo.setFile(FILE);
		mojo.setOutputFile(OUTPUT_FILE);
		mojo.setBasedir(BASE_DIR);
		mojo.execute();
		
		assertSame(FILE, mojo.getFile());
		verify(processor).replace(replacements, REGEX, BASE_DIR + File.separator + FILE, OUTPUT_FILE, REGEX_PATTERN_FLAGS);
		verify(summaryBuilder).add(BASE_DIR + File.separator + FILE, OUTPUT_FILE, log);
		verify(summaryBuilder).print(log);
	}
	
	@Test
	public void shouldReplaceContentsInLocalFile() throws Exception {
		Replacement replacement = mock(Replacement.class);<commit_msg>Adding coverage when ignore missing file set even when file exists. This flag should have no effect in these conditions.<commit_after>package com.google.code.maven_replacer_plugin;


import static java.util.Arrays.asList;
import static junit.framework.Assert.assertSame;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.google.code.maven_replacer_plugin.file.FileUtils;
import com.google.code.maven_replacer_plugin.include.FileSelector;

@RunWith(MockitoJUnitRunner.class)
public class ReplacerMojoTest {

	private static final String XPATH = "xpath";
	private static final String REGEX_FLAG = "regex flag";
	private static final String FILE = "file";
	private static final boolean REGEX = true;
	private static final String OUTPUT_FILE = "output file";
	private static final int REGEX_PATTERN_FLAGS = 999;
	private static final String BASE_DIR = "base dir";
	private static final String TOKEN_VALUE_MAP = "token value map";
	private static final String TOKEN_FILE = "token file";
	private static final String VALUE_FILE = "value file";
	private static final String TOKEN = "token";
	private static final String VALUE = "value";

	@Mock
	private FileUtils fileUtils;
	@Mock
	private ReplacementProcessor processor;
	@Mock
	private ReplacerFactory replacerFactory;
	@Mock
	private TokenValueMapFactory tokenValueMapFactory;
	@Mock
	private FileSelector fileSelector;
	@Mock
	private PatternFlagsFactory patternFlagsFactory;
	@Mock
	private Log log;
	@Mock
	private OutputFilenameBuilder outputFilenameBuilder;
	@Mock
	private SummaryBuilder summaryBuilder;
	
	private List<String> regexFlags;
	private ReplacerMojo mojo;

	@Before
	public void setUp() throws Exception {
		regexFlags = asList(REGEX_FLAG);
		when(patternFlagsFactory.buildFlags(regexFlags)).thenReturn(REGEX_PATTERN_FLAGS);

		mojo = new ReplacerMojo(fileUtils, processor, replacerFactory, tokenValueMapFactory,
				fileSelector, patternFlagsFactory, outputFilenameBuilder, summaryBuilder) {
			@Override
			public Log getLog() {
				return log;
			}
		};
		when(outputFilenameBuilder.buildFrom(FILE, mojo)).thenReturn(OUTPUT_FILE);
	}

	@Test
	public void shouldReplaceContentsInReplacements() throws Exception {
		Replacement replacement = mock(Replacement.class);
		List<Replacement> replacements = asList(replacement);

		mojo.setRegexFlags(regexFlags);
		mojo.setRegex(REGEX);
		mojo.setReplacements(replacements);
		mojo.setFile(FILE);
		mojo.setIgnoreMissingFile(true);
		mojo.setOutputFile(OUTPUT_FILE);
		mojo.setBasedir(BASE_DIR);
		mojo.execute();
		
		assertSame(FILE, mojo.getFile());
		verify(processor).replace(replacements, REGEX, BASE_DIR + File.separator + FILE, OUTPUT_FILE, REGEX_PATTERN_FLAGS);
		verify(summaryBuilder).add(BASE_DIR + File.separator + FILE, OUTPUT_FILE, log);
		verify(summaryBuilder).print(log);
	}
	
	@Test
	public void shouldReplaceContentsInLocalFile() throws Exception {
		Replacement replacement = mock(Replacement.class);
{'fraction_non_alphanumeric': 0.08869741510390268, 'fraction_numerical': 0.0007602635580334517, 'mean_word_length': 6.059928443649373, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '377645', 'n_tokens_mistral': 2499, 'n_tokens_neox': 2376, 'n_words': 579}