Source Code Cross Referenced for DefaultListModelTest.java in  » Apache-Harmony-Java-SE » javax-package » javax » swing » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » javax package » javax.swing 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:        /**
018:         * @author Anton Avtamonov
019:         * @version $Revision$
020:         */package javax.swing;
021:
022:        import java.util.Enumeration;
023:        import java.util.NoSuchElementException;
024:        import java.util.Vector;
025:        import javax.swing.event.ListDataEvent;
026:        import javax.swing.event.ListDataListener;
027:
028:        public class DefaultListModelTest extends SwingTestCase {
029:            private DefaultListModel model;
030:
031:            private TestListener listener;
032:
033:            public DefaultListModelTest(final String name) {
034:                super (name);
035:            }
036:
037:            @Override
038:            protected void setUp() throws Exception {
039:                model = new DefaultListModel();
040:                listener = new TestListener();
041:                model.addListDataListener(listener);
042:            }
043:
044:            @Override
045:            protected void tearDown() throws Exception {
046:                model = null;
047:                listener = null;
048:            }
049:
050:            public void testAddInsertElementAt() throws Exception {
051:                Object element1 = new Object();
052:                model.add(0, element1);
053:                assertEquals(element1, model.get(0));
054:                checkListDataEvent(ListDataEvent.INTERVAL_ADDED, 0, 0);
055:                listener.reset();
056:                Object element2 = new Object();
057:                model.insertElementAt(element2, 1);
058:                assertEquals(element2, model.get(1));
059:                checkListDataEvent(ListDataEvent.INTERVAL_ADDED, 1, 1);
060:                listener.reset();
061:                Object element3 = new Object();
062:                model.add(1, element3);
063:                assertEquals(element3, model.get(1));
064:                checkListDataEvent(ListDataEvent.INTERVAL_ADDED, 1, 1);
065:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
066:                    @Override
067:                    public void exceptionalAction() {
068:                        model.add(-1, "any");
069:                    }
070:                });
071:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
072:                    @Override
073:                    public void exceptionalAction() {
074:                        model.insertElementAt("any", -1);
075:                    }
076:                });
077:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
078:                    @Override
079:                    public void exceptionalAction() {
080:                        model.add(4, "any");
081:                    }
082:                });
083:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
084:                    @Override
085:                    public void exceptionalAction() {
086:                        model.insertElementAt("any", 4);
087:                    }
088:                });
089:            }
090:
091:            public void testAddElement() throws Exception {
092:                Object element1 = new Object();
093:                model.addElement(element1);
094:                checkListDataEvent(ListDataEvent.INTERVAL_ADDED, 0, 0);
095:                listener.reset();
096:                Object element2 = new Object();
097:                model.addElement(element2);
098:                checkListDataEvent(ListDataEvent.INTERVAL_ADDED, 1, 1);
099:                assertEquals(element1, model.get(0));
100:                assertEquals(element2, model.get(1));
101:            }
102:
103:            public void testCapacity() throws Exception {
104:                assertEquals(new Vector<Object>().capacity(), model.capacity());
105:            }
106:
107:            public void testClear() throws Exception {
108:                Object element1 = new Object();
109:                model.addElement(element1);
110:                Object element2 = new Object();
111:                model.addElement(element2);
112:                assertEquals(2, model.getSize());
113:                listener.reset();
114:                model.clear();
115:                checkListDataEvent(ListDataEvent.INTERVAL_REMOVED, 0, 1);
116:                assertEquals(0, model.getSize());
117:                listener.reset();
118:                model.clear();
119:                assertNull(listener.getEvent());
120:                assertEquals(0, model.getSize());
121:                model.addElement(element1);
122:                model.addElement(element2);
123:                assertEquals(2, model.getSize());
124:                listener.reset();
125:                model.removeAllElements();
126:                checkListDataEvent(ListDataEvent.INTERVAL_REMOVED, 0, 1);
127:                assertEquals(0, model.getSize());
128:            }
129:
130:            public void testContains() throws Exception {
131:                Object element1 = new Object();
132:                model.addElement(element1);
133:                Object element2 = new Object();
134:                model.addElement(element2);
135:                assertTrue(model.contains(element1));
136:                assertTrue(model.contains(element2));
137:                assertFalse(model.contains("any"));
138:            }
139:
140:            public void testCopyInfo() throws Exception {
141:                Object[] copied = new Object[2];
142:                model.copyInto(copied);
143:                assertNotNull(copied);
144:                assertNull(copied[0]);
145:                assertNull(copied[1]);
146:                Object element1 = new Object();
147:                model.addElement(element1);
148:                Object element2 = new Object();
149:                model.addElement(element2);
150:                model.copyInto(copied);
151:                assertEquals(element1, copied[0]);
152:                assertEquals(element2, copied[1]);
153:            }
154:
155:            public void testElementAt() throws Exception {
156:                Object element1 = new Object();
157:                model.addElement(element1);
158:                Object element2 = new Object();
159:                model.addElement(element2);
160:                assertEquals(element1, model.elementAt(0));
161:                assertEquals(element2, model.elementAt(1));
162:            }
163:
164:            public void testElements() throws Exception {
165:                assertFalse(model.elements().hasMoreElements());
166:                Object element = new Object();
167:                model.addElement(element);
168:                Enumeration<?> e = model.elements();
169:                assertTrue(e.hasMoreElements());
170:                assertEquals(element, e.nextElement());
171:                assertFalse(e.hasMoreElements());
172:            }
173:
174:            public void testEnsureCapacity() throws Exception {
175:                model.ensureCapacity(500);
176:                assertEquals(500, model.capacity());
177:                model.ensureCapacity(1000);
178:                assertEquals(1000, model.capacity());
179:                model.ensureCapacity(800);
180:                assertEquals(1000, model.capacity());
181:            }
182:
183:            public void testFirstElement() throws Exception {
184:                testExceptionalCase(new ExceptionalCase() {
185:                    @Override
186:                    public void exceptionalAction() {
187:                        model.firstElement();
188:                    }
189:
190:                    @Override
191:                    public Class<?> expectedExceptionClass() {
192:                        return NoSuchElementException.class;
193:                    }
194:                });
195:                Object element1 = new Object();
196:                model.addElement(element1);
197:                Object element2 = new Object();
198:                model.addElement(element2);
199:                assertEquals(element1, model.firstElement());
200:                Object element0 = new Object();
201:                model.add(0, element0);
202:                assertEquals(element0, model.firstElement());
203:            }
204:
205:            public void testGetGetElementAt() throws Exception {
206:                Object element1 = new Object();
207:                model.addElement(element1);
208:                Object element2 = new Object();
209:                model.addElement(element2);
210:                assertEquals(element1, model.get(0));
211:                assertEquals(element1, model.getElementAt(0));
212:                assertEquals(element2, model.get(1));
213:                assertEquals(element2, model.getElementAt(1));
214:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
215:                    @Override
216:                    public void exceptionalAction() {
217:                        model.get(-1);
218:                    }
219:                });
220:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
221:                    @Override
222:                    public void exceptionalAction() {
223:                        model.getElementAt(-1);
224:                    }
225:                });
226:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
227:                    @Override
228:                    public void exceptionalAction() {
229:                        model.get(2);
230:                    }
231:                });
232:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
233:                    @Override
234:                    public void exceptionalAction() {
235:                        model.getElementAt(2);
236:                    }
237:                });
238:            }
239:
240:            public void testSizeGetSize() throws Exception {
241:                assertEquals(0, model.size());
242:                assertEquals(0, model.getSize());
243:                model.addElement("any");
244:                assertEquals(1, model.size());
245:                assertEquals(1, model.getSize());
246:                model.addElement("any");
247:                assertEquals(2, model.size());
248:                assertEquals(2, model.getSize());
249:            }
250:
251:            public void testIndexOf() throws Exception {
252:                assertEquals(-1, model.indexOf("1"));
253:                model.addElement("1");
254:                model.addElement("2");
255:                model.addElement("3");
256:                model.addElement("2");
257:                assertEquals(0, model.indexOf("1"));
258:                assertEquals(1, model.indexOf("2"));
259:                assertEquals(2, model.indexOf("3"));
260:                assertEquals(3, model.indexOf("2", 2));
261:                assertEquals(-1, model.indexOf("1", 2));
262:                assertEquals(-1, model.indexOf("1", 20));
263:                testExceptionalCase(new ExceptionalCase() {
264:                    @Override
265:                    public void exceptionalAction() {
266:                        model.indexOf("1", -1);
267:                    }
268:
269:                    @Override
270:                    public Class<?> expectedExceptionClass() {
271:                        return IndexOutOfBoundsException.class;
272:                    }
273:                });
274:            }
275:
276:            public void testIsEmpty() throws Exception {
277:                assertTrue(model.isEmpty());
278:                model.addElement("1");
279:                assertFalse(model.isEmpty());
280:            }
281:
282:            public void testLastElement() throws Exception {
283:                testExceptionalCase(new ExceptionalCase() {
284:                    @Override
285:                    public void exceptionalAction() {
286:                        model.lastElement();
287:                    }
288:
289:                    @Override
290:                    public Class<?> expectedExceptionClass() {
291:                        return NoSuchElementException.class;
292:                    }
293:                });
294:                Object element1 = new Object();
295:                model.addElement(element1);
296:                Object element2 = new Object();
297:                model.addElement(element2);
298:                assertEquals(element2, model.lastElement());
299:                Object element3 = new Object();
300:                model.add(2, element3);
301:                assertEquals(element3, model.lastElement());
302:            }
303:
304:            public void testLastIndexOf() throws Exception {
305:                assertEquals(-1, model.indexOf("1"));
306:                model.addElement("1");
307:                model.addElement("2");
308:                model.addElement("3");
309:                model.addElement("2");
310:                model.addElement("1");
311:                model.addElement("0");
312:                assertEquals(5, model.lastIndexOf("0"));
313:                assertEquals(4, model.lastIndexOf("1"));
314:                assertEquals(3, model.lastIndexOf("2"));
315:                assertEquals(2, model.lastIndexOf("3"));
316:                assertEquals(3, model.lastIndexOf("2", 3));
317:                assertEquals(-1, model.lastIndexOf("0", 1));
318:                assertEquals(-1, model.lastIndexOf("0", -1));
319:                testExceptionalCase(new ExceptionalCase() {
320:                    @Override
321:                    public void exceptionalAction() {
322:                        model.lastIndexOf("1", 100);
323:                    }
324:
325:                    @Override
326:                    public Class<?> expectedExceptionClass() {
327:                        return IndexOutOfBoundsException.class;
328:                    }
329:                });
330:            }
331:
332:            public void testRemove() throws Exception {
333:                Object element = new Object();
334:                model.addElement(element);
335:                model.addElement(element);
336:                assertEquals(2, model.size());
337:                listener.reset();
338:                assertEquals(element, model.remove(0));
339:                checkListDataEvent(ListDataEvent.INTERVAL_REMOVED, 0, 0);
340:                assertEquals(1, model.size());
341:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
342:                    @Override
343:                    public void exceptionalAction() {
344:                        model.remove(4);
345:                    }
346:                });
347:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
348:                    @Override
349:                    public void exceptionalAction() {
350:                        model.remove(-1);
351:                    }
352:                });
353:            }
354:
355:            public void testRemoveElement() throws Exception {
356:                assertFalse(model.removeElement("1"));
357:                model.addElement("1");
358:                model.addElement("2");
359:                listener.reset();
360:                assertTrue(model.removeElement("2"));
361:                checkListDataEvent(ListDataEvent.INTERVAL_REMOVED, 1, 1);
362:                listener.reset();
363:                assertFalse(model.removeElement("2"));
364:                assertNull(listener.getEvent());
365:                assertEquals(1, model.size());
366:            }
367:
368:            public void testRemoveElementAt() throws Exception {
369:                Object element = new Object();
370:                model.addElement(element);
371:                assertEquals(1, model.size());
372:                model.removeElementAt(0);
373:                checkListDataEvent(ListDataEvent.INTERVAL_REMOVED, 0, 0);
374:                assertEquals(0, model.size());
375:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
376:                    @Override
377:                    public void exceptionalAction() {
378:                        model.removeElementAt(4);
379:                    }
380:                });
381:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
382:                    @Override
383:                    public void exceptionalAction() {
384:                        model.removeElementAt(-1);
385:                    }
386:                });
387:            }
388:
389:            public void testRemoveRange() throws Exception {
390:                model.addElement("1");
391:                model.addElement("2");
392:                model.addElement("3");
393:                model.removeRange(1, 2);
394:                checkListDataEvent(ListDataEvent.INTERVAL_REMOVED, 1, 2);
395:                assertEquals(1, model.size());
396:                assertEquals("1", model.get(0));
397:                listener.reset();
398:                model.addElement("2");
399:                model.addElement("3");
400:                model.removeRange(0, 1);
401:                checkListDataEvent(ListDataEvent.INTERVAL_REMOVED, 0, 1);
402:                assertEquals(1, model.size());
403:                assertEquals("3", model.get(0));
404:                listener.reset();
405:                model.addElement("5");
406:                model.addElement("6");
407:                model.addElement("7");
408:                model.removeRange(0, 3);
409:                checkListDataEvent(ListDataEvent.INTERVAL_REMOVED, 0, 3);
410:                assertEquals(0, model.size());
411:                listener.reset();
412:                model.addElement("1");
413:                model.addElement("2");
414:                model.addElement("3");
415:                model.removeRange(2, 2);
416:                checkListDataEvent(ListDataEvent.INTERVAL_REMOVED, 2, 2);
417:                assertEquals(2, model.size());
418:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
419:                    @Override
420:                    public void exceptionalAction() {
421:                        model.removeRange(-1, 1);
422:                    }
423:                });
424:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
425:                    @Override
426:                    public void exceptionalAction() {
427:                        model.removeRange(1, 2);
428:                    }
429:                });
430:            }
431:
432:            public void testRemoveRange_1() {
433:                try {
434:                    model.removeRange(0, -1);
435:                    fail("IllegalArgumentException have to throw");
436:                } catch (IllegalArgumentException iae) {
437:                }
438:            }
439:
440:            public void testSet() throws Exception {
441:                model.addElement("1");
442:                model.addElement("2");
443:                model.addElement("3");
444:                assertEquals("2", model.get(1));
445:                listener.reset();
446:                model.set(1, "another1");
447:                checkListDataEvent(ListDataEvent.CONTENTS_CHANGED, 1, 1);
448:                assertEquals("another1", model.get(1));
449:                listener.reset();
450:                model.setElementAt("another2", 2);
451:                checkListDataEvent(ListDataEvent.CONTENTS_CHANGED, 2, 2);
452:                assertEquals("another2", model.get(2));
453:                assertEquals(3, model.size());
454:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
455:                    @Override
456:                    public void exceptionalAction() {
457:                        model.set(-1, "any");
458:                    }
459:                });
460:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
461:                    @Override
462:                    public void exceptionalAction() {
463:                        model.set(3, "any");
464:                    }
465:                });
466:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
467:                    @Override
468:                    public void exceptionalAction() {
469:                        model.setElementAt("any", -1);
470:                    }
471:                });
472:                testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
473:                    @Override
474:                    public void exceptionalAction() {
475:                        model.setElementAt("any", 3);
476:                    }
477:                });
478:            }
479:
480:            public void testSetSize() throws Exception {
481:                model.setSize(1000);
482:                assertEquals(1000, model.getSize());
483:                model.setSize(500);
484:                assertEquals(500, model.getSize());
485:            }
486:
487:            public void testToArray() throws Exception {
488:                assertEquals(0, model.toArray().length);
489:                model.addElement("1");
490:                model.addElement("2");
491:                Object[] array = model.toArray();
492:                assertEquals(2, array.length);
493:                assertEquals("1", array[0]);
494:                assertEquals("2", array[1]);
495:            }
496:
497:            public void testTrimToSize() throws Exception {
498:                model.ensureCapacity(1000);
499:                assertEquals(1000, model.capacity());
500:                model.addElement("1");
501:                model.trimToSize();
502:                assertEquals(1, model.capacity());
503:            }
504:
505:            public void testToString() throws Exception {
506:                model.addElement("1");
507:                model.addElement("2");
508:                assertEquals("[1, 2]", model.toString());
509:            }
510:
511:            private class TestListener implements  ListDataListener {
512:                private ListDataEvent event;
513:
514:                private int eventType = -1;
515:
516:                public void contentsChanged(final ListDataEvent e) {
517:                    event = e;
518:                    eventType = ListDataEvent.CONTENTS_CHANGED;
519:                }
520:
521:                public void intervalAdded(final ListDataEvent e) {
522:                    event = e;
523:                    eventType = ListDataEvent.INTERVAL_ADDED;
524:                }
525:
526:                public void intervalRemoved(final ListDataEvent e) {
527:                    event = e;
528:                    eventType = ListDataEvent.INTERVAL_REMOVED;
529:                }
530:
531:                public ListDataEvent getEvent() {
532:                    return event;
533:                }
534:
535:                public int getType() {
536:                    return eventType;
537:                }
538:
539:                public void reset() {
540:                    event = null;
541:                    eventType = -1;
542:                }
543:            }
544:
545:            private void checkListDataEvent(final int expectedEventType,
546:                    final int expectedIndex0, final int expectedIndex1) {
547:                assertNotNull(listener.getEvent());
548:                assertEquals(model, listener.getEvent().getSource());
549:                assertEquals(expectedEventType, listener.getEvent().getType());
550:                assertEquals(expectedIndex0, listener.getEvent().getIndex0());
551:                assertEquals(expectedIndex1, listener.getEvent().getIndex1());
552:            }
553:
554:            private abstract class ArrayIndexOutOfBoundsCase extends
555:                    ExceptionalCase {
556:                @Override
557:                public Class<?> expectedExceptionClass() {
558:                    return ArrayIndexOutOfBoundsException.class;
559:                }
560:            }
561:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.