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 23 package tests.security.spec; 24 25 import junit.framework.TestCase; 26 27 import java.security.spec.InvalidKeySpecException; 28 29 /** 30 * Tests for <code>InvalidKeySpecException</code> class constructors and 31 * methods. 32 * 33 */ 34 public class InvalidKeySpecExceptionTest 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>InvalidKeySpecException()</code> constructor Assertion: 45 * constructs InvalidKeySpecException with no detail message 46 */ 47 public void testInvalidKeySpecException01() { 48 InvalidKeySpecException tE = new InvalidKeySpecException(); 49 assertNull("getMessage() must return null.", tE.getMessage()); 50 assertNull("getCause() must return null", tE.getCause()); 51 } 52 53 /** 54 * Test for <code>InvalidKeySpecException(String)</code> constructor 55 * Assertion: constructs InvalidKeySpecException with detail message msg. 56 * Parameter <code>msg</code> is not null. 57 */ 58 public void testInvalidKeySpecException02() { 59 InvalidKeySpecException tE; 60 for (int i = 0; i < msgs.length; i++) { 61 tE = new InvalidKeySpecException(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>InvalidKeySpecException(String)</code> constructor 70 * Assertion: constructs InvalidKeySpecException when <code>msg</code> is 71 * null 72 */ 73 public void testInvalidKeySpecException03() { 74 String msg = null; 75 InvalidKeySpecException tE = new InvalidKeySpecException(msg); 76 assertNull("getMessage() must return null.", tE.getMessage()); 77 assertNull("getCause() must return null", tE.getCause()); 78 } 79 80 /** 81 * Test for <code>InvalidKeySpecException(Throwable)</code> constructor 82 * Assertion: constructs InvalidKeySpecException when <code>cause</code> 83 * is null 84 */ 85 public void testInvalidKeySpecException04() { 86 Throwable cause = null; 87 InvalidKeySpecException tE = new InvalidKeySpecException(cause); 88 assertNull("getMessage() must return null.", tE.getMessage()); 89 assertNull("getCause() must return null", tE.getCause()); 90 } 91 92 /** 93 * Test for <code>InvalidKeySpecException(Throwable)</code> constructor 94 * Assertion: constructs InvalidKeySpecException when <code>cause</code> 95 * is not null 96 */ 97 public void testInvalidKeySpecException05() { 98 InvalidKeySpecException tE = new InvalidKeySpecException(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>InvalidKeySpecException(String, Throwable)</code> 112 * constructor Assertion: constructs InvalidKeySpecException when 113 * <code>cause</code> is null <code>msg</code> is null 114 */ 115 public void testInvalidKeySpecException06() { 116 InvalidKeySpecException tE = new InvalidKeySpecException(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>InvalidKeySpecException(String, Throwable)</code> 123 * constructor Assertion: constructs InvalidKeySpecException when 124 * <code>cause</code> is null <code>msg</code> is not null 125 */ 126 public void testInvalidKeySpecException07() { 127 InvalidKeySpecException tE; 128 for (int i = 0; i < msgs.length; i++) { 129 tE = new InvalidKeySpecException(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>InvalidKeySpecException(String, Throwable)</code> 138 * constructor Assertion: constructs InvalidKeySpecException when 139 * <code>cause</code> is not null <code>msg</code> is null 140 */ 141 public void testInvalidKeySpecException08() { 142 InvalidKeySpecException tE = new InvalidKeySpecException(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>InvalidKeySpecException(String, Throwable)</code> 156 * constructor Assertion: constructs InvalidKeySpecException when 157 * <code>cause</code> is not null <code>msg</code> is not null 158 */ 159 public void testInvalidKeySpecException09() { 160 InvalidKeySpecException tE; 161 for (int i = 0; i < msgs.length; i++) { 162 tE = new InvalidKeySpecException(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