All Downloads are FREE. Search and download functionalities are using the official Maven repository.

patterntesting.runtime.util.ExceptionThrower Maven / Gradle / Ivy

Go to download

PatternTesting Runtime (patterntesting-rt) is the runtime component for the PatternTesting framework. It provides the annotations and base classes for the PatternTesting testing framework (e.g. patterntesting-check, patterntesting-concurrent or patterntesting-exception) but can be also used standalone for classpath monitoring or profiling. It uses AOP and AspectJ to perform this feat.

There is a newer version: 2.4.0
Show newest version
/*
 * $Id: ExceptionThrower.java,v 1.5 2011/07/09 21:43:22 oboehm Exp $
 *
 * Copyright (c) 2010 by Oliver Boehm
 *
 * 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 orimplied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * (c)reated 30.01.2010 by oliver ([email protected])
 */

package patterntesting.runtime.util;

import java.lang.reflect.Constructor;

import org.junit.Test;
import org.slf4j.*;

/**
 * Because we need the functionality of throwing any exception not only in
 * PatternTesting Exception but also here this functionality was shifted to
 * PatternTesting Runtime.
 * 
* This class is not intended for public use. If you do so use it on your own * risk! * * @author oliver * @since 1.0 (30.01.2010) */ public final class ExceptionThrower { private static Logger log = LoggerFactory.getLogger(ExceptionThrower.class); /** only a utility class - no need to instantiate it. */ private ExceptionThrower() {} /** * 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. *
* WARNING: If the desired exception can't be instantiated an * InstantiationException or IllegalAccessException may be thrown. *
* WARNING(2): This method is not synchronized. * * @param type e.g. IOException.class */ public static void provoke(final Class type) { Throwable t; try { t = create(type); } catch (Exception ex) { log.debug("can't create " + type, ex); t = ex; } Thrower.provoke(t); } /** * This method throws the expected exception wrapped into the * {@link Test} annotation. * * @param test with the expected exception */ public static void provoke(final Test test) { Class expected = test.expected(); if ((expected != Test.None.class) && (expected != null)) { ExceptionThrower.provoke(expected); } } /** * Creates any desired exception you want. * If the desired exception can't be instantiated an * InstantiationException or IllegalAccessException may be thrown. * * @param type the exception class you want to be created * * @return the instantiated exception or the caught exception * * @throws IllegalAccessException the illegal access exception * @throws InstantiationException the instantiation exception */ public static Throwable create(final Class type) throws InstantiationException, IllegalAccessException { try { Constructor ctor = type .getConstructor(String.class); return ctor.newInstance("created by ExceptionThrower"); } catch (Exception e) { if (log.isTraceEnabled()) { log.trace("can't call 'new " + type + "(String)'", e); } return type.newInstance(); } } /** * The trick here is to use the constructor to throw any desired exception. * So you can throw any exception without the need to have it as throws * clause. * * @author oliver */ static final class Thrower { private static Throwable throwable; private Thrower() throws Throwable { throw throwable; } /** * Provoke an exception. * * @param t the Throwable which should be used as provoked exception. */ public static void provoke(final Throwable t) { throwable = t; try { Thrower.class.newInstance(); } catch (InstantiationException unexpected) { log.debug("can't instantiate Thrower class", unexpected); } catch (IllegalAccessException unexpected) { log.debug("can't access Thrower constructor", unexpected); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy