com.gantzgulch.sharing.matchers.UserMatcher.java Source code

Java tutorial

Introduction

Here is the source code for com.gantzgulch.sharing.matchers.UserMatcher.java

Source

/*
 * Copyright 2011 GantzGulch, Inc.
 * 
 * This file is part of GantzFileSharing.
 * 
 * GantzFileSharing 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.
 * 
 * GantzFileSharing 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 GantzFileSharing.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.gantzgulch.sharing.matchers;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;

import com.gantzgulch.sharing.domain.User;
import com.gantzgulch.sharing.util.Cast;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

public class UserMatcher {

    public static Matcher<List<User>> containsUsername(final String username) {
        return new BaseMatcher<List<User>>() {

            @Override
            public boolean matches(Object arg0) {
                List<User> users = Cast.cast(arg0);
                return Iterables.any(users, new Predicate<User>() {
                    @Override
                    public boolean apply(User input) {
                        return StringUtils.equals(input.getUsername(), username);
                    }
                });
            }

            @Override
            public void describeTo(Description arg0) {
                arg0.appendText("User with username : " + username);
            }
        };
    }

    public static Matcher<List<User>> containsId(final String id) {
        return new BaseMatcher<List<User>>() {

            @Override
            public boolean matches(Object arg0) {
                List<User> users = Cast.cast(arg0);
                return Iterables.any(users, new Predicate<User>() {
                    @Override
                    public boolean apply(User input) {
                        return StringUtils.equals(input.getId(), id);
                    }
                });
            }

            @Override
            public void describeTo(Description arg0) {
                arg0.appendText("User with id : " + id);
            }
        };
    }
}