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 dalvik.annotation.TestTargets;
     26 import dalvik.annotation.TestLevel;
     27 import dalvik.annotation.TestTargetNew;
     28 import dalvik.annotation.TestTargetClass;
     29 
     30 import junit.framework.TestCase;
     31 
     32 import java.security.cert.CertificateException;
     33 
     34 
     35 /**
     36  * Tests for <code>CertificateException</code> class constructors and methods.
     37  *
     38  */
     39 @TestTargetClass(CertificateException.class)
     40 public class CertificateExceptionTest extends TestCase {
     41 
     42     private static String[] msgs = {
     43             "",
     44             "Check new message",
     45             "Check new message Check new message Check new message Check new message Check new message" };
     46 
     47     private static Throwable tCause = new Throwable("Throwable for exception");
     48 
     49     /**
     50      * Test for <code>CertificateException()</code> constructor Assertion:
     51      * constructs CertificateException with no detail message
     52      */
     53     @TestTargetNew(
     54         level = TestLevel.COMPLETE,
     55         notes = "",
     56         method = "CertificateException",
     57         args = {}
     58     )
     59     public void testCertificateException01() {
     60         CertificateException tE = new CertificateException();
     61         assertNull("getMessage() must return null.", tE.getMessage());
     62         assertNull("getCause() must return null", tE.getCause());
     63     }
     64 
     65     /**
     66      * Test for <code>CertificateException(String)</code> constructor
     67      * Assertion: constructs CertificateException with detail message msg.
     68      * Parameter <code>msg</code> is not null.
     69      */
     70     @TestTargetNew(
     71         level = TestLevel.PARTIAL_COMPLETE,
     72         notes = "",
     73         method = "CertificateException",
     74         args = {java.lang.String.class}
     75     )
     76     public void testCertificateException02() {
     77         CertificateException tE;
     78         for (int i = 0; i < msgs.length; i++) {
     79             tE = new CertificateException(msgs[i]);
     80             assertEquals("getMessage() must return: ".concat(msgs[i]), tE
     81                     .getMessage(), msgs[i]);
     82             assertNull("getCause() must return null", tE.getCause());
     83         }
     84     }
     85 
     86     /**
     87      * Test for <code>CertificateException(String)</code> constructor
     88      * Assertion: constructs CertificateException when <code>msg</code> is
     89      * null
     90      */
     91     @TestTargetNew(
     92         level = TestLevel.PARTIAL_COMPLETE,
     93         notes = "Verifies null as a parameter.",
     94         method = "CertificateException",
     95         args = {java.lang.String.class}
     96     )
     97     public void testCertificateException03() {
     98         String msg = null;
     99         CertificateException tE = new CertificateException(msg);
    100         assertNull("getMessage() must return null.", tE.getMessage());
    101         assertNull("getCause() must return null", tE.getCause());
    102     }
    103 
    104     /**
    105      * Test for <code>CertificateException(Throwable)</code> constructor
    106      * Assertion: constructs CertificateException when <code>cause</code> is
    107      * null
    108      */
    109     @TestTargetNew(
    110         level = TestLevel.PARTIAL_COMPLETE,
    111         notes = "Verifies null as a parameter.",
    112         method = "CertificateException",
    113         args = {java.lang.Throwable.class}
    114     )
    115     public void testCertificateException04() {
    116         Throwable cause = null;
    117         CertificateException tE = new CertificateException(cause);
    118         assertNull("getMessage() must return null.", tE.getMessage());
    119         assertNull("getCause() must return null", tE.getCause());
    120     }
    121 
    122     /**
    123      * Test for <code>CertificateException(Throwable)</code> constructor
    124      * Assertion: constructs CertificateException when <code>cause</code> is
    125      * not null
    126      */
    127     @TestTargetNew(
    128         level = TestLevel.PARTIAL_COMPLETE,
    129         notes = "",
    130         method = "CertificateException",
    131         args = {java.lang.Throwable.class}
    132     )
    133     public void testCertificateException05() {
    134         CertificateException tE = new CertificateException(tCause);
    135         if (tE.getMessage() != null) {
    136             String toS = tCause.toString();
    137             String getM = tE.getMessage();
    138             assertTrue("getMessage() should contain ".concat(toS), (getM
    139                     .indexOf(toS) != -1));
    140         }
    141         assertNotNull("getCause() must not return null", tE.getCause());
    142         assertEquals("getCause() must return ".concat(tCause.toString()), tE
    143                 .getCause(), tCause);
    144     }
    145 
    146     /**
    147      * Test for <code>CertificateException(String, Throwable)</code>
    148      * constructor Assertion: constructs CertificateException when
    149      * <code>cause</code> is null <code>msg</code> is null
    150      */
    151     @TestTargetNew(
    152         level = TestLevel.PARTIAL_COMPLETE,
    153         notes = "Verifies null as parameters.",
    154         method = "CertificateException",
    155         args = {java.lang.String.class, java.lang.Throwable.class}
    156     )
    157     public void testCertificateException06() {
    158         CertificateException tE = new CertificateException(null, null);
    159         assertNull("getMessage() must return null", tE.getMessage());
    160         assertNull("getCause() must return null", tE.getCause());
    161     }
    162 
    163     /**
    164      * Test for <code>CertificateException(String, Throwable)</code>
    165      * constructor Assertion: constructs CertificateException when
    166      * <code>cause</code> is null <code>msg</code> is not null
    167      */
    168     @TestTargetNew(
    169         level = TestLevel.PARTIAL_COMPLETE,
    170         notes = "Verifies null as Throwable parameter.",
    171         method = "CertificateException",
    172         args = {java.lang.String.class, java.lang.Throwable.class}
    173     )
    174     public void testCertificateException07() {
    175         CertificateException tE;
    176         for (int i = 0; i < msgs.length; i++) {
    177             tE = new CertificateException(msgs[i], null);
    178             assertEquals("getMessage() must return: ".concat(msgs[i]), tE
    179                     .getMessage(), msgs[i]);
    180             assertNull("getCause() must return null", tE.getCause());
    181         }
    182     }
    183 
    184     /**
    185      * Test for <code>CertificateException(String, Throwable)</code>
    186      * constructor Assertion: constructs CertificateException when
    187      * <code>cause</code> is not null <code>msg</code> is null
    188      */
    189     @TestTargetNew(
    190         level = TestLevel.PARTIAL_COMPLETE,
    191         notes = "Verifies as String parameter.",
    192         method = "CertificateException",
    193         args = {java.lang.String.class, java.lang.Throwable.class}
    194     )
    195     public void testCertificateException08() {
    196         CertificateException tE = new CertificateException(null, tCause);
    197         if (tE.getMessage() != null) {
    198             String toS = tCause.toString();
    199             String getM = tE.getMessage();
    200             assertTrue("getMessage() must should ".concat(toS), (getM
    201                     .indexOf(toS) != -1));
    202         }
    203         assertNotNull("getCause() must not return null", tE.getCause());
    204         assertEquals("getCause() must return ".concat(tCause.toString()), tE
    205                 .getCause(), tCause);
    206     }
    207 
    208     /**
    209      * Test for <code>CertificateException(String, Throwable)</code>
    210      * constructor Assertion: constructs CertificateException when
    211      * <code>cause</code> is not null <code>msg</code> is not null
    212      */
    213     @TestTargetNew(
    214         level = TestLevel.PARTIAL_COMPLETE,
    215         notes = "",
    216         method = "CertificateException",
    217         args = {java.lang.String.class, java.lang.Throwable.class}
    218     )
    219     public void testCertificateException09() {
    220         CertificateException tE;
    221         for (int i = 0; i < msgs.length; i++) {
    222             tE = new CertificateException(msgs[i], tCause);
    223             String getM = tE.getMessage();
    224             String toS = tCause.toString();
    225             if (msgs[i].length() > 0) {
    226                 assertTrue("getMessage() must contain ".concat(msgs[i]), getM
    227                         .indexOf(msgs[i]) != -1);
    228                 if (!getM.equals(msgs[i])) {
    229                     assertTrue("getMessage() should contain ".concat(toS), getM
    230                             .indexOf(toS) != -1);
    231                 }
    232             }
    233             assertNotNull("getCause() must not return null", tE.getCause());
    234             assertEquals("getCause() must return ".concat(tCause.toString()),
    235                     tE.getCause(), tCause);
    236         }
    237     }
    238 }
    239