All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ext.test4j.hamcrest.object.IsEventFrom Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
/*  Copyright (c) 2000-2006 hamcrest.org
 */
package ext.test4j.hamcrest.object;

import java.util.EventObject;

import ext.test4j.hamcrest.Description;
import ext.test4j.hamcrest.Factory;
import ext.test4j.hamcrest.Matcher;
import ext.test4j.hamcrest.TypeSafeDiagnosingMatcher;


/**
 * Tests if the value is an event announced by a specific object.
 */
public class IsEventFrom extends TypeSafeDiagnosingMatcher {
    private final Class eventClass;
    private final Object source;

    public IsEventFrom(Class eventClass, Object source) {
        this.eventClass = eventClass;
        this.source = source;
    }

    @Override
    public boolean matchesSafely(EventObject item, Description mismatchDescription) {
        if (!eventClass.isInstance(item)) {
          mismatchDescription.appendText("item type was " + item.getClass().getName());
          return false;
        }
        
        if (!eventHasSameSource(item)) {
          mismatchDescription.appendText("source was ").appendValue(item.getSource());
          return false;
        }
        return true;
    }

    
    private boolean eventHasSameSource(EventObject ev) {
        return ev.getSource() == source;
    }

    public void describeTo(Description description) {
        description.appendText("an event of type ")
                .appendText(eventClass.getName())
                .appendText(" from ")
                .appendValue(source);
    }

    /**
     * Constructs an IsEventFrom Matcher that returns true for any object
     * derived from eventClass announced by source.
     */
    @Factory
    public static Matcher eventFrom(Class eventClass, Object source) {
        return new IsEventFrom(eventClass, source);
    }

    /**
     * Constructs an IsEventFrom Matcher that returns true for any object
     * derived from {@link java.util.EventObject} announced by source
     * .
     */
    @Factory
    public static Matcher eventFrom(Object source) {
        return eventFrom(EventObject.class, source);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy