Source Code Cross Referenced for PanelComponentSearchTest.java in  » Testing » UISpec4J » org » uispec4j » finder » 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 » Testing » UISpec4J » org.uispec4j.finder 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.uispec4j.finder;
002:
003:        import org.uispec4j.Button;
004:        import org.uispec4j.*;
005:        import org.uispec4j.Panel;
006:        import org.uispec4j.utils.ComponentUtils;
007:        import org.uispec4j.utils.UIComponentAnalyzer;
008:        import org.uispec4j.utils.UIComponentFactory;
009:        import org.uispec4j.xml.XmlAssert;
010:
011:        import javax.swing.*;
012:        import javax.swing.text.JTextComponent;
013:        import java.awt.*;
014:        import java.lang.reflect.Constructor;
015:        import java.util.HashMap;
016:        import java.util.Map;
017:
018:        public class PanelComponentSearchTest extends
019:                PanelComponentFinderTestCase {
020:            private static final Class[] COMPONENT_CLASSES = new Class[] {
021:                    Button.class, CheckBox.class, ComboBox.class,
022:                    org.uispec4j.Desktop.class, ListBox.class, TabGroup.class,
023:                    TextBox.class, PasswordField.class, Panel.class,
024:                    Spinner.class, Slider.class, ProgressBar.class,
025:                    RadioButton.class };
026:            private Map componentAccessors;
027:
028:            protected void setUp() throws Exception {
029:                super .setUp();
030:                componentAccessors = createAccessors(panel);
031:            }
032:
033:            public void testGetComponentTypeName() {
034:                assertEquals("panel", UIComponentFactory.createUIComponent(
035:                        new JPanel()).getDescriptionTypeName());
036:            }
037:
038:            public void testGetComponentWithText() throws Exception {
039:                checkGetComponentWithText(JButton.class, Button.TYPE_NAME);
040:                checkGetComponentWithText(JCheckBox.class, CheckBox.TYPE_NAME);
041:                checkGetComponentWithText(JLabel.class, TextBox.TYPE_NAME);
042:                checkGetComponentWithText(JRadioButton.class,
043:                        RadioButton.TYPE_NAME);
044:            }
045:
046:            public void testGetComponentWithClassAndName() throws Exception {
047:                for (int i = 0; i < COMPONENT_CLASSES.length; i++) {
048:                    checkGetComponentWithClassAndName(COMPONENT_CLASSES[i]);
049:                }
050:            }
051:
052:            public void testGetComponentWithClass() throws Exception {
053:                for (int i = 0; i < COMPONENT_CLASSES.length; i++) {
054:                    if (COMPONENT_CLASSES[i] != Panel.class) {
055:                        checkGetComponentWithClass(COMPONENT_CLASSES[i]);
056:                    }
057:                }
058:            }
059:
060:            public void testGetComponentWithCustomMatcher() throws Exception {
061:                for (int i = 0; i < COMPONENT_CLASSES.length; i++) {
062:                    if (COMPONENT_CLASSES[i] != Panel.class) {
063:                        checkGetComponentWithCustomMatcher(COMPONENT_CLASSES[i]);
064:                    }
065:                }
066:            }
067:
068:            public void testComponentNotFoundErrors() throws Exception {
069:                for (int i = 0; i < COMPONENT_CLASSES.length; i++) {
070:                    checkComponentNotFound(UIComponentAnalyzer
071:                            .getTypeName(COMPONENT_CLASSES[i]));
072:                }
073:            }
074:
075:            public void testComponentTypeMismatch() throws Exception {
076:                checkComponentTypeMismatch("name1", JList.class,
077:                        Button.TYPE_NAME);
078:                checkComponentTypeMismatch("name2", JList.class,
079:                        TextBox.TYPE_NAME);
080:                checkComponentTypeMismatch("name3", JList.class,
081:                        CheckBox.TYPE_NAME);
082:                checkComponentTypeMismatch("name5", JList.class,
083:                        TextBox.TYPE_NAME);
084:                checkComponentTypeMismatch("name6", JList.class,
085:                        TabGroup.TYPE_NAME);
086:                checkComponentTypeMismatch("name7", JList.class,
087:                        ComboBox.TYPE_NAME);
088:                checkComponentTypeMismatch("name8", JList.class,
089:                        RadioButton.TYPE_NAME);
090:            }
091:
092:            public void testPasswordField() throws Exception {
093:                JPasswordField jPasswordField = new JPasswordField();
094:                jPanel.add(jPasswordField);
095:                PasswordField passwordField = panel.getPasswordField();
096:                assertSame(jPasswordField, passwordField.getAwtComponent());
097:            }
098:
099:            public void testSearchTraversalByClassExploresInDepthAllSubComponents()
100:                    throws Exception {
101:                JTextField innerTextField = new JTextField("hello");
102:                JPanel innerPanel = new JPanel();
103:                innerPanel.add(innerTextField);
104:                jPanel.add(innerPanel);
105:
106:                JTextField textField = new JTextField("world");
107:                jPanel.add(textField);
108:
109:                TypedComponentAccessor labelAccessor = getAccessor(TextBox.TYPE_NAME);
110:                try {
111:                    labelAccessor.getComponent();
112:                    fail();
113:                } catch (Exception e) {
114:                    assertEquals(Messages.computeAmbiguityMessage(new String[] {
115:                            "world", "hello" }, TextBox.TYPE_NAME, null), e
116:                            .getMessage());
117:                }
118:
119:                TestUtils.assertUIComponentRefersTo(textField, labelAccessor
120:                        .getComponent("world"));
121:                TestUtils.assertUIComponentRefersTo(innerTextField,
122:                        labelAccessor.getComponent("hello"));
123:            }
124:
125:            public void testSearchTraversalByNameExploresInDepthAllSubComponents()
126:                    throws Exception {
127:                JLabel innerLabel = new JLabel();
128:                innerLabel.setName("Hello Marc");
129:                JPanel innerPanel = new JPanel();
130:                innerPanel.add(innerLabel);
131:                jPanel.add(innerPanel);
132:
133:                JLabel label = new JLabel();
134:                addComponentToPanelWithName(label, "Hello Regis");
135:
136:                TypedComponentAccessor labelAccessor = getAccessor(TextBox.TYPE_NAME);
137:                try {
138:                    labelAccessor.getComponent("hello");
139:                    fail();
140:                } catch (Exception e) {
141:                    assertEquals(Messages.computeAmbiguityMessage(new String[] {
142:                            "Hello Regis", "Hello Marc" }, TextBox.TYPE_NAME,
143:                            "hello"), e.getMessage());
144:                }
145:                TestUtils.assertUIComponentRefersTo(innerLabel, labelAccessor
146:                        .getComponent("Hello marc"));
147:                TestUtils.assertUIComponentRefersTo(label, labelAccessor
148:                        .getComponent("Hello regis"));
149:            }
150:
151:            public void testSearchByNameStrategyIsFirstOnDisplayedName()
152:                    throws Exception {
153:                JLabel label = new JLabel("label toto");
154:
155:                JLabel labelWithInnerName = new JLabel("do not chose me");
156:                labelWithInnerName.setName("toto");
157:
158:                jPanel.add(label);
159:                jPanel.add(labelWithInnerName);
160:
161:                TypedComponentAccessor labelAccessor = getAccessor(TextBox.TYPE_NAME);
162:                TestUtils.assertUIComponentRefersTo(label, labelAccessor
163:                        .getComponent("toto"));
164:            }
165:
166:            public void testSearchWithinAComplexPanel() throws Exception {
167:                JPanel main = new JPanel();
168:                for (int i = 0; i < 10; i++) {
169:                    JPanel containedPanel = new JPanel();
170:                    containedPanel.add(new JButton("button" + i));
171:                    addWithScrollPane(containedPanel, main);
172:
173:                    JPanel topPanel = new JPanel();
174:                    JPanel bottomPanel = new JPanel();
175:                    JSplitPane split = new JSplitPane(
176:                            JSplitPane.VERTICAL_SPLIT, topPanel, bottomPanel);
177:                    JTable table = new JTable();
178:                    table.setName("table" + i);
179:                    addWithScrollPane(table, topPanel);
180:                    JTree tree = new JTree();
181:                    tree.setName("tree" + i);
182:                    addWithScrollPane(tree, topPanel);
183:                    bottomPanel.add(new JLabel("label" + i));
184:                    addWithScrollPane(split, main);
185:
186:                    for (int j = 0; j < 5; j++) {
187:                        JPanel sub = new JPanel();
188:                        sub.add(new JCheckBox("checkBox" + i + "." + j));
189:                        addWithScrollPane(sub, bottomPanel);
190:
191:                        JPanel subSub = new JPanel();
192:                        subSub.add(new JRadioButton("radio" + i + "." + j));
193:                        addWithScrollPane(subSub, sub);
194:                    }
195:                }
196:
197:                Panel mainPanel = new Panel(main);
198:                assertNotNull(mainPanel.getTextBox("label5"));
199:                assertNotNull(mainPanel.getButton("button7"));
200:                assertNotNull(mainPanel.getTable("table6"));
201:                assertNotNull(mainPanel.getTree("tree5"));
202:                assertNotNull(mainPanel.getCheckBox("checkBox3.4"));
203:                assertNotNull(mainPanel.getRadioButton("radio2.3"));
204:            }
205:
206:            public void testComponentNameAmbiguityException() throws Exception {
207:                jPanel.add(new JButton("myButton1"));
208:                jPanel.add(new JButton("myButton2"));
209:                checkComponentNameAmbiguity("button", Button.TYPE_NAME,
210:                        new String[] { "myButton1", "myButton2" });
211:
212:                jPanel.add(new JLabel("myLabel1"));
213:                jPanel.add(new JLabel("myLabel2"));
214:                checkComponentNameAmbiguity("label", TextBox.TYPE_NAME,
215:                        new String[] { "myLabel1", "myLabel2" });
216:
217:                jPanel.add(new JCheckBox("myCheckBox1"));
218:                jPanel.add(new JCheckBox("myCheckBox2"));
219:                checkComponentNameAmbiguity("myCheckBox", CheckBox.TYPE_NAME,
220:                        new String[] { "myCheckBox1", "myCheckBox2" });
221:
222:                createTwoComponentsWithSameName(JPanel.class, "myPanel");
223:                checkComponentNameAmbiguity("panel", Panel.TYPE_NAME,
224:                        new String[] { "myPanel", "myPanel" });
225:
226:                createTwoComponentsWithSameName(JTextField.class, "myText");
227:                checkComponentNameAmbiguity("text", TextBox.TYPE_NAME,
228:                        new String[] { "myText", "myText" });
229:
230:                createTwoComponentsWithSameName(JTabbedPane.class, "myTabbed");
231:                checkComponentNameAmbiguity("myTabbed", TabGroup.TYPE_NAME,
232:                        new String[] { "myTabbed", "myTabbed" });
233:
234:                createTwoComponentsWithSameName(JComboBox.class, "myCombo");
235:                checkComponentNameAmbiguity("myCombo", ComboBox.TYPE_NAME,
236:                        new String[] { "myCombo", "myCombo" });
237:            }
238:
239:            public void testAmbiguityExceptionIsDetectedBetweenLabelAndTextComponents()
240:                    throws Exception {
241:                JLabel jLabel = new JLabel("myText1");
242:                jPanel.add(jLabel);
243:                JTextField jTextField = new JTextField("myText2");
244:                jTextField.setText("myText2");
245:                jPanel.add(jTextField);
246:                checkComponentNameAmbiguity("text", TextBox.TYPE_NAME,
247:                        new String[] { "myText1", "myText2" });
248:
249:                panel = new Panel(jPanel);
250:                try {
251:                    panel.getTextBox();
252:                    fail();
253:                } catch (ComponentAmbiguityException e) {
254:                    assertEquals(Messages.computeAmbiguityMessage(new String[] {
255:                            "myText1", "myText2" }, TextBox.TYPE_NAME, null), e
256:                            .getMessage());
257:                }
258:            }
259:
260:            public void testComponentsWithSameNameAreFoundAccordingToTheirType()
261:                    throws Exception {
262:                JLabel label = new JLabel("test");
263:                JTable table = new JTable();
264:                table.setName("test");
265:
266:                jPanel.add(label);
267:                jPanel.add(table);
268:
269:                TestUtils.assertUIComponentRefersTo(label, panel
270:                        .getTextBox("test"));
271:                TestUtils.assertUIComponentRefersTo(table, panel
272:                        .getTable("test"));
273:            }
274:
275:            public void testComponentsWithSamePatternInNameAreFoundAccordingToTheirType()
276:                    throws Exception {
277:                JLabel label = new JLabel("another label");
278:                JTable table = new JTable();
279:                table.setName("another table");
280:
281:                jPanel.add(label);
282:                jPanel.add(table);
283:
284:                TestUtils.assertUIComponentRefersTo(label, panel
285:                        .getTextBox("another"));
286:                TestUtils.assertUIComponentRefersTo(table, panel
287:                        .getTable("another"));
288:            }
289:
290:            public void testComponentThatShouldBeUniqueInPanel()
291:                    throws Exception {
292:                checkComponentUnicityAmbiguity(JTable.class, Table.TYPE_NAME,
293:                        "myTable");
294:                checkComponentUnicityAmbiguity(JList.class, ListBox.TYPE_NAME,
295:                        "myList");
296:                checkComponentUnicityAmbiguity(JDesktopPane.class,
297:                        org.uispec4j.Desktop.TYPE_NAME, "myDesktop");
298:                checkComponentUnicityAmbiguity(JTextField.class,
299:                        TextBox.TYPE_NAME, "myText");
300:                checkComponentUnicityAmbiguity(JTree.class, Tree.TYPE_NAME,
301:                        "myTree");
302:            }
303:
304:            public void testComponentsAreFoundDeepInTheComponentsHierarchy()
305:                    throws Exception {
306:                JPanel newPanel = new JPanel();
307:                JButton button = new JButton("button");
308:                newPanel.add(button);
309:                jPanel.add(newPanel);
310:                TestUtils.assertUIComponentRefersTo(button, panel
311:                        .getButton("button"));
312:            }
313:
314:            public void testContainmentSkipsScrollpaneButtons()
315:                    throws Exception {
316:                JPanel rootPanel = new JPanel();
317:                rootPanel.setName("rootPanel");
318:                JPanel innerUnnamedPanel = new JPanel();
319:                rootPanel.add(innerUnnamedPanel);
320:                JScrollPane scroll = new JScrollPane();
321:                innerUnnamedPanel.add(scroll);
322:                XmlAssert.assertEquivalent("<panel name='rootPanel'/>",
323:                        new Panel(rootPanel).getDescription());
324:            }
325:
326:            public void testContainmentSkipsUnnamedContainers()
327:                    throws Exception {
328:                JPanel rootPanel = new JPanel();
329:                rootPanel.setName("rootPanel");
330:                JPanel innerUnnamedPanel = new JPanel();
331:                rootPanel.add(innerUnnamedPanel);
332:                JScrollPane scroll = new JScrollPane();
333:                innerUnnamedPanel.add(scroll);
334:                JButton button = new JButton("ok");
335:                JPanel viewportPanel = new JPanel();
336:                viewportPanel.add(button);
337:                scroll.getViewport().add(viewportPanel);
338:                XmlAssert.assertEquivalent("<panel name='rootPanel'>"
339:                        + "  <button label='ok'/>" + "</panel>", new Panel(
340:                        rootPanel).getDescription());
341:            }
342:
343:            public void testSelectedPanelIsTheOneVisibleInCardLayout()
344:                    throws Exception {
345:                JPanel cardPanel = new JPanel();
346:                JPanel firstPanel = new JPanel();
347:                firstPanel.add(new JList());
348:                JButton firstButton = new JButton("myButton");
349:                firstPanel.add(firstButton);
350:
351:                JPanel secondPanel = new JPanel();
352:                JButton secondButton = new JButton("myButton");
353:                secondPanel.add(secondButton);
354:
355:                CardLayout cardLayout = new CardLayout();
356:                cardPanel.setLayout(cardLayout);
357:                cardPanel.add(firstPanel, "first");
358:                cardPanel.add(secondPanel, "second");
359:                addComponentToPanelWithName(cardPanel, "panelWithCardLayout");
360:
361:                Panel panelWithCardLayoutChecker = panel
362:                        .getPanel("panelWithCardLayout");
363:
364:                cardLayout.show(cardPanel, "first");
365:                ListBox list1Checker = panelWithCardLayoutChecker.getListBox();
366:                assertNotNull(list1Checker);
367:                assertNotNull(panelWithCardLayoutChecker.getButton("myButton"));
368:                XmlAssert.assertEquivalent("<panel name='panelWithCardLayout'>"
369:                        + "  <listBox/>" + "  <button label='myButton'/>"
370:                        + "</panel>", panelWithCardLayoutChecker
371:                        .getDescription());
372:
373:                cardLayout.show(cardPanel, "second");
374:                try {
375:                    panelWithCardLayoutChecker.getListBox();
376:                    fail();
377:                } catch (ItemNotFoundException e) {
378:                }
379:                assertNotNull(panelWithCardLayoutChecker.getButton("myButton"));
380:                XmlAssert.assertEquivalent("<panel name='panelWithCardLayout'>"
381:                        + "  <button label='myButton'/>" + "</panel>",
382:                        panelWithCardLayoutChecker.getDescription());
383:            }
384:
385:            public void testAmbiguityMessageContents() throws Exception {
386:                JButton apply = new JButton("apply");
387:                JButton noApply = new JButton(" don't apply");
388:                Component[] components = new Component[] { apply, noApply };
389:                StringBuffer message = new StringBuffer();
390:                message.append("Several components are of type '").append(
391:                        Button.TYPE_NAME).append("' in this panel: [");
392:                for (int i = 0; i < components.length; i++) {
393:                    Component component = components[i];
394:                    String displayedName = ComponentUtils
395:                            .getDisplayedName(component);
396:                    message.append((displayedName == null) ? component
397:                            .getName() : displayedName);
398:                    message.append((i < components.length - 1) ? ',' : ']');
399:                }
400:                assertEquals(
401:                        "Several components are of type 'button' in this panel: [apply, don't apply]",
402:                        message.toString());
403:            }
404:
405:            public void testGetInputTextBoxExcludesJLabels() throws Exception {
406:                JLabel label = new JLabel("label");
407:                label.setName("name");
408:                JTextField textField = new JTextField("textField");
409:                textField.setName("name");
410:                jPanel.add(label);
411:                jPanel.add(textField);
412:
413:                panel.getInputTextBox("name").textEquals("textField");
414:                panel.getInputTextBox().textEquals("textField");
415:            }
416:
417:            public void testGetPanelSearchesForSpecificClasses()
418:                    throws Exception {
419:                checkGetPanel(new JPanel());
420:                checkGetPanel(new JInternalFrame());
421:            }
422:
423:            private void checkGetPanel(Container innerPanel) throws Exception {
424:                jPanel.removeAll();
425:                JTextField textField = new JTextField();
426:                textField.setName("innerText");
427:                jPanel.add(textField);
428:                innerPanel.setName("innerPanel");
429:                jPanel.add(innerPanel);
430:                assertNotNull(panel.getPanel("inner"));
431:            }
432:
433:            private void addComponentToPanelWithName(Component component,
434:                    String name) {
435:                component.setName(name);
436:                jPanel.add(component);
437:            }
438:
439:            private void createTwoComponentsWithSameName(Class component,
440:                    String componentName) throws Exception {
441:                addComponentToPanelWithName(((JComponent) component
442:                        .newInstance()), componentName);
443:                addComponentToPanelWithName(((JComponent) component
444:                        .newInstance()), componentName);
445:            }
446:
447:            private void checkComponentNotFound(String uiComponentType)
448:                    throws Exception {
449:                try {
450:                    getAccessor(uiComponentType).getComponent();
451:                    fail();
452:                } catch (ItemNotFoundException e) {
453:                    assertEquals(Messages.computeNotFoundMessage(
454:                            uiComponentType, null, null), e.getMessage());
455:                }
456:
457:                String componentName = "unknown";
458:                try {
459:                    getAccessor(uiComponentType).getComponent(componentName);
460:                    fail();
461:                } catch (ItemNotFoundException e) {
462:                    assertEquals(Messages.computeNotFoundMessage(
463:                            uiComponentType, componentName, null), e
464:                            .getMessage());
465:                }
466:
467:                try {
468:                    getAccessor(uiComponentType).getComponent(
469:                            new ComponentMatcher() {
470:                                public boolean matches(Component component) {
471:                                    return false;
472:                                }
473:                            });
474:                    fail();
475:                } catch (ItemNotFoundException e) {
476:                    assertEquals(Messages.computeNotFoundMessage(null, null,
477:                            null), e.getMessage());
478:                }
479:            }
480:
481:            private void checkComponentTypeMismatch(String componentName,
482:                    Class actualComponentType, String uiComponentType)
483:                    throws Exception {
484:                addComponentToPanelWithName((Component) actualComponentType
485:                        .newInstance(), componentName);
486:                try {
487:                    getAccessor(uiComponentType).getComponent(componentName);
488:                    fail();
489:                } catch (ItemNotFoundException e) {
490:                    assertEquals(Messages.computeNotFoundMessage(
491:                            uiComponentType, componentName, null), e
492:                            .getMessage());
493:                }
494:            }
495:
496:            private void checkComponentNameAmbiguity(String componentName,
497:                    String uiComponentType, String[] candidates)
498:                    throws Exception {
499:                try {
500:                    getAccessor(uiComponentType).getComponent(componentName);
501:                    fail();
502:                } catch (ComponentAmbiguityException e) {
503:                    assertEquals(Messages.computeAmbiguityMessage(candidates,
504:                            uiComponentType, componentName), e.getMessage());
505:                }
506:            }
507:
508:            private void checkComponentUnicityAmbiguity(Class componentClass,
509:                    String uiComponentType, String componentName)
510:                    throws Exception {
511:                Component component1 = createSwingInstance(componentClass);
512:                addComponentToPanelWithName(component1, componentName + "1");
513:                Component component2 = createSwingInstance(componentClass);
514:                addComponentToPanelWithName(component2, componentName + "2");
515:                try {
516:                    getAccessor(uiComponentType).getComponent();
517:                    fail();
518:                } catch (ComponentAmbiguityException e) {
519:                    Component[] components = new Component[] { component1,
520:                            component2 };
521:                    String[] names = new String[components.length];
522:                    for (int i = 0; i < components.length; i++) {
523:                        Component component = components[i];
524:                        String displayedName = ComponentUtils
525:                                .getDisplayedName(component);
526:                        names[i] = (displayedName == null || displayedName
527:                                .length() == 0) ? component.getName()
528:                                : displayedName;
529:                    }
530:                    assertEquals(Messages.computeAmbiguityMessage(names,
531:                            uiComponentType, null), e.getMessage());
532:                }
533:            }
534:
535:            private void checkGetComponentWithText(Class componentClass,
536:                    String uiComponentType) throws Exception {
537:                String name = "wholename";
538:                String shortName = "lena";
539:                Component component = createComponentWithText(componentClass,
540:                        name);
541:                jPanel.add(component);
542:                checkGetComponent(component, getAccessor(uiComponentType),
543:                        name, shortName);
544:            }
545:
546:            private void checkGetComponentWithName(Class componentClass,
547:                    String uiComponentType) throws Exception {
548:                String componentName = componentClass.getName()
549:                        + "#componentName";
550:                String shortName = componentClass.getName() + "#c";
551:                Component component = createSwingInstance(componentClass);
552:                addComponentToPanelWithName(component, componentName);
553:                checkGetComponent(component, getAccessor(uiComponentType),
554:                        componentName, shortName);
555:            }
556:
557:            private Component createSwingInstance(Class componentClass)
558:                    throws InstantiationException, IllegalAccessException {
559:                if (componentClass.equals(JTextComponent.class)) {
560:                    return new JTextField();
561:                }
562:                return (Component) componentClass.newInstance();
563:            }
564:
565:            private void checkGetComponentWithClassAndName(
566:                    Class uiComponentClass) throws Exception {
567:                String typeName = UIComponentAnalyzer
568:                        .getTypeName(uiComponentClass);
569:                Class[] swingClasses = UIComponentAnalyzer
570:                        .getSwingClasses(uiComponentClass);
571:                for (int i = 0; i < swingClasses.length; i++) {
572:                    checkGetComponentWithName(swingClasses[i], typeName);
573:                }
574:            }
575:
576:            private void checkGetComponent(Component expectedComponent,
577:                    TypedComponentAccessor accessor, String name,
578:                    String shortName) throws Exception {
579:                TestUtils.assertUIComponentRefersTo(expectedComponent, accessor
580:                        .getComponent(name));
581:                TestUtils.assertUIComponentRefersTo(expectedComponent, accessor
582:                        .getComponent(name.toUpperCase()));
583:                TestUtils.assertUIComponentRefersTo(expectedComponent, accessor
584:                        .getComponent(name.toLowerCase()));
585:                TestUtils.assertUIComponentRefersTo(expectedComponent, accessor
586:                        .getComponent(shortName));
587:                TestUtils.assertUIComponentRefersTo(expectedComponent, accessor
588:                        .getComponent(shortName.toUpperCase()));
589:                TestUtils.assertUIComponentRefersTo(expectedComponent, accessor
590:                        .getComponent(shortName.toLowerCase()));
591:                try {
592:                    accessor.getComponent("toto");
593:                    fail();
594:                } catch (Throwable e) {
595:                }
596:            }
597:
598:            private void checkGetComponentWithClass(Class uiComponentClass)
599:                    throws Exception {
600:                Class[] swingClasses = UIComponentAnalyzer
601:                        .getSwingClasses(uiComponentClass);
602:                String uiComponentType = UIComponentAnalyzer
603:                        .getTypeName(uiComponentClass);
604:                for (int i = 0; i < swingClasses.length; i++) {
605:                    jPanel.removeAll();
606:                    Component component = createSwingInstance(swingClasses[i]);
607:                    jPanel.add(component);
608:                    TestUtils.assertUIComponentRefersTo(component, getAccessor(
609:                            uiComponentType).getComponent());
610:                }
611:            }
612:
613:            private void checkGetComponentWithCustomMatcher(
614:                    Class uiComponentClass) throws Exception {
615:                Class[] swingClasses = UIComponentAnalyzer
616:                        .getSwingClasses(uiComponentClass);
617:                String uiComponentType = UIComponentAnalyzer
618:                        .getTypeName(uiComponentClass);
619:                for (int i = 0; i < swingClasses.length; i++) {
620:                    jPanel.removeAll();
621:                    Component component = createSwingInstance(swingClasses[i]);
622:                    jPanel.add(component);
623:                    ComponentMatcher matcher = new ComponentMatcher() {
624:                        public boolean matches(Component component) {
625:                            return component.isEnabled();
626:                        }
627:                    };
628:                    TestUtils.assertUIComponentRefersTo(component, getAccessor(
629:                            uiComponentType).getComponent(matcher));
630:                }
631:            }
632:
633:            private Component createComponentWithText(Class componentClass,
634:                    String componentName) throws Exception {
635:                Constructor componentConstructor = componentClass
636:                        .getConstructor(new Class[] { String.class });
637:                return (Component) componentConstructor
638:                        .newInstance(new Object[] { componentName });
639:            }
640:
641:            private TypedComponentAccessor getAccessor(String typeName) {
642:                return (TypedComponentAccessor) componentAccessors
643:                        .get(typeName);
644:            }
645:
646:            private static Map createAccessors(final Panel panel) {
647:                HashMap map = new HashMap();
648:                map.put(Button.TYPE_NAME, new ComponentAccessorAdapter() {
649:                    public UIComponent getComponent(ComponentMatcher matcher)
650:                            throws Exception {
651:                        return panel.getButton(matcher);
652:                    }
653:
654:                    public UIComponent getComponent(String componentName)
655:                            throws Exception {
656:                        return panel.getButton(componentName);
657:                    }
658:
659:                    public UIComponent getComponent() throws Exception {
660:                        return panel.getButton();
661:                    }
662:                });
663:                map.put(ToggleButton.TYPE_NAME, new ComponentAccessorAdapter() {
664:                    public UIComponent getComponent(ComponentMatcher matcher)
665:                            throws Exception {
666:                        return panel.getToggleButton(matcher);
667:                    }
668:
669:                    public UIComponent getComponent(String componentName)
670:                            throws Exception {
671:                        return panel.getToggleButton(componentName);
672:                    }
673:
674:                    public UIComponent getComponent() throws Exception {
675:                        return panel.getToggleButton();
676:                    }
677:                });
678:                map.put(CheckBox.TYPE_NAME, new ComponentAccessorAdapter() {
679:                    public UIComponent getComponent(ComponentMatcher matcher)
680:                            throws Exception {
681:                        return panel.getCheckBox(matcher);
682:                    }
683:
684:                    public UIComponent getComponent(String componentName)
685:                            throws Exception {
686:                        return panel.getCheckBox(componentName);
687:                    }
688:
689:                    public UIComponent getComponent() throws Exception {
690:                        return panel.getCheckBox();
691:                    }
692:                });
693:                map.put(TextBox.TYPE_NAME, new ComponentAccessorAdapter() {
694:                    public UIComponent getComponent() throws Exception {
695:                        return panel.getTextBox();
696:                    }
697:
698:                    public UIComponent getComponent(ComponentMatcher matcher)
699:                            throws Exception {
700:                        return panel.getTextBox(matcher);
701:                    }
702:
703:                    public UIComponent getComponent(String componentName)
704:                            throws Exception {
705:                        return panel.getTextBox(componentName);
706:                    }
707:                });
708:                map.put(TabGroup.TYPE_NAME, new ComponentAccessorAdapter() {
709:                    public UIComponent getComponent() throws Exception {
710:                        return panel.getTabGroup();
711:                    }
712:
713:                    public UIComponent getComponent(ComponentMatcher matcher)
714:                            throws Exception {
715:                        return panel.getTabGroup(matcher);
716:                    }
717:
718:                    public UIComponent getComponent(String componentName)
719:                            throws Exception {
720:                        return panel.getTabGroup(componentName);
721:                    }
722:                });
723:                map.put(ComboBox.TYPE_NAME, new ComponentAccessorAdapter() {
724:                    public UIComponent getComponent() throws Exception {
725:                        return panel.getComboBox();
726:                    }
727:
728:                    public UIComponent getComponent(ComponentMatcher matcher)
729:                            throws Exception {
730:                        return panel.getComboBox(matcher);
731:                    }
732:
733:                    public UIComponent getComponent(String componentName)
734:                            throws Exception {
735:                        return panel.getComboBox(componentName);
736:                    }
737:                });
738:                map.put(org.uispec4j.Desktop.TYPE_NAME,
739:                        new ComponentAccessorAdapter() {
740:                            public UIComponent getComponent() throws Exception {
741:                                return panel.getDesktop();
742:                            }
743:
744:                            public UIComponent getComponent(
745:                                    ComponentMatcher matcher) throws Exception {
746:                                return panel.getDesktop(matcher);
747:                            }
748:
749:                            public UIComponent getComponent(String componentName)
750:                                    throws Exception {
751:                                return panel.getDesktop(componentName);
752:                            }
753:                        });
754:                map.put(ProgressBar.TYPE_NAME, new ComponentAccessorAdapter() {
755:                    public UIComponent getComponent() throws Exception {
756:                        return panel.getProgressBar();
757:                    }
758:
759:                    public UIComponent getComponent(ComponentMatcher matcher)
760:                            throws Exception {
761:                        return panel.getProgressBar(matcher);
762:                    }
763:
764:                    public UIComponent getComponent(String componentName)
765:                            throws Exception {
766:                        return panel.getProgressBar(componentName);
767:                    }
768:                });
769:                map.put(RadioButton.TYPE_NAME, new ComponentAccessorAdapter() {
770:                    public UIComponent getComponent(ComponentMatcher matcher)
771:                            throws Exception {
772:                        return panel.getRadioButton(matcher);
773:                    }
774:
775:                    public UIComponent getComponent(String componentName)
776:                            throws Exception {
777:                        return panel.getRadioButton(componentName);
778:                    }
779:
780:                    public UIComponent getComponent() throws Exception {
781:                        return panel.getRadioButton();
782:                    }
783:
784:                });
785:                map.put(Panel.TYPE_NAME, new ComponentAccessorAdapter() {
786:
787:                    public UIComponent getComponent(ComponentMatcher matcher)
788:                            throws Exception {
789:                        return panel.getPanel(matcher);
790:                    }
791:
792:                    public UIComponent getComponent(String componentName)
793:                            throws Exception {
794:                        return panel.getPanel(componentName);
795:                    }
796:
797:                    public UIComponent getComponent() throws Exception {
798:                        return panel.getPanel();
799:                    }
800:                });
801:                map.put(ListBox.TYPE_NAME, new ComponentAccessorAdapter() {
802:                    public UIComponent getComponent(ComponentMatcher matcher)
803:                            throws Exception {
804:                        return panel.getListBox(matcher);
805:                    }
806:
807:                    public UIComponent getComponent(String componentName)
808:                            throws Exception {
809:                        return panel.getListBox(componentName);
810:                    }
811:
812:                    public UIComponent getComponent() throws Exception {
813:                        return panel.getListBox();
814:                    }
815:                });
816:                map.put(Table.TYPE_NAME, new ComponentAccessorAdapter() {
817:                    public UIComponent getComponent(ComponentMatcher matcher)
818:                            throws Exception {
819:                        return panel.getTable(matcher);
820:                    }
821:
822:                    public UIComponent getComponent(String componentName)
823:                            throws Exception {
824:                        return panel.getTable(componentName);
825:                    }
826:
827:                    public UIComponent getComponent() throws Exception {
828:                        return panel.getTable();
829:                    }
830:                });
831:                map.put(Tree.TYPE_NAME, new ComponentAccessorAdapter() {
832:                    public UIComponent getComponent(ComponentMatcher matcher)
833:                            throws Exception {
834:                        return panel.getTree(matcher);
835:                    }
836:
837:                    public UIComponent getComponent(String componentName)
838:                            throws Exception {
839:                        return panel.getTree(componentName);
840:                    }
841:
842:                    public UIComponent getComponent() throws Exception {
843:                        return panel.getTree();
844:                    }
845:                });
846:                map.put(Spinner.TYPE_NAME, new ComponentAccessorAdapter() {
847:                    public UIComponent getComponent(ComponentMatcher matcher)
848:                            throws Exception {
849:                        return panel.getSpinner(matcher);
850:                    }
851:
852:                    public UIComponent getComponent(String componentName)
853:                            throws Exception {
854:                        return panel.getSpinner(componentName);
855:                    }
856:
857:                    public UIComponent getComponent() throws Exception {
858:                        return panel.getSpinner();
859:                    }
860:                });
861:                map.put(Slider.TYPE_NAME, new ComponentAccessorAdapter() {
862:                    public UIComponent getComponent(ComponentMatcher matcher)
863:                            throws Exception {
864:                        return panel.getSlider(matcher);
865:                    }
866:
867:                    public UIComponent getComponent(String componentName)
868:                            throws Exception {
869:                        return panel.getSlider(componentName);
870:                    }
871:
872:                    public UIComponent getComponent() throws Exception {
873:                        return panel.getSlider();
874:                    }
875:                });
876:                map.put(PasswordField.TYPE_NAME,
877:                        new ComponentAccessorAdapter() {
878:                            public UIComponent getComponent(
879:                                    ComponentMatcher matcher) throws Exception {
880:                                return panel.getPasswordField(matcher);
881:                            }
882:
883:                            public UIComponent getComponent(String componentName)
884:                                    throws Exception {
885:                                return panel.getPasswordField(componentName);
886:                            }
887:
888:                            public UIComponent getComponent() throws Exception {
889:                                return panel.getPasswordField();
890:                            }
891:                        });
892:                return map;
893:            }
894:
895:            private interface TypedComponentAccessor {
896:                UIComponent getComponent(ComponentMatcher matcher)
897:                        throws Exception;
898:
899:                UIComponent getComponent(String componentName) throws Exception;
900:
901:                UIComponent getComponent() throws Exception;
902:            }
903:
904:            private abstract static class ComponentAccessorAdapter implements 
905:                    TypedComponentAccessor {
906:                public UIComponent getComponent(String componentName)
907:                        throws Exception {
908:                    throw new RuntimeException("No such method");
909:                }
910:            }
911:
912:            private void addWithScrollPane(Component containedComponent,
913:                    JPanel main) {
914:                JScrollPane scroll = new JScrollPane();
915:                scroll.getViewport().add(containedComponent);
916:                main.add(scroll);
917:            }
918:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.