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.InvalidKeyException; 24 25import junit.framework.TestCase; 26 27 28/** 29 * Tests for <code>InvalidKeyException</code> class constructors and methods. 30 * 31 */ 32public class InvalidKeyExceptionTest extends TestCase { 33 34 public static void main(String[] args) { 35 } 36 37 /** 38 * Constructor for InvalidKeyExceptionTests. 39 * 40 * @param arg0 41 */ 42 public InvalidKeyExceptionTest(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>InvalidKeyException()</code> constructor Assertion: 55 * constructs InvalidKeyException with no detail message 56 */ 57 public void testInvalidKeyException01() { 58 InvalidKeyException tE = new InvalidKeyException(); 59 assertNull("getMessage() must return null.", tE.getMessage()); 60 assertNull("getCause() must return null", tE.getCause()); 61 } 62 63 /** 64 * Test for <code>InvalidKeyException(String)</code> constructor 65 * Assertion: constructs InvalidKeyException with detail message msg. 66 * Parameter <code>msg</code> is not null. 67 */ 68 public void testInvalidKeyException02() { 69 InvalidKeyException tE; 70 for (int i = 0; i < msgs.length; i++) { 71 tE = new InvalidKeyException(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>InvalidKeyException(String)</code> constructor 80 * Assertion: constructs InvalidKeyException when <code>msg</code> is null 81 */ 82 public void testInvalidKeyException03() { 83 String msg = null; 84 InvalidKeyException tE = new InvalidKeyException(msg); 85 assertNull("getMessage() must return null.", tE.getMessage()); 86 assertNull("getCause() must return null", tE.getCause()); 87 } 88 89 /** 90 * Test for <code>InvalidKeyException(Throwable)</code> constructor 91 * Assertion: constructs InvalidKeyException when <code>cause</code> is 92 * null 93 */ 94 public void testInvalidKeyException04() { 95 Throwable cause = null; 96 InvalidKeyException tE = new InvalidKeyException(cause); 97 assertNull("getMessage() must return null.", tE.getMessage()); 98 assertNull("getCause() must return null", tE.getCause()); 99 } 100 101 /** 102 * Test for <code>InvalidKeyException(Throwable)</code> constructor 103 * Assertion: constructs InvalidKeyException when <code>cause</code> is 104 * not null 105 */ 106 public void testInvalidKeyException05() { 107 InvalidKeyException tE = new InvalidKeyException(tCause); 108 if (tE.getMessage() != null) { 109 String toS = tCause.toString(); 110 String getM = tE.getMessage(); 111 assertTrue("getMessage() should contain ".concat(toS), (getM 112 .indexOf(toS) != -1)); 113 } 114 assertNotNull("getCause() must not return null", tE.getCause()); 115 assertEquals("getCause() must return ".concat(tCause.toString()), tE 116 .getCause(), tCause); 117 } 118 119 /** 120 * Test for <code>InvalidKeyException(String, Throwable)</code> 121 * constructor Assertion: constructs InvalidKeyException when 122 * <code>cause</code> is null <code>msg</code> is null 123 */ 124 public void testInvalidKeyException06() { 125 InvalidKeyException tE = new InvalidKeyException(null, null); 126 assertNull("getMessage() must return null", tE.getMessage()); 127 assertNull("getCause() must return null", tE.getCause()); 128 } 129 130 /** 131 * Test for <code>InvalidKeyException(String, Throwable)</code> 132 * constructor Assertion: constructs InvalidKeyException when 133 * <code>cause</code> is null <code>msg</code> is not null 134 */ 135 public void testInvalidKeyException07() { 136 InvalidKeyException tE; 137 for (int i = 0; i < msgs.length; i++) { 138 tE = new InvalidKeyException(msgs[i], null); 139 assertEquals("getMessage() must return: ".concat(msgs[i]), tE 140 .getMessage(), msgs[i]); 141 assertNull("getCause() must return null", tE.getCause()); 142 } 143 } 144 145 /** 146 * Test for <code>InvalidKeyException(String, Throwable)</code> 147 * constructor Assertion: constructs InvalidKeyException when 148 * <code>cause</code> is not null <code>msg</code> is null 149 */ 150 public void testInvalidKeyException08() { 151 InvalidKeyException tE = new InvalidKeyException(null, tCause); 152 if (tE.getMessage() != null) { 153 String toS = tCause.toString(); 154 String getM = tE.getMessage(); 155 assertTrue("getMessage() must should ".concat(toS), (getM 156 .indexOf(toS) != -1)); 157 } 158 assertNotNull("getCause() must not return null", tE.getCause()); 159 assertEquals("getCause() must return ".concat(tCause.toString()), tE 160 .getCause(), tCause); 161 } 162 163 /** 164 * Test for <code>InvalidKeyException(String, Throwable)</code> 165 * constructor Assertion: constructs InvalidKeyException when 166 * <code>cause</code> is not null <code>msg</code> is not null 167 */ 168 public void testInvalidKeyException09() { 169 InvalidKeyException tE; 170 for (int i = 0; i < msgs.length; i++) { 171 tE = new InvalidKeyException(msgs[i], tCause); 172 String getM = tE.getMessage(); 173 String toS = tCause.toString(); 174 if (msgs[i].length() > 0) { 175 assertTrue("getMessage() must contain ".concat(msgs[i]), getM 176 .indexOf(msgs[i]) != -1); 177 if (!getM.equals(msgs[i])) { 178 assertTrue("getMessage() should contain ".concat(toS), getM 179 .indexOf(toS) != -1); 180 } 181 } 182 assertNotNull("getCause() must not return null", tE.getCause()); 183 assertEquals("getCause() must return ".concat(tCause.toString()), 184 tE.getCause(), tCause); 185 } 186 } 187} 188