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 Maxim V. Makarov 20*/ 21 22package org.apache.harmony.auth.tests.javax.security.auth.callback; 23 24import javax.security.auth.callback.ConfirmationCallback; 25 26import junit.framework.TestCase; 27 28/** 29 * Tests ConfirmationCallback class 30 */ 31 32public class ConfirmationCallbackTest extends TestCase { 33 34 ConfirmationCallback cc; 35 int mt[] = { ConfirmationCallback.INFORMATION, 36 ConfirmationCallback.WARNING, 37 ConfirmationCallback.ERROR 38 }; 39 int ot[] = { ConfirmationCallback.YES_NO_OPTION, 40 ConfirmationCallback.YES_NO_CANCEL_OPTION , 41 ConfirmationCallback.OK_CANCEL_OPTION 42 }; 43 int dopt[] = { ConfirmationCallback.YES, 44 ConfirmationCallback.NO, 45 ConfirmationCallback.CANCEL, 46 ConfirmationCallback.OK 47 }; 48 49 /** 50 * Ctor test 51 */ 52 public final void testConfirmationCallback_01() throws IllegalArgumentException { 53 try { 54 for (int i = 0; i < mt.length; i++) { 55 cc = new ConfirmationCallback(mt[i], ot[1], dopt[1]); 56 } 57 } catch (IllegalArgumentException e){} 58 } 59 60 /** 61 * Expected IAE, if messageType is not either INFORMATION, WARNING,ERROR; 62 * if optionType is not either YES_NO_OPTION, YES_NO_CANCEL_OPTION,OK_CANCEL_OPTION; 63 * if defaultOption does not lie within the array boundaries of options. 64 */ 65 public final void testConfirmationCallback_02() throws IllegalArgumentException{ 66 try { 67 cc = new ConfirmationCallback(3, ot[1], dopt[1]); 68 fail("Message type should be either INFORMATION, WARNING or ERROR"); 69 }catch (IllegalArgumentException e){} 70 try { 71 cc = new ConfirmationCallback(mt[1], 3, dopt[1]); 72 fail("Option type should be either YES_NO_OPTION, YES_NO_CANCEL_OPTION or OK_CANCEL_OPTION"); 73 } catch (IllegalArgumentException e){} 74 try { 75 cc = new ConfirmationCallback(mt[1], ot[1], 4); 76 fail("Default option type should be either YES, NO, CANCEL or OK"); 77 } catch (IllegalArgumentException e){} 78 } 79 80 81 /** 82 * Expected IAE, if optionType is YES_NO_OPTION and defaultOption is not YES/NO, 83 * else ok. 84 */ 85 86 public final void testConfirmationCallback_03() throws IllegalArgumentException { 87 try { 88 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, ot[0], dopt[0]); 89 } catch(IllegalArgumentException e){} 90 try { 91 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, ot[0], dopt[1]); 92 } catch(IllegalArgumentException e){} 93 try { 94 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, ot[0], dopt[2]); 95 fail("1. If option type is YES_NO_OPTION then default option should be YES/NO"); 96 } catch(IllegalArgumentException e){} 97 try { 98 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, ot[0], dopt[3]); 99 fail("2. If option type is YES_NO_OPTION then default option should be YES/NO "); 100 } catch(IllegalArgumentException e){} 101 102} 103 104 105 /** 106 * Expected IAE, if optionType is YES_NO_CANCEL_OPTION and defaultOption is not YES/NO/CANCEL, 107 * else ok. 108 */ 109 public final void testConfirmationCallback_04() throws IllegalArgumentException { 110 try { 111 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, ot[1], dopt[0]); 112 } catch(IllegalArgumentException e){} 113 try { 114 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, ot[1], dopt[1]); 115 } catch(IllegalArgumentException e){} 116 try { 117 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, ot[1], dopt[2]); 118 } catch(IllegalArgumentException e){} 119 try { 120 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, ot[1], dopt[3]); 121 fail("1. If option type is YES_NO_CANCEL_OPTION then default option should be YES/NO/CANCEL"); 122 } catch(IllegalArgumentException e){} 123 124 } 125 126 /** 127 * Expected IAE, if optionType is OK_CANCEL_OPTION and defaultOption is not CANCEL/OK 128 * else ok. 129 */ 130 public final void testConfirmationCallback_05() throws IllegalArgumentException { 131 try { 132 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, ot[2], dopt[0]); 133 fail("1. If option type is OK_CANCEL_OPTION then default option should be CANCEL/OK"); 134 } catch(IllegalArgumentException e){} 135 try { 136 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, ot[2], dopt[1]); 137 fail("2. If option type is OK_CANCEL_OPTION then default option should be CANCEL/OK"); 138 } catch(IllegalArgumentException e){} 139 try { 140 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, ot[2], dopt[2]); 141 } catch(IllegalArgumentException e){} 142 try { 143 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, ot[2], dopt[3]); 144 } catch(IllegalArgumentException e){} 145 } 146 147 /** 148 * Expected IAE, if optionType is null or an empty and 149 * defaultOption type does not lie within the array boundaries of options, 150 * else ok. 151 */ 152 153 public final void testConfirmationCallback_06() { 154 String[] opt = {"CONTINUE", "ABORT"}; 155 String[] opt1 = {"START", ""}; 156 String[] opt2 = {"START", null}; 157 try { 158 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, opt, 1); 159 } catch(IllegalArgumentException e){} 160 try { 161 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, opt1, 1); 162 fail("1. Option type should not be null and empty "); 163 } catch(IllegalArgumentException e){} 164 try { 165 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, opt2, 1); 166 fail("2. Option type should not be null and empty "); 167 } catch(IllegalArgumentException e){} 168 try { 169 cc = new ConfirmationCallback(ConfirmationCallback.INFORMATION, opt, 3); 170 fail("Default Option does not lie within the array boundaries of options "); 171 } catch(IllegalArgumentException e){ 172 } 173 } 174 175 /** 176 * Expected IAE, if prompt is null or an empty, else ok. 177 */ 178 public final void testConfirmationCallback_07() { 179 try { 180 cc = new ConfirmationCallback("prompt", ConfirmationCallback.INFORMATION, ot[0], dopt[0]); 181 } catch(IllegalArgumentException e){} 182 try { 183 cc = new ConfirmationCallback("", ConfirmationCallback.INFORMATION, ot[0], dopt[0]); 184 fail("2. Prompt should not be null and empty "); 185 } catch(IllegalArgumentException e){} 186 try { 187 cc = new ConfirmationCallback(null, ConfirmationCallback.INFORMATION, ot[0], dopt[0]); 188 fail("2. Prompt should not be null and empty "); 189 } catch(IllegalArgumentException e){} 190 } 191 192 /** 193 * Expected IAE, if optionType and prompt is null or an empty, else ok. 194 */ 195 public final void testConfirmationCallback_08() { 196 String[] opt = {"CONTINUE", "ABORT"}; 197 String[] opt1 = {"START", ""}; 198 String[] opt2 = {"START", null}; 199 try { 200 cc = new ConfirmationCallback("prompt", ConfirmationCallback.INFORMATION, opt, 1); 201 } catch(IllegalArgumentException e){} 202 try { 203 cc = new ConfirmationCallback("prompt", ConfirmationCallback.INFORMATION, opt1, 1); 204 fail("1. Option type should not be null and empty "); 205 } catch(IllegalArgumentException e){} 206 try { 207 cc = new ConfirmationCallback("prompt", ConfirmationCallback.INFORMATION, opt2, 1); 208 fail("2. Option type should not be null and empty "); 209 } catch(IllegalArgumentException e){} 210 try { 211 cc = new ConfirmationCallback("", ConfirmationCallback.INFORMATION, opt, 1); 212 fail("3. Prompt should not be null and empty "); 213 } catch(IllegalArgumentException e){} 214 try { 215 cc = new ConfirmationCallback(null, ConfirmationCallback.INFORMATION, opt, 1); 216 fail("4. Prompt should not be null and empty "); 217 } catch(IllegalArgumentException e){ 218 } 219 220 } 221 222 /** 223 * test of methods getOptionType(), getDefaultOption(), getMessageType() 224 * getOptions(), set/getSelectedIndex 225 */ 226 public final void testConfirmationCallback_09() throws IllegalArgumentException { 227 String[] opt = {"CONTINUE", "ABORT"}; 228 String[] s; 229 230 cc = new ConfirmationCallback("prompt", mt[0], opt, 1); 231 assertEquals(1, cc.getDefaultOption()); 232 assertEquals(ConfirmationCallback.INFORMATION, cc.getMessageType()); 233 assertEquals(opt,cc.getOptions()); 234 s = cc.getOptions(); 235 for (int i = 0; i < opt.length; i++){ 236 assertEquals(opt[i], s[i]); 237 } 238 assertEquals("prompt", cc.getPrompt()); 239 assertEquals(-1, cc.getOptionType()); 240 assertEquals(ConfirmationCallback.UNSPECIFIED_OPTION, cc.getOptionType()); 241 assertNotNull(cc.getOptions()); 242 cc.setSelectedIndex(1); 243 assertEquals(1, cc.getSelectedIndex()); 244 } 245 246 /** 247 * test of methods getOptions(), set/getSelectedIndex when options is null 248 */ 249 public final void testConfirmationCallback_10() throws IllegalArgumentException { 250 251 cc = new ConfirmationCallback("prompt", mt[0], ot[0], dopt[0]); 252 assertNull(cc.getOptions()); 253 cc.setSelectedIndex(1); 254 assertEquals(1, cc.getSelectedIndex()); 255 } 256 257 /** 258 * if optionType was specified in ctor, then selection represented as YES, NO, OK or CANCEL 259 * i.e. defaultOption, thus expected IAE if selection does not lie within the array 260 * boundaries of options. 261 */ 262 public final void testSetSelection() throws IllegalArgumentException { 263 cc = new ConfirmationCallback("prompt", mt[0], ot[0], dopt[0]); 264 try { 265 cc.setSelectedIndex(3); 266 assertEquals(3, cc.getSelectedIndex()); 267 fail("There is not enough an information about setSelection in java doc"); 268 }catch (IllegalArgumentException e) { 269 } catch (ArrayIndexOutOfBoundsException e){} 270 271 } 272 273 /* 274 * Regression test for bug in setSelectedIndex 275 */ 276 public final void testSetSelectedIndex() throws Exception { 277 cc = new ConfirmationCallback("prompt", mt[0], ot[0], dopt[0]); 278 cc.setSelectedIndex(ConfirmationCallback.YES); 279 } 280} 281