Coder Social home page Coder Social logo

Comments (7)

dsaff avatar dsaff commented on August 22, 2024

Sorry for the long delay on this. Unfortunately, I'm having a difficult time pulling up the original SourceForge bugs. Can you give an example test that would pass once this is fixed? Thanks.

from junit4.

reinholdfuereder avatar reinholdfuereder commented on August 22, 2024

No problem, any active work on JUnit is great.
I'll try to find some time during first half of next week to provide such a test.

On 15.04.2011, at 21:57, dsaff wrote:

Sorry for the long delay on this. Unfortunately, I'm having a difficult time pulling up the original SourceForge bugs. Can you give an example test that would pass once this is fixed? Thanks.

Reply to this email directly or view it on GitHub:
https://github.com/KentBeck/junit/issues/38#comment_1010505

from junit4.

reinholdfuereder avatar reinholdfuereder commented on August 22, 2024

This "slightly" overloaded test class contains the items touched by this issue.

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

public class ParentRunnerFilteringTest {

private static class SingleMethodNameFilter extends Filter {

    private String methodName;

    public SingleMethodNameFilter(String methodName) {
        this.methodName = methodName;
    }

    @Override
    public boolean shouldRun(Description description) {
        return description.getMethodName() == null
                || !description.getMethodName().equals(methodName);
    }

    @Override
    public String describe() {
        return "filter method name: " + methodName;
    }

}

private static class CountingFilter extends Filter {

    private final Map<Description, Integer> countMap = new HashMap<Description, Integer>();

    @Override
    public boolean shouldRun(Description description) {
        Integer count = countMap.get(description);
        if (count == null) {
            countMap.put(description, 1);
        } else {
            countMap.put(description, count + 1);
        }
        return true;
    }

    @Override
    public String describe() {
        return "filter counter";
    }

    public void printStats() {
        final StringBuffer sb = new StringBuffer();
        sb.append("CountingFilter Stats:");
        for (Entry<Description, Integer> entry : countMap.entrySet()) {
            sb.append("\n");
            sb.append("\t");
            sb.append(entry.getKey());
            sb.append(": ");
            sb.append(entry.getValue());
        }
        System.out.println(sb);
    }

    public Integer getCount(final Description desc) {
        Integer count = null;
        if (countMap.containsKey(desc)) {
            count = countMap.get(desc);
        } else {
            boolean found = false;
            Iterator<Description> it = countMap.keySet().iterator();
            while (!found && it.hasNext()) {
                Description curDesc = it.next();
                if (curDesc.getDisplayName().equals(desc.getDisplayName())) {
                    found = true;
                    count = countMap.get(curDesc);
                }
            }
        }
        return count;
    }
}


public static class ExampleTest {

    @Test
    public void test1() throws Exception {
        System.out.println("Run: test1");
        assertEquals(1, 1);
    }

}


@RunWith(Suite.class)
@SuiteClasses({
    ExampleTest.class
})
public static class ExampleSuite {

}


@Test
public void testClassFiltering() throws Exception {
    Request request = Request.aClass(ExampleTest.class);
    Runner runner = request.getRunner();

    Filter filter = new SingleMethodNameFilter("test1");
    try {
        filter.apply(runner);
        fail("Expected 'NoTestsRemainException' due to complete filtering");
    } catch (NoTestsRemainException e) {
        // Expected exception
    }
}

@Test
public void testRunClassFiltering() throws Exception {
    JUnitCore junitCore = new JUnitCore();
    Request request = Request.aClass(ExampleTest.class);
    Request requestFiltered = request.filterWith(new SingleMethodNameFilter("test1"));
    Result result = junitCore.run(requestFiltered);
    assertEquals(1, result.getRunCount());
    assertEquals(1, result.getFailureCount());
    assertTrue(result.getFailures().get(0).toString().startsWith(
            "initializationError(org.junit.runner.manipulation.Filter): No tests found matching filter method name: test1 "));
}

@Test
public void testSuiteFiltering() throws Exception {
    Request request = Request.aClass(ExampleSuite.class);
    Runner runner = request.getRunner();

    assertTrue(runner instanceof Suite);
    Suite suite = (Suite) runner;

    Filter filter = new SingleMethodNameFilter("test1");
    try {
        suite.filter(filter);
        fail("Expected 'NoTestsRemainException' due to complete filtering"); // TODO: fails here currently
    } catch (NoTestsRemainException e) {
        // Expected exception
    }
}

@Test
public void testSuiteFiltering2() throws Exception {
    Request request = Request.aClass(ExampleSuite.class);
    Runner runner = request.getRunner();

    assertTrue(runner instanceof Suite);

    Filter filter = new SingleMethodNameFilter("test1");
    try {
        filter.apply(runner);
        fail("Expected 'NoTestsRemainException' due to complete filtering"); // TODO: fails here currently
    } catch (NoTestsRemainException e) {
        // Expected exception
    }
}

@Test
public void testRunSuiteFiltering() throws Exception {
    JUnitCore junitCore = new JUnitCore();
    Request request = Request.aClass(ExampleSuite.class);
    Request requestFiltered = request.filterWith(new SingleMethodNameFilter("test1"));
    Result result = junitCore.run(requestFiltered);
    assertEquals(0, result.getRunCount());
    assertEquals(0, result.getFailureCount());
}


@Test
public void testCountClassFiltering() throws Exception {
    Class<ExampleTest> clazz = ExampleTest.class;

    JUnitCore junitCore = new JUnitCore();
    Request request = Request.aClass(clazz);
    CountingFilter countingFilter = new CountingFilter();
    Request requestFiltered = request.filterWith(countingFilter);
    Result result = junitCore.run(requestFiltered);
    assertEquals(1, result.getRunCount());
    assertEquals(0, result.getFailureCount());
    countingFilter.printStats();

    Description desc = Description.createTestDescription(clazz, "test1");
    int count = countingFilter.getCount(desc);
    //TODO: now count == 4, but is this reasonably?
    assertTrue("Filtering of " + desc + " was called too often: " + count + " times", count < 4); // TODO: fails here currently
}

@Test
public void testCountSuiteFiltering() throws Exception {
    Class<ExampleSuite> suiteClazz = ExampleSuite.class;
    Class<ExampleTest> clazz = ExampleTest.class;

    JUnitCore junitCore = new JUnitCore();
    Request request = Request.aClass(suiteClazz);
    CountingFilter countingFilter = new CountingFilter();
    Request requestFiltered = request.filterWith(countingFilter);
    Result result = junitCore.run(requestFiltered);
    assertEquals(1, result.getRunCount());
    assertEquals(0, result.getFailureCount());
    countingFilter.printStats();

    Description suiteDesc = Description.createSuiteDescription(clazz);
    int suiteCount = countingFilter.getCount(suiteDesc);
    //TODO: now count == 4, but is this reasonably?
    assertTrue("Filtering of " + suiteDesc + " was called too often: " + suiteCount + " times", suiteCount < 4); // TODO: fails here currently

    Description desc = Description.createTestDescription(ExampleTest.class, "test1");
    int count = countingFilter.getCount(desc);
    //TODO: now count == 9 (!), but is this reasonably?
    assertTrue("Filtering of " + desc + " was called too often: " + count + " times", count < 9); // TODO: fails here currently
}

}

from junit4.

dsaff avatar dsaff commented on August 22, 2024

Great! Thanks.

On Tue, Apr 19, 2011 at 11:08 AM, reinholdfuereder
[email protected]
wrote:

This "slightly" overloaded test class contains the items touched by this issue.

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

public class ParentRunnerFilteringTest {

       private static class SingleMethodNameFilter extends Filter {

           private String methodName;

           public SingleMethodNameFilter(String methodName) {
               this.methodName = methodName;
           }

           @OverRide
           public boolean shouldRun(Description description) {
               return description.getMethodName() == null
                               || !description.getMethodName().equals(methodName);
           }

           @OverRide
           public String describe() {
               return "filter method name: " + methodName;
           }

       }

       private static class CountingFilter extends Filter {

           private final Map<Description, Integer> countMap = new HashMap<Description, Integer>();

           @OverRide
           public boolean shouldRun(Description description) {
               Integer count = countMap.get(description);
               if (count == null) {
                       countMap.put(description, 1);
               } else {
                       countMap.put(description, count + 1);
               }
               return true;
           }

           @OverRide
           public String describe() {
               return "filter counter";
           }

           public void printStats() {
               final StringBuffer sb = new StringBuffer();
               sb.append("CountingFilter Stats:");
               for (Entry<Description, Integer> entry : countMap.entrySet()) {
                       sb.append("\n");
                       sb.append("\t");
                       sb.append(entry.getKey());
                       sb.append(": ");
                       sb.append(entry.getValue());
                       }
               System.out.println(sb);
           }

               public Integer getCount(final Description desc) {
                       Integer count = null;
                       if (countMap.containsKey(desc)) {
                               count = countMap.get(desc);
                       } else {
                               boolean found = false;
                               Iterator it = countMap.keySet().iterator();
                               while (!found && it.hasNext()) {
                                       Description curDesc = it.next();
                                       if (curDesc.getDisplayName().equals(desc.getDisplayName())) {
                                               found = true;
                                               count = countMap.get(curDesc);
                                       }
                               }
                       }
                       return count;
               }
       }

       public static class ExampleTest {

               @test
               public void test1() throws Exception {
                   System.out.println("Run: test1");
                   assertEquals(1, 1);
               }

       }

       @RunWith(Suite.class)
       @SuiteClasses({
               ExampleTest.class
       })
       public static class ExampleSuite {

       }

       @test
       public void testClassFiltering() throws Exception {
               Request request = Request.aClass(ExampleTest.class);
               Runner runner = request.getRunner();

               Filter filter = new SingleMethodNameFilter("test1");
               try {
                       filter.apply(runner);
                       fail("Expected 'NoTestsRemainException' due to complete filtering");
               } catch (NoTestsRemainException e) {
                       // Expected exception
               }
       }

       @test
       public void testRunClassFiltering() throws Exception {
               JUnitCore junitCore = new JUnitCore();
           Request request = Request.aClass(ExampleTest.class);
           Request requestFiltered = request.filterWith(new SingleMethodNameFilter("test1"));
           Result result = junitCore.run(requestFiltered);
           assertEquals(1, result.getRunCount());
           assertEquals(1, result.getFailureCount());
           assertTrue(result.getFailures().get(0).toString().startsWith(
                       "initializationError(org.junit.runner.manipulation.Filter): No tests found matching filter method name: test1 "));
       }

       @test
       public void testSuiteFiltering() throws Exception {
               Request request = Request.aClass(ExampleSuite.class);
               Runner runner = request.getRunner();

               assertTrue(runner instanceof Suite);
               Suite suite = (Suite) runner;

               Filter filter = new SingleMethodNameFilter("test1");
               try {
                       suite.filter(filter);
                       fail("Expected 'NoTestsRemainException' due to complete filtering"); // TODO: fails here currently
               } catch (NoTestsRemainException e) {
                       // Expected exception
               }
       }

       @test
       public void testSuiteFiltering2() throws Exception {
               Request request = Request.aClass(ExampleSuite.class);
               Runner runner = request.getRunner();

               assertTrue(runner instanceof Suite);

               Filter filter = new SingleMethodNameFilter("test1");
               try {
                       filter.apply(runner);
                       fail("Expected 'NoTestsRemainException' due to complete filtering"); // TODO: fails here currently
               } catch (NoTestsRemainException e) {
                       // Expected exception
               }
       }

       @test
       public void testRunSuiteFiltering() throws Exception {
               JUnitCore junitCore = new JUnitCore();
           Request request = Request.aClass(ExampleSuite.class);
           Request requestFiltered = request.filterWith(new SingleMethodNameFilter("test1"));
           Result result = junitCore.run(requestFiltered);
           assertEquals(0, result.getRunCount());
           assertEquals(0, result.getFailureCount());
       }

       @test
       public void testCountClassFiltering() throws Exception {
               Class clazz = ExampleTest.class;

               JUnitCore junitCore = new JUnitCore();
               Request request = Request.aClass(clazz);
           CountingFilter countingFilter = new CountingFilter();
               Request requestFiltered = request.filterWith(countingFilter);
           Result result = junitCore.run(requestFiltered);
           assertEquals(1, result.getRunCount());
           assertEquals(0, result.getFailureCount());
           countingFilter.printStats();

           Description desc = Description.createTestDescription(clazz, "test1");
           int count = countingFilter.getCount(desc);
           //TODO: now count == 4, but is this reasonably?
           assertTrue("Filtering of " + desc + " was called too often: " + count + " times", count < 4); // TODO: fails here currently
       }

       @test
       public void testCountSuiteFiltering() throws Exception {
               Class suiteClazz = ExampleSuite.class;
               Class clazz = ExampleTest.class;

               JUnitCore junitCore = new JUnitCore();
               Request request = Request.aClass(suiteClazz);
           CountingFilter countingFilter = new CountingFilter();
               Request requestFiltered = request.filterWith(countingFilter);
           Result result = junitCore.run(requestFiltered);
           assertEquals(1, result.getRunCount());
           assertEquals(0, result.getFailureCount());
           countingFilter.printStats();

           Description suiteDesc = Description.createSuiteDescription(clazz);
           int suiteCount = countingFilter.getCount(suiteDesc);
           //TODO: now count == 4, but is this reasonably?
           assertTrue("Filtering of " + suiteDesc + " was called too often: " + suiteCount + " times", suiteCount < 4); // TODO: fails here currently

           Description desc = Description.createTestDescription(ExampleTest.class, "test1");
           int count = countingFilter.getCount(desc);
           //TODO: now count == 9 (!), but is this reasonably?
           assertTrue("Filtering of " + desc + " was called too often: " + count + " times", count < 9); // TODO: fails here currently
       }

}

Reply to this email directly or view it on GitHub:
https://github.com/KentBeck/junit/issues/38#comment_1027251

from junit4.

dsaff avatar dsaff commented on August 22, 2024

I took your suggestions, tweaked them, and came up with:

https://github.com/KentBeck/junit/pull/224

I'll push to the master on Monday unless you see something wrong. Thanks.

from junit4.

reinholdfuereder avatar reinholdfuereder commented on August 22, 2024

Great, thanks.

On 29.04.2011, at 20:36, dsaff wrote:

I took your suggestions, tweaked them, and came up with:

https://github.com/KentBeck/junit/pull/224

I'll push to the master on Monday unless you see something wrong. Thanks.

Reply to this email directly or view it on GitHub:
https://github.com/KentBeck/junit/issues/38#comment_1076571

from junit4.

dsaff avatar dsaff commented on August 22, 2024

Fixed with https://github.com/KentBeck/junit/commit/01b427b3b89ce04d01f0ddbd746d13a175feec5b

from junit4.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.