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.GeneralSecurityException; 24 25import junit.framework.TestCase; 26 27 28/** 29 * Tests for <code>GeneralSecurityException</code> class constructors and 30 * methods. 31 * 32 */ 33public class GeneralSecurityExceptionTest extends TestCase { 34 35 public static void main(String[] args) { 36 } 37 38 /** 39 * Constructor for GeneralSecurityExceptionTests. 40 * 41 * @param arg0 42 */ 43 public GeneralSecurityExceptionTest(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>GeneralSecurityException()</code> constructor Assertion: 56 * constructs GeneralSecurityException with no detail message 57 */ 58 public void testGeneralSecurityException01() { 59 GeneralSecurityException tE = new GeneralSecurityException(); 60 assertNull("getMessage() must return null.", tE.getMessage()); 61 assertNull("getCause() must return null", tE.getCause()); 62 } 63 64 /** 65 * Test for <code>GeneralSecurityException(String)</code> constructor 66 * Assertion: constructs GeneralSecurityException with detail message msg. 67 * Parameter <code>msg</code> is not null. 68 */ 69 public void testGeneralSecurityException02() { 70 GeneralSecurityException tE; 71 for (int i = 0; i < msgs.length; i++) { 72 tE = new GeneralSecurityException(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>GeneralSecurityException(String)</code> constructor 81 * Assertion: constructs GeneralSecurityException when <code>msg</code> is 82 * null 83 */ 84 public void testGeneralSecurityException03() { 85 String msg = null; 86 GeneralSecurityException tE = new GeneralSecurityException(msg); 87 assertNull("getMessage() must return null.", tE.getMessage()); 88 assertNull("getCause() must return null", tE.getCause()); 89 } 90 91 /** 92 * Test for <code>GeneralSecurityException(Throwable)</code> constructor 93 * Assertion: constructs GeneralSecurityException when <code>cause</code> 94 * is null 95 */ 96 public void testGeneralSecurityException04() { 97 Throwable cause = null; 98 GeneralSecurityException tE = new GeneralSecurityException(cause); 99 assertNull("getMessage() must return null.", tE.getMessage()); 100 assertNull("getCause() must return null", tE.getCause()); 101 } 102 103 /** 104 * Test for <code>GeneralSecurityException(Throwable)</code> constructor 105 * Assertion: constructs GeneralSecurityException when <code>cause</code> 106 * is not null 107 */ 108 public void testGeneralSecurityException05() { 109 GeneralSecurityException tE = new GeneralSecurityException(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>GeneralSecurityException(String, Throwable)</code> 123 * constructor Assertion: constructs GeneralSecurityException when 124 * <code>cause</code> is null <code>msg</code> is null 125 */ 126 public void testGeneralSecurityException06() { 127 GeneralSecurityException tE = new GeneralSecurityException(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>GeneralSecurityException(String, Throwable)</code> 134 * constructor Assertion: constructs GeneralSecurityException when 135 * <code>cause</code> is null <code>msg</code> is not null 136 */ 137 public void testGeneralSecurityException07() { 138 GeneralSecurityException tE; 139 for (int i = 0; i < msgs.length; i++) { 140 tE = new GeneralSecurityException(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>GeneralSecurityException(String, Throwable)</code> 149 * constructor Assertion: constructs GeneralSecurityException when 150 * <code>cause</code> is not null <code>msg</code> is null 151 */ 152 public void testGeneralSecurityException08() { 153 GeneralSecurityException tE = new GeneralSecurityException(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>GeneralSecurityException(String, Throwable)</code> 167 * constructor Assertion: constructs GeneralSecurityException when 168 * <code>cause</code> is not null <code>msg</code> is not null 169 */ 170 public void testGeneralSecurityException09() { 171 GeneralSecurityException tE; 172 for (int i = 0; i < msgs.length; i++) { 173 tE = new GeneralSecurityException(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