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.auth.tests.javax.security.sasl; 23 24import javax.security.sasl.RealmChoiceCallback; 25 26import junit.framework.TestCase; 27 28/** 29 * Tests for constructor and methods ofRealmChoiceCallback class 30 * 31 */ 32 33public class RealmChoiceCallbackTest extends TestCase { 34 private static final String[] prompts = { 35 "Prompts", 36 "Another prompt", 37 "Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt Long String for prompt" }; 38 39 private static final String [] emptyCh = {}; 40 private static final String [] wrongCh1 = {"Default1", "", "Default2"}; 41 private static final String [] wrongCh2 = {"Default1", null, "Default2"}; 42 private static final String [] choices = { 43 "DefaultRealmInfo", 44 "Another default realm info", 45 "Long string for default realm info Long string for default realm info Long string for default realm info Long string for default realm info Long string for default realm info Long string for default realm info Long string for default realm info Long string for default realm info Long string for default realm info Long string for default realm info Long string for default realm info Long string for default realm info Long string for default realm info Long string for default realm info Long string for default realm info Long string for default realm info " }; 46 private static final int indexes[] = { Integer.MIN_VALUE, -1, 0, 100, 47 20000, Integer.MAX_VALUE }; 48 49 /** 50 * Test for <code>RealmChoiceCallback(String prompt, String[] choices, 51 * int defaultChoice, boolean multiple) </code> constructor 52 * 53 * Assertion: throws IllegalArgumentException when 54 * string parameters are null or empty and if defaultChoice is not 55 * within of choices array 56 */ 57 public void test01() { 58 try { 59 new RealmChoiceCallback(null, choices, 0, true); 60 fail("IllegalArgumentException should be thrown for null prompt"); 61 } catch (IllegalArgumentException e) { 62 } 63 try { 64 new RealmChoiceCallback("", choices, 0, true); 65 fail("IllegalArgumentException should be thrown for empty prompt"); 66 } catch (IllegalArgumentException e) { 67 } 68 try { 69 new RealmChoiceCallback("prompt", null, 0, true); 70 fail("IllegalArgumentException should be thrown for null choices"); 71 } catch (IllegalArgumentException e) { 72 } 73 try { 74 new RealmChoiceCallback("prompt", emptyCh, 0, true); 75 fail("IllegalArgumentException should be thrown for empty choices"); 76 } catch (IllegalArgumentException e) { 77 } 78 try { 79 new RealmChoiceCallback("prompt", wrongCh1, 0, true); 80 fail("IllegalArgumentException should be thrown for incorrect choices"); 81 } catch (IllegalArgumentException e) { 82 } 83 try { 84 new RealmChoiceCallback("prompt", wrongCh2, 0, false); 85 fail("IllegalArgumentException should be thrown for incorrect choices"); 86 } catch (IllegalArgumentException e) { 87 } 88 try { 89 new RealmChoiceCallback("prompt", choices, -1, true); 90 fail("IllegalArgumentException should be thrown for incorrect choices"); 91 } catch (IllegalArgumentException e) { 92 } 93 try { 94 new RealmChoiceCallback("prompt", choices, choices.length, true); 95 fail("IllegalArgumentException should be thrown for incorrect default index"); 96 } catch (IllegalArgumentException e) { 97 } 98 try { 99 new RealmChoiceCallback("prompt", choices, choices.length + 1, true); 100 fail("IllegalArgumentException should be thrown for incorrect default index"); 101 } catch (IllegalArgumentException e) { 102 } 103 } 104 105 /** 106 * Test for <code>RealmChoiceCallback(String prompt, String[] choices, 107 * int defaultChoice, boolean multiple) </code> constructor 108 * 109 * Assertion: creates RealmChoiceCallback object which does not allow 110 * multiple choices 111 */ 112 public void test02() { 113 RealmChoiceCallback rCCB; 114 for (int i = 0; i < prompts.length; i++) { 115 rCCB = new RealmChoiceCallback(prompts[i], choices, 0, false); 116 assertEquals("Incorrect prompt", rCCB.getPrompt(), prompts[i]); 117 String [] ch = rCCB.getChoices(); 118 assertEquals("Incorrect choices length", ch.length, choices.length); 119 for (int j = 0; j < ch.length; j++) { 120 assertEquals("Incorrect choice number: " + j, ch[j], choices[j]); 121 } 122 assertFalse("Incorrect multiple", rCCB.allowMultipleSelections()); 123 int [] ind = rCCB.getSelectedIndexes(); 124 assertNull("Incorrect selected indexes", ind); 125 ind = new int[3]; 126 try { 127 rCCB.setSelectedIndexes(ind); 128 fail("UnsupportedOperationException should be thrown for non-multiple callback"); 129 } catch (UnsupportedOperationException e) { 130 } 131 try { 132 rCCB.setSelectedIndexes(null); 133 fail("UnsupportedOperationException should be thrown for non-multiple callback"); 134 } catch (UnsupportedOperationException e) { 135 } 136 for (int j = 0; j < indexes.length; j++) { 137 rCCB.setSelectedIndex(indexes[j]); 138 ind = rCCB.getSelectedIndexes(); 139 assertEquals("Incorrect index length", ind.length, 1); 140 assertEquals("Incorrect index", ind[0], indexes[j]); 141 } 142 } 143 } 144 145 /** 146 * Test for <code>RealmChoiceCallback(String prompt, String[] choices, 147 * int defaultChoice, boolean multiple) </code> constructor 148 * 149 * Assertion: creates RealmChoiceCallback object which allows 150 * multiple choices 151 */ 152 public void test03() { 153 RealmChoiceCallback rCCB; 154 for (int i = 0; i < prompts.length; i++) { 155 rCCB = new RealmChoiceCallback(prompts[i], choices, 0, true); 156 assertEquals("Incorrect prompt", rCCB.getPrompt(), prompts[i]); 157 String[] ch = rCCB.getChoices(); 158 assertEquals("Incorrect choices length", ch.length, choices.length); 159 for (int j = 0; j < ch.length; j++) { 160 assertEquals("Incorrect choice number: " + j, ch[j], choices[j]); 161 } 162 assertTrue("Incorrect multiple", rCCB.allowMultipleSelections()); 163 int[] ind = rCCB.getSelectedIndexes(); 164 assertNull("Incorrect selected indexes", ind); 165 rCCB.setSelectedIndexes(indexes); 166 ind = rCCB.getSelectedIndexes(); 167 assertEquals("Incorrect index length", ind.length, indexes.length); 168 for (int j = 0; j < indexes.length; j++) { 169 assertEquals("Incorrect index number: " + Integer.toString(j), 170 ind[j], indexes[j]); 171 } 172 for (int j = indexes.length - 1; j >= 0; j--) { 173 rCCB.setSelectedIndex(indexes[j]); 174 ind = rCCB.getSelectedIndexes(); 175 assertEquals("Incorrect index length", ind.length, 1); 176 assertEquals("Incorrect index", ind[0], indexes[j]); 177 } 178 rCCB.setSelectedIndexes(indexes); 179 ind = rCCB.getSelectedIndexes(); 180 assertEquals("Incorrect index length", ind.length, indexes.length); 181 for (int j = 0; j < indexes.length; j++) { 182 assertEquals("Incorrect index number: " + Integer.toString(j), 183 ind[j], indexes[j]); 184 } 185 rCCB.setSelectedIndexes(null); 186 assertNull("Incorrect indexes array", rCCB.getSelectedIndexes()); 187 } 188 } 189} 190