Home | History | Annotate | Download | only in ssl
      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 package org.apache.harmony.tests.javax.net.ssl;
     19 
     20 import javax.net.ssl.SSLException;
     21 
     22 import junit.framework.TestCase;
     23 
     24 /**
     25  * Tests for <code>SSLException</code> class constructors and methods.
     26  *
     27  */
     28 public class SSLExceptionTest extends TestCase {
     29 
     30     private static String[] msgs = {
     31             "",
     32             "Check new message",
     33             "Check new message Check new message Check new message Check new message Check new message" };
     34 
     35     private static Throwable tCause = new Throwable("Throwable for exception");
     36 
     37     /**
     38      * Test for <code>SSLException(String)</code> constructor Assertion:
     39      * constructs SSLException with detail message msg. Parameter
     40      * <code>msg</code> is not null.
     41      */
     42     public void testSSLException01() {
     43         SSLException sE;
     44         for (int i = 0; i < msgs.length; i++) {
     45             sE = new SSLException(msgs[i]);
     46             assertEquals("getMessage() must return: ".concat(msgs[i]), sE.getMessage(), msgs[i]);
     47             assertNull("getCause() must return null", sE.getCause());
     48         }
     49     }
     50 
     51     /**
     52      * Test for <code>SSLException(String)</code> constructor Assertion:
     53      * constructs SSLException when <code>msg</code> is null
     54      */
     55     public void testSSLException02() {
     56         String msg = null;
     57         SSLException sE = new SSLException(msg);
     58         assertNull("getMessage() must return null.", sE.getMessage());
     59         assertNull("getCause() must return null", sE.getCause());
     60     }
     61 
     62     /**
     63      * Test for <code>SSLException(Throwable)</code> constructor
     64      * Assertion: constructs SSLException when <code>cause</code> is null
     65      */
     66     public void testSSLException03() {
     67         Throwable cause = null;
     68         SSLException sE = new SSLException(cause);
     69         assertNull("getMessage() must return null.", sE.getMessage());
     70         assertNull("getCause() must return null", sE.getCause());
     71     }
     72 
     73     /**
     74      * Test for <code>SSLException(Throwable)</code> constructor
     75      * Assertion: constructs SSLException when <code>cause</code> is not
     76      * null
     77      */
     78     public void testSSLException04() {
     79         SSLException sE = new SSLException(tCause);
     80         if (sE.getMessage() != null) {
     81             String toS = tCause.toString();
     82             String getM = sE.getMessage();
     83             assertTrue("getMessage() should contain ".concat(toS), (getM
     84                     .indexOf(toS) != -1));
     85         }
     86         assertNotNull("getCause() must not return null", sE.getCause());
     87         assertEquals("getCause() must return ".concat(tCause.toString()), sE.getCause(), tCause);
     88     }
     89 
     90     /**
     91      * Test for <code>SSLException(String, Throwable)</code> constructor
     92      * Assertion: constructs SSLException when <code>cause</code> is null
     93      * <code>msg</code> is null
     94      */
     95     public void testSSLException05() {
     96         SSLException sE = new SSLException(null, null);
     97         assertNull("getMessage() must return null", sE.getMessage());
     98         assertNull("getCause() must return null", sE.getCause());
     99     }
    100 
    101     /**
    102      * Test for <code>SSLException(String, Throwable)</code> constructor
    103      * Assertion: constructs SSLException when <code>cause</code> is null
    104      * <code>msg</code> is not null
    105      */
    106     public void testSSLException06() {
    107         SSLException sE;
    108         for (int i = 0; i < msgs.length; i++) {
    109             sE = new SSLException(msgs[i], null);
    110             assertEquals("getMessage() must return: ".concat(msgs[i]), sE
    111                     .getMessage(), msgs[i]);
    112             assertNull("getCause() must return null", sE.getCause());
    113         }
    114     }
    115 
    116     /**
    117      * Test for <code>SSLException(String, Throwable)</code> constructor
    118      * Assertion: constructs SSLException when <code>cause</code> is not
    119      * null <code>msg</code> is null
    120      */
    121     public void testSSLException07() {
    122         SSLException sE = new SSLException(null, tCause);
    123         if (sE.getMessage() != null) {
    124             String toS = tCause.toString();
    125             String getM = sE.getMessage();
    126             assertTrue("getMessage() must should ".concat(toS), (getM
    127                     .indexOf(toS) != -1));
    128         }
    129         assertNotNull("getCause() must not return null", sE.getCause());
    130         assertEquals("getCause() must return ".concat(tCause.toString()), sE
    131                 .getCause(), tCause);
    132     }
    133 
    134     /**
    135      * Test for <code>SSLException(String, Throwable)</code> constructor
    136      * Assertion: constructs SSLException when <code>cause</code> is not
    137      * null <code>msg</code> is not null
    138      */
    139     public void testSSLException08() {
    140         SSLException sE;
    141         for (int i = 0; i < msgs.length; i++) {
    142             sE = new SSLException(msgs[i], tCause);
    143             String getM = sE.getMessage();
    144             String toS = tCause.toString();
    145             if (msgs[i].length() > 0) {
    146                 assertTrue("getMessage() must contain ".concat(msgs[i]), getM
    147                         .indexOf(msgs[i]) != -1);
    148                 if (!getM.equals(msgs[i])) {
    149                     assertTrue("getMessage() should contain ".concat(toS), getM
    150                             .indexOf(toS) != -1);
    151                 }
    152             }
    153             assertNotNull("getCause() must not return null", sE.getCause());
    154             assertEquals("getCause() must return ".concat(tCause.toString()),
    155                     sE.getCause(), tCause);
    156         }
    157     }
    158 }
    159