<commit_before>package org.jmock.integration.junit4;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.jmock.Mockery;
import org.junit.internal.runners.InitializationError;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.internal.runners.TestMethod;
import org.junit.runner.Runner;


/**
 * A test {@link Runner} that asserts that all expectations have been met after
 * the test has finished and before the fixture is torn down.
 * 
 * @author nat
 * 
 */
public class JMock extends JUnit4ClassRunner {
    private Field mockeryField;

    public JMock(Class<?> testClass) throws InitializationError {
        super(testClass);
        mockeryField = findMockeryField(testClass);
        mockeryField.setAccessible(true);
    }

    @Override
    protected TestMethod wrapMethod(Method method) {
        return new TestMethod(method, getTestClass()) {
            @Override
            public void invoke(Object testFixture)
                throws IllegalAccessException, InvocationTargetException {
                try {
                    super.invoke(testFixture);
                    assertMockeryIsSatisfied(testFixture);
                }
                catch (InvocationTargetException e) {
                    Throwable actual = e.getTargetException();
                    Class<? extends Throwable> expectedType = this.getExpectedException();
                    
                    if (expectedType != null && expectedType.isInstance(actual)) {
                        assertMockeryIsSatisfied(testFixture);
                    }
                    
                    throw e;
                }
            }
        };
    }
    
    private void assertMockeryIsSatisfied(Object testFixture) {
        mockeryOf(testFixture).assertIsSatisfied();
    }
    
    protected Mockery mockeryOf(Object test) {
        try {
            Mockery mockery = (Mockery)mockeryField.get(test);
            if (mockery == null) {
                throw new IllegalStateException("Mockery named '"
                    + mockeryField.getName() + "' is null");
            }
            return mockery;
        }
        catch (IllegalAccessException e) {
            throw new IllegalStateException("cannot get value of field "
                + mockeryField.getName(), e);
        }
    }

    static Field findMockeryField(Class<?> testClass) throws InitializationError {
        for (Class<?> c = testClass; c != Object.class; c = c.getSuperclass()) {
            for (Field field: c.getDeclaredFields()) {
                if (Mockery.class.isAssignableFrom(field.getType())) {
                    return field;
                }
            }
        }
        
        throw new InitializationError("no Mockery found in test class "
            + testClass);
    }
}
<commit_msg>Reapplied upgrade to JUnit 4.5<commit_after>package org.jmock.integration.junit4;

import java.lang.reflect.Field;

import org.jmock.Mockery;
import org.junit.runner.Runner;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;


/**
 * A test {@link Runner} that asserts that all expectations have been met after
 * the test has finished and before the fixture is torn down.
 * 
 * @author nat
 * 
 */
public class JMock extends BlockJUnit4ClassRunner {
    private Field mockeryField;

    public JMock(Class<?> testClass) throws InitializationError {
        super(testClass);
        mockeryField = findMockeryField(testClass);
        mockeryField.setAccessible(true);
    }
    
    @Override
    protected Statement possiblyExpectingExceptions(FrameworkMethod method, Object test, Statement next) {
        return verify(method, test, 
                      super.possiblyExpectingExceptions(method, test, next));
    }
    
    protected Statement verify(
        @SuppressWarnings("unused") FrameworkMethod method, 
        final Object test, 
        final Statement link) 
    {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                link.evaluate();
                assertMockeryIsSatisfied(test);
            }
        };
    }
    
    protected Mockery mockeryOf(Object test) {
        try {
            Mockery mockery = (Mockery)mockeryField.get(test);
            if (mockery == null) {
                throw new IllegalStateException("Mockery named '" + mockeryField.getName() + "' is null");
            }
            return mockery;
        }
        catch (IllegalAccessException e) {
            throw new IllegalStateException("cannot get value of field " + mockeryField.getName(), e);
        }
    }
    
    protected void assertMockeryIsSatisfied(Object test) {
        mockeryOf(test).assertIsSatisfied();
    }
    
    static Field findMockeryField(Class<?> testClass) throws InitializationError {
        for (Class<?> c = testClass; c != Object.class; c = c.getSuperclass()) {
            for (Field field: c.getDeclaredFields()) {
                if (Mockery.class.isAssignableFrom(field.getType())) {
                    return field;
                }
            }
        }
        
        throw new InitializationError("no Mockery found in test class " + testClass);
    }
}

{'fraction_non_alphanumeric': 0.08726197078942503, 'fraction_numerical': 0.0014790164540580515, 'mean_word_length': 2.1199538638985005, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '6339512', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1372, 'n_words': 336}