View Javadoc
1 /* 2 * Scope: a generic MVC framework. 3 * Copyright (c) 2000-2002, The Scope team 4 * All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Neither the name "Scope" nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * 36 * $Id: TestSComboBox.java,v 1.10 2002/09/12 19:09:37 ludovicc Exp $ 37 */ 38 package test.view.swing; 39 40 41 import java.util.ArrayList; 42 import java.util.Arrays; 43 import javax.swing.JTextField; 44 import org.apache.commons.logging.Log; 45 import org.apache.commons.logging.LogFactory; 46 import junit.framework.TestCase; 47 import junit.swingui.TestRunner; 48 import org.scopemvc.core.Selector; 49 import org.scopemvc.util.convertor.StringStringConvertor; 50 import org.scopemvc.view.swing.SComboBox; 51 import org.scopemvc.view.swing.SComboBoxEditor; 52 import org.scopemvc.view.swing.SComboBoxModel; 53 import org.scopemvc.view.swing.SListCellRenderer; 54 import org.scopemvc.view.swing.SPanel; 55 56 /*** 57 * <P> 58 * 59 * </P> 60 * 61 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net>;Steve Meyfroidt</A> 62 * @created 10 September 2002 63 * @version $Revision: 1.10 $ $Date: 2002/09/12 19:09:37 $ 64 */ 65 public final class TestSComboBox extends TestCase { 66 67 private static final Log LOG = LogFactory.getLog(TestSComboBox.class); 68 69 private SComboBox combo; 70 private SwingDummyController controller; 71 private SPanel view; 72 private SwingDummyModel model; 73 74 75 /*** 76 * Constructor for the TestSComboBox object 77 * 78 * @param inName Name of the test 79 */ 80 public TestSComboBox(String inName) { 81 super(inName); 82 } 83 84 85 /*** 86 * A unit test for JUnit 87 * 88 * @throws Exception Any abnormal exception 89 */ 90 public void testCreateModel() throws Exception { 91 assertTrue(combo.getModel() instanceof SComboBoxModel); 92 } 93 94 95 /*** 96 * A unit test for JUnit 97 * 98 * @throws Exception Any abnormal exception 99 */ 100 public void testCreateRenderer() throws Exception { 101 assertTrue(combo.getRenderer() instanceof SListCellRenderer); 102 } 103 104 105 /*** 106 * A unit test for JUnit 107 * 108 * @throws Exception Any abnormal exception 109 */ 110 public void testConveniences() throws Exception { 111 Selector s = Selector.fromString("xyz"); 112 113 assertNull(((SComboBoxModel) combo.getModel()).getSelectionSelector()); 114 combo.setSelectionSelector(s); 115 assertSame(s, ((SComboBoxModel) combo.getModel()).getSelectionSelector()); 116 117 assertNull(((SListCellRenderer) combo.getRenderer()).getTextSelector()); 118 combo.setRendererSelector(s); 119 assertSame(s, ((SListCellRenderer) combo.getRenderer()).getTextSelector()); 120 121 assertNull(((SListCellRenderer) combo.getRenderer()).getIconSelector()); 122 combo.setRendererIconSelector(s); 123 assertSame(s, ((SListCellRenderer) combo.getRenderer()).getIconSelector()); 124 } 125 126 127 /*** 128 * A unit test for JUnit 129 * 130 * @throws Exception Any abnormal exception 131 */ 132 public void testConveniences2() throws Exception { 133 Selector s = Selector.fromString("xyz"); 134 135 assertNull(((SComboBoxModel) combo.getModel()).getSelectionSelector()); 136 combo.setSelectionSelectorString("xyz"); 137 assertEquals(s, ((SComboBoxModel) combo.getModel()).getSelectionSelector()); 138 139 assertNull(((SListCellRenderer) combo.getRenderer()).getTextSelector()); 140 combo.setRendererSelectorString("xyz"); 141 assertEquals(s, ((SListCellRenderer) combo.getRenderer()).getTextSelector()); 142 143 assertNull(((SListCellRenderer) combo.getRenderer()).getIconSelector()); 144 combo.setRendererIconSelectorString("xyz"); 145 assertEquals(s, ((SListCellRenderer) combo.getRenderer()).getIconSelector()); 146 } 147 148 149 /*** 150 * A unit test for JUnit 151 * 152 * @throws Exception Any abnormal exception 153 */ 154 public void testUnbound() throws Exception { 155 SuiteViewSwing.waitForAWT(); 156 assertTrue(!combo.isEnabled()); 157 assertNull(combo.getBoundModel()); 158 159 combo.setSelector(Selector.fromString("stringNonIndexedProperty")); 160 SuiteViewSwing.waitForAWT(); 161 assertEquals(Selector.fromString("stringNonIndexedProperty"), ((SComboBoxModel) combo.getModel()).getSelector()); 162 assertTrue(!combo.isEnabled()); 163 } 164 165 166 /*** 167 * A unit test for JUnit 168 * 169 * @throws Exception Any abnormal exception 170 */ 171 public void testBind1() throws Exception { 172 combo.setSelector(Selector.fromString("stringNonIndexedProperty")); 173 assertEquals(Selector.fromString("stringNonIndexedProperty"), ((SComboBoxModel) combo.getModel()).getSelector()); 174 175 controller.setModel(model); 176 SuiteViewSwing.waitForAWT(); 177 178 assertSame(view.getBoundModel(), model); 179 assertSame(model, combo.getBoundModel()); 180 assertTrue(!combo.isEnabled()); 181 assertSame(model.getStringNonIndexedProperty(), ((SComboBoxModel) combo.getModel()).getShownModel()); 182 } 183 184 185 /*** 186 * A unit test for JUnit 187 * 188 * @throws Exception Any abnormal exception 189 */ 190 public void testBind2() throws Exception { 191 combo.setSelector(Selector.fromString("stringNonIndexedProperty")); 192 controller.setModel(model); 193 SuiteViewSwing.waitForAWT(); 194 assertTrue(!combo.isEnabled()); 195 196 ArrayList newList = new ArrayList(java.util.Arrays.asList(new String[]{"1", "2", "3"})); 197 model.setStringNonIndexedProperty(newList); 198 SuiteViewSwing.waitForAWT(); 199 200 assertSame(view.getBoundModel(), model); 201 assertSame(model, combo.getBoundModel()); 202 assertSame(newList, ((SComboBoxModel) combo.getModel()).getShownModel()); 203 } 204 205 206 /*** 207 * A unit test for JUnit 208 * 209 * @throws Exception Any abnormal exception 210 */ 211 public void testValidation() throws Exception { 212 combo.setSelector(Selector.fromString("stringNonIndexedProperty")); 213 combo.setSelectionSelector(Selector.fromString("stringProperty")); 214 model.setStringProperty(null); 215 controller.setModel(model); 216 SuiteViewSwing.waitForAWT(); 217 218 combo.setSelectedItem("Illegal"); 219 assertTrue(combo.isEnabled()); 220 SuiteViewSwing.waitForAWT(); 221 assertEquals(null, combo.getSelectedItem()); 222 assertTrue("getStringProperty <" + model.getStringProperty() + ">", model.getStringProperty() == null); 223 } 224 225 226 /*** 227 * A unit test for JUnit 228 * 229 * @throws Exception Any abnormal exception 230 */ 231 public void testKeepSelection() throws Exception { 232 combo.setSelector(Selector.fromString("stringNonIndexedProperty")); 233 combo.setSelectionSelector(Selector.fromString("stringProperty")); 234 controller.setModel(model); 235 SuiteViewSwing.waitForAWT(); 236 assertTrue(combo.isEnabled()); 237 238 combo.setSelectedItem(model.getStringNonIndexedProperty(1)); 239 java.util.ArrayList newList = (ArrayList) model.getStringNonIndexedProperty().clone(); 240 model.setStringNonIndexedProperty(newList); 241 SuiteViewSwing.waitForAWT(); 242 assertEquals(model.getStringNonIndexedProperty(1), combo.getSelectedItem()); 243 } 244 245 246 /*** 247 * A unit test for JUnit 248 * 249 * @throws Exception Any abnormal exception 250 */ 251 public void testEditable() throws Exception { 252 combo.setSelector(Selector.fromString("stringNonIndexedProperty")); 253 combo.setSelectionSelector(Selector.fromString("stringProperty")); 254 combo.setEditable(true); 255 controller.setModel(model); 256 SuiteViewSwing.waitForAWT(); 257 assertTrue(combo.isEnabled()); 258 259 // change the selected item with an item not in the list 260 combo.setSelectedItem("newvalue"); 261 SuiteViewSwing.waitForAWT(); 262 assertEquals("newvalue", combo.getSelectedItem()); 263 assertEquals("newvalue", model.getStringProperty()); 264 265 // change the list of items but keep the previously selected item 266 Object oldValue = model.getStringNonIndexedProperty(1); 267 combo.setSelectedItem(model.getStringNonIndexedProperty(1)); 268 java.util.ArrayList newList = new ArrayList(); 269 newList.add("x1"); 270 newList.add("x2"); 271 model.setStringNonIndexedProperty(newList); 272 SuiteViewSwing.waitForAWT(); 273 assertEquals(oldValue, combo.getSelectedItem()); 274 } 275 276 277 /*** 278 * A unit test for JUnit 279 * 280 * @throws Exception Any abnormal exception 281 */ 282 public void testNonEditable() throws Exception { 283 combo.setSelector(Selector.fromString("stringNonIndexedProperty")); 284 combo.setSelectionSelector(Selector.fromString("stringProperty")); 285 controller.setModel(model); 286 SuiteViewSwing.waitForAWT(); 287 assertTrue(combo.isEnabled()); 288 289 combo.setSelectedItem(model.getStringNonIndexedProperty(1)); 290 java.util.ArrayList newList = new ArrayList(); 291 newList.add("x1"); 292 newList.add("x2"); 293 model.setStringNonIndexedProperty(newList); 294 SuiteViewSwing.waitForAWT(); 295 assertEquals(model.getStringNonIndexedProperty(0), combo.getSelectedItem()); 296 } 297 298 299 /*** 300 * A unit test for JUnit 301 * 302 * @throws Exception Any abnormal exception 303 * @todo Implement this test 304 */ 305 public void testControlIssue() throws Exception { 306 // ***** How to implement? 307 } 308 309 310 /*** 311 * A unit test for JUnit 312 * 313 * @throws Exception Any abnormal exception 314 */ 315 public void testBindNoMCE() throws Exception { 316 combo.setSelector(Selector.fromString("stringNonIndexedProperty")); 317 assertEquals(Selector.fromString("stringNonIndexedProperty"), ((SComboBoxModel) combo.getModel()).getSelector()); 318 319 SwingDummyModelNoMCE model = new SwingDummyModelNoMCE(); 320 321 controller.setModel(model); 322 SuiteViewSwing.waitForAWT(); 323 assertSame(view.getBoundModel(), model); 324 assertSame(model, combo.getBoundModel()); 325 assertSame(model.getStringNonIndexedProperty(), ((SComboBoxModel) combo.getModel()).getShownModel()); 326 } 327 328 329 /*** 330 * A unit test for JUnit 331 * 332 * @throws Exception Any abnormal exception 333 */ 334 public void testSetStringConvertor() throws Exception { 335 StringStringConvertor sc = new StringStringConvertor(); 336 sc.setNullAsString("xyz"); 337 combo.setStringConvertor(sc); 338 SComboBoxEditor e = (SComboBoxEditor) combo.getEditor(); 339 e.setItem(null); 340 SuiteViewSwing.waitForAWT(); 341 assertEquals("xyz", ((JTextField) combo.getEditor().getEditorComponent()).getText()); 342 } 343 344 345 /*** 346 * The JUnit setup method 347 * 348 * @throws Exception Any abnormal exception 349 */ 350 protected void setUp() throws Exception { 351 352 combo = new SComboBox(); 353 354 view = new SPanel(); 355 view.add(combo); 356 357 controller = new SwingDummyController(); 358 controller.setView(view); 359 controller.startup(); 360 // does showView() 361 362 model = new SwingDummyModel(); 363 model.setStringProperty(model.getStringNonIndexedProperty(0)); 364 } 365 366 367 /*** 368 * The teardown method for JUnit 369 */ 370 protected void tearDown() { 371 controller.shutdown(); 372 } 373 }

This page was automatically generated by Maven