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-2009 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.concat; 020 import static org.fest.util.Strings.isEmpty; 021 022 /** 023 * Provides utility methods related to formatting. 024 * 025 * @author Yvonne Wang 026 */ 027 public final class Formatting { 028 029 private static final String EMPTY_MESSAGE = ""; 030 031 static String createMessageFrom(Description description, Object[] message) { 032 return format(description, concat(message)); 033 } 034 035 /** 036 * Returns the given message formatted as follows: 037 * <pre> 038 * [description] message. 039 * </pre> 040 * @param description the description of the actual value in the failed assertion. It can be <code>null</code>. 041 * @param message the message to format. 042 * @return the formatted message. 043 * @since 1.2 044 */ 045 public static String format(Description description, String message) { 046 String s = valueOf(description); 047 return concat(format(s), message); 048 } 049 050 /** 051 * Returns the value of the given <code>{@link Description}</code>. 052 * @param description the given <code>Description</code>. 053 * @return the value of the given <code>Description</code>, or <code>null</code> if the given <code>Description</code> 054 * is <code>null</code>. 055 */ 056 public static String valueOf(Description description) { 057 return description == null ? null : description.value(); 058 } 059 060 /** 061 * Formats the given message: <li>if it is <code>null</code> or empty, an empty <code>String</code> is returned, 062 * otherwise uses the following format: 063 * <pre> 064 * [message]{whitespace} 065 * </pre> 066 * @param message the message to format. 067 * @return the formatted message. 068 */ 069 public static String format(String message) { 070 if (isEmpty(message)) return EMPTY_MESSAGE; 071 return concat("[", message, "] "); 072 } 073 074 /** 075 * Returns the <code>String</code> representation of the given object in between brackets ("<" and ">"). This method 076 * has special support for arrays, <code>Class<?></code>, <code>Collection</code>s, <code>Map</code>s, 077 * <code>File</code>s and <code>Dimension</code>s. For any other types, this method simply calls its 078 * <code>toString</code> implementation. 079 * @param o the given object. 080 * @return the <code>String</code> representation of the given object in between brackets. 081 */ 082 public static String inBrackets(Object o) { 083 return doBracketAround(toStringOf(o)); 084 } 085 086 private static String doBracketAround(Object o) { 087 return concat("<", o, ">"); 088 } 089 090 private Formatting() {} 091 }