org.scalatest.exceptions.TestCanceledException.scala Maven / Gradle / Ivy
/*
* Copyright 2001-2013 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalatest.exceptions
import org.scalactic.exceptions.NullArgumentException
import org.scalactic.Requirements._
import org.scalactic.source
import StackDepthExceptionHelper.posOrElseStackDepthFun
/**
* Exception thrown to indicate a test has been canceled.
*
*
* A canceled test is one that is unable to run because a needed dependency, such as
* an external database, is missing.
*
*
*
* Canceled tests are ones that complete abruptly with a TestCanceledException
after
* starting.
*
*
* @param messageFun a function that return an optional detail message for this TestCanceledException
.
* @param cause an optional cause, the Throwable
that caused this TestCanceledException
to be thrown.
* @param posOrStackDepthFun either a source position or a function that return the depth in the stack trace of this exception at which the line of test code that failed resides.
* @param payload an optional payload, which ScalaTest will include in a resulting TestCanceled
event
*
* @author Travis Stevens
* @author Chee Seng
*/
class TestCanceledException(
messageFun: StackDepthException => Option[String],
cause: Option[Throwable],
posOrStackDepthFun: Either[source.Position, StackDepthException => Int],
val payload: Option[Any]
) extends StackDepthException(messageFun, cause, posOrStackDepthFun/*, posOrElseStackDepthFun(pos, failedCodeStackDepthFun)*/) with ModifiableMessage[TestCanceledException] with PayloadField with ModifiablePayload[TestCanceledException] {
/**
* Constructs a TestCanceledException
with the given error message function, optional cause, source position and optional payload.
*
* @param messageFun a function that return an optional detail message for this TestCanceledException
.
* @param cause an optional cause, the Throwable
that caused this TestCanceledException
to be thrown.
* @param pos a source position
* @param payload an optional payload, which ScalaTest will include in a resulting TestCanceled
event
*/
def this(
messageFun: StackDepthException => Option[String],
cause: Option[Throwable],
pos: source.Position,
payload: Option[Any]
) = this(messageFun, cause, Left(pos), payload)
/**
* Constructs a TestCanceledException
with the given error message function, optional cause and source position.
*
* @param messageFun a function that return an optional detail message for this TestCanceledException
.
* @param cause an optional cause, the Throwable
that caused this TestCanceledException
to be thrown.
* @param pos a source position
*/
def this(
messageFun: StackDepthException => Option[String],
cause: Option[Throwable],
pos: source.Position
) = this(messageFun, cause, Left(pos), None)
/**
* Constructs a TestCanceledException
with pre-determined message
and failedCodeStackDepth
. (This was
* the primary constructor form prior to ScalaTest 1.5.)
*
* @param message an optional detail message for this TestCanceledException
.
* @param cause an optional cause, the Throwable
that caused this TestCanceledException
to be thrown.
* @param failedCodeStackDepth the depth in the stack trace of this exception at which the line of test code that failed resides.
*
* @throws NullArgumentException if either message
of cause
is null
, or Some(null)
.
*/
def this(message: Option[String], cause: Option[Throwable], failedCodeStackDepth: Int) =
this(
StackDepthException.toExceptionFunction(message),
cause,
Right((_: StackDepthException) => failedCodeStackDepth),
None
)
/**
* Create a TestCanceledException
with specified stack depth and no detail message or cause.
*
* @param failedCodeStackDepth the depth in the stack trace of this exception at which the line of test code that failed resides.
*
*/
def this(failedCodeStackDepth: Int) =
this((_: StackDepthException) => None, None, Right((_: StackDepthException) => failedCodeStackDepth), None)
/**
* Create a TestCanceledException
with a specified stack depth and detail message.
*
* @param message A detail message for this TestCanceledException
.
* @param failedCodeStackDepth the depth in the stack trace of this exception at which the line of test code that failed resides.
*
* @throws NullArgumentException if message
is null
.
*/
def this(message: String, failedCodeStackDepth: Int) =
this(
{
(_: StackDepthException) => Option(message)
},
None,
Right((_: StackDepthException) => failedCodeStackDepth),
None
)
/**
* Create a TestCanceledException
with the specified stack depth and cause. The
* message
field of this exception object will be initialized to
* if (cause.getMessage == null) "" else cause.getMessage
.
*
* @param cause the cause, the Throwable
that caused this TestCanceledException
to be thrown.
* @param failedCodeStackDepth the depth in the stack trace of this exception at which the line of test code that failed resides.
*
* @throws NullArgumentException if cause
is null
.
*/
def this(cause: Throwable, failedCodeStackDepth: Int) =
this(
{
requireNonNull(cause)
(_: StackDepthException) => if (cause.getMessage == null) None else Some(cause.getMessage)
},
Some(cause),
Right((_: StackDepthException) => failedCodeStackDepth),
None
)
/**
* Create a TestCancelledException
with the specified stack depth, detail
* message, and cause.
*
* Note that the detail message associated with cause is
* not automatically incorporated in this throwable's detail
* message.
*
* @param message A detail message for this TestCanceledException
.
* @param cause the cause, the Throwable
that caused this TestCanceledException
to be thrown.
* @param failedCodeStackDepth the depth in the stack trace of this exception at which the line of test code that failed resides.
*
* @throws NullArgumentException if either message
or cause
is null
.
*/
def this(message: String, cause: Throwable, failedCodeStackDepth: Int) =
this(
{
requireNonNull(message)
(_: StackDepthException) => Some(message)
}, {
requireNonNull(cause)
Some(cause)
},
Right((_: StackDepthException) => failedCodeStackDepth),
None
)
/**
* Constructs a TestCanceledException
with the given error message function, optional cause, stack depth function and optional payload.
*
* @param messageFun a function that return an optional detail message for this TestCanceledException
.
* @param cause an optional cause, the Throwable
that caused this TestCanceledException
to be thrown.
* @param failedCodeStackDepthFun a function that return the depth in the stack trace of this exception at which the line of test code that failed resides.
* @param payload an optional payload, which ScalaTest will include in a resulting TestCanceled
event
*/
def this(messageFun: StackDepthException => Option[String],
cause: Option[Throwable],
failedCodeStackDepthFun: StackDepthException => Int,
payload: Option[Any]) = this(messageFun, cause, Right(failedCodeStackDepthFun), payload)
/**
* Returns an exception of class TestCanceledException
with failedExceptionStackDepth
set to 0 and
* all frames above this stack depth severed off. This can be useful when working with tools (such as IDEs) that do not
* directly support ScalaTest. (Tools that directly support ScalaTest can use the stack depth information delivered
* in the StackDepth exceptions.)
*/
def severedAtStackDepth: TestCanceledException = {
val truncated = getStackTrace.drop(failedCodeStackDepth)
val e = new TestCanceledException(messageFun, cause, posOrStackDepthFun, payload)
e.setStackTrace(truncated)
e
}
/**
* Returns an instance of this exception's class, identical to this exception,
* except with the detail message option string replaced with the result of passing
* the current detail message to the passed function, fun
.
*
* @param fun A function that, given the current optional detail message, will produce
* the modified optional detail message for the result instance of TestCanceledException
.
*/
def modifyMessage(fun: Option[String] => Option[String]): TestCanceledException = {
val mod = new TestCanceledException(StackDepthException.toExceptionFunction(fun(message)), cause, posOrStackDepthFun, payload)
mod.setStackTrace(getStackTrace)
mod
}
/**
* Returns an instance of this exception's class, identical to this exception,
* except with the payload option replaced with the result of passing
* the current payload option to the passed function, fun
.
*
* @param fun A function that, given the current optional payload, will produce
* the modified optional payload for the result instance of TestCanceledException
.
*/
def modifyPayload(fun: Option[Any] => Option[Any]): TestCanceledException = {
val currentPayload = payload
val mod = new TestCanceledException(messageFun, cause, posOrStackDepthFun, fun(currentPayload)) // TODO: Should I be lazy about replacing the payload?
mod.setStackTrace(getStackTrace)
mod
}
/**
* Indicates whether this object can be equal to the passed object.
*/
override def canEqual(other: Any): Boolean = other.isInstanceOf[TestCanceledException]
/**
* Indicates whether this object is equal to the passed object. If the passed object is
* a TestCanceledException
, equality requires equal message
,
* cause
, and failedCodeStackDepth
fields, as well as equal
* return values of getStackTrace
.
*/
override def equals(other: Any): Boolean =
other match {
case that: TestCanceledException => super.equals(that)
case _ => false
}
/**
* Returns a hash code value for this object.
*/
// Don't need to change it. Implementing it only so as to not freak out people who know
// that if you override equals you must override hashCode.
override def hashCode: Int = super.hashCode
}