<commit_before>package com.dc.test.context.junit;

import static java.util.Arrays.asList;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import com.dc.test.context.AfterContext;
import com.dc.test.context.BeforeContext;
import com.dc.test.context.Context;

/**
 * Junit Rule that invokes any methods annotated as "Before" the context of the test being run.
 * 
 * @author dave
 * 
 */
public class ContextRule implements TestRule {

    private Object testInstance;
    private Map<String, List<Method>> beforeContexts = new HashMap<String, List<Method>>();
    private Map<String, List<Method>> afterContexts = new HashMap<String, List<Method>>();

    public ContextRule(Object testInstance) {
        this.testInstance = testInstance;
        for (Method method : testInstance.getClass().getMethods()) {
            storeBeforeContext(method);
            storeAfterContext(method);
        }
    }

    private void storeBeforeContext(Method method) {
        BeforeContext beforeContext = method.getAnnotation(BeforeContext.class);
        if (beforeContext != null) {
            addToStore(beforeContext.value(), method, beforeContexts);
        }
    }

    private void storeAfterContext(Method method) {
        AfterContext afterContext = method.getAnnotation(AfterContext.class);
        if (afterContext != null) {
            addToStore(afterContext.value(), method, afterContexts);
        }
    }

    private void addToStore(String contextName, Method method, Map<String, List<Method>> contextStore) {
        List<Method> methods = contextStore.get(contextName);
        if (methods == null) {
            methods = new ArrayList<Method>();
            contextStore.put(contextName, methods);
        }
        methods.add(method);
    }

    @Override
    public Statement apply(final Statement base, final Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                List<String> contextNames = getContextNames(description);
                executeBeforeContexts(contextNames);
                base.evaluate();
                executeAfterContexts(contextNames);
            }
        };
    }

    private List<String> getContextNames(Description description) {
        Context context = description.getAnnotation(Context.class);
        if (context.whenEachOf().length >= 1) {
            return asList(context.whenEachOf());
        }
        return asList(context.when());
    }

    private void executeBeforeContexts(List<String> contextNames) {
        executeContexts(contextNames, beforeContexts);
    }

    private void executeAfterContexts(List<String> contextNames) {
        executeContexts(contextNames, afterContexts);
    }

    private void executeContexts(List<String> contextNames, Map<String, List<Method>> contextStore) {
        for (String name : contextNames) {
            List<Method> methods = contextStore.get(name);
            if (methods == null) {
                continue;
            }
            invokeEachMethod(methods);
        }
    }<commit_msg>Add finally block around statement evaluation to ensure after runs<commit_after>package com.dc.test.context.junit;

import static java.util.Arrays.asList;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import com.dc.test.context.AfterContext;
import com.dc.test.context.BeforeContext;
import com.dc.test.context.Context;

/**
 * Junit Rule that invokes any methods annotated as "Before" the context of the test being run.
 * 
 * @author dave
 * 
 */
public class ContextRule implements TestRule {

    private Object testInstance;
    private Map<String, List<Method>> beforeContexts = new HashMap<String, List<Method>>();
    private Map<String, List<Method>> afterContexts = new HashMap<String, List<Method>>();

    public ContextRule(Object testInstance) {
        this.testInstance = testInstance;
        for (Method method : testInstance.getClass().getMethods()) {
            storeBeforeContext(method);
            storeAfterContext(method);
        }
    }

    private void storeBeforeContext(Method method) {
        BeforeContext beforeContext = method.getAnnotation(BeforeContext.class);
        if (beforeContext != null) {
            addToStore(beforeContext.value(), method, beforeContexts);
        }
    }

    private void storeAfterContext(Method method) {
        AfterContext afterContext = method.getAnnotation(AfterContext.class);
        if (afterContext != null) {
            addToStore(afterContext.value(), method, afterContexts);
        }
    }

    private void addToStore(String contextName, Method method, Map<String, List<Method>> contextStore) {
        List<Method> methods = contextStore.get(contextName);
        if (methods == null) {
            methods = new ArrayList<Method>();
            contextStore.put(contextName, methods);
        }
        methods.add(method);
    }

    @Override
    public Statement apply(final Statement base, final Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                List<String> contextNames = getContextNames(description);
                executeBeforeContexts(contextNames);
                try {
                    base.evaluate();
                } finally {
                    executeAfterContexts(contextNames);
                }
            }
        };
    }

    private List<String> getContextNames(Description description) {
        Context context = description.getAnnotation(Context.class);
        if (context.whenEachOf().length >= 1) {
            return asList(context.whenEachOf());
        }
        return asList(context.when());
    }

    private void executeBeforeContexts(List<String> contextNames) {
        executeContexts(contextNames, beforeContexts);
    }

    private void executeAfterContexts(List<String> contextNames) {
        executeContexts(contextNames, afterContexts);
    }

    private void executeContexts(List<String> contextNames, Map<String, List<Method>> contextStore) {
        for (String name : contextNames) {
            List<Method> methods = contextStore.get(name);
            if (methods == null) {
                continue;
            }
            invokeEachMethod(methods);
        }
    }
{'fraction_non_alphanumeric': 0.09840386586615903, 'fraction_numerical': 0.0002928686484111876, 'mean_word_length': 2.798665183537264, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 45, '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': '4155023', 'n_tokens_mistral': 1761, 'n_tokens_neox': 1720, 'n_words': 403}