FileDocCategorySizeDatePackage
CertPathBuilder2Test.javaAPI DocAndroid 1.5 API10838Wed May 06 22:41:06 BST 2009tests.security.cert

CertPathBuilder2Test

public class CertPathBuilder2Test extends TestCase
Tests for CertPathBuilder class constructors and methods

Fields Summary
private static final String
defaultAlg
private static final String
CertPathBuilderProviderClass
private static final String[]
invalidValues
private static final String[]
validValues
Provider
mProv
Constructors Summary
Methods Summary
private voidcheckResult(java.security.cert.CertPathBuilder certBuild)

        String dt = CertPathBuilder.getDefaultType();
        String propName = CertPathBuilder1Test.DEFAULT_TYPE_PROPERTY;
        String dtN;
        for (int i = 0; i <invalidValues.length; i++) {
            Security.setProperty(propName, invalidValues[i]);
            dtN = CertPathBuilder.getDefaultType();
            if (!dtN.equals(invalidValues[i]) && !dtN.equals(dt)) {
                fail("Incorrect default type: ".concat(dtN));
            }
        }
        Security.setProperty(propName, dt);
        assertEquals("Incorrect default type", CertPathBuilder.getDefaultType(),
                dt);
        try {
            certBuild.build(null);
            fail("CertPathBuilderException must be thrown");
        } catch (CertPathBuilderException e) {
        }    
        CertPathBuilderResult cpbResult = certBuild.build(null);
        assertNull("Not null CertPathBuilderResult", cpbResult);
    
public static voidmain(java.lang.String[] args)

        junit.textui.TestRunner.run(CertPathBuilder2Test.class);
    
protected voidsetUp()

        super.setUp();
        mProv = (new SpiEngUtils()).new MyProvider("MyCertPathBuilderProvider",
                "Provider for testing", CertPathBuilder1Test.srvCertPathBuilder
                        + "." + defaultAlg,

                CertPathBuilderProviderClass);
        Security.insertProviderAt(mProv, 1);
    
protected voidtearDown()

        super.tearDown();
        Security.removeProvider(mProv.getName());
    
public voidtestGetInstance01()
Test for getInstance(String algorithm) method Assertions: throws throws NullPointerException when algorithm is null throws NoSuchAlgorithmException when algorithm is not correct returns CertPathBuilder object

        try {
            CertPathBuilder.getInstance(null);
            fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null");
        } catch (NullPointerException e) {
        } catch (NoSuchAlgorithmException e) {
        }
        for (int i = 0; i < invalidValues.length; i++) {
            try {
                CertPathBuilder.getInstance(invalidValues[i]);
                fail("NoSuchAlgorithmException must be thrown (type: ".concat(
                        invalidValues[i]).concat(")"));
            } catch (NoSuchAlgorithmException e) {
            }
        }
        CertPathBuilder cerPB;
        for (int i = 0; i < validValues.length; i++) {
            cerPB = CertPathBuilder.getInstance(validValues[i]);
            assertEquals("Incorrect type", cerPB.getAlgorithm(), validValues[i]);
            assertEquals("Incorrect provider", cerPB.getProvider(), mProv);
            checkResult(cerPB);
        }
    
public voidtestGetInstance02()
Test for getInstance(String algorithm, String provider) method Assertions: throws NullPointerException when algorithm is null throws NoSuchAlgorithmException when algorithm is not correct throws IllegalArgumentException when provider is null or empty; throws NoSuchProviderException when provider is available; returns CertPathBuilder object

        try {
            CertPathBuilder.getInstance(null, mProv.getName());
            fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null");
        } catch (NullPointerException e) {
        } catch (NoSuchAlgorithmException e) {
        }
        for (int i = 0; i < invalidValues.length; i++) {
            try {
                CertPathBuilder.getInstance(invalidValues[i], mProv
                        .getName());
                fail("NoSuchAlgorithmException must be thrown (type: ".concat(
                        invalidValues[i]).concat(")"));
            } catch (NoSuchAlgorithmException e) {
            }
        }
        String prov = null;
        for (int i = 0; i < validValues.length; i++) {
            try {
                CertPathBuilder.getInstance(validValues[i], prov);
                fail("IllegalArgumentException must be thrown when provider is null (type: "
                        .concat(validValues[i]).concat(")"));
            } catch (IllegalArgumentException e) {
            }
            try {
                CertPathBuilder.getInstance(validValues[i], "");
                fail("IllegalArgumentException must be thrown when provider is empty (type: "
                        .concat(validValues[i]).concat(")"));
            } catch (IllegalArgumentException e) {
            }
        }
        for (int i = 0; i < validValues.length; i++) {
            for (int j = 1; j < invalidValues.length; j++) {
                try {
                    CertPathBuilder.getInstance(validValues[i],
                            invalidValues[j]);
                    fail("NoSuchProviderException must be thrown (type: "
                            .concat(validValues[i]).concat(" provider: ")
                            .concat(invalidValues[j]).concat(")"));
                } catch (NoSuchProviderException e) {
                }
            }
        }
        CertPathBuilder cerPB;
        for (int i = 0; i < validValues.length; i++) {
            cerPB = CertPathBuilder.getInstance(validValues[i], mProv
                    .getName());
            assertEquals("Incorrect type", cerPB.getAlgorithm(), validValues[i]);
            assertEquals("Incorrect provider", cerPB.getProvider().getName(),
                    mProv.getName());
            checkResult(cerPB);
        }
    
public voidtestGetInstance03()
Test for getInstance(String algorithm, Provider provider) method Assertions: throws NullPointerException when algorithm is null throws NoSuchAlgorithmException when algorithm is not correct returns CertPathBuilder object

        try {
            CertPathBuilder.getInstance(null, mProv);
            fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null");
        } catch (NullPointerException e) {
        } catch (NoSuchAlgorithmException e) {
        }
        for (int i = 0; i < invalidValues.length; i++) {
            try {
                CertPathBuilder.getInstance(invalidValues[i], mProv);
                fail("NoSuchAlgorithmException must be thrown (type: ".concat(
                        invalidValues[i]).concat(")"));
            } catch (NoSuchAlgorithmException e) {
            }
        }
        Provider prov = null;
        for (int i = 0; i < validValues.length; i++) {
            try {
                CertPathBuilder.getInstance(validValues[i], prov);
                fail("IllegalArgumentException must be thrown when provider is null (type: "
                        .concat(validValues[i]).concat(")"));
            } catch (IllegalArgumentException e) {
            }
        }
        CertPathBuilder cerPB;
        for (int i = 0; i < validValues.length; i++) {
            cerPB = CertPathBuilder.getInstance(validValues[i], mProv);
            assertEquals("Incorrect type", cerPB.getAlgorithm(), validValues[i]);
            assertEquals("Incorrect provider", cerPB.getProvider(), mProv);
            checkResult(cerPB);
        }