001 /* 002 * Created on Jan 28, 2011 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 @2011 the original author or authors. 014 */ 015 package org.fest.assertions.error; 016 017 import static org.fest.util.Systems.LINE_SEPARATOR; 018 019 import java.io.File; 020 import java.io.InputStream; 021 import java.util.List; 022 023 /** 024 * Creates an error message indicating that an assertion that verifies that two files/inputStreams have equal content failed. 025 * 026 * @author Yvonne Wang 027 * @author Matthieu Baechler 028 */ 029 public class ShouldHaveEqualContent extends BasicErrorMessageFactory { 030 031 /** 032 * Creates a new <code>{@link ShouldHaveEqualContent}</code>. 033 * @param actual the actual file in the failed assertion. 034 * @param expected the expected file in the failed assertion. 035 * @param diffs the differences between {@code actual} and {@code expected}. 036 * @return the created {@code ErrorMessageFactory}. 037 */ 038 public static ErrorMessageFactory shouldHaveEqualContent(File actual, File expected, List<String> diffs) { 039 return new ShouldHaveEqualContent(actual, expected, diffsAsString(diffs)); 040 } 041 042 /** 043 * Creates a new <code>{@link ShouldHaveEqualContent}</code>. 044 * @param actual the actual InputStream in the failed assertion. 045 * @param expected the expected Stream in the failed assertion. 046 * @param diffs the differences between {@code actual} and {@code expected}. 047 * @return the created {@code ErrorMessageFactory}. 048 */ 049 public static ErrorMessageFactory shouldHaveEqualContent(InputStream actual, InputStream expected, List<String> diffs) { 050 return new ShouldHaveEqualContent(actual, expected, diffsAsString(diffs)); 051 } 052 053 private static String diffsAsString(List<String> diffs) { 054 StringBuilder b = new StringBuilder(); 055 for (String diff : diffs) 056 b.append(LINE_SEPARATOR).append(diff); 057 return b.toString(); 058 } 059 060 private ShouldHaveEqualContent(File actual, File expected, String diffs) { 061 super("file:<%s> and file:<%s> do not have equal content:" + diffs, actual, expected); 062 } 063 064 private ShouldHaveEqualContent(InputStream actual, InputStream expected, String diffs) { 065 super("InputStreams do not have equal content:" + diffs, actual, expected); 066 } 067 }