001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.test;
021    
022    import com.google.common.base.CharMatcher;
023    import org.apache.commons.lang.builder.EqualsBuilder;
024    import org.fest.assertions.Condition;
025    
026    import java.util.Collection;
027    
028    /**
029     * Conditions for use with FestAssert.
030     */
031    public final class MoreConditions {
032      private static final CharMatcher EOLS = CharMatcher.anyOf("\n\r");
033    
034      private MoreConditions() {
035        // static utility class
036      }
037    
038      public static Condition<String> equalsIgnoreEOL(String text) {
039        final String strippedText = EOLS.removeFrom(text);
040    
041        return new Condition<String>() {
042          @Override
043          public boolean matches(String value) {
044            return EOLS.removeFrom(value).equals(strippedText);
045          }
046        }.as("equal to " + strippedText);
047      }
048    
049      public static Condition<Collection<?>> contains(final Object expected) {
050        return new Condition<Collection<?>>() {
051          @Override
052          public boolean matches(Collection<?> collection) {
053            for (Object actual : collection) {
054              if (EqualsBuilder.reflectionEquals(expected, actual)) {
055                return true;
056              }
057            }
058            return false;
059          }
060        };
061      }
062    
063      public static Condition<Object> reflectionEqualTo(final Object expected) {
064        return new Condition<Object>() {
065          @Override
066          public boolean matches(Object actual) {
067            return EqualsBuilder.reflectionEquals(expected, actual);
068          }
069        };
070      }
071    }