⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 componentmatchers.java

📁 基于Junit的 功能和单元测试的的测试工具。只支持Swing.
💻 JAVA
字号:
package org.uispec4j.finder;

import org.uispec4j.utils.ComponentUtils;

import java.awt.*;

/**
 * Standard searching policies, implemented as {@link ComponentMatcher} objects.
 */
public class ComponentMatchers {

  /**
   * Matches components whose displayed name is exactly the same as the reference.
   */
  public static ComponentMatcher displayedNameIdentity(String reference) {
    return new DisplayedNameComponentMatcher(StringMatcher.identity(reference));
  }

  /**
   * Matches components whose displayed name is a substring of the reference.
   */
  public static ComponentMatcher displayedNameSubstring(String reference) {
    return new DisplayedNameComponentMatcher(StringMatcher.substring(reference));
  }

  /**
   * Matches components whose displayed name matches with the regexp reference.
   */
  public static ComponentMatcher displayedNameRegexp(String reference) {
    return new DisplayedNameComponentMatcher(StringMatcher.regexp(reference));
  }

  /**
   * Matches components whose inner name is exactly the same as the reference.
   */
  public static ComponentMatcher innerNameIdentity(String reference) {
    return new InnerNameComponentMatcher(StringMatcher.identity(reference));
  }

  /**
   * Matches components whose inner name is a substring of the reference.
   */
  public static ComponentMatcher innerNameSubstring(String reference) {
    return new InnerNameComponentMatcher(StringMatcher.substring(reference));
  }

  /**
   * Matches components whose inner name matches with the regexp reference.
   */
  public static ComponentMatcher innerNameRegexp(String reference) {
    return new InnerNameComponentMatcher(StringMatcher.regexp(reference));
  }

  /**
   * Matches components that are instances of the class.
   */
  public static ComponentMatcher fromClass(final Class swingClass) {
    return new ComponentMatcher() {
      public boolean matches(Component component) {
        return swingClass.isInstance(component);
      }
    };
  }

  /**
   * Matches components that match all its sub-matchers.
   */
  public static ComponentMatcher intersection(final ComponentMatcher[] matchers) {
    return new ComponentMatcher() {
      public boolean matches(Component component) {
        for (int i = 0; i < matchers.length; i++) {
          ComponentMatcher matcher = matchers[i];
          if (!matcher.matches(component)) {
            return false;
          }
        }
        return true;
      }
    };
  }

  /**
   * Matches components that match at least one of its sub-matchers.
   */
  public static ComponentMatcher union(final ComponentMatcher[] matchers) {
    return new ComponentMatcher() {
      public boolean matches(Component component) {
        for (int i = 0; i < matchers.length; i++) {
          ComponentMatcher matcher = matchers[i];
          if (matcher.matches(component)) {
            return true;
          }
        }
        return false;
      }
    };
  }

  /**
   * Matches components rejected by the inner matcher.
   */
  public static ComponentMatcher not(final ComponentMatcher matcher) {
    return new ComponentMatcher() {
      public boolean matches(Component component) {
        return !matcher.matches(component);
      }
    };
  }

  private static class DisplayedNameComponentMatcher implements ComponentMatcher {
    private final StringMatcher stringMatcher;

    public DisplayedNameComponentMatcher(StringMatcher stringMatcher) {
      this.stringMatcher = stringMatcher;
    }

    public boolean matches(Component component) {
      if (!ComponentUtils.hasDisplayedName(component.getClass())) {
        return false;
      }
      String displayedName = ComponentUtils.getDisplayedName(component);
      return stringMatcher.matches(displayedName);
    }
  }

  private static class InnerNameComponentMatcher implements ComponentMatcher {
    private final StringMatcher stringMatcher;

    public InnerNameComponentMatcher(StringMatcher stringMatcher) {
      this.stringMatcher = stringMatcher;
    }

    public boolean matches(Component component) {
      return stringMatcher.matches(component.getName());
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -