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

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

/*
 * $Id: ExceptionThrower.java,v 1.10 2016/03/05 17:46:00 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 (InstantiationException ex) { LOG.debug("Cannot instantiate " + type + ":", ex); t = ex; } catch (IllegalAccessException ex) { LOG.debug("Cannot access " + 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 InstantiationException the instantiation exception * @throws IllegalAccessException the illegal access 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 synchronized 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 - 2025 Weber Informatics LLC | Privacy Policy