001    /*
002     * Created on Feb 28, 2008
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005     * in compliance with the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License
010     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011     * or implied. See the License for the specific language governing permissions and limitations under
012     * the License.
013     *
014     * Copyright @2008-2010 the original author or authors.
015     */
016    package org.fest.swing.driver;
017    
018    import static org.fest.swing.driver.TextAssert.verifyThat;
019    
020    import java.util.regex.Pattern;
021    
022    import javax.swing.JLabel;
023    
024    import org.fest.swing.annotation.RunsInEDT;
025    import org.fest.swing.core.Robot;
026    
027    /**
028     * Understands functional testing of <code>{@link JLabel}</code>s:
029     * <ul>
030     * <li>user input simulation</li>
031     * <li>state verification</li>
032     * <li>property value query</li>
033     * </ul>
034     * This class is intended for internal use only. Please use the classes in the package
035     * <code>{@link org.fest.swing.fixture}</code> in your tests.
036     *
037     * @author Alex Ruiz
038     */
039    public class JLabelDriver extends JComponentDriver implements TextDisplayDriver<JLabel> {
040    
041      private static final String TEXT_PROPERTY = "text";
042    
043      /**
044       * Creates a new </code>{@link JLabelDriver}</code>.
045       * @param robot the robot to use to simulate user input.
046       */
047      public JLabelDriver(Robot robot) {
048        super(robot);
049      }
050    
051      /**
052       * Asserts that the text of the <code>{@link JLabel}</code> is equal to the specified <code>String</code>.
053       * @param label the target <code>JLabel</code>.
054       * @param expected the text to match.
055       * @throws AssertionError if the text of the <code>JLabel</code> is not equal to the given one.
056       */
057      @RunsInEDT
058      public void requireText(JLabel label, String expected) {
059        verifyThat(textOf(label)).as(propertyName(label, TEXT_PROPERTY)).isEqualOrMatches(expected);
060      }
061    
062      /**
063       * Asserts that the text of the <code>{@link JLabel}</code> matches the given regular expression pattern.
064       * @param label the target <code>JLabel</code>.
065       * @param pattern the regular expression pattern to match.
066       * @throws AssertionError if the text of the <code>JLabel</code> does not match the given regular expression pattern.
067       * @throws NullPointerException if the given regular expression pattern is <code>null</code>.
068       * @since 1.2
069       */
070      @RunsInEDT
071      public void requireText(JLabel label, Pattern pattern) {
072        verifyThat(textOf(label)).as(propertyName(label, TEXT_PROPERTY)).matches(pattern);
073      }
074    
075      /**
076       * Returns the text of the given <code>{@link JLabel}</code>.
077       * @param label the given <code>JLabel</code>.
078       * @return the text of the given <code>JLabel</code>.
079       */
080      @RunsInEDT
081      public String textOf(JLabel label) {
082        return JLabelTextQuery.textOf(label);
083      }
084    }