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