Home | History | Annotate | Download | only in security
      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 org.apache.harmony.security.tests.java.security;
     24 
     25 import java.security.KeyManagementException;
     26 
     27 import junit.framework.TestCase;
     28 
     29 /**
     30  * Tests for <code>KeyManagementException</code> class constructors and
     31  * methods.
     32  *
     33  */
     34 public class KeyManagementExceptionTest 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>KeyManagementException()</code> constructor Assertion:
     45      * constructs KeyManagementException with no detail message
     46      */
     47     public void testKeyManagementException01() {
     48         KeyManagementException tE = new KeyManagementException();
     49         assertNull("getMessage() must return null.", tE.getMessage());
     50         assertNull("getCause() must return null", tE.getCause());
     51     }
     52 
     53     /**
     54      * Test for <code>KeyManagementException(String)</code> constructor
     55      * Assertion: constructs KeyManagementException with detail message msg.
     56      * Parameter <code>msg</code> is not null.
     57      */
     58     public void testKeyManagementException02() {
     59         KeyManagementException tE;
     60         for (int i = 0; i < msgs.length; i++) {
     61             tE = new KeyManagementException(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>KeyManagementException(String)</code> constructor
     70      * Assertion: constructs KeyManagementException when <code>msg</code> is
     71      * null
     72      */
     73     public void testKeyManagementException03() {
     74         String msg = null;
     75         KeyManagementException tE = new KeyManagementException(msg);
     76         assertNull("getMessage() must return null.", tE.getMessage());
     77         assertNull("getCause() must return null", tE.getCause());
     78     }
     79 
     80     /**
     81      * Test for <code>KeyManagementException(Throwable)</code> constructor
     82      * Assertion: constructs KeyManagementException when <code>cause</code> is
     83      * null
     84      */
     85     public void testKeyManagementException04() {
     86         Throwable cause = null;
     87         KeyManagementException tE = new KeyManagementException(cause);
     88         assertNull("getMessage() must return null.", tE.getMessage());
     89         assertNull("getCause() must return null", tE.getCause());
     90     }
     91 
     92     /**
     93      * Test for <code>KeyManagementException(Throwable)</code> constructor
     94      * Assertion: constructs KeyManagementException when <code>cause</code> is
     95      * not null
     96      */
     97     public void testKeyManagementException05() {
     98         KeyManagementException tE = new KeyManagementException(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>KeyManagementException(String, Throwable)</code>
    112      * constructor Assertion: constructs KeyManagementException when
    113      * <code>cause</code> is null <code>msg</code> is null
    114      */
    115     public void testKeyManagementException06() {
    116         KeyManagementException tE = new KeyManagementException(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>KeyManagementException(String, Throwable)</code>
    123      * constructor Assertion: constructs KeyManagementException when
    124      * <code>cause</code> is null <code>msg</code> is not null
    125      */
    126     public void testKeyManagementException07() {
    127         KeyManagementException tE;
    128         for (int i = 0; i < msgs.length; i++) {
    129             tE = new KeyManagementException(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>KeyManagementException(String, Throwable)</code>
    138      * constructor Assertion: constructs KeyManagementException when
    139      * <code>cause</code> is not null <code>msg</code> is null
    140      */
    141     public void testKeyManagementException08() {
    142         KeyManagementException tE = new KeyManagementException(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>KeyManagementException(String, Throwable)</code>
    156      * constructor Assertion: constructs KeyManagementException when
    157      * <code>cause</code> is not null <code>msg</code> is not null
    158      */
    159     public void testKeyManagementException09() {
    160         KeyManagementException tE;
    161         for (int i = 0; i < msgs.length; i++) {
    162             tE = new KeyManagementException(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