001: package org.uispec4j.finder;
002:
003: import org.uispec4j.*;
004:
005: import javax.swing.JButton;
006: import javax.swing.JLabel;
007: import javax.swing.JTable;
008: import javax.swing.JTextField;
009: import java.awt.Component;
010:
011: public class PanelUIComponentFinderTest extends
012: PanelComponentFinderTestCase {
013: private JButton button;
014: private static final ComponentMatcher CUSTOM_MATCHER = new ComponentMatcher() {
015: public boolean matches(Component component) {
016: String name = component.getName();
017: return (name != null) && name.startsWith("custom");
018: }
019: };
020:
021: protected void setUp() throws Exception {
022: super .setUp();
023: button = (JButton) addComponent(JButton.class, "button1");
024: }
025:
026: public void testFindUIComponentByClass() throws Exception {
027: assertNull(panel.findUIComponent(TextBox.class));
028:
029: TestUtils.assertUIComponentRefersTo(button, panel
030: .findUIComponent(Button.class));
031:
032: addComponent(JButton.class, "button2");
033: try {
034: panel.findUIComponent(Button.class);
035: fail();
036: } catch (ComponentAmbiguityException e) {
037: assertEquals(Messages.computeAmbiguityMessage(new String[] {
038: "button1", "button2" }, Button.TYPE_NAME, null), e
039: .getMessage());
040: }
041: }
042:
043: public void testFindUIComponentByName() throws Exception {
044: assertNull(panel.findUIComponent(Button.class, "unknown"));
045: assertNull(panel.findUIComponent(TextBox.class, "button"));
046: TestUtils.assertUIComponentRefersTo(button, panel
047: .findUIComponent(Button.class, "button1"));
048: TestUtils.assertUIComponentRefersTo(button, panel
049: .findUIComponent(Button.class, "button"));
050:
051: addComponent(JButton.class, "button2");
052: try {
053: panel.findUIComponent(Button.class, "button");
054: fail();
055: } catch (ComponentAmbiguityException e) {
056: assertEquals(
057: Messages.computeAmbiguityMessage(new String[] {
058: "button1", "button2" }, Button.TYPE_NAME,
059: "button"), e.getMessage());
060: }
061: }
062:
063: public void testFindUIComponentWithCustomComponentMatcher()
064: throws Exception {
065: assertNull(panel.findUIComponent(CUSTOM_MATCHER));
066:
067: button.setName("custom button");
068: TestUtils.assertUIComponentRefersTo(button, panel
069: .findUIComponent(CUSTOM_MATCHER));
070:
071: Component textField = addComponent(JTextField.class, "text");
072: TestUtils.assertUIComponentRefersTo(button, panel
073: .findUIComponent(CUSTOM_MATCHER));
074:
075: textField.setName("custom text");
076: try {
077: panel.findUIComponent(CUSTOM_MATCHER);
078: } catch (ComponentAmbiguityException e) {
079: assertEquals(Messages.computeAmbiguityMessage(
080: new Component[] { button, textField }, null, null),
081: e.getMessage());
082: }
083: }
084:
085: public void testGetUIComponentsByClass() throws Exception {
086: JLabel label1 = (JLabel) addComponent(JLabel.class, "label1");
087: JLabel label2 = (JLabel) addComponent(JLabel.class, "label2");
088: JTextField jTextfield = (JTextField) addComponent(
089: JTextField.class, "textField");
090:
091: TestUtils.assertUIComponentsReferTo(new Component[] { label2,
092: jTextfield, label1 }, panel
093: .getUIComponents(TextBox.class));
094: TestUtils.assertSwingComponentsEquals(new Component[] { label2,
095: label1 }, panel.getSwingComponents(JLabel.class));
096:
097: TestUtils.assertUIComponentsReferTo(new Component[0], panel
098: .getUIComponents(Table.class));
099:
100: TestUtils.assertSwingComponentsEquals(new Component[0], panel
101: .getSwingComponents(JTable.class));
102: }
103:
104: public void testGetUIComponentsByName() throws Exception {
105: JLabel label1 = (JLabel) addComponent(JLabel.class, "name1");
106: JLabel label2 = (JLabel) addComponent(JLabel.class, "name2");
107: JTextField jTextfield = (JTextField) addComponent(
108: JTextField.class, "name2");
109: addComponent(JLabel.class, "other label");
110: addComponent(JTable.class, "table");
111:
112: TestUtils.assertUIComponentsReferTo(new Component[] { label1,
113: label2, jTextfield }, panel.getUIComponents(
114: TextBox.class, "name"));
115: TestUtils.assertSwingComponentsEquals(new Component[] { label1,
116: label2 }, panel
117: .getSwingComponents(JLabel.class, "name"));
118:
119: TestUtils.assertUIComponentsReferTo(new Component[0], panel
120: .getUIComponents(Table.class, "name"));
121: TestUtils.assertSwingComponentsEquals(new Component[0], panel
122: .getSwingComponents(JTable.class, "name"));
123: }
124:
125: public void testGetUIComponentsWithCustomComponentMatcher()
126: throws Exception {
127: TestUtils.assertUIComponentsReferTo(new Component[0], panel
128: .getUIComponents(CUSTOM_MATCHER));
129:
130: button.setName("custom button");
131: TestUtils.assertUIComponentsReferTo(new Component[] { button },
132: panel.getUIComponents(CUSTOM_MATCHER));
133:
134: Component table = addComponent(JTable.class, "table");
135: TestUtils.assertUIComponentsReferTo(new Component[] { button },
136: panel.getUIComponents(CUSTOM_MATCHER));
137:
138: table.setName("custom table");
139: TestUtils.assertUIComponentsReferTo(new Component[] { button,
140: table }, panel.getUIComponents(CUSTOM_MATCHER));
141: }
142: }
|