<commit_before>package com.nhn.pinpoint.profiler.junit4;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;

import org.junit.internal.runners.statements.Fail;
import org.junit.internal.runners.statements.RunAfters;
import org.junit.internal.runners.statements.RunBefores;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nhn.pinpoint.bootstrap.config.ProfilerConfig;
import com.nhn.pinpoint.bootstrap.logging.PLoggerFactory;
import com.nhn.pinpoint.common.ServiceType;
import com.nhn.pinpoint.profiler.DefaultAgent;
import com.nhn.pinpoint.profiler.DummyInstrumentation;
import com.nhn.pinpoint.profiler.logging.Slf4jLoggerBinder;
import com.nhn.pinpoint.profiler.util.MockAgent;
import com.nhn.pinpoint.profiler.util.TestClassLoader;

/**
 *
 * @author <NAME>
 */
public final class PinpointJUnit4ClassRunner extends BlockJUnit4ClassRunner {
	
	private static final Logger logger = LoggerFactory.getLogger(PinpointJUnit4ClassRunner.class);
	private static final Class<TestClassLoader> defaultTestClassLoader = TestClassLoader.class;
	
	private final TestClassLoader testClassLoader;
	private final TestContext testContext;

	public PinpointJUnit4ClassRunner(Class<?> clazz) throws InitializationError {
		super(clazz);
		if (logger.isDebugEnabled()) {
			logger.debug("PinpointJUnit4ClassRunner constructor called with [" + clazz + "].");
		}
		this.testClassLoader = getTestClassLoader(clazz);
		this.testClassLoader.initialize();
		try {
			this.testContext = new TestContext(this.testClassLoader, clazz);
		} catch (ClassNotFoundException e) {
			throw new InitializationError(e);
		}
	}
	
	private final TestClassLoader getTestClassLoader(Class<?> testClass) throws InitializationError {
		PinpointTestClassLoader pinpointTestClassLoader = testClass.getAnnotation(PinpointTestClassLoader.class);
		
		if (pinpointTestClassLoader == null) {
			if (logger.isInfoEnabled()) {
				logger.info(String.format("@PinpointTestClassLoader not found for class [%s]", testClass));
			}
			return createTestClassLoader(defaultTestClassLoader);
		} else {
			if (logger.isTraceEnabled()) {
				logger.trace(String.format("Retrieved @PinpointTestClassLoader [%s] for class [%s]", pinpointTestClassLoader, testClass));
			}
			return createTestClassLoader(pinpointTestClassLoader.value());
		}
	}
	
	private final <T extends TestClassLoader> TestClassLoader createTestClassLoader(Class<T> testClassLoader) throws InitializationError {
		System.setProperty("catalina.home", "test");
		PLoggerFactory.initialize(new Slf4jLoggerBinder());
		
		ProfilerConfig profilerConfig = new ProfilerConfig();
		
		String path = ProfilerConfig.class.getClassLoader().getResource("pinpoint.config").getPath();
		try {
			profilerConfig.readConfigFile(path);
		} catch (IOException e) {
			throw new InitializationError("Unable to read pinpoint.config");
		}
		
		profilerConfig.setApplicationServerType(ServiceType.STAND_ALONE);
		DefaultAgent agent = new MockAgent("", new DummyInstrumentation(), profilerConfig);
		try {
			Constructor<T> c = testClassLoader.getConstructor(DefaultAgent.class);
			T classLoader = c.newInstance(agent);
			return classLoader;
		} catch (Exception e) {
			// 어떤 exception이 발생하던 결국 InitializationError.
			throw new InitializationError("Error instantiating Test");
		}
	}
	
	@Override
	protected Statement methodBlock(FrameworkMethod frameworkMethod) {
		FrameworkMethod newFrameworkMethod = null;
		try {
			Thread.currentThread().setContextClassLoader(this.testClassLoader);
			Method newMethod = this.testContext.getTestClass().getJavaClass().getMethod(frameworkMethod.getName());
			newFrameworkMethod = new FrameworkMethod(newMethod);
			return super.methodBlock(newFrameworkMethod);
		} catch (NoSuchMethodException e) {
			return new Fail(e);
		} catch (SecurityException e) {<commit_msg>[정현길] [PROFILER-20] TestClassLoader을 사용하는 JUnit ClassRunner 수정<commit_after>package com.nhn.pinpoint.profiler.junit4;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;

import org.junit.internal.runners.statements.Fail;
import org.junit.internal.runners.statements.RunAfters;
import org.junit.internal.runners.statements.RunBefores;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nhn.pinpoint.bootstrap.config.ProfilerConfig;
import com.nhn.pinpoint.bootstrap.logging.PLoggerFactory;
import com.nhn.pinpoint.common.ServiceType;
import com.nhn.pinpoint.profiler.DefaultAgent;
import com.nhn.pinpoint.profiler.DummyInstrumentation;
import com.nhn.pinpoint.profiler.logging.Slf4jLoggerBinder;
import com.nhn.pinpoint.profiler.util.MockAgent;
import com.nhn.pinpoint.profiler.util.TestClassLoader;

/**
 *
 * @author <NAME>
 */
public final class PinpointJUnit4ClassRunner extends BlockJUnit4ClassRunner {
	
	private static final Logger logger = LoggerFactory.getLogger(PinpointJUnit4ClassRunner.class);
	private static final Class<TestClassLoader> defaultTestClassLoader = TestClassLoader.class;
	
	private final TestClassLoader testClassLoader;
	private final TestContext testContext;

	public PinpointJUnit4ClassRunner(Class<?> clazz) throws InitializationError {
		super(clazz);
		if (logger.isDebugEnabled()) {
			logger.debug("PinpointJUnit4ClassRunner constructor called with [" + clazz + "].");
		}
		this.testClassLoader = getTestClassLoader(clazz);
		this.testClassLoader.initialize();
		try {
			this.testContext = new TestContext(this.testClassLoader, clazz);
		} catch (ClassNotFoundException e) {
			throw new InitializationError(e);
		}
	}
	
	private final TestClassLoader getTestClassLoader(Class<?> testClass) throws InitializationError {
		PinpointTestClassLoader pinpointTestClassLoader = testClass.getAnnotation(PinpointTestClassLoader.class);
		
		if (pinpointTestClassLoader == null) {
			if (logger.isInfoEnabled()) {
				logger.info(String.format("@PinpointTestClassLoader not found for class [%s]", testClass));
			}
			return createTestClassLoader(defaultTestClassLoader);
		} else {
			if (logger.isTraceEnabled()) {
				logger.trace(String.format("Retrieved @PinpointTestClassLoader [%s] for class [%s]", pinpointTestClassLoader, testClass));
			}
			return createTestClassLoader(pinpointTestClassLoader.value());
		}
	}
	
	private final <T extends TestClassLoader> TestClassLoader createTestClassLoader(Class<T> testClassLoader) throws InitializationError {
		System.setProperty("catalina.home", "test");
		PLoggerFactory.initialize(new Slf4jLoggerBinder());
		
		ProfilerConfig profilerConfig = new ProfilerConfig();
		
		String path = ProfilerConfig.class.getClassLoader().getResource("pinpoint.config").getPath();
		try {
			profilerConfig.readConfigFile(path);
		} catch (IOException e) {
			throw new InitializationError("Unable to read pinpoint.config");
		}
		
		profilerConfig.setApplicationServerType(ServiceType.STAND_ALONE);
		DefaultAgent agent = new MockAgent("", new DummyInstrumentation(), profilerConfig);
		try {
			Constructor<T> c = testClassLoader.getConstructor(DefaultAgent.class);
			T classLoader = c.newInstance(agent);
			return classLoader;
		} catch (Exception e) {
			// 어떤 exception이 발생하던 결국 InitializationError.
			throw new InitializationError("Error instantiating Test");
		}
	}

	@Override
	protected void runChild(FrameworkMethod method, RunNotifier notifier) {
		ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
		try {
			Thread.currentThread().setContextClassLoader(this.testClassLoader);
			super.runChild(method, notifier);
		} finally {
			Thread.currentThread().setContextClassLoader(originalClassLoader);
		}
	}
	
	@Override
	protected Statement methodBlock(FrameworkMethod frameworkMethod) {
		try {
			Method newMethod = this.testContext.getTestClass().getJavaClass().getMethod(frameworkMethod.getName());
			FrameworkMethod newFrameworkMethod = new FrameworkMethod(newMethod);
			return super.methodBlock(newFrameworkMethod);
		} catch (NoSuchMethodException e) {
			return new Fail(e);
		} catch (SecurityException e) {
{'fraction_non_alphanumeric': 0.10202898550724637, 'fraction_numerical': 0.002782608695652174, 'mean_word_length': 5.990275526742302, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '5014976', 'n_tokens_mistral': 2737, 'n_tokens_neox': 2556, 'n_words': 475}