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