001    /*
002     * Created on Mar 19, 2012
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005     * the License. You may obtain a copy of the License at
006     * 
007     * http://www.apache.org/licenses/LICENSE-2.0
008     * 
009     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     * 
013     * Copyright @2012 the original author or authors.
014     */
015    package org.fest.assertions.error;
016    
017    import java.util.List;
018    
019    /**
020     * Creates an <code>{@link AssertionError}</code> indicating that an assertion that verifies that two objects are
021     * lenient equal by ignoring fields failed.
022     * 
023     * @author Nicolas François
024     * @author Joel Costigliola
025     */
026    public class ShouldBeLenientEqualByIgnoring extends BasicErrorMessageFactory {
027    
028      /**
029       * Creates a new </code>{@link ShouldBeLenientEqualByIgnoring}</code>.
030       * @param actual the actual value in the failed assertion.
031       * @param rejectedFields fields name not matching
032       * @param expectedValues fields value not matching
033       * @param ignoredFields fields which are not base the lenient equality
034       * @return the created {@code ErrorMessageFactory}.
035       */
036      public static <E> ErrorMessageFactory shouldBeLenientEqualByIgnoring(Object actual, List<String> rejectedFields,
037          List<Object> expectedValues, List<String> ignoredFields) {
038        if (rejectedFields.size() == 1) {
039          if (ignoredFields.isEmpty()) {
040            return new ShouldBeLenientEqualByIgnoring(actual, rejectedFields.get(0), expectedValues.get(0));
041          } else {
042            return new ShouldBeLenientEqualByIgnoring(actual, rejectedFields.get(0), expectedValues.get(0), ignoredFields);
043          }
044        } else {
045          if (ignoredFields.isEmpty()) {
046            return new ShouldBeLenientEqualByIgnoring(actual, rejectedFields, expectedValues);
047          } else {
048            return new ShouldBeLenientEqualByIgnoring(actual, rejectedFields, expectedValues, ignoredFields);
049          }
050        }
051      }
052    
053      private ShouldBeLenientEqualByIgnoring(Object actual, List<String> rejectedFields, List<Object> expectedValues,
054          List<String> ignoredFields) {
055        super("expected values:\n<%s>\n in fields:\n<%s>\n of <%s>.\nComparison was performed on all fields but <%s>", expectedValues,
056            rejectedFields, actual, ignoredFields);
057      }
058    
059      private ShouldBeLenientEqualByIgnoring(Object actual, String rejectedField, Object expectedValue, List<String> ignoredFields) {
060        super("expected value <%s> in field <%s> of <%s>.\nComparison was performed on all fields but <%s>", expectedValue,
061            rejectedField, actual, ignoredFields);
062      }
063    
064      private ShouldBeLenientEqualByIgnoring(Object actual, List<String> rejectedFields, List<Object> expectedValue) {
065        super("expected values:\n<%s>\n in fields:\n<%s>\n of <%s>.\nComparison was performed on all fields", expectedValue, rejectedFields,
066            actual);
067      }
068    
069      private ShouldBeLenientEqualByIgnoring(Object actual, String rejectedField, Object expectedValue) {
070        super("expected value <%s> in field <%s> of <%s>.\nComparison was performed on all fields", expectedValue, rejectedField,
071            actual);
072      }
073    
074    }