001 /*
002 * Created on Sep 14, 2007
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with 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
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 *
014 * Copyright @2007-2010 the original author or authors.
015 */
016 package org.fest.assertions;
017
018 import static org.fest.assertions.ToString.toStringOf;
019 import static org.fest.util.Strings.*;
020
021 /**
022 * Provides utility methods related to formatting.
023 *
024 * @author Yvonne Wang
025 */
026 public final class Formatting {
027
028 private static final String EMPTY_MESSAGE = "";
029
030 static String createMessageFrom(Description description, Object[] message) {
031 return format(description, concat(message));
032 }
033
034 /**
035 * Returns the given message formatted as follows:
036 * <pre>
037 * [description] message.
038 * </pre>
039 * @param description the description of the actual value in the failed assertion. It can be <code>null</code>.
040 * @param message the message to format.
041 * @return the formatted message.
042 * @since 1.2
043 */
044 public static String format(Description description, String message) {
045 String s = valueOf(description);
046 return concat(format(s), message);
047 }
048
049 /**
050 * Returns the value of the given <code>{@link Description}</code>.
051 * @param description the given <code>Description</code>.
052 * @return the value of the given <code>Description</code>, or <code>null</code> if the given <code>Description</code>
053 * is <code>null</code>.
054 */
055 public static String valueOf(Description description) {
056 return description == null ? null : description.value();
057 }
058
059 /**
060 * Formats the given message: <li>if it is <code>null</code> or empty, an empty <code>String</code> is returned,
061 * otherwise uses the following format:
062 * <pre>
063 * [message]{whitespace}
064 * </pre>
065 * @param message the message to format.
066 * @return the formatted message.
067 */
068 public static String format(String message) {
069 if (isEmpty(message)) return EMPTY_MESSAGE;
070 return concat("[", message, "] ");
071 }
072
073 /**
074 * Returns the <code>String</code> representation of the given object in between brackets ("<" and ">"). This method
075 * has special support for arrays, <code>Class<?></code>, <code>Collection</code>s, <code>Map</code>s,
076 * <code>File</code>s and <code>Dimension</code>s. For any other types, this method simply calls its
077 * <code>toString</code> implementation.
078 * @param o the given object.
079 * @return the <code>String</code> representation of the given object in between brackets.
080 */
081 public static String inBrackets(Object o) {
082 return doBracketAround(toStringOf(o));
083 }
084
085 private static String doBracketAround(Object o) {
086 return concat("<", o, ">");
087 }
088
089 private Formatting() {}
090 }