JUnit 核心类源代码

1、Assert类

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package junit.framework;

/** @deprecated */
@Deprecated
public class Assert {
    protected Assert() {
    }

    public static void assertTrue(String message, boolean condition) {
        if (!condition) {
            fail(message);
        }

    }

    public static void assertTrue(boolean condition) {
        assertTrue((String)null, condition);
    }

    public static void assertFalse(String message, boolean condition) {
        assertTrue(message, !condition);
    }

    public static void assertFalse(boolean condition) {
        assertFalse((String)null, condition);
    }

    public static void fail(String message) {
        if (message == null) {
            throw new AssertionFailedError();
        } else {
            throw new AssertionFailedError(message);
        }
    }

    public static void fail() {
        fail((String)null);
    }

    public static void assertEquals(String message, Object expected, Object actual) {
        if (expected != null || actual != null) {
            if (expected == null || !expected.equals(actual)) {
                failNotEquals(message, expected, actual);
            }
        }
    }

    public static void assertEquals(Object expected, Object actual) {
        assertEquals((String)null, (Object)expected, (Object)actual);
    }

    public static void assertEquals(String message, String expected, String actual) {
        if (expected != null || actual != null) {
            if (expected == null || !expected.equals(actual)) {
                String cleanMessage = message == null ? "" : message;
                throw new ComparisonFailure(cleanMessage, expected, actual);
            }
        }
    }

    public static void assertEquals(String expected, String actual) {
        assertEquals((String)null, (String)expected, (String)actual);
    }

    public static void assertEquals(String message, double expected, double actual, double delta) {
        if (Double.compare(expected, actual) != 0) {
            if (Math.abs(expected - actual) > delta) {
                failNotEquals(message, new Double(expected), new Double(actual));
            }

        }
    }

    public static void assertEquals(double expected, double actual, double delta) {
        assertEquals((String)null, expected, actual, delta);
    }

    public static void assertEquals(String message, float expected, float actual, float delta) {
        if (Float.compare(expected, actual) != 0) {
            if (Math.abs(expected - actual) > delta) {
                failNotEquals(message, new Float(expected), new Float(actual));
            }

        }
    }

    public static void assertEquals(float expected, float actual, float delta) {
        assertEquals((String)null, expected, actual, delta);
    }

    public static void assertEquals(String message, long expected, long actual) {
        assertEquals(message, (Object)expected, (Object)actual);
    }

    public static void assertEquals(long expected, long actual) {
        assertEquals((String)null, expected, actual);
    }

    public static void assertEquals(String message, boolean expected, boolean actual) {
        assertEquals(message, (Object)expected, (Object)actual);
    }

    public static void assertEquals(boolean expected, boolean actual) {
        assertEquals((String)null, expected, actual);
    }

    public static void assertEquals(String message, byte expected, byte actual) {
        assertEquals(message, (Object)expected, (Object)actual);
    }

    public static void assertEquals(byte expected, byte actual) {
        assertEquals((String)null, (byte)expected, (byte)actual);
    }

    public static void assertEquals(String message, char expected, char actual) {
        assertEquals(message, (Object)expected, (Object)actual);
    }

    public static void assertEquals(char expected, char actual) {
        assertEquals((String)null, (char)expected, (char)actual);
    }

    public static void assertEquals(String message, short expected, short actual) {
        assertEquals(message, (Object)expected, (Object)actual);
    }

    public static void assertEquals(short expected, short actual) {
        assertEquals((String)null, (short)expected, (short)actual);
    }

    public static void assertEquals(String message, int expected, int actual) {
        assertEquals(message, (Object)expected, (Object)actual);
    }

    public static void assertEquals(int expected, int actual) {
        assertEquals((String)null, (int)expected, (int)actual);
    }

    public static void assertNotNull(Object object) {
        assertNotNull((String)null, object);
    }

    public static void assertNotNull(String message, Object object) {
        assertTrue(message, object != null);
    }

    public static void assertNull(Object object) {
        if (object != null) {
            assertNull("Expected: <null> but was: " + object.toString(), object);
        }

    }

    public static void assertNull(String message, Object object) {
        assertTrue(message, object == null);
    }

    public static void assertSame(String message, Object expected, Object actual) {
        if (expected != actual) {
            failNotSame(message, expected, actual);
        }
    }

    public static void assertSame(Object expected, Object actual) {
        assertSame((String)null, expected, actual);
    }

    public static void assertNotSame(String message, Object expected, Object actual) {
        if (expected == actual) {
            failSame(message);
        }

    }

    public static void assertNotSame(Object expected, Object actual) {
        assertNotSame((String)null, expected, actual);
    }

    public static void failSame(String message) {
        String formatted = message != null ? message + " " : "";
        fail(formatted + "expected not same");
    }

    public static void failNotSame(String message, Object expected, Object actual) {
        String formatted = message != null ? message + " " : "";
        fail(formatted + "expected same:<" + expected + "> was not:<" + actual + ">");
    }

    public static void failNotEquals(String message, Object expected, Object actual) {
        fail(format(message, expected, actual));
    }

    public static String format(String message, Object expected, Object actual) {
        String formatted = "";
        if (message != null && message.length() > 0) {
            formatted = message + " ";
        }

        return formatted + "expected:<" + expected + "> but was:<" + actual + ">";
    }
}

2、TestCase类

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package junit.framework;

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

public abstract class TestCase extends Assert implements Test {
    private String fName;

    public TestCase() {
        this.fName = null;
    }

    public TestCase(String name) {
        this.fName = name;
    }

    public int countTestCases() {
        return 1;
    }

    protected TestResult createResult() {
        return new TestResult();
    }

    public TestResult run() {
        TestResult result = this.createResult();
        this.run(result);
        return result;
    }

    public void run(TestResult result) {
        result.run(this);
    }

    public void runBare() throws Throwable {
        Throwable exception = null;
        this.setUp();

        try {
            this.runTest();
        } catch (Throwable var10) {
            exception = var10;
        } finally {
            try {
                this.tearDown();
            } catch (Throwable var11) {
                if (exception == null) {
                    exception = var11;
                }
            }

        }

        if (exception != null) {
            throw exception;
        }
    }

    protected void runTest() throws Throwable {
        assertNotNull("TestCase.fName cannot be null", this.fName);
        Method runMethod = null;

        try {
            runMethod = this.getClass().getMethod(this.fName, (Class[])null);
        } catch (NoSuchMethodException var5) {
            fail("Method \"" + this.fName + "\" not found");
        }

        if (!Modifier.isPublic(runMethod.getModifiers())) {
            fail("Method \"" + this.fName + "\" should be public");
        }

        try {
            runMethod.invoke(this);
        } catch (InvocationTargetException var3) {
            var3.fillInStackTrace();
            throw var3.getTargetException();
        } catch (IllegalAccessException var4) {
            var4.fillInStackTrace();
            throw var4;
        }
    }

    public static void assertTrue(String message, boolean condition) {
        Assert.assertTrue(message, condition);
    }

    public static void assertTrue(boolean condition) {
        Assert.assertTrue(condition);
    }

    public static void assertFalse(String message, boolean condition) {
        Assert.assertFalse(message, condition);
    }

    public static void assertFalse(boolean condition) {
        Assert.assertFalse(condition);
    }

    public static void fail(String message) {
        Assert.fail(message);
    }

    public static void fail() {
        Assert.fail();
    }

    public static void assertEquals(String message, Object expected, Object actual) {
        Assert.assertEquals(message, expected, actual);
    }

    public static void assertEquals(Object expected, Object actual) {
        Assert.assertEquals(expected, actual);
    }

    public static void assertEquals(String message, String expected, String actual) {
        Assert.assertEquals(message, expected, actual);
    }

    public static void assertEquals(String expected, String actual) {
        Assert.assertEquals(expected, actual);
    }

    public static void assertEquals(String message, double expected, double actual, double delta) {
        Assert.assertEquals(message, expected, actual, delta);
    }

    public static void assertEquals(double expected, double actual, double delta) {
        Assert.assertEquals(expected, actual, delta);
    }

    public static void assertEquals(String message, float expected, float actual, float delta) {
        Assert.assertEquals(message, expected, actual, delta);
    }

    public static void assertEquals(float expected, float actual, float delta) {
        Assert.assertEquals(expected, actual, delta);
    }

    public static void assertEquals(String message, long expected, long actual) {
        Assert.assertEquals(message, expected, actual);
    }

    public static void assertEquals(long expected, long actual) {
        Assert.assertEquals(expected, actual);
    }

    public static void assertEquals(String message, boolean expected, boolean actual) {
        Assert.assertEquals(message, expected, actual);
    }

    public static void assertEquals(boolean expected, boolean actual) {
        Assert.assertEquals(expected, actual);
    }

    public static void assertEquals(String message, byte expected, byte actual) {
        Assert.assertEquals(message, expected, actual);
    }

    public static void assertEquals(byte expected, byte actual) {
        Assert.assertEquals(expected, actual);
    }

    public static void assertEquals(String message, char expected, char actual) {
        Assert.assertEquals(message, expected, actual);
    }

    public static void assertEquals(char expected, char actual) {
        Assert.assertEquals(expected, actual);
    }

    public static void assertEquals(String message, short expected, short actual) {
        Assert.assertEquals(message, expected, actual);
    }

    public static void assertEquals(short expected, short actual) {
        Assert.assertEquals(expected, actual);
    }

    public static void assertEquals(String message, int expected, int actual) {
        Assert.assertEquals(message, expected, actual);
    }

    public static void assertEquals(int expected, int actual) {
        Assert.assertEquals(expected, actual);
    }

    public static void assertNotNull(Object object) {
        Assert.assertNotNull(object);
    }

    public static void assertNotNull(String message, Object object) {
        Assert.assertNotNull(message, object);
    }

    public static void assertNull(Object object) {
        Assert.assertNull(object);
    }

    public static void assertNull(String message, Object object) {
        Assert.assertNull(message, object);
    }

    public static void assertSame(String message, Object expected, Object actual) {
        Assert.assertSame(message, expected, actual);
    }

    public static void assertSame(Object expected, Object actual) {
        Assert.assertSame(expected, actual);
    }

    public static void assertNotSame(String message, Object expected, Object actual) {
        Assert.assertNotSame(message, expected, actual);
    }

    public static void assertNotSame(Object expected, Object actual) {
        Assert.assertNotSame(expected, actual);
    }

    public static void failSame(String message) {
        Assert.failSame(message);
    }

    public static void failNotSame(String message, Object expected, Object actual) {
        Assert.failNotSame(message, expected, actual);
    }

    public static void failNotEquals(String message, Object expected, Object actual) {
        Assert.failNotEquals(message, expected, actual);
    }

    public static String format(String message, Object expected, Object actual) {
        return Assert.format(message, expected, actual);
    }

    protected void setUp() throws Exception {
    }

    protected void tearDown() throws Exception {
    }

    public String toString() {
        return this.getName() + "(" + this.getClass().getName() + ")";
    }

    public String getName() {
        return this.fName;
    }

    public void setName(String name) {
        this.fName = name;
    }
}

3、TestResult类

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package junit.framework;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

public class TestResult {
    protected List<TestFailure> fFailures = new ArrayList();
    protected List<TestFailure> fErrors = new ArrayList();
    protected List<TestListener> fListeners = new ArrayList();
    protected int fRunTests = 0;
    private boolean fStop = false;

    public TestResult() {
    }

    public synchronized void addError(Test test, Throwable e) {
        this.fErrors.add(new TestFailure(test, e));
        Iterator i$ = this.cloneListeners().iterator();

        while(i$.hasNext()) {
            TestListener each = (TestListener)i$.next();
            each.addError(test, e);
        }

    }

    public synchronized void addFailure(Test test, AssertionFailedError e) {
        this.fFailures.add(new TestFailure(test, e));
        Iterator i$ = this.cloneListeners().iterator();

        while(i$.hasNext()) {
            TestListener each = (TestListener)i$.next();
            each.addFailure(test, e);
        }

    }

    public synchronized void addListener(TestListener listener) {
        this.fListeners.add(listener);
    }

    public synchronized void removeListener(TestListener listener) {
        this.fListeners.remove(listener);
    }

    private synchronized List<TestListener> cloneListeners() {
        List<TestListener> result = new ArrayList();
        result.addAll(this.fListeners);
        return result;
    }

    public void endTest(Test test) {
        Iterator i$ = this.cloneListeners().iterator();

        while(i$.hasNext()) {
            TestListener each = (TestListener)i$.next();
            each.endTest(test);
        }

    }

    public synchronized int errorCount() {
        return this.fErrors.size();
    }

    public synchronized Enumeration<TestFailure> errors() {
        return Collections.enumeration(this.fErrors);
    }

    public synchronized int failureCount() {
        return this.fFailures.size();
    }

    public synchronized Enumeration<TestFailure> failures() {
        return Collections.enumeration(this.fFailures);
    }

    protected void run(final TestCase test) {
        this.startTest(test);
        Protectable p = new Protectable() {
            public void protect() throws Throwable {
                test.runBare();
            }
        };
        this.runProtected(test, p);
        this.endTest(test);
    }

    public synchronized int runCount() {
        return this.fRunTests;
    }

    public void runProtected(Test test, Protectable p) {
        try {
            p.protect();
        } catch (AssertionFailedError var4) {
            this.addFailure(test, var4);
        } catch (ThreadDeath var5) {
            throw var5;
        } catch (Throwable var6) {
            this.addError(test, var6);
        }

    }

    public synchronized boolean shouldStop() {
        return this.fStop;
    }

    public void startTest(Test test) {
        int count = test.countTestCases();
        synchronized(this) {
            this.fRunTests += count;
        }

        Iterator i$ = this.cloneListeners().iterator();

        while(i$.hasNext()) {
            TestListener each = (TestListener)i$.next();
            each.startTest(test);
        }

    }

    public synchronized void stop() {
        this.fStop = true;
    }

    public synchronized boolean wasSuccessful() {
        return this.failureCount() == 0 && this.errorCount() == 0;
    }
}

4、TestSuite类

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package junit.framework;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.junit.internal.MethodSorter;
import org.junit.internal.Throwables;

public class TestSuite implements Test {
    private String fName;
    private Vector<Test> fTests;

    public static Test createTest(Class<?> theClass, String name) {
        Constructor constructor;
        try {
            constructor = getTestConstructor(theClass);
        } catch (NoSuchMethodException var8) {
            return warning("Class " + theClass.getName() + " has no public constructor TestCase(String name) or TestCase()");
        }

        Object test;
        try {
            if (constructor.getParameterTypes().length == 0) {
                test = constructor.newInstance();
                if (test instanceof TestCase) {
                    ((TestCase)test).setName(name);
                }
            } else {
                test = constructor.newInstance(name);
            }
        } catch (InstantiationException var5) {
            return warning("Cannot instantiate test case: " + name + " (" + Throwables.getStacktrace(var5) + ")");
        } catch (InvocationTargetException var6) {
            return warning("Exception in constructor: " + name + " (" + Throwables.getStacktrace(var6.getTargetException()) + ")");
        } catch (IllegalAccessException var7) {
            return warning("Cannot access test case: " + name + " (" + Throwables.getStacktrace(var7) + ")");
        }

        return (Test)test;
    }

    public static Constructor<?> getTestConstructor(Class<?> theClass) throws NoSuchMethodException {
        try {
            return theClass.getConstructor(String.class);
        } catch (NoSuchMethodException var2) {
            return theClass.getConstructor();
        }
    }

    public static Test warning(final String message) {
        return new TestCase("warning") {
            protected void runTest() {
                fail(message);
            }
        };
    }

    public TestSuite() {
        this.fTests = new Vector(10);
    }

    public TestSuite(Class<?> theClass) {
        this.fTests = new Vector(10);
        this.addTestsFromTestCase(theClass);
    }

    private void addTestsFromTestCase(Class<?> theClass) {
        this.fName = theClass.getName();

        try {
            getTestConstructor(theClass);
        } catch (NoSuchMethodException var8) {
            this.addTest(warning("Class " + theClass.getName() + " has no public constructor TestCase(String name) or TestCase()"));
            return;
        }

        if (!Modifier.isPublic(theClass.getModifiers())) {
            this.addTest(warning("Class " + theClass.getName() + " is not public"));
        } else {
            Class<?> superClass = theClass;

            for(ArrayList names = new ArrayList(); Test.class.isAssignableFrom(superClass); superClass = superClass.getSuperclass()) {
                Method[] arr$ = MethodSorter.getDeclaredMethods(superClass);
                int len$ = arr$.length;

                for(int i$ = 0; i$ < len$; ++i$) {
                    Method each = arr$[i$];
                    this.addTestMethod(each, names, theClass);
                }
            }

            if (this.fTests.size() == 0) {
                this.addTest(warning("No tests found in " + theClass.getName()));
            }

        }
    }

    public TestSuite(Class<? extends TestCase> theClass, String name) {
        this(theClass);
        this.setName(name);
    }

    public TestSuite(String name) {
        this.fTests = new Vector(10);
        this.setName(name);
    }

    public TestSuite(Class<?>... classes) {
        this.fTests = new Vector(10);
        Class[] arr$ = classes;
        int len$ = classes.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            Class<?> each = arr$[i$];
            this.addTest(this.testCaseForClass(each));
        }

    }

    private Test testCaseForClass(Class<?> each) {
        return (Test)(TestCase.class.isAssignableFrom(each) ? new TestSuite(each.asSubclass(TestCase.class)) : warning(each.getCanonicalName() + " does not extend TestCase"));
    }

    public TestSuite(Class<? extends TestCase>[] classes, String name) {
        this(classes);
        this.setName(name);
    }

    public void addTest(Test test) {
        this.fTests.add(test);
    }

    public void addTestSuite(Class<? extends TestCase> testClass) {
        this.addTest(new TestSuite(testClass));
    }

    public int countTestCases() {
        int count = 0;

        Test each;
        for(Iterator i$ = this.fTests.iterator(); i$.hasNext(); count += each.countTestCases()) {
            each = (Test)i$.next();
        }

        return count;
    }

    public String getName() {
        return this.fName;
    }

    public void run(TestResult result) {
        Iterator i$ = this.fTests.iterator();

        while(i$.hasNext()) {
            Test each = (Test)i$.next();
            if (result.shouldStop()) {
                break;
            }

            this.runTest(each, result);
        }

    }

    public void runTest(Test test, TestResult result) {
        test.run(result);
    }

    public void setName(String name) {
        this.fName = name;
    }

    public Test testAt(int index) {
        return (Test)this.fTests.get(index);
    }

    public int testCount() {
        return this.fTests.size();
    }

    public Enumeration<Test> tests() {
        return this.fTests.elements();
    }

    public String toString() {
        return this.getName() != null ? this.getName() : super.toString();
    }

    private void addTestMethod(Method m, List<String> names, Class<?> theClass) {
        String name = m.getName();
        if (!names.contains(name)) {
            if (!this.isPublicTestMethod(m)) {
                if (this.isTestMethod(m)) {
                    this.addTest(warning("Test method isn't public: " + m.getName() + "(" + theClass.getCanonicalName() + ")"));
                }

            } else {
                names.add(name);
                this.addTest(createTest(theClass, name));
            }
        }
    }

    private boolean isPublicTestMethod(Method m) {
        return this.isTestMethod(m) && Modifier.isPublic(m.getModifiers());
    }

    private boolean isTestMethod(Method m) {
        return m.getParameterTypes().length == 0 && m.getName().startsWith("test") && m.getReturnType().equals(Void.TYPE);
    }
}

5、package org.junit.runner.runner.notification.JUnitCore类

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.junit.runner;

import junit.framework.Test;
import junit.runner.Version;
import org.junit.internal.JUnitSystem;
import org.junit.internal.RealSystem;
import org.junit.internal.TextListener;
import org.junit.internal.runners.JUnit38ClassRunner;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;

public class JUnitCore {
    private final RunNotifier notifier = new RunNotifier();

    public JUnitCore() {
    }

    public static void main(String... args) {
        Result result = (new JUnitCore()).runMain(new RealSystem(), args);
        System.exit(result.wasSuccessful() ? 0 : 1);
    }

    public static Result runClasses(Class<?>... classes) {
        return runClasses(defaultComputer(), classes);
    }

    public static Result runClasses(Computer computer, Class<?>... classes) {
        return (new JUnitCore()).run(computer, classes);
    }

    Result runMain(JUnitSystem system, String... args) {
        system.out().println("JUnit version " + Version.id());
        JUnitCommandLineParseResult jUnitCommandLineParseResult = JUnitCommandLineParseResult.parse(args);
        RunListener listener = new TextListener(system);
        this.addListener(listener);
        return this.run(jUnitCommandLineParseResult.createRequest(defaultComputer()));
    }

    public String getVersion() {
        return Version.id();
    }

    public Result run(Class<?>... classes) {
        return this.run(defaultComputer(), classes);
    }

    public Result run(Computer computer, Class<?>... classes) {
        return this.run(Request.classes(computer, classes));
    }

    public Result run(Request request) {
        return this.run(request.getRunner());
    }

    public Result run(Test test) {
        return this.run((Runner)(new JUnit38ClassRunner(test)));
    }

    public Result run(Runner runner) {
        Result result = new Result();
        RunListener listener = result.createListener();
        this.notifier.addFirstListener(listener);

        try {
            this.notifier.fireTestRunStarted(runner.getDescription());
            runner.run(this.notifier);
            this.notifier.fireTestRunFinished(result);
        } finally {
            this.removeListener(listener);
        }

        return result;
    }

    public void addListener(RunListener listener) {
        this.notifier.addListener(listener);
    }

    public void removeListener(RunListener listener) {
        this.notifier.removeListener(listener);
    }

    static Computer defaultComputer() {
        return new Computer();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值