001 /*
002 * Created on Sep 8, 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 org.fest.util.ToString.toStringOf;
018
019 import java.awt.image.BufferedImage;
020
021 import org.fest.assertions.description.Description;
022 import org.fest.util.*;
023
024 /**
025 * Formats the messages to be included in assertion errors.
026 *
027 * @author Alex Ruiz
028 */
029 public class MessageFormatter {
030
031 private static final MessageFormatter INSTANCE = new MessageFormatter();
032
033 /**
034 * Returns the singleton instance of this class.
035 * @return the singleton instance of this class.
036 */
037 public static MessageFormatter instance() {
038 return INSTANCE;
039 }
040
041 @VisibleForTesting DescriptionFormatter descriptionFormatter = DescriptionFormatter.instance();
042
043 @VisibleForTesting MessageFormatter() {}
044
045 /**
046 * Interprets a printf-style format {@code String} for failed assertion messages. It is similar to
047 * <code>{@link String#format(String, Object...)}</code>, except for:
048 * <ol>
049 * <li>the value of the given <code>{@link Description}</code> is used as the first argument referenced in the format
050 * string</li>
051 * <li>each of the arguments in the given array is converted to a {@code String} by invoking
052 * <code>{@link ToString#toStringOf(Object)}</code>.
053 * </ol>
054 * @param d the description of the failed assertion, may be {@code null}.
055 * @param format the format string.
056 * @param args arguments referenced by the format specifiers in the format string.
057 * @throws NullPointerException if the format string is {@code null}.
058 * @return A formatted {@code String}.
059 */
060 public String format(Description d, String format, Object... args) {
061 if (format == null) throw new NullPointerException("The format string should not be null");
062 if (args == null) throw new NullPointerException("The array of arguments should not be null");
063 return descriptionFormatter.format(d) + String.format(format, format(args));
064 }
065
066 private Object[] format(Object[] args) {
067 int argCount = args.length;
068 String[] formatted = new String[argCount];
069 for (int i = 0; i < argCount; i++)
070 formatted[i] = asText(args[i]);
071 return formatted;
072 }
073
074 private String asText(Object o) {
075 if (o instanceof BufferedImage) return "image";
076 if (o instanceof ComparatorBasedComparisonStrategy) return " according to " + o + " comparator";
077 if (o instanceof StandardComparisonStrategy) return "";
078 return toStringOf(o);
079 }
080 }