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

org.mockito.internal.configuration.ClassPathLoader Maven / Gradle / Ivy

There is a newer version: 2.0.2-beta
Show newest version
/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package org.mockito.internal.configuration;

import org.mockito.configuration.IMockitoConfiguration;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.exceptions.misusing.MockitoConfigurationException;
import org.mockito.plugins.MockMaker;
import org.mockito.plugins.StackTraceCleanerProvider;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

/**
 * Loads configuration or extension points available in the classpath.
 *
 * 

*

    *
  • * Can load the mockito configuration. The user who want to provide his own mockito configuration * should write the class org.mockito.configuration.MockitoConfiguration that implements * {@link IMockitoConfiguration}. For example : *
    
     * package org.mockito.configuration;
     *
     * //...
     *
     * public class MockitoConfiguration implements IMockitoConfiguration {
     *     boolean enableClassCache() { return false; }
     *
     *     // ...
     * }
     *     
    *
  • *
  • * Can load available mockito extensions. Currently Mockito only have one extension point the * {@link MockMaker}. This extension point allows a user to provide his own bytecode engine to build mocks. *
    Suppose you wrote an extension to create mocks with some Awesome library, in order to tell * Mockito to use it you need to put in your classpath *
      *
    1. The implementation itself, for example org.awesome.mockito.AwesomeMockMaker.
    2. *
    3. A file named org.mockito.plugins.MockMaker in a folder named * mockito-extensions, the content of this file need to have one line with * the qualified name org.awesome.mockito.AwesomeMockMaker.
    4. *
    *
  • *
*

*/ public class ClassPathLoader { public static final String MOCKITO_CONFIGURATION_CLASS_NAME = "org.mockito.configuration.MockitoConfiguration"; /** * @return configuration loaded from classpath or null */ @SuppressWarnings({"unchecked"}) public IMockitoConfiguration loadConfiguration() { //Trying to get config from classpath Class configClass; try { configClass = (Class) Class.forName(MOCKITO_CONFIGURATION_CLASS_NAME); } catch (ClassNotFoundException e) { //that's ok, it means there is no global config, using default one. return null; } try { return (IMockitoConfiguration) configClass.newInstance(); } catch (ClassCastException e) { throw new MockitoConfigurationException("MockitoConfiguration class must implement " + IMockitoConfiguration.class.getName() + " interface.", e); } catch (Exception e) { throw new MockitoConfigurationException("Unable to instantiate " + MOCKITO_CONFIGURATION_CLASS_NAME +" class. Does it have a safe, no-arg constructor?", e); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy