<commit_before>
package com.exedio.cope.instrument;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.util.LinkedList;

import junit.framework.TestCase;

public abstract class InjectorTest extends TestCase
{
	private final String resourceName;

	protected InjectorTest(String name, final String resourceName)
	{
		super(name);
		this.resourceName = resourceName;
	}

	private LinkedList injectionEvents;
	private TestInjectionConsumer testInjectionConsumer;

	public abstract void assertInjection();

	public void testInjection()
		throws IOException, InjectorParseException
	{	
		Reader input = null;
		StringWriter output = null;
		String inputfile = InjectorTest.class.getResource(resourceName).getFile();
		input = new InputStreamReader(new FileInputStream(inputfile));
		output = new StringWriter();

		injectionEvents = new LinkedList();
		testInjectionConsumer = new TestInjectionConsumer(output);
		(new Injector(input, output, testInjectionConsumer)).parseFile();
		input.close();
		input = null;
		output.close();
		output = null;
		
		assertInjection();
		injectionEvents = null;
	}
	
	private InjectionEvent fetchEvent()
	{
		return (InjectionEvent)injectionEvents.removeFirst();
	}

	private String format(final String s)
	{
		return s.replace('\n', '=').replace(' ', '_');
	}

	protected void assertText(final String text)
	{
		final InjectionEvent event = fetchEvent();
		final String actualText = ((TextEvent)event).text;
		assertEquals("ZAPP \n>"+format(text)+"<\n>"+format(actualText)+"<\n", text, actualText);
	}

	protected void assertPackage(final String packageName)
	{
		final InjectionEvent event = fetchEvent();
		assertEquals(packageName, ((PackageEvent)event).javafile.getPackageName());
	}

	protected void assertImport(final String importText)
	{
		final InjectionEvent event = fetchEvent();
		assertEquals(importText, ((ImportEvent)event).importText);
	}

	protected void assertDocComment(final String docComment)
	{
		final InjectionEvent event = fetchEvent();
		assertEquals(docComment, ((DocCommentEvent)event).docComment);
	}

	protected void assertFileDocComment(final String docComment)
	{
		final InjectionEvent event = fetchEvent();
		assertEquals(docComment, ((FileDocCommentEvent)event).docComment);
	}

	protected void assertClass(final String className)
	{
		final InjectionEvent event = fetchEvent();
		final JavaClass javaClass = ((ClassEvent)event).javaClass;
		assertEquals(className, javaClass.getName());
	}
	
	protected void assertAttributeHeader(final String name, final String type, final int modifier)
	{
		final InjectionEvent event = fetchEvent();
		final JavaAttribute javaAttribute = ((AttributeHeaderEvent)event).javaAttribute;
		assertEquals(name, javaAttribute.getName());
		assertEquals(type, javaAttribute.getType());
		assertEquals(modifier, javaAttribute.getModifiers());
	}
	
	protected void assertAttribute(final String featureName, final String docComment)
	{
		final InjectionEvent event = fetchEvent();
		final JavaFeature javaFeature = ((ClassFeatureEvent)event).javaFeature;
		assertEquals(featureName, javaFeature.getName());
		assertEquals(docComment, ((ClassFeatureEvent)event).docComment);
	}


	private static class InjectionEvent
	{
	}

	private static class TextEvent extends InjectionEvent
	{<commit_msg>introduce assertMethod<commit_after>
package com.exedio.cope.instrument;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.util.LinkedList;

import junit.framework.TestCase;

public abstract class InjectorTest extends TestCase
{
	private final String resourceName;

	protected InjectorTest(String name, final String resourceName)
	{
		super(name);
		this.resourceName = resourceName;
	}

	private LinkedList injectionEvents;
	private TestInjectionConsumer testInjectionConsumer;

	public abstract void assertInjection();

	public void testInjection()
		throws IOException, InjectorParseException
	{	
		Reader input = null;
		StringWriter output = null;
		String inputfile = InjectorTest.class.getResource(resourceName).getFile();
		input = new InputStreamReader(new FileInputStream(inputfile));
		output = new StringWriter();

		injectionEvents = new LinkedList();
		testInjectionConsumer = new TestInjectionConsumer(output);
		(new Injector(input, output, testInjectionConsumer)).parseFile();
		input.close();
		input = null;
		output.close();
		output = null;
		
		assertInjection();
		injectionEvents = null;
	}
	
	private InjectionEvent fetchEvent()
	{
		return (InjectionEvent)injectionEvents.removeFirst();
	}

	private String format(final String s)
	{
		return s.replace('\n', '=').replace(' ', '_');
	}

	protected void assertText(final String text)
	{
		final InjectionEvent event = fetchEvent();
		final String actualText = ((TextEvent)event).text;
		assertEquals("ZAPP \n>"+format(text)+"<\n>"+format(actualText)+"<\n", text, actualText);
	}

	protected void assertPackage(final String packageName)
	{
		final InjectionEvent event = fetchEvent();
		assertEquals(packageName, ((PackageEvent)event).javafile.getPackageName());
	}

	protected void assertImport(final String importText)
	{
		final InjectionEvent event = fetchEvent();
		assertEquals(importText, ((ImportEvent)event).importText);
	}

	protected void assertDocComment(final String docComment)
	{
		final InjectionEvent event = fetchEvent();
		assertEquals(docComment, ((DocCommentEvent)event).docComment);
	}

	protected void assertFileDocComment(final String docComment)
	{
		final InjectionEvent event = fetchEvent();
		assertEquals(docComment, ((FileDocCommentEvent)event).docComment);
	}

	protected void assertClass(final String className)
	{
		final InjectionEvent event = fetchEvent();
		final JavaClass javaClass = ((ClassEvent)event).javaClass;
		assertEquals(className, javaClass.getName());
	}
	
	protected void assertAttributeHeader(final String name, final String type, final int modifier)
	{
		final InjectionEvent event = fetchEvent();
		final JavaAttribute javaAttribute = ((AttributeHeaderEvent)event).javaAttribute;
		assertEquals(name, javaAttribute.getName());
		assertEquals(type, javaAttribute.getType());
		assertEquals(modifier, javaAttribute.getModifiers());
	}
	
	private void assertFeature(final String name, final String docComment)
	{
		final InjectionEvent event = fetchEvent();
		final JavaFeature javaFeature = ((ClassFeatureEvent)event).javaFeature;
		assertEquals(name, javaFeature.getName());
		assertEquals(docComment, ((ClassFeatureEvent)event).docComment);
	}
	
	protected void assertAttribute(final String name, final String docComment)
	{
		assertFeature(name, docComment);
	}
	
	protected void assertMethod(final String name, final String docComment)
	{
		assertFeature(name, docComment);
	}


	private static class InjectionEvent
	{
	}

	private static class TextEvent extends InjectionEvent
	{
{'fraction_non_alphanumeric': 0.1021689497716895, 'fraction_numerical': 0.0, 'mean_word_length': 5.925889328063241, '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': '2560118', 'n_tokens_mistral': 2061, 'n_tokens_neox': 1921, 'n_words': 483}