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.xnet.tests.javax.net.ssl;
     19 
     20 import javax.net.ssl.SSLException;
     21 
     22 import junit.framework.TestCase;
     23 
     24 
     25 /**
     26  * Tests for <code>SSLException</code> class constructors and methods.
     27  */
     28 public class SSLExceptionTest extends TestCase {
     29 
     30     public static void main(String[] args) {
     31     }
     32 
     33     /**
     34      * Constructor for SSLExceptionTests.
     35      *
     36      * @param arg0
     37      */
     38     public SSLExceptionTest(String arg0) {
     39         super(arg0);
     40     }
     41 
     42     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     static Throwable tCause = new Throwable("Throwable for exception");
     48 
     49     /**
     50      * Test for <code>SSLException(String)</code> constructor Assertion:
     51      * constructs SSLException with detail message msg. Parameter
     52      * <code>msg</code> is not null.
     53      */
     54     public void testSSLException01() {
     55         SSLException tE;
     56         for (int i = 0; i < msgs.length; i++) {
     57             tE = new SSLException(msgs[i]);
     58             assertEquals("getMessage() must return: ".concat(msgs[i]), tE
     59                     .getMessage(), msgs[i]);
     60             assertNull("getCause() must return null", tE.getCause());
     61         }
     62     }
     63 
     64     /**
     65      * Test for <code>SSLException(String)</code> constructor Assertion:
     66      * constructs SSLException when <code>msg</code> is null
     67      */
     68     public void testSSLException02() {
     69         String msg = null;
     70         SSLException tE = new SSLException(msg);
     71         assertNull("getMessage() must return null.", tE.getMessage());
     72         assertNull("getCause() must return null", tE.getCause());
     73     }
     74 
     75     /**
     76      * Test for <code>DigestException(Throwable)</code> constructor Assertion:
     77      * constructs DigestException when <code>cause</code> is null
     78      */
     79     public void testSSLException03() {
     80         Throwable cause = null;
     81         SSLException tE = new SSLException(cause);
     82         assertNull("getMessage() must return null.", tE.getMessage());
     83         assertNull("getCause() must return null", tE.getCause());
     84     }
     85 
     86     /**
     87      * Test for <code>SSLException(Throwable)</code> constructor Assertion:
     88      * constructs SSLException when <code>cause</code> is not null
     89      */
     90     public void testSSLException04() {
     91         SSLException tE = new SSLException(tCause);
     92         if (tE.getMessage() != null) {
     93             String toS = tCause.toString();
     94             String getM = tE.getMessage();
     95             assertTrue("getMessage() should contain ".concat(toS), (getM
     96                     .indexOf(toS) != -1));
     97         }
     98         // SSLException is subclass of IOException, but IOException has not
     99         // constructors with Throwable parameters
    100         if (tE.getCause() != null) {
    101             //	assertNotNull("getCause() must not return null", tE.getCause());
    102             assertEquals("getCause() must return ".concat(tCause.toString()),
    103                     tE.getCause(), tCause);
    104         }
    105     }
    106 
    107     /**
    108      * Test for <code>SSLException(String, Throwable)</code> constructor
    109      * Assertion: constructs SSLException when <code>cause</code> is null
    110      * <code>msg</code> is null
    111      */
    112     public void testSSLException05() {
    113         SSLException tE = new SSLException(null, null);
    114         assertNull("getMessage() must return null", tE.getMessage());
    115         assertNull("getCause() must return null", tE.getCause());
    116     }
    117 
    118     /**
    119      * Test for <code>SSLException(String, Throwable)</code> constructor
    120      * Assertion: constructs SSLException when <code>cause</code> is null
    121      * <code>msg</code> is not null
    122      */
    123     public void testSSLException06() {
    124         SSLException tE;
    125         for (int i = 0; i < msgs.length; i++) {
    126             tE = new SSLException(msgs[i], null);
    127             assertEquals("getMessage() must return: ".concat(msgs[i]), tE
    128                     .getMessage(), msgs[i]);
    129             assertNull("getCause() must return null", tE.getCause());
    130         }
    131     }
    132 
    133     /**
    134      * Test for <code>SSLException(String, Throwable)</code> constructor
    135      * Assertion: constructs SSLException when <code>cause</code> is not null
    136      * <code>msg</code> is null
    137      */
    138     public void testSSLException07() {
    139         SSLException tE = new SSLException(null, tCause);
    140         if (tE.getMessage() != null) {
    141             String toS = tCause.toString();
    142             String getM = tE.getMessage();
    143             assertTrue("getMessage() must should ".concat(toS), (getM
    144                     .indexOf(toS) != -1));
    145         }
    146         // SSLException is subclass of IOException, but IOException has not
    147         // constructors with Throwable parameters
    148         if (tE.getCause() != null) {
    149             //	assertNotNull("getCause() must not return null", tE.getCause());
    150             assertEquals("getCause() must return ".concat(tCause.toString()),
    151                     tE.getCause(), tCause);
    152         }
    153     }
    154 
    155     /**
    156      * Test for <code>SSLException(String, Throwable)</code> constructor
    157      * Assertion: constructs SSLException when <code>cause</code> is not null
    158      * <code>msg</code> is not null
    159      */
    160     public void testSSLException08() {
    161         SSLException tE;
    162         for (int i = 0; i < msgs.length; i++) {
    163             tE = new SSLException(msgs[i], tCause);
    164             String getM = tE.getMessage();
    165             String toS = tCause.toString();
    166             if (msgs[i].length() > 0) {
    167                 assertTrue("getMessage() must contain ".concat(msgs[i]), getM
    168                         .indexOf(msgs[i]) != -1);
    169                 if (!getM.equals(msgs[i])) {
    170                     assertTrue("getMessage() should contain ".concat(toS), getM
    171                             .indexOf(toS) != -1);
    172                 }
    173             }
    174             // SSLException is subclass of IOException, but IOException has not
    175             // constructors with Throwable parameters
    176             if (tE.getCause() != null) {
    177                 //	assertNotNull("getCause() must not return null",
    178                 // tE.getCause());
    179                 assertEquals("getCause() must return "
    180                         .concat(tCause.toString()), tE.getCause(), tCause);
    181             }
    182         }
    183     }
    184 
    185 }
    186