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

com.melessoftware.hamcrest.extras.HasPropertyPathWithValue Maven / Gradle / Ivy

The newest version!
/*
 * Meles Hamcrest Extras
 * Copyright (C) 2013  Neil Green
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package com.melessoftware.hamcrest.extras;

import static com.melessoftware.hamcrest.extras.Cast.cast;
import static com.melessoftware.hamcrest.extras.PropertyConditions.follow;
import static com.melessoftware.hamcrest.extras.PropertyConditions.property;

import org.hamcrest.Condition;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;

public class HasPropertyPathWithValue extends TypeSafeDiagnosingMatcher {

    private final String propertyPath;
    private final Matcher valueMatcher;

    public HasPropertyPathWithValue(String propertyPath, Matcher valueMatcher) {
        this.propertyPath = propertyPath;
        this.valueMatcher = valueMatcher;
    }

    @Override
    protected boolean matchesSafely(T item, Description mismatchDescription) {
        final String[] pathParts = propertyPath.split("\\.");
        Condition condition = property(pathParts[0], item, mismatchDescription);
        for (int i = 1; i < pathParts.length; i++) {
            condition = condition.and(follow(pathParts[i]));
        }
        return condition.matching(valueMatcher, "\" ");
    }

    @Override
    public void describeTo(Description description) {
        description
                .appendText("has property path ")
                .appendValue(propertyPath)
                .appendText(" with value ");
        valueMatcher.describeTo(description);
    }

    public static  Matcher hasPropertyPath(String propertyPath, Matcher matcher) {
        return new HasPropertyPathWithValue(propertyPath, cast(matcher));
    }
}