<commit_before>/*
 * (c) 2003-2004 ThoughtWorks Ltd
 *
 * See license.txt for license details
 */
package jbehave.junit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import jbehave.core.Ensure;
import jbehave.core.mock.UsingConstraints;
import jbehave.junit.JUnitAdapter.BehavioursAdapter;
import junit.framework.Test;
import junit.framework.TestResult;


/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <NAME>
 */
public class JUnitAdapterBehaviour extends UsingConstraints {
    private static final List sequenceOfEvents = new ArrayList();
    
    public void setUp() {
        sequenceOfEvents.clear();
    }
    
    public static class HasSingleMethod {
        public void shouldDoSomething() throws Exception {
        }
    }
    
    public void shouldCountSingleBehaviourMethodAsTest() throws Exception {
        // setup
        JUnitAdapter.setBehaviours(new BehavioursAdapter(HasSingleMethod.class));
        Test suite = JUnitAdapter.suite();
        
        // execute
        int testCaseCount = suite.countTestCases();
        
        // verify
        ensureThat(testCaseCount, eq(1));
    }
    
    public static class HasTwoMethods {
        public void shouldDoSomething() throws Exception {
        }
        
        public void shouldDoSomethingElse() throws Exception {
        }
    }
    
    public void shouldCountMultipleBehaviourMethodsAsTests() throws Exception {
        // setup
        JUnitAdapter.setBehaviours(new BehavioursAdapter(HasTwoMethods.class));
        Test suite = JUnitAdapter.suite();
        // execute
        int testCaseCount = suite.countTestCases();
        
        // verify
        ensureThat(testCaseCount, eq(2));
    }
    
    public static class HasFailingMethod {
        public void shouldDoSomething() throws Exception {
            Ensure.impossible("should not be invoked");
        }
    }
    
    public void shouldNotExecuteBehaviourMethodsWhileCountingThem() throws Exception {
        // setup
        JUnitAdapter.setBehaviours(new BehavioursAdapter(HasFailingMethod.class));
        Test suite = JUnitAdapter.suite();
        
        // execute
        suite.countTestCases();
    }
    
    public static class SomeBehaviourClass {
        public void shouldDoSomething() throws Exception {
            sequenceOfEvents.add("shouldDoSomething");
        }
        public void shouldDoSomethingElse() throws Exception {
            sequenceOfEvents.add("shouldDoSomethingElse");
        }
    }
    
    public void shouldExecuteBehaviourMethods() throws Exception {
        // setup
        JUnitAdapter.setBehaviours(new BehavioursAdapter(SomeBehaviourClass.class));
        Test suite = JUnitAdapter.suite();
        
        TestResult testResult = new TestResult() {
            public void startTest(Test test) {
                sequenceOfEvents.add("startTest");
            }
            public void endTest(Test test) {
                sequenceOfEvents.add("endTest");
            }
        };
        
        // execute
        suite.run(testResult);
        
        // verify
        List expectedSequenceOfEvents = Arrays.asList(new String[] {
                "startTest", "shouldDoSomething", "endTest",
                "startTest", "shouldDoSomethingElse", "endTest"
        });
        Ensure.that(testResult.wasSuccessful());
        for ( Iterator i = expectedSequenceOfEvents.iterator(); i.hasNext(); ){
            String event = (String)i.next();
            ensureThat(event, isContainedIn(sequenceOfEvents));            
        }
    }
}
<commit_msg>Increased behaviour coverage.<commit_after>/*
 * (c) 2003-2004 ThoughtWorks Ltd
 *
 * See license.txt for license details
 */
package jbehave.junit;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import jbehave.core.Ensure;
import jbehave.core.behaviour.Behaviours;
import jbehave.core.exception.JBehaveFrameworkError;
import jbehave.core.mock.UsingConstraints;
import jbehave.junit.JUnitAdapter.BehavioursAdapter;
import junit.framework.Test;
import junit.framework.TestResult;


/**
 * @author <a href="mailto:<EMAIL>@thoughtworks.com"><NAME></a>
 * @author <NAME>
 */
public class JUnitAdapterBehaviour extends UsingConstraints {
    private static final List sequenceOfEvents = new ArrayList();
    
    public void setUp() {
        sequenceOfEvents.clear();
    }
    
    public static class HasSingleMethod {
        public void shouldDoSomething() throws Exception {
        }
    }
    
    public void shouldCountSingleBehaviourMethodAsTest() throws Exception {
        // setup
        JUnitAdapter.setBehaviours(new BehavioursAdapter(HasSingleMethod.class));
        Test suite = JUnitAdapter.suite();
        
        // execute
        int testCaseCount = suite.countTestCases();
        
        // verify
        ensureThat(testCaseCount, eq(1));
    }
    
    public static class HasTwoMethods {
        public void shouldDoSomething() throws Exception {
        }
        
        public void shouldDoSomethingElse() throws Exception {
        }
    }
    
    public void shouldCountMultipleBehaviourMethodsAsTests() throws Exception {
        // setup
        JUnitAdapter.setBehaviours(new BehavioursAdapter(HasTwoMethods.class));
        Test suite = JUnitAdapter.suite();
        // execute
        int testCaseCount = suite.countTestCases();
        
        // verify
        ensureThat(testCaseCount, eq(2));
    }
    
    public static class HasFailingMethod {
        public void shouldDoSomething() throws Exception {
            Ensure.impossible("should not be invoked");
        }
    }
    
    public void shouldNotExecuteBehaviourMethodsWhileCountingThem() throws Exception {
        // setup
        JUnitAdapter.setBehaviours(new BehavioursAdapter(HasFailingMethod.class));
        Test suite = JUnitAdapter.suite();
        
        // execute
        suite.countTestCases();
    }
    
    public static class SomeBehaviourClass {
        public void shouldDoSomething() throws Exception {
            sequenceOfEvents.add("shouldDoSomething");
        }
        public void shouldDoSomethingElse() throws Exception {
            sequenceOfEvents.add("shouldDoSomethingElse");
        }
    }
    
    public void shouldExecuteBehavioursInjected() throws Exception {
        // setup
        JUnitAdapter.setBehaviours(new BehavioursAdapter(SomeBehaviourClass.class));
        Test suite = JUnitAdapter.suite();
        
        assertEventsAreInvoked(suite);
    }


    public void shouldExecuteBehavioursLoadedFromBehavioursClassProperty() throws Exception {
        // setup
        JUnitAdapter.setBehaviours(null);
        JUnitAdapter.setClassLoader(new BehavioursClassLoader("behavioursClass="+SomeBehaviours.class.getName()));
        Test suite = JUnitAdapter.suite();

        assertEventsAreInvoked(suite);
    }    

    public void shouldExecuteBehavioursLoadedFromBehaviourClassProperty() throws Exception {
        // setup
        JUnitAdapter.setBehaviours(null);
        JUnitAdapter.setClassLoader(new BehavioursClassLoader("behaviourClass="+SomeBehaviourClass.class.getName()));
        Test suite = JUnitAdapter.suite();

        assertEventsAreInvoked(suite);
    }    

    public void shouldThrowErrorIfBehavioursAreNotFound() throws Exception {
        // setup
        JUnitAdapter.setBehaviours(null);
        JUnitAdapter.setClassLoader(new BehavioursClassLoader(""));
        try {
            JUnitAdapter.suite();
        } catch ( JBehaveFrameworkError e) {
            // expected
        }        
    }    

    
    private void assertEventsAreInvoked(Test suite) {
        TestResult testResult = new TestResult() {
            public void startTest(Test test) {
                sequenceOfEvents.add("startTest");
            }
            public void endTest(Test test) {
                sequenceOfEvents.add("endTest");
            }
        };
        
        // execute
        suite.run(testResult);
        
        // verify
        List expectedSequenceOfEvents = Arrays.asList(new String[] {
                "startTest", "shouldDoSomething", "endTest",
                "startTest", "shouldDoSomethingElse", "endTest"
        });
        Ensure.that(testResult.wasSuccessful());
        for ( Iterator i = expectedSequenceOfEvents.iterator(); i.hasNext(); ){
            String event = (String)i.next();
            ensureThat(event, isContainedIn(sequenceOfEvents));            
        }
    }

    
    public static class SomeBehaviours implements Behaviours {
        public Class[] getBehaviours() {
            return new Class[]{SomeBehaviourClass.class};
        }        
    }
    
    static class BehavioursClassLoader extends ClassLoader {
        private String property;
        
        public BehavioursClassLoader(String property) {
            this.property = property;
        }

        public InputStream getResourceAsStream(String name){
            return new ReaderInputStream(new StringReader(property));
        }
    }
    
    static class ReaderInputStream extends InputStream {
        private Reader reader;

        public ReaderInputStream(Reader isr) {
            this.reader = isr;
        }

        public int read() throws IOException {
            return reader.read();
        }
    }

}

{'fraction_non_alphanumeric': 0.08855972265994327, 'fraction_numerical': 0.0021010610358230906, 'mean_word_length': 2.361581920903955, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3875559', 'n_tokens_mistral': 2540, 'n_tokens_neox': 2485, 'n_words': 515}