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