// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.util.test;

import java.util.Enumeration;
import java.util.Vector;
import java.io.PrintStream;
import org.bouncycastle.util.Arrays;

public abstract class SimpleTest implements Test
{
    @Override
    public abstract String getName();
    
    private TestResult success() {
        return SimpleTestResult.successful(this, "Okay");
    }
    
    public void fail(final String s) {
        throw new TestFailedException(SimpleTestResult.failed(this, s));
    }
    
    protected void isTrue(final boolean b) {
        if (!b) {
            throw new TestFailedException(SimpleTestResult.failed(this, "no message"));
        }
    }
    
    public void isTrue(final String s, final boolean b) {
        if (!b) {
            throw new TestFailedException(SimpleTestResult.failed(this, s));
        }
    }
    
    protected void isEquals(final Object o, final Object obj) {
        if (!o.equals(obj)) {
            throw new TestFailedException(SimpleTestResult.failed(this, "no message"));
        }
    }
    
    protected void isEquals(final int n, final int n2) {
        if (n != n2) {
            throw new TestFailedException(SimpleTestResult.failed(this, "no message"));
        }
    }
    
    protected void isEquals(final long n, final long n2) {
        if (n != n2) {
            throw new TestFailedException(SimpleTestResult.failed(this, "no message"));
        }
    }
    
    protected void isEquals(final boolean b, final boolean b2) {
        if (b != b2) {
            throw new TestFailedException(SimpleTestResult.failed(this, "no message"));
        }
    }
    
    protected void isEquals(final String s, final boolean b, final boolean b2) {
        if (b != b2) {
            throw new TestFailedException(SimpleTestResult.failed(this, s));
        }
    }
    
    protected void isEquals(final String s, final long n, final long n2) {
        if (n != n2) {
            throw new TestFailedException(SimpleTestResult.failed(this, s));
        }
    }
    
    protected void isEquals(final String s, final Object o, final Object obj) {
        if (o == null && obj == null) {
            return;
        }
        if (o == null) {
            throw new TestFailedException(SimpleTestResult.failed(this, s));
        }
        if (obj == null) {
            throw new TestFailedException(SimpleTestResult.failed(this, s));
        }
        if (!o.equals(obj)) {
            throw new TestFailedException(SimpleTestResult.failed(this, s));
        }
    }
    
    protected boolean areEqual(final byte[][] array, final byte[][] array2) {
        if (array == null && array2 == null) {
            return true;
        }
        if (array == null || array2 == null) {
            return false;
        }
        if (array.length != array2.length) {
            return false;
        }
        for (int i = 0; i < array.length; ++i) {
            if (!this.areEqual(array[i], array2[i])) {
                return false;
            }
        }
        return true;
    }
    
    protected void fail(final String s, final Throwable t) {
        throw new TestFailedException(SimpleTestResult.failed(this, s, t));
    }
    
    public void fail(final String s, final Object o, final Object o2) {
        throw new TestFailedException(SimpleTestResult.failed(this, s, o, o2));
    }
    
    protected boolean areEqual(final byte[] array, final byte[] array2) {
        return Arrays.areEqual(array, array2);
    }
    
    public boolean areEqual(final byte[] array, final int n, final int n2, final byte[] array2, final int n3, final int n4) {
        return Arrays.areEqual(array, n, n2, array2, n3, n4);
    }
    
    @Override
    public TestResult perform() {
        try {
            this.performTest();
            return this.success();
        }
        catch (final TestFailedException ex) {
            return ex.getResult();
        }
        catch (final Exception obj) {
            return SimpleTestResult.failed(this, "Exception: " + obj, obj);
        }
    }
    
    public abstract void performTest() throws Exception;
    
    public static void runTest(final Test test) {
        runTest(test, System.out);
    }
    
    public static void runTest(final Test test, final PrintStream s) {
        final TestResult perform = test.perform();
        if (perform.getException() != null) {
            perform.getException().printStackTrace(s);
        }
        s.println(perform);
    }
    
    public static void runTests(final Test[] array) {
        runTests(array, System.out);
    }
    
    public static void runTests(final Test[] array, final PrintStream s) {
        final Vector vector = new Vector();
        for (int i = 0; i != array.length; ++i) {
            final TestResult perform = array[i].perform();
            if (!perform.isSuccessful()) {
                vector.addElement(perform);
            }
            if (perform.getException() != null) {
                perform.getException().printStackTrace(s);
            }
            s.println(perform);
        }
        s.println("-----");
        if (vector.isEmpty()) {
            s.println("All tests successful.");
        }
        else {
            s.println("Completed with " + vector.size() + " FAILURES:");
            final Enumeration elements = vector.elements();
            while (elements.hasMoreElements()) {
                System.out.println("=>  " + elements.nextElement());
            }
        }
    }
    
    public Exception testException(final String str, final String str2, final TestExceptionOperation testExceptionOperation) {
        try {
            testExceptionOperation.operation();
            this.fail(str);
        }
        catch (final Exception ex) {
            if (str != null) {
                this.isTrue(ex.getMessage(), ex.getMessage().indexOf(str) >= 0);
            }
            this.isTrue(ex.getMessage(), ex.getClass().getName().indexOf(str2) >= 0);
            return ex;
        }
        return null;
    }
    
    protected interface TestExceptionOperation
    {
        void operation() throws Exception;
    }
}
