001 /* 002 * Created on Jul 29, 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.core; 016 017 import static java.lang.String.format; 018 import static org.fest.assertions.core.DescriptionValidations.checkIsNotNull; 019 import static org.fest.util.Strings.quote; 020 021 import org.fest.assertions.description.*; 022 023 /** 024 * Writable information about an assertion. 025 * 026 * @author Alex Ruiz 027 * @author Yvonne Wang 028 */ 029 public final class WritableAssertionInfo implements AssertionInfo { 030 031 private String overridingErrorMessage; 032 private Description description; 033 034 /** {@inheritDoc} */ 035 public String overridingErrorMessage() { 036 return overridingErrorMessage; 037 } 038 039 /** 040 * Sets the message that will replace the default message of an assertion failure. 041 * @param newErrorMessage the new message. It can be {@code null}. 042 */ 043 public void overridingErrorMessage(String newErrorMessage) { 044 overridingErrorMessage = newErrorMessage; 045 } 046 047 /** {@inheritDoc} */ 048 public Description description() { 049 return description; 050 } 051 052 /** 053 * Returns the text of this object's description, or {@code null} if such description is {@code null}. 054 * @return the text of this object's description, or {@code null} if such description is {@code null}. 055 */ 056 public String descriptionText() { 057 return description != null ? description.value() : null; 058 } 059 060 /** 061 * Sets the description of an assertion. 062 * @param newDescription the new description. 063 * @throws NullPointerException if the given description is {@code null}. 064 * @see #description(Description) 065 */ 066 public void description(String newDescription) { 067 description = checkIsNotNull(newDescription); 068 } 069 070 /** 071 * Sets the description of an assertion. To remove or clear the description, pass a 072 * <code>{@link EmptyTextDescription}</code> as argument. 073 * @param newDescription the new description. 074 * @throws NullPointerException if the given description is {@code null}. 075 */ 076 public void description(Description newDescription) { 077 description = checkIsNotNull(newDescription); 078 } 079 080 /** {@inheritDoc} */ 081 @Override public String toString() { 082 String format = "%s[overridingErrorMessage=%s, description=%s]"; 083 return format(format, getClass().getSimpleName(), quote(overridingErrorMessage()), quote(descriptionText())); 084 } 085 }