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 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 @2007-2011 the original author or authors.
014     */
015    package org.fest.assertions;
016    
017    import static org.fest.util.Strings.*;
018    import static org.fest.util.ToString.toStringOf;
019    
020    import java.util.IllegalFormatException;
021    
022    /**
023     * Utility methods for formatting values.
024     *
025     * @author Yvonne Wang
026     * @author Alex Ruiz
027     */
028    public final class Formatting {
029    
030      private static final String EMPTY_MESSAGE = "";
031    
032      static String createMessageFrom(Description description, Object[] message) {
033        return format(description, concat(message));
034      }
035    
036      /**
037       * Returns the given message formatted as follows:
038       *
039       * <pre>
040       * [description] message.
041       * </pre>
042       * @param description the description of the actual value in the failed assertion. It can be {@code null}.
043       * @param message the message to format.
044       * @return the formatted message.
045       * @since 1.2
046       */
047      public static String format(Description description, String message) {
048        String s = valueOf(description);
049        return format(s) + message;
050      }
051    
052      /**
053       * Returns the value of the given <code>{@link Description}</code>.
054       * @param description the given <code>Description</code>.
055       * @return the value of the given <code>Description</code>, or {@code null} if the given <code>Description</code> is
056       *         {@code null}.
057       */
058      public static String valueOf(Description description) {
059        return description == null ? null : description.value();
060      }
061    
062      /**
063       * Formats the given message: <li>if it is {@code null} or empty, an empty <code>String</code> is returned, otherwise
064       * uses the following format:
065       *
066       * <pre>
067       * [message]{whitespace}
068       * </pre>
069       * @param message the message to format.
070       * @return the formatted message.
071       */
072      public static String format(String message) {
073        if (isEmpty(message)) return EMPTY_MESSAGE;
074        return String.format("[%s] ", message);
075      }
076    
077      /**
078       * Returns the <code>String</code> representation of the given object in between brackets ("<" and ">"). This method
079       * has special support for arrays, <code>Class<?></code>, {@code Collection}s, {@code Map}s, {@code File}s and
080       * <code>Dimension</code>s. For any other types, this method simply calls its <code>toString</code> implementation.
081       * @param o the given object.
082       * @return the <code>String</code> representation of the given object in between brackets.
083       */
084      public static String inBrackets(Object o) {
085        return doBracketAround(toStringOf(o));
086      }
087    
088      private static String doBracketAround(String s) {
089        return String.format("<%s>", s);
090      }
091    
092      /**
093       * Returns a formatted string using the specified format {@code String} and arguments. This method is similar to
094       * <code>{@link String#format(String, Object...)}</code>. The only difference is that this method uses FEST-Assert's
095       * own {@code toString} representation of the arguments.
096       * @param format a format {@code String}.
097       * @param args Arguments referenced by the format specifiers in the format {@code String}. If there are more arguments
098       * than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero.
099       * @throws IllegalFormatException If a format string contains an illegal syntax, a format specifier that is
100       * incompatible with the given arguments, insufficient arguments given the format {@code String}, or other illegal
101       * conditions.
102       * @throws NullPointerException if the given format is {@code null}.
103       * @return A formatted {@code String}.
104       * @since 1.3.1
105       */
106      public static String format(String format, Object... args) {
107        Object[] argsAsText = new String[args.length];
108        for (int i = 0; i < args.length; i++)
109          argsAsText[i] = toStringOf(args[i]);
110        return String.format(format, argsAsText);
111      }
112    
113      private Formatting() {}
114    }