DigestExceptionTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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;
23import java.security.DigestException;
24
25import junit.framework.TestCase;
26
27
28/**
29 * Tests for <code>DigestException</code> class constructors and methods.
30 *
31 */
32public class DigestExceptionTest extends TestCase {
33
34    public static void main(String[] args) {
35    }
36
37    /**
38     * Constructor for DigestExceptionTests.
39     *
40     * @param arg0
41     */
42    public DigestExceptionTest(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>DigestException()</code> constructor Assertion:
55     * constructs DigestException with no detail message
56     */
57    public void testDigestException01() {
58        DigestException tE = new DigestException();
59        assertNull("getMessage() must return null.", tE.getMessage());
60        assertNull("getCause() must return null", tE.getCause());
61    }
62
63    /**
64     * Test for <code>DigestException(String)</code> constructor Assertion:
65     * constructs DigestException with detail message msg. Parameter
66     * <code>msg</code> is not null.
67     */
68    public void testDigestException02() {
69        DigestException tE;
70        for (int i = 0; i < msgs.length; i++) {
71            tE = new DigestException(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>DigestException(String)</code> constructor Assertion:
80     * constructs DigestException when <code>msg</code> is null
81     */
82    public void testDigestException03() {
83        String msg = null;
84        DigestException tE = new DigestException(msg);
85        assertNull("getMessage() must return null.", tE.getMessage());
86        assertNull("getCause() must return null", tE.getCause());
87    }
88
89    /**
90     * Test for <code>DigestException(Throwable)</code> constructor Assertion:
91     * constructs DigestException when <code>cause</code> is null
92     */
93    public void testDigestException04() {
94        Throwable cause = null;
95        DigestException tE = new DigestException(cause);
96        assertNull("getMessage() must return null.", tE.getMessage());
97        assertNull("getCause() must return null", tE.getCause());
98    }
99
100    /**
101     * Test for <code>DigestException(Throwable)</code> constructor Assertion:
102     * constructs DigestException when <code>cause</code> is not null
103     */
104    public void testDigestException05() {
105        DigestException tE = new DigestException(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>DigestException(String, Throwable)</code> constructor
119     * Assertion: constructs DigestException when <code>cause</code> is null
120     * <code>msg</code> is null
121     */
122    public void testDigestException06() {
123        DigestException tE = new DigestException(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>DigestException(String, Throwable)</code> constructor
130     * Assertion: constructs DigestException when <code>cause</code> is null
131     * <code>msg</code> is not null
132     */
133    public void testDigestException07() {
134        DigestException tE;
135        for (int i = 0; i < msgs.length; i++) {
136            tE = new DigestException(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>DigestException(String, Throwable)</code> constructor
145     * Assertion: constructs DigestException when <code>cause</code> is not
146     * null <code>msg</code> is null
147     */
148    public void testDigestException08() {
149        DigestException tE = new DigestException(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>DigestException(String, Throwable)</code> constructor
163     * Assertion: constructs DigestException when <code>cause</code> is not
164     * null <code>msg</code> is not null
165     */
166    public void testDigestException09() {
167        DigestException tE;
168        for (int i = 0; i < msgs.length; i++) {
169            tE = new DigestException(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