001    /*
002     * Created on Oct 18, 2010
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 @2010-2011 the original author or authors.
014     */
015    package org.fest.assertions.error;
016    
017    import static java.lang.String.format;
018    
019    import static org.fest.util.Dates.formatAsDatetimeWithMs;
020    
021    import java.util.Date;
022    
023    /**
024     * Creates an error message indicating that an assertion that verifies that a {@link Date} is close to another one from
025     * some delta failed.
026     * 
027     * @author Joel Costigliola
028     */
029    public class ShouldBeCloseTo extends BasicErrorMessageFactory {
030    
031      /**
032       * Creates a new </code>{@link ShouldBeCloseTo}</code>.
033       * @param actual the actual value in the failed assertion.
034       * @param other the value used in the failed assertion to compare the actual value to.
035       * @param deltaInMilliseconds the delta used for date comparison, expressed in milliseconds.
036       * @param difference the difference in milliseconds between actual and other dates.
037       * @return the created {@code ErrorMessageFactory}.
038       */
039      public static ErrorMessageFactory shouldBeCloseTo(Date actual, Date other, long deltaInMilliseconds, long difference) {
040        return new ShouldBeCloseTo(actual, other, deltaInMilliseconds, difference);
041      }
042    
043      private ShouldBeCloseTo(Date actual, Date other, long deltaInMilliseconds, long difference) {
044        // format Date up to the given ms, because defaut format is the second, thus dates with a difference less than 1s
045        // seems equal in the error message.
046        // Use standard formatting to avoid calling ToString.toStringOf for long that adds a 'L' (like 100L) to
047        // differentiate integer from long (here there is no ambiguity).
048        super(format("expected '%s' to be close to '%s' by less than %sms but difference was of %sms",
049            formatAsDatetimeWithMs(actual), formatAsDatetimeWithMs(other), deltaInMilliseconds, difference));
050      }
051    }