patterntesting.exception
Class ExceptionFactory

java.lang.Object
  extended by patterntesting.exception.ExceptionFactory
All Implemented Interfaces:
ExceptionFactoryMBean

public final class ExceptionFactory
extends Object
implements ExceptionFactoryMBean

This class can create for testing purpose any desired checked exception. Implemented with the help of Thomas.Darimont@web.de (via mailing list of aspectj-users@eclipse.org).
It is realized as Singleton to be able to extract an MBean interface for the use via JMX.
The exceptions are controlled by AbstractTestExceptionFactory. You will see the ExceptionFactory not before the first method marked as @TestException has finished (because it is realized as after advice). If you want to see it before call ExceptionFactory.getInstance() (with the creation of the instance it is also registered as MBean).

Since:
05.03.2009
Version:
$Revision: 1.11 $
Author:
oliver

Field Summary
 
Fields inherited from interface patterntesting.exception.ExceptionFactoryMBean
ALL_CLASSES
 
Method Summary
 void activate()
          Activate.
 void activateOnce()
          Activate once.
 void deactivate()
          Deactivate.
 String getFire()
          Only exceptions of this returned type will be fired.
static ExceptionFactory getInstance()
          It is realized as singleton because of the MBean interface.
 Throwable getLastProvoked()
          Gets the last provoked exception.
 String getLimitedTo()
          Deprecated. replaced by getFire() (will be removed with 1.2)
 long getMaxNumberOfProvoked()
          Gets the maximal number of provoked exceptions (default is Long.MAX_VALUE).
 long getNumberOfProvoked()
          Gets the number of provoked exceptions.
 String getScope()
          Returns the scope for which the exceptions will be thrown.
 boolean isActive()
          Checks if is active.
 boolean isAssertsEnabled()
          To see if the (Sun) Java-VM was called with the option "-ea" ("enable asserts") this getter shows it.
 void provoke(Class<? extends Throwable> type)
          Be careful - you can provoke any Exception with the method without the need to declare it with a throws statement.
 void provokeFor(JoinPoint jp)
          This methods throws one of the exception which is possible for the given joinpoint.
protected  void provokeOneOf(Class<? extends Throwable>[] types)
          This method throws the first given Throwable type.
static void registerAsMBean()
          Normally the ExceptionFactory register itself at JMX.
 void reset()
          Resets all preferences and deactivates ExceptionFactory.
 void resetFire()
          Allows again that all exceptions would be fired.
 void resetScope()
          To set the scope back to "all classes" use this method here.
 void setActive(boolean active)
          You can only provoke Exceptions if the active flag is set.
 void setFire(Class<? extends Throwable> fire)
          You want to provoked an SocketException whenever it is possible?
 void setFire(String classname)
          You want to provoked an SocketException whenever it is possible?
 void setLimitedTo(Class<? extends Throwable> limitedTo)
          Deprecated. replaced by setFire(String) (will be removed with 1.2)
 void setLimitedTo(String limitedTo)
          Deprecated. replaced by setFire(String) (will be removed with 1.2)
 void setMaxNumberOfProvoked(long n)
          Limit the number of provoked maximal total number of provoked exceptions to n (default is Long.MAX_VALUE).
 void setScope(Class<?> target)
          To limit the exception to be thrown for a given class you can use this method here.
 void setScope(String classname)
          To limit the exception to be thrown for a given class you can use this method here.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

registerAsMBean

public static void registerAsMBean()
                            throws JMException
Normally the ExceptionFactory register itself at JMX. But only if the class is loaded. To force the classloader to load the ExceptionFactory you can use the getInstance() method or this method here.

Throws:
JMException - if it can't be registerd at JMX
Since:
1.0

getInstance

public static ExceptionFactory getInstance()
It is realized as singleton because of the MBean interface.

Returns:
the only instance

getMaxNumberOfProvoked

public long getMaxNumberOfProvoked()
Gets the maximal number of provoked exceptions (default is Long.MAX_VALUE).

Specified by:
getMaxNumberOfProvoked in interface ExceptionFactoryMBean
Returns:
the number of provoked exceptions
See Also:
ExceptionFactoryMBean.getMaxNumberOfProvoked()

getNumberOfProvoked

public long getNumberOfProvoked()
Gets the number of provoked exceptions.

Specified by:
getNumberOfProvoked in interface ExceptionFactoryMBean
Returns:
the number of provoked exceptions
See Also:
ExceptionFactoryMBean.getNumberOfProvoked()

setMaxNumberOfProvoked

public void setMaxNumberOfProvoked(long n)
Limit the number of provoked maximal total number of provoked exceptions to n (default is Long.MAX_VALUE).

Specified by:
setMaxNumberOfProvoked in interface ExceptionFactoryMBean
Parameters:
n - the maximal number of provoked exceptions
See Also:
ExceptionFactoryMBean.setMaxNumberOfProvoked(long)

isActive

public boolean isActive()
Checks if is active.

Specified by:
isActive in interface ExceptionFactoryMBean
Returns:
true if active
See Also:
ExceptionFactoryMBean.isActive()

setActive

public void setActive(boolean active)
You can only provoke Exceptions if the active flag is set.

Specified by:
setActive in interface ExceptionFactoryMBean
Parameters:
active - true or false
See Also:
ExceptionFactoryMBean.setActive(boolean)

activate

public void activate()
Activate.

Specified by:
activate in interface ExceptionFactoryMBean
See Also:
ExceptionFactoryMBean.activate()

activateOnce

public void activateOnce()
Activate once.

Specified by:
activateOnce in interface ExceptionFactoryMBean
See Also:
ExceptionFactoryMBean.activateOnce()

deactivate

public void deactivate()
Deactivate.

Specified by:
deactivate in interface ExceptionFactoryMBean
See Also:
ExceptionFactoryMBean.deactivate()

isAssertsEnabled

public boolean isAssertsEnabled()
To see if the (Sun) Java-VM was called with the option "-ea" ("enable asserts") this getter shows it.

Specified by:
isAssertsEnabled in interface ExceptionFactoryMBean
Returns:
true if asserts are enabled
See Also:
ExceptionFactoryMBean.isAssertsEnabled()

getLastProvoked

public Throwable getLastProvoked()
Gets the last provoked exception.

Specified by:
getLastProvoked in interface ExceptionFactoryMBean
Returns:
the last exception which was thrown by one of the provoke methods
See Also:
ExceptionFactoryMBean.getLastProvoked()

getLimitedTo

public String getLimitedTo()
Deprecated. replaced by getFire() (will be removed with 1.2)

Only exceptions of this returned type (or subclasses of it) are thrown. The return value is a String because of the use as MBean (works better in the JConsole).

Specified by:
getLimitedTo in interface ExceptionFactoryMBean
Returns:
the limitedTo (e.g. "java.lang.Throwable")

setLimitedTo

public void setLimitedTo(Class<? extends Throwable> limitedTo)
Deprecated. replaced by setFire(String) (will be removed with 1.2)

You want to limit the provoked exceptions to IOException (or subclass)? Then give it as parameter to this setter method.
If you want to reset it and allow all Exceptions call setLimitedTo(Trowable.class).

Parameters:
limitedTo - the limitedTo to set (e.g. IOException.class)

setLimitedTo

public void setLimitedTo(String limitedTo)
                  throws ClassNotFoundException
Deprecated. replaced by setFire(String) (will be removed with 1.2)

You want to limit the provoked exceptions to IOException (or subclass)? Then give it as parameter to this setter method.
If you want to reset it and allow all Exceptions call setLimitedTo("java.lang.Throwable").
For better use with the JConsole the class could given as String. But don't forget to give the complete classname (with package) as String.

Specified by:
setLimitedTo in interface ExceptionFactoryMBean
Parameters:
limitedTo - e.g. "java.io.IOException"
Throws:
ClassNotFoundException - the class not found exception

provoke

public void provoke(Class<? extends Throwable> type)
Be careful - you can provoke any Exception with the method without the need to declare it with a throws statement. For example
provoke(IOException.class)
would throw an IOException.

Parameters:
type - e.g. IOException.class
See Also:
ExceptionThrower.provoke(Class)

provokeFor

public void provokeFor(JoinPoint jp)
This methods throws one of the exception which is possible for the given joinpoint. But only if the joinpoint matches one of the registered objects, classes or threads.

Parameters:
jp - the joinpoint for which an exception should be provoked

provokeOneOf

protected void provokeOneOf(Class<? extends Throwable>[] types)
This method throws the first given Throwable type. If this fails the next element array is tried to be created as Throwable.
You can only provoke an Exception if the active flag is set.

Parameters:
types - a class array with exception types

setScope

public void setScope(Class<?> target)
To limit the exception to be thrown for a given class you can use this method here.
This setter is not part of the ExceptionFactoryMBean interface because JMX allows only one setter for an attribute. Otherwise you'll get an javax.management.NotCompliantMBeanException: Attribute Scope has more than one setter.

Parameters:
target - the target
See Also:
ExceptionFactoryMBean.setScope(java.lang.String)

setScope

public void setScope(String classname)
To limit the exception to be thrown for a given class you can use this method here.
A string is excepted instead of a Class object because of the jconsole. With the jconsole only basic types or strings are available as input fields.

Specified by:
setScope in interface ExceptionFactoryMBean
Parameters:
classname - for which an exception should be thrown
Since:
1.1
See Also:
ExceptionFactoryMBean.setScope(java.lang.String)

resetScope

public void resetScope()
To set the scope back to "all classes" use this method here.

Specified by:
resetScope in interface ExceptionFactoryMBean
Since:
1.1

getScope

public String getScope()
Returns the scope for which the exceptions will be thrown. The default value is "all classes" if the scope is not set.

Specified by:
getScope in interface ExceptionFactoryMBean
Returns:
class for which an exception will be thrown.
Since:
1.1
See Also:
ExceptionFactoryMBean.getScope()

getFire

public String getFire()
Only exceptions of this returned type will be fired. The return value is a String because of the use as MBean (works better in the JConsole).

Specified by:
getFire in interface ExceptionFactoryMBean
Returns:
the exeption to be fired (e.g. "java.lang.Throwable")
Since:
1.1
See Also:
ExceptionFactoryMBean.getFire()

setFire

public void setFire(String classname)
             throws ClassNotFoundException
You want to provoked an SocketException whenever it is possible? Then give it as parameter to this setter method.
For better use with the JConsole the class could given as String. But don't forget to give the complete classname (with package) as String.

Specified by:
setFire in interface ExceptionFactoryMBean
Parameters:
classname - e.g. "java.net.SocketException"
Throws:
ClassNotFoundException - if parameter is not a class name
Since:
1.1
See Also:
ExceptionFactoryMBean.setFire(java.lang.String)

setFire

public void setFire(Class<? extends Throwable> fire)
You want to provoked an SocketException whenever it is possible? Then give it as parameter to this setter method.

Parameters:
fire - e.g. SocketException.class
Since:
1.1

resetFire

public void resetFire()
Allows again that all exceptions would be fired.

Specified by:
resetFire in interface ExceptionFactoryMBean
Since:
1.1
See Also:
ExceptionFactoryMBean.resetFire()

reset

public void reset()
Resets all preferences and deactivates ExceptionFactory.

Specified by:
reset in interface ExceptionFactoryMBean
Since:
1.1
See Also:
ExceptionFactoryMBean.reset()


Copyright © 2009-2010 PatternTesting Team. All Rights Reserved.