Package org.junit.tests

Source Code of org.junit.tests.SortableTest$UnsortableRunnersAreHandledWithoutCrashing$UnsortableRunner

package org.junit.tests;

import static org.junit.Assert.assertEquals;

import java.util.Comparator;

import org.junit.Before;
import org.junit.Test;
import org.junit.internal.runners.EmptyDescription;
import org.junit.internal.runners.InitializationError;
import org.junit.internal.runners.TestClassRunner;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Sorter;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.Enclosed;

@RunWith(Enclosed.class)
public class SortableTest {
  private static Comparator<Description> forward() {
    return new Comparator<Description>() {
      public int compare(Description o1, Description o2) {
        return o1.getDisplayName().compareTo(o2.getDisplayName());
      }
    };
  }
 
  private static Comparator<Description> backward() {
    return new Comparator<Description>() {
      public int compare(Description o1, Description o2) {
        return o2.getDisplayName().compareTo(o1.getDisplayName());
      }
    };
  }

  public static class TestClassRunnerIsSortable {
    private static String log= "";
   
    public static class SortMe {
      @Test public void a() { log+= "a"; }
      @Test public void b() { log+= "b"; }
      @Test public void c() { log+= "c"; }
    }
   
    @Before public void resetLog() {
      log= "";
    }
   
    @Test public void sortingForwardWorksOnTestClassRunner() {
      Request forward= Request.aClass(SortMe.class).sortWith(forward());
     
      new JUnitCore().run(forward);
      assertEquals("abc", log);
    }

    @Test public void sortingBackwardWorksOnTestClassRunner() {
      Request backward= Request.aClass(SortMe.class).sortWith(backward());
     
      new JUnitCore().run(backward);
      assertEquals("cba", log);
    }
   
    @RunWith(Enclosed.class)
    public static class Enclosing {
      public static class A {
        @Test public void a() { log+= "Aa"; }
        @Test public void b() { log+= "Ab"; }
        @Test public void c() { log+= "Ac"; }       
      }
      public static class B {
        @Test public void a() { log+= "Ba"; }
        @Test public void b() { log+= "Bb"; }
        @Test public void c() { log+= "Bc"; }               
      }
    }

    @Test public void sortingForwardWorksOnSuite() {
      Request forward= Request.aClass(Enclosing.class).sortWith(forward());
     
      new JUnitCore().run(forward);
      assertEquals("AaAbAcBaBbBc", log);
    }

    @Test public void sortingBackwardWorksOnSuite() {
      Request backward= Request.aClass(Enclosing.class).sortWith(backward());
     
      new JUnitCore().run(backward);
      assertEquals("BcBbBaAcAbAa", log);
    }

  }
 
  public static class UnsortableRunnersAreHandledWithoutCrashing {
    public static class UnsortableRunner extends Runner {
      public UnsortableRunner(Class<?> klass) {
      }
     
      @Override
      public Description getDescription() {
        return new EmptyDescription();
      }
     
      @Override
      public void run(RunNotifier notifier) {
      }
    }
   
    @RunWith(UnsortableRunner.class)
    public static class Unsortable {
      @Test public void a() {}   
    }
   
    @Test public void unsortablesAreHandledWithoutCrashing() {
      Request unsorted= Request.aClass(Unsortable.class).sortWith(forward());
      new JUnitCore().run(unsorted);
    }
   
    @Test public void testClassRunnerCanBeWrappedAroundUnsortable() throws InitializationError {
      TestClassRunner runner= new TestClassRunner(Unsortable.class, new UnsortableRunner(Unsortable.class));
      runner.sort(new Sorter(forward()));
    }
  }
}
TOP

Related Classes of org.junit.tests.SortableTest$UnsortableRunnersAreHandledWithoutCrashing$UnsortableRunner

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.