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