Home | History | Annotate | Download | only in cert
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 /**
     19 * @author Vera Y. Petrashkova
     20 * @version $Revision$
     21 */
     22 
     23 package tests.security.cert;
     24 
     25 import junit.framework.TestCase;
     26 
     27 import java.security.cert.CertificateParsingException;
     28 
     29 
     30 /**
     31  * Tests for <code>CertificateParsingException</code> class constructors and
     32  * methods.
     33  *
     34  */
     35 public class CertificateParsingExceptionTest extends TestCase {
     36 
     37     private static String[] msgs = {
     38             "",
     39             "Check new message",
     40             "Check new message Check new message Check new message Check new message Check new message" };
     41 
     42     private static Throwable tCause = new Throwable("Throwable for exception");
     43 
     44     /**
     45      * Test for <code>CertificateParsingException()</code> constructor
     46      * Assertion: constructs CertificateParsingException with no detail message
     47      */
     48     public void testCertificateParsingException01() {
     49         CertificateParsingException tE = new CertificateParsingException();
     50         assertNull("getMessage() must return null.", tE.getMessage());
     51         assertNull("getCause() must return null", tE.getCause());
     52     }
     53 
     54     /**
     55      * Test for <code>CertificateParsingException(String)</code> constructor
     56      * Assertion: constructs CertificateParsingException with detail message
     57      * msg. Parameter <code>msg</code> is not null.
     58      */
     59     public void testCertificateParsingException02() {
     60         CertificateParsingException tE;
     61         for (int i = 0; i < msgs.length; i++) {
     62             tE = new CertificateParsingException(msgs[i]);
     63             assertEquals("getMessage() must return: ".concat(msgs[i]), tE
     64                     .getMessage(), msgs[i]);
     65             assertNull("getCause() must return null", tE.getCause());
     66         }
     67     }
     68 
     69     /**
     70      * Test for <code>CertificateParsingException(String)</code> constructor
     71      * Assertion: constructs CertificateParsingException when <code>msg</code>
     72      * is null
     73      */
     74     public void testCertificateParsingException03() {
     75         String msg = null;
     76         CertificateParsingException tE = new CertificateParsingException(msg);
     77         assertNull("getMessage() must return null.", tE.getMessage());
     78         assertNull("getCause() must return null", tE.getCause());
     79     }
     80 
     81     /**
     82      * Test for <code>CertificateParsingException(Throwable)</code>
     83      * constructor Assertion: constructs CertificateParsingException when
     84      * <code>cause</code> is null
     85      */
     86     public void testCertificateParsingException04() {
     87         Throwable cause = null;
     88         CertificateParsingException tE = new CertificateParsingException(cause);
     89         assertNull("getMessage() must return null.", tE.getMessage());
     90         assertNull("getCause() must return null", tE.getCause());
     91     }
     92 
     93     /**
     94      * Test for <code>CertificateParsingException(Throwable)</code>
     95      * constructor Assertion: constructs CertificateParsingException when
     96      * <code>cause</code> is not null
     97      */
     98     public void testCertificateParsingException05() {
     99         CertificateParsingException tE = new CertificateParsingException(tCause);
    100         if (tE.getMessage() != null) {
    101             String toS = tCause.toString();
    102             String getM = tE.getMessage();
    103             assertTrue("getMessage() should contain ".concat(toS), (getM
    104                     .indexOf(toS) != -1));
    105         }
    106         assertNotNull("getCause() must not return null", tE.getCause());
    107         assertEquals("getCause() must return ".concat(tCause.toString()), tE
    108                 .getCause(), tCause);
    109     }
    110 
    111     /**
    112      * Test for <code>CertificateParsingException(String, Throwable)</code>
    113      * constructor Assertion: constructs CertificateParsingException when
    114      * <code>cause</code> is null <code>msg</code> is null
    115      */
    116     public void testCertificateParsingException06() {
    117         CertificateParsingException tE = new CertificateParsingException(null,
    118                 null);
    119         assertNull("getMessage() must return null", tE.getMessage());
    120         assertNull("getCause() must return null", tE.getCause());
    121     }
    122 
    123     /**
    124      * Test for <code>CertificateParsingException(String, Throwable)</code>
    125      * constructor Assertion: constructs CertificateParsingException when
    126      * <code>cause</code> is null <code>msg</code> is not null
    127      */
    128     public void testCertificateParsingException07() {
    129         CertificateParsingException tE;
    130         for (int i = 0; i < msgs.length; i++) {
    131             tE = new CertificateParsingException(msgs[i], null);
    132             assertEquals("getMessage() must return: ".concat(msgs[i]), tE
    133                     .getMessage(), msgs[i]);
    134             assertNull("getCause() must return null", tE.getCause());
    135         }
    136     }
    137 
    138     /**
    139      * Test for <code>CertificateParsingException(String, Throwable)</code>
    140      * constructor Assertion: constructs CertificateParsingException when
    141      * <code>cause</code> is not null <code>msg</code> is null
    142      */
    143     public void testCertificateParsingException08() {
    144         CertificateParsingException tE = new CertificateParsingException(null,
    145                 tCause);
    146         if (tE.getMessage() != null) {
    147             String toS = tCause.toString();
    148             String getM = tE.getMessage();
    149             assertTrue("getMessage() must should ".concat(toS), (getM
    150                     .indexOf(toS) != -1));
    151         }
    152         assertNotNull("getCause() must not return null", tE.getCause());
    153         assertEquals("getCause() must return ".concat(tCause.toString()), tE
    154                 .getCause(), tCause);
    155     }
    156 
    157     /**
    158      * Test for <code>CertificateParsingException(String, Throwable)</code>
    159      * constructor Assertion: constructs CertificateParsingException when
    160      * <code>cause</code> is not null <code>msg</code> is not null
    161      */
    162     public void testCertificateParsingException09() {
    163         CertificateParsingException tE;
    164         for (int i = 0; i < msgs.length; i++) {
    165             tE = new CertificateParsingException(msgs[i], tCause);
    166             String getM = tE.getMessage();
    167             String toS = tCause.toString();
    168             if (msgs[i].length() > 0) {
    169                 assertTrue("getMessage() must contain ".concat(msgs[i]), getM
    170                         .indexOf(msgs[i]) != -1);
    171                 if (!getM.equals(msgs[i])) {
    172                     assertTrue("getMessage() should contain ".concat(toS), getM
    173                             .indexOf(toS) != -1);
    174                 }
    175             }
    176             assertNotNull("getCause() must not return null", tE.getCause());
    177             assertEquals("getCause() must return ".concat(tCause.toString()),
    178                     tE.getCause(), tCause);
    179         }
    180     }
    181 }
    182