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.ProviderException;
25
26import junit.framework.TestCase;
27
28
29/**
30 * Tests for <code>ProviderException</code> class constructors and methods.
31 */
32public class ProviderExceptionTest extends TestCase {
33
34    public static void main(String[] args) {
35    }
36
37    /**
38     * Constructor for ProviderExceptionTests.
39     *
40     * @param arg0
41     */
42    public ProviderExceptionTest(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>ProviderException()</code> constructor Assertion:
55     * constructs ProviderException with no detail message
56     */
57    public void testProviderException01() {
58        ProviderException tE = new ProviderException();
59        assertNull("getMessage() must return null.", tE.getMessage());
60        assertNull("getCause() must return null", tE.getCause());
61    }
62
63    /**
64     * Test for <code>ProviderException(String)</code> constructor Assertion:
65     * constructs ProviderException with detail message msg. Parameter
66     * <code>msg</code> is not null.
67     */
68    public void testProviderException02() {
69        ProviderException tE;
70        for (int i = 0; i < msgs.length; i++) {
71            tE = new ProviderException(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>ProviderException(String)</code> constructor Assertion:
80     * constructs ProviderException when <code>msg</code> is null
81     */
82    public void testProviderException03() {
83        String msg = null;
84        ProviderException tE = new ProviderException(msg);
85        assertNull("getMessage() must return null.", tE.getMessage());
86        assertNull("getCause() must return null", tE.getCause());
87    }
88
89    /**
90     * Test for <code>ProviderException(Throwable)</code> constructor
91     * Assertion: constructs ProviderException when <code>cause</code> is null
92     */
93    public void testProviderException04() {
94        Throwable cause = null;
95        ProviderException tE = new ProviderException(cause);
96        assertNull("getMessage() must return null.", tE.getMessage());
97        assertNull("getCause() must return null", tE.getCause());
98    }
99
100    /**
101     * Test for <code>ProviderException(Throwable)</code> constructor
102     * Assertion: constructs ProviderException when <code>cause</code> is not
103     * null
104     */
105    public void testProviderException05() {
106        ProviderException tE = new ProviderException(tCause);
107        if (tE.getMessage() != null) {
108            String toS = tCause.toString();
109            String getM = tE.getMessage();
110            assertTrue("getMessage() should contain ".concat(toS), (getM
111                    .indexOf(toS) != -1));
112        }
113        assertNotNull("getCause() must not return null", tE.getCause());
114        assertEquals("getCause() must return ".concat(tCause.toString()), tE
115                .getCause(), tCause);
116    }
117
118    /**
119     * Test for <code>ProviderException(String, Throwable)</code> constructor
120     * Assertion: constructs ProviderException when <code>cause</code> is null
121     * <code>msg</code> is null
122     */
123    public void testProviderException06() {
124        ProviderException tE = new ProviderException(null, null);
125        assertNull("getMessage() must return null", tE.getMessage());
126        assertNull("getCause() must return null", tE.getCause());
127    }
128
129    /**
130     * Test for <code>ProviderException(String, Throwable)</code> constructor
131     * Assertion: constructs ProviderException when <code>cause</code> is null
132     * <code>msg</code> is not null
133     */
134    public void testProviderException07() {
135        ProviderException tE;
136        for (int i = 0; i < msgs.length; i++) {
137            tE = new ProviderException(msgs[i], null);
138            assertEquals("getMessage() must return: ".concat(msgs[i]), tE
139                    .getMessage(), msgs[i]);
140            assertNull("getCause() must return null", tE.getCause());
141        }
142    }
143
144    /**
145     * Test for <code>ProviderException(String, Throwable)</code> constructor
146     * Assertion: constructs ProviderException when <code>cause</code> is not
147     * null <code>msg</code> is null
148     */
149    public void testProviderException08() {
150        ProviderException tE = new ProviderException(null, tCause);
151        if (tE.getMessage() != null) {
152            String toS = tCause.toString();
153            String getM = tE.getMessage();
154            assertTrue("getMessage() must should ".concat(toS), (getM
155                    .indexOf(toS) != -1));
156        }
157        assertNotNull("getCause() must not return null", tE.getCause());
158        assertEquals("getCause() must return ".concat(tCause.toString()), tE
159                .getCause(), tCause);
160    }
161
162    /**
163     * Test for <code>ProviderException(String, Throwable)</code> constructor
164     * Assertion: constructs ProviderException when <code>cause</code> is not
165     * null <code>msg</code> is not null
166     */
167    public void testProviderException09() {
168        ProviderException tE;
169        for (int i = 0; i < msgs.length; i++) {
170            tE = new ProviderException(msgs[i], tCause);
171            String getM = tE.getMessage();
172            String toS = tCause.toString();
173            if (msgs[i].length() > 0) {
174                assertTrue("getMessage() must contain ".concat(msgs[i]), getM
175                        .indexOf(msgs[i]) != -1);
176                if (!getM.equals(msgs[i])) {
177                    assertTrue("getMessage() should contain ".concat(toS), getM
178                            .indexOf(toS) != -1);
179                }
180            }
181            assertNotNull("getCause() must not return null", tE.getCause());
182            assertEquals("getCause() must return ".concat(tCause.toString()),
183                    tE.getCause(), tCause);
184        }
185    }
186}
187