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.CertificateException;
     28 
     29 
     30 /**
     31  * Tests for <code>CertificateException</code> class constructors and methods.
     32  *
     33  */
     34 public class CertificateExceptionTest extends TestCase {
     35 
     36     private static String[] msgs = {
     37             "",
     38             "Check new message",
     39             "Check new message Check new message Check new message Check new message Check new message" };
     40 
     41     private static Throwable tCause = new Throwable("Throwable for exception");
     42 
     43     /**
     44      * Test for <code>CertificateException()</code> constructor Assertion:
     45      * constructs CertificateException with no detail message
     46      */
     47     public void testCertificateException01() {
     48         CertificateException tE = new CertificateException();
     49         assertNull("getMessage() must return null.", tE.getMessage());
     50         assertNull("getCause() must return null", tE.getCause());
     51     }
     52 
     53     /**
     54      * Test for <code>CertificateException(String)</code> constructor
     55      * Assertion: constructs CertificateException with detail message msg.
     56      * Parameter <code>msg</code> is not null.
     57      */
     58     public void testCertificateException02() {
     59         CertificateException tE;
     60         for (int i = 0; i < msgs.length; i++) {
     61             tE = new CertificateException(msgs[i]);
     62             assertEquals("getMessage() must return: ".concat(msgs[i]), tE
     63                     .getMessage(), msgs[i]);
     64             assertNull("getCause() must return null", tE.getCause());
     65         }
     66     }
     67 
     68     /**
     69      * Test for <code>CertificateException(String)</code> constructor
     70      * Assertion: constructs CertificateException when <code>msg</code> is
     71      * null
     72      */
     73     public void testCertificateException03() {
     74         String msg = null;
     75         CertificateException tE = new CertificateException(msg);
     76         assertNull("getMessage() must return null.", tE.getMessage());
     77         assertNull("getCause() must return null", tE.getCause());
     78     }
     79 
     80     /**
     81      * Test for <code>CertificateException(Throwable)</code> constructor
     82      * Assertion: constructs CertificateException when <code>cause</code> is
     83      * null
     84      */
     85     public void testCertificateException04() {
     86         Throwable cause = null;
     87         CertificateException tE = new CertificateException(cause);
     88         assertNull("getMessage() must return null.", tE.getMessage());
     89         assertNull("getCause() must return null", tE.getCause());
     90     }
     91 
     92     /**
     93      * Test for <code>CertificateException(Throwable)</code> constructor
     94      * Assertion: constructs CertificateException when <code>cause</code> is
     95      * not null
     96      */
     97     public void testCertificateException05() {
     98         CertificateException tE = new CertificateException(tCause);
     99         if (tE.getMessage() != null) {
    100             String toS = tCause.toString();
    101             String getM = tE.getMessage();
    102             assertTrue("getMessage() should contain ".concat(toS), (getM
    103                     .indexOf(toS) != -1));
    104         }
    105         assertNotNull("getCause() must not return null", tE.getCause());
    106         assertEquals("getCause() must return ".concat(tCause.toString()), tE
    107                 .getCause(), tCause);
    108     }
    109 
    110     /**
    111      * Test for <code>CertificateException(String, Throwable)</code>
    112      * constructor Assertion: constructs CertificateException when
    113      * <code>cause</code> is null <code>msg</code> is null
    114      */
    115     public void testCertificateException06() {
    116         CertificateException tE = new CertificateException(null, null);
    117         assertNull("getMessage() must return null", tE.getMessage());
    118         assertNull("getCause() must return null", tE.getCause());
    119     }
    120 
    121     /**
    122      * Test for <code>CertificateException(String, Throwable)</code>
    123      * constructor Assertion: constructs CertificateException when
    124      * <code>cause</code> is null <code>msg</code> is not null
    125      */
    126     public void testCertificateException07() {
    127         CertificateException tE;
    128         for (int i = 0; i < msgs.length; i++) {
    129             tE = new CertificateException(msgs[i], null);
    130             assertEquals("getMessage() must return: ".concat(msgs[i]), tE
    131                     .getMessage(), msgs[i]);
    132             assertNull("getCause() must return null", tE.getCause());
    133         }
    134     }
    135 
    136     /**
    137      * Test for <code>CertificateException(String, Throwable)</code>
    138      * constructor Assertion: constructs CertificateException when
    139      * <code>cause</code> is not null <code>msg</code> is null
    140      */
    141     public void testCertificateException08() {
    142         CertificateException tE = new CertificateException(null, tCause);
    143         if (tE.getMessage() != null) {
    144             String toS = tCause.toString();
    145             String getM = tE.getMessage();
    146             assertTrue("getMessage() must should ".concat(toS), (getM
    147                     .indexOf(toS) != -1));
    148         }
    149         assertNotNull("getCause() must not return null", tE.getCause());
    150         assertEquals("getCause() must return ".concat(tCause.toString()), tE
    151                 .getCause(), tCause);
    152     }
    153 
    154     /**
    155      * Test for <code>CertificateException(String, Throwable)</code>
    156      * constructor Assertion: constructs CertificateException when
    157      * <code>cause</code> is not null <code>msg</code> is not null
    158      */
    159     public void testCertificateException09() {
    160         CertificateException tE;
    161         for (int i = 0; i < msgs.length; i++) {
    162             tE = new CertificateException(msgs[i], tCause);
    163             String getM = tE.getMessage();
    164             String toS = tCause.toString();
    165             if (msgs[i].length() > 0) {
    166                 assertTrue("getMessage() must contain ".concat(msgs[i]), getM
    167                         .indexOf(msgs[i]) != -1);
    168                 if (!getM.equals(msgs[i])) {
    169                     assertTrue("getMessage() should contain ".concat(toS), getM
    170                             .indexOf(toS) != -1);
    171                 }
    172             }
    173             assertNotNull("getCause() must not return null", tE.getCause());
    174             assertEquals("getCause() must return ".concat(tCause.toString()),
    175                     tE.getCause(), tCause);
    176         }
    177     }
    178 }
    179