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