<commit_before>/* 
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.grinder.scriptengine.groovy.junit;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.grinder.common.GrinderProperties;
import net.grinder.engine.process.JUnitThreadContextInitializer;
import net.grinder.engine.process.JUnitThreadContextUpdater;
import net.grinder.script.Grinder;
import net.grinder.scriptengine.exception.AbstractExceptionProcessor;
import net.grinder.scriptengine.groovy.GroovyExceptionProcessor;
import net.grinder.scriptengine.groovy.junit.annotation.AfterProcess;
import net.grinder.scriptengine.groovy.junit.annotation.AfterThread;
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess;
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread;
import net.grinder.scriptengine.groovy.junit.annotation.Repeat;
import net.grinder.scriptengine.groovy.junit.annotation.RunRate;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.internal.AssumptionViolatedException;
import org.junit.internal.runners.model.EachTestNotifier;
import org.junit.internal.runners.model.MultipleFailureException;
import org.junit.internal.runners.statements.RunAfters;
import org.junit.internal.runners.statements.RunBefores;
import org.junit.rules.MethodRule;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
import org.junit.runner.notification.StoppedByUserException;
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.junit.runners.model.TestClass;

/**
 * Grinder JUnit Runner. Grinder JUnit Runner is the custom {@link Runner} which lets the user can
 * run the Grinder script in the JUnit context.
 * 
 * This runner has a little bit different characteristic from conventional JUnit test.
 * <ul>
 * <li>All Test annotated tests are executed with a single instance.</li>
 * <li>{@link BeforeProcess} and {@link AfterProcess} annotated methods are executed per each
 * process.</li>
 * <li>{@link BeforeThread} and {@link AfterThread} annotated methods are executed per each thread.</li>
 * <li>{@link Repeat} annotated
 * </ul>
 * 
 * In addition, it contains a little different behavior from generic grinder test script.
 * <ul>
 * <li>It only initiates only 1 process and 1 thread.</li>
 * <li>Each <code>&#064;test</code> annotated method are independent to run. So one failure from one
 * method doesn't block the other methods' runs</li>
 * </ul>
 * 
 * @author <NAME>
 * @author Mavlarn
 * @since 1.0
 * @see BeforeProcess
 * @see BeforeThread
 * @see AfterThread
 * @see AfterProcess
 * @see Repeat
 * 
 */
public class GrinderRunner extends BlockJUnit4ClassRunner {
	private JUnitThreadContextInitializer threadContextInitializer;
	private JUnitThreadContextUpdater threadContextUpdater;
	private TestObjectFactory testTargetFactory;
	private PerThreadStatement finalPerThreadStatement;
	private AbstractExceptionProcessor exceptionProcessor = new GroovyExceptionProcessor();
	private boolean enableRateRunner = true;
	private Map<FrameworkMethod, Statement> frameworkMethodCache = new HashMap<FrameworkMethod, Statement>();

	/**
	 * Constructor.
	 * 
	 * @param klass
	 *            klass
	 * @throws InitializationError
	 *             class initialization error.
	 */
	public GrinderRunner(Class<?> klass) throws InitializationError {
		super(klass);
		this.testTargetFactory = new TestObjectFactory() {
			@Override
			public TestClass getTestClass() {
				return GrinderRunner.this.getTestClass();
			}

			@Override
			public Object createTest() throws Exception {
				return GrinderRunner.this.createTest();
			}
		};

		initializeGrinderContext();
	}

	protected void initializeGrinderContext() {
		this.threadContextInitializer = new JUnitThreadContextInitializer();
		this.threadContextInitializer.initialize();
		this.threadContextUpdater = threadContextInitializer.getThreadContextUpdater();
		this.finalPerThreadStatement = new PerThreadStatement() {
			@Override
			void before() {
				attachWorker();
			}

			@Override
			void after() {
				detachWorker();
			}
		};
	}

	@Override
	protected List<FrameworkMethod> getChildren() {
		List<FrameworkMethod> children = super.getChildren();
		return children;
	}

	@Override
	public void run(RunNotifier notifier) {
		registerRunNotifierListener(notifier);
		Description description = getDescription();
		if (description.testCount() == 1 || Grinder.grinder == null || Grinder.grinder.getProperties() == null
						|| Grinder.grinder.getProperties().getBoolean("grinder.script.validation", false)) {
			enableRateRunner = false;
		}
		EachTestNotifier testNotifier = new EachTestNotifier(notifier, description);
		try {
			Statement statement = classBlock(notifier);
			statement.evaluate();
		} catch (AssumptionViolatedException e) {
			testNotifier.fireTestIgnored();
		} catch (StoppedByUserException e) {<commit_msg>[NGRINDER-626] Add @RunRate annotation to control the run frequency <commit_after>/* 
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.grinder.scriptengine.groovy.junit;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.grinder.common.GrinderProperties;
import net.grinder.engine.process.JUnitThreadContextInitializer;
import net.grinder.engine.process.JUnitThreadContextUpdater;
import net.grinder.script.Grinder;
import net.grinder.scriptengine.exception.AbstractExceptionProcessor;
import net.grinder.scriptengine.groovy.GroovyExceptionProcessor;
import net.grinder.scriptengine.groovy.junit.annotation.AfterProcess;
import net.grinder.scriptengine.groovy.junit.annotation.AfterThread;
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess;
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread;
import net.grinder.scriptengine.groovy.junit.annotation.Repeat;
import net.grinder.scriptengine.groovy.junit.annotation.RunRate;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.internal.AssumptionViolatedException;
import org.junit.internal.runners.model.EachTestNotifier;
import org.junit.internal.runners.model.MultipleFailureException;
import org.junit.internal.runners.statements.RunAfters;
import org.junit.internal.runners.statements.RunBefores;
import org.junit.rules.MethodRule;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
import org.junit.runner.notification.StoppedByUserException;
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.junit.runners.model.TestClass;

/**
 * Grinder JUnit Runner. Grinder JUnit Runner is the custom {@link Runner} which lets the user can
 * run the Grinder script in the JUnit context.
 * 
 * This runner has a little bit different characteristic from conventional JUnit test.
 * <ul>
 * <li>All Test annotated tests are executed with a single instance.</li>
 * <li>{@link BeforeProcess} and {@link AfterProcess} annotated methods are executed per each
 * process.</li>
 * <li>{@link BeforeThread} and {@link AfterThread} annotated methods are executed per each thread.</li>
 * <li>{@link Repeat} annotated
 * </ul>
 * 
 * In addition, it contains a little different behavior from generic grinder test script.
 * <ul>
 * <li>It only initiates only 1 process and 1 thread.</li>
 * <li>Each <code>&#064;test</code> annotated method are independent to run. So one failure from one
 * method doesn't block the other methods' runs</li>
 * </ul>
 * 
 * @author <NAME>
 * @author Mavlarn
 * @since 1.0
 * @see BeforeProcess
 * @see BeforeThread
 * @see AfterThread
 * @see AfterProcess
 * @see Repeat
 * 
 */
public class GrinderRunner extends BlockJUnit4ClassRunner {
	private JUnitThreadContextInitializer threadContextInitializer;
	private JUnitThreadContextUpdater threadContextUpdater;
	private TestObjectFactory testTargetFactory;
	private PerThreadStatement finalPerThreadStatement;
	private AbstractExceptionProcessor exceptionProcessor = new GroovyExceptionProcessor();
	private boolean enableRateRunner = true;
	private Map<FrameworkMethod, Statement> frameworkMethodCache = new HashMap<FrameworkMethod, Statement>();

	/**
	 * Constructor.
	 * 
	 * @param klass
	 *            klass
	 * @throws InitializationError
	 *             class initialization error.
	 */
	public GrinderRunner(Class<?> klass) throws InitializationError {
		super(klass);
		this.testTargetFactory = new TestObjectFactory() {
			@Override
			public TestClass getTestClass() {
				return GrinderRunner.this.getTestClass();
			}

			@Override
			public Object createTest() throws Exception {
				return GrinderRunner.this.createTest();
			}
		};

		initializeGrinderContext();
	}

	protected void initializeGrinderContext() {
		this.threadContextInitializer = new JUnitThreadContextInitializer();
		this.threadContextInitializer.initialize();
		this.threadContextUpdater = threadContextInitializer.getThreadContextUpdater();
		this.finalPerThreadStatement = new PerThreadStatement() {
			@Override
			void before() {
				attachWorker();
			}

			@Override
			void after() {
				detachWorker();
			}
		};
	}

	@Override
	protected List<FrameworkMethod> getChildren() {
		List<FrameworkMethod> children = super.getChildren();
		return children;
	}

	@Override
	public void run(RunNotifier notifier) {
		registerRunNotifierListener(notifier);
		Description description = getDescription();
		if (description.testCount() == 1
						|| Grinder.grinder == null
						|| (Grinder.grinder.getProperties() != null && Grinder.grinder.getProperties().getBoolean(
										"grinder.script.validation", false))) {
			enableRateRunner = false;
		}
		EachTestNotifier testNotifier = new EachTestNotifier(notifier, description);
		try {
			Statement statement = classBlock(notifier);
			statement.evaluate();
		} catch (AssumptionViolatedException e) {
			testNotifier.fireTestIgnored();
		} catch (StoppedByUserException e) {
{'fraction_non_alphanumeric': 0.09579399141630901, 'fraction_numerical': 0.0026609442060085836, 'mean_word_length': 5.030538302277432, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, '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': '2019964', 'n_tokens_mistral': 3443, 'n_tokens_neox': 3202, 'n_words': 854}