/*
* $HeadURL$
*
* Copyright (c) 2010 MindTree Ltd.
*
* This file is part of Infix Maven Plugins
*
* Infix Maven Plugins is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Infix Maven Plugins is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* Infix Maven Plugins. If not, see <http://www.gnu.org/licenses/>.
*/
package com.mindtree.techworks.infix.pluginscommon.test;
import static org.junit.Assert.*;
import java.io.File;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.codehaus.plexus.ContainerConfiguration;
import org.codehaus.plexus.DefaultContainerConfiguration;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.PlexusContainerException;
import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.DefaultContext;
import org.codehaus.plexus.logging.Logger;
import com.mindtree.techworks.infix.pluginscommon.mojo.MojoInfo;
/**
* This class is loosely based on
* http://fisheye.codehaus.org/browse/plexus/plexus-containers/trunk/plexus-container-default/src/main/java/org/codehaus/plexus/PlexusTestCase.java
* and we are rewriting it simply because the original class is based on Junit 3x
* and we want to move on to Junit 4!
*
* The entire functionality of the class has not yet been copied over and will
* be added as required.
*
* @author Bindul Bhowmik
* @version $Revision$ $Date$
*/
public class PlexusTestCase {
/**
* The plexus container
*/
private PlexusContainer container;
/**
* Base directory path
*/
private String baseDirPath;
/**
* The log for the test cases
*/
private Log log = new SystemStreamLog ();
/**
* The mojo information for our tests
*/
private MojoInfo mojoInfo;
private void setupContainer () {
// Context Setup
Context context = new DefaultContext();
context.put("basedir", getBaseDir());
customizeContext(context);
if (!context.contains("plexus.home")) {
File plexusHome = new File (getBaseDir(), "target/plexus-home");
if (!plexusHome.isDirectory()) {
plexusHome.mkdirs();
}
context.put("plexus.home", plexusHome.getAbsoluteFile());
}
log.debug("Setting up context:" + context.toString());
// Configuration
ContainerConfiguration containerConfiguration = new DefaultContainerConfiguration()
.setName("test").setContext(context.getContextData());
// String custom configuration
containerConfiguration.setContainerConfiguration(getConfigurationName());
customizeContainerConfiguration( containerConfiguration );
try {
container = new DefaultPlexusContainer( containerConfiguration );
} catch ( PlexusContainerException e ) {
log.error("Failed to create plexus context", e);
fail( "Failed to create plexus container." );
}
}
private PlexusContainer getContainer() {
if (null == container) {
setupContainer();
}
return container;
}
// --------------------------------- Hooks for extensions to customize stuff
/**
* Allows the extension to further customize the context
*/
protected void customizeContext ( Context context ) {
// Default implementation does nothing
}
/**
* Allow the retrieval of a container configuration that is based on the
* name of the test class being run. So if you have a test class called
* org.foo.FunTest, then this will produce a resource name of
* org/foo/FunTest.xml which would be used to configure the Plexus container
* before running your test.
*
* Extending classes can override this method to return a different
* configuration name if required.
*/
protected String getConfigurationName () {
String customConfigName = getClass().getName().replace( '.', '/' ) + ".xml";
if (null != getClass().getResource("/" + customConfigName)) {
return customConfigName;
}
return null;
}
protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration ) {
// Default implementation does nothing
}
protected MojoInfo createMojoInfo () {
return new MojoInfoTCImpl();
}
// --------------------------------------------- Container access and lookup
protected String getBaseDir() {
if (null == baseDirPath) {
baseDirPath = System.getProperty( "basedir" );
if (null == baseDirPath) {
baseDirPath = new File ("").getAbsolutePath();
}
}
return baseDirPath;
}
/**
* Looks up the default component for the role
*/
protected <T> T lookup ( Class<T> role ) throws ComponentLookupException {
return getContainer().lookup( role );
}
protected <T> T lookup ( Class<T> role, String roleHint ) throws ComponentLookupException {
return getContainer().lookup( role, roleHint );
}
protected <T> void release ( T component ) throws ComponentLifecycleException {
getContainer().release( component );
}
protected Logger getLog () {
return new Logger() {
@Override
public void warn (String message, Throwable throwable) {
log.warn(message, throwable);
}
@Override
public void warn (String message) {
log.warn(message);
}
@Override
public void setThreshold (int threshold) {
// DO nothing
}
@Override
public boolean isWarnEnabled () {
return log.isWarnEnabled();
}
@Override
public boolean isInfoEnabled () {
return log.isInfoEnabled();
}
@Override
public boolean isFatalErrorEnabled () {
return isErrorEnabled();
}
@Override
public boolean isErrorEnabled () {
return log.isErrorEnabled();
}
@Override
public boolean isDebugEnabled () {
return log.isDebugEnabled();
}
@Override
public void info (String message, Throwable throwable) {
log.info(message, throwable);
}
@Override
public void info (String message) {
log.info(message);
}
@Override
public int getThreshold () {
if (isErrorEnabled()) {
return LEVEL_ERROR;
} else if (isWarnEnabled()) {
return LEVEL_WARN;
} else if (isInfoEnabled()) {
return LEVEL_INFO;
} else if (isDebugEnabled()){
return LEVEL_DEBUG;
}
return LEVEL_DISABLED;
}
@Override
public String getName () {
return "";
}
@Override
public Logger getChildLogger (String name) {
return this;
}
@Override
public void fatalError (String message, Throwable throwable) {
log.error(message, throwable);
}
@Override
public void fatalError (String message) {
log.error(message);
}
@Override
public void error (String message, Throwable throwable) {
log.error(message, throwable);
}
@Override
public void error (String message) {
log.error(message);
}
@Override
public void debug (String message, Throwable throwable) {
log.debug(message, throwable);
}
@Override
public void debug (String message) {
log.debug(message);
}
};
}
protected MojoInfo getMojoInfo () {
if (null == mojoInfo) {
this.mojoInfo = createMojoInfo();
}
return mojoInfo;
}
}