001 /* 002 * Created on Aug 5, 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.internal; 016 017 import static org.fest.util.Strings.isEmpty; 018 019 import org.fest.assertions.core.AssertionInfo; 020 import org.fest.assertions.error.AssertionErrorFactory; 021 import org.fest.assertions.error.ErrorMessageFactory; 022 import org.fest.assertions.error.ShouldBeEqual; 023 import org.fest.util.Throwables; 024 import org.fest.util.VisibleForTesting; 025 026 /** 027 * Failure actions. 028 * 029 * @author Yvonne Wang 030 * @author Alex Ruiz 031 */ 032 public class Failures { 033 034 private static final Failures INSTANCE = new Failures(); 035 036 /** 037 * Returns the singleton instance of this class. 038 * @return the singleton instance of this class. 039 */ 040 public static Failures instance() { 041 return INSTANCE; 042 } 043 044 /** 045 * flag indicating wether or not we remove elements related to Fest from assertion error stack trace. 046 */ 047 private boolean removeFestRelatedElementsFromStackTrace = true; 048 049 /** 050 * Sets wether we remove elements related to Fest from assertion error stack trace. 051 * @param removeFestRelatedElementsFromStackTrace flag 052 */ 053 public void setRemoveFestRelatedElementsFromStackTrace(boolean removeFestRelatedElementsFromStackTrace) { 054 this.removeFestRelatedElementsFromStackTrace = removeFestRelatedElementsFromStackTrace; 055 } 056 057 @VisibleForTesting 058 Failures() {} 059 060 /** 061 * Creates a <code>{@link AssertionError}</code> following this pattern: 062 * <ol> 063 * <li>creates a <code>{@link AssertionError}</code> using <code>{@link AssertionInfo#overridingErrorMessage()}</code> 064 * as the error message if such value is not {@code null}, or</li> 065 * <li>uses the given <code>{@link AssertionErrorFactory}</code> to create an <code>{@link AssertionError}</code>, 066 * prepending the value of <code>{@link AssertionInfo#description()}</code> to the error message</li> 067 * </ol> 068 * @param info contains information about the failed assertion. 069 * @param factory knows how to create {@code AssertionError}s. 070 * @return the created <code>{@link AssertionError}</code>. 071 */ 072 public AssertionError failure(AssertionInfo info, AssertionErrorFactory factory) { 073 AssertionError error = failureIfErrorMessageIsOverriden(info); 074 if (error != null) return error; 075 return factory.newAssertionError(info.description()); 076 } 077 078 /** 079 * Creates a <code>{@link AssertionError}</code> following this pattern: 080 * <ol> 081 * <li>creates a <code>{@link AssertionError}</code> using <code>{@link AssertionInfo#overridingErrorMessage()}</code> 082 * as the error message if such value is not {@code null}, or</li> 083 * <li>uses the given <code>{@link ErrorMessageFactory}</code> to create the detail message of the 084 * <code>{@link AssertionError}</code>, prepending the value of <code>{@link AssertionInfo#description()}</code> to 085 * the error message</li> 086 * </ol> 087 * @param info contains information about the failed assertion. 088 * @param message knows how to create detail messages for {@code AssertionError}s. 089 * @return the created <code>{@link AssertionError}</code>. 090 */ 091 public AssertionError failure(AssertionInfo info, ErrorMessageFactory message) { 092 AssertionError error = failureIfErrorMessageIsOverriden(info); 093 if (error != null) return error; 094 AssertionError assertionError = new AssertionError(message.create(info.description())); 095 removeFestRelatedElementsFromStackTraceIfNeeded(assertionError); 096 return assertionError; 097 } 098 099 private AssertionError failureIfErrorMessageIsOverriden(AssertionInfo info) { 100 String overridingErrorMessage = info.overridingErrorMessage(); 101 if (!isEmpty(overridingErrorMessage)) return failure(overridingErrorMessage); 102 return null; 103 } 104 105 /** 106 * Creates a <code>{@link AssertionError}</code> using the given {@code String} as message. 107 * <p> 108 * It filters the AssertionError stack trace be default, to have full stack trace use {@link #setRemoveFestRelatedElementsFromStackTrace(boolean)}. 109 * @param message the message of the {@code AssertionError} to create. 110 * @return the created <code>{@link AssertionError}</code>. 111 */ 112 public AssertionError failure(String message) { 113 AssertionError assertionError = new AssertionError(message); 114 removeFestRelatedElementsFromStackTraceIfNeeded(assertionError); 115 return assertionError; 116 } 117 118 /** 119 * If is {@link #removeFestRelatedElementsFromStackTrace} is true, it filters the stack trace of the given {@link AssertionError} 120 * by removing stack trace elements related to Fest in order to get a more readable stack trace. 121 * <p> 122 * See example below : 123 * <pre> 124 --------------- stack trace not filtered ----------------- 125 org.junit.ComparisonFailure: expected:<'[Ronaldo]'> but was:<'[Messi]'> 126 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 127 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 128 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 129 at java.lang.reflect.Constructor.newInstance(Constructor.java:501) 130 at org.fest.assertions.error.ConstructorInvoker.newInstance(ConstructorInvoker.java:34) 131 at org.fest.assertions.error.ShouldBeEqual.newComparisonFailure(ShouldBeEqual.java:111) 132 at org.fest.assertions.error.ShouldBeEqual.comparisonFailure(ShouldBeEqual.java:103) 133 at org.fest.assertions.error.ShouldBeEqual.newAssertionError(ShouldBeEqual.java:81) 134 at org.fest.assertions.internal.Failures.failure(Failures.java:76) 135 at org.fest.assertions.internal.Objects.assertEqual(Objects.java:116) 136 at org.fest.assertions.api.AbstractAssert.isEqualTo(AbstractAssert.java:74) 137 at examples.StackTraceFilterExample.main(StackTraceFilterExample.java:13) 138 139 --------------- stack trace filtered ----------------- 140 org.junit.ComparisonFailure: expected:<'[Ronaldo]'> but was:<'[Messi]'> 141 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 142 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 143 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 144 at examples.StackTraceFilterExample.main(StackTraceFilterExample.java:20) 145 * </pre> 146 * 147 * Method is public because we need to call it from {@link ShouldBeEqual#newAssertionError(org.fest.assertions.description.Description)} that is building a junit ComparisonFailure by reflection. 148 * 149 * @param assertionError the {@code AssertionError} to filter stack trace if option is set. 150 */ 151 public void removeFestRelatedElementsFromStackTraceIfNeeded(AssertionError assertionError) { 152 if (removeFestRelatedElementsFromStackTrace) { 153 Throwables.removeFestRelatedElementsFromStackTrace(assertionError); 154 } 155 } 156 157 }