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

org.codehaus.plexus.spring.PlexusInSpringTestCase Maven / Gradle / Ivy

The newest version!
package org.codehaus.plexus.spring;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.File;
import java.io.InputStream;

import junit.framework.TestCase;

import org.codehaus.plexus.PlexusContainer;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * Mimic org.codehaus.plexus.PlexusTestCase as simple replacement for test
 * cases.
 *
 * @author Nicolas De Loof
 * @version $Id: PlexusInSpringTestCase.java 7779 2008-10-08 21:37:56Z olamy $
 */
public class PlexusInSpringTestCase
    extends TestCase
{
    protected ConfigurableApplicationContext applicationContext;

    protected void setUp()
        throws Exception
    {
        applicationContext = new PlexusClassPathXmlApplicationContext( getConfigLocations() );
    }

    protected String[] getConfigLocations()
    {
        return new String[] { 
            "classpath*:META-INF/spring-context.xml", 
            "classpath*:META-INF/plexus/components.xml",
            "classpath*:" + getPlexusConfigLocation(), 
            "classpath*:" + getSpringConfigLocation() };
    }

    protected String getSpringConfigLocation()
    {
        return getClass().getName().replace( '.', '/' ) + "-context.xml";
    }

    protected String getPlexusConfigLocation()
    {
        return getClass().getName().replace( '.', '/' ) + ".xml";
    }

    /**
     * {@inheritDoc}
     *
     * @see junit.framework.TestCase#tearDown()
     */
    protected void tearDown()
        throws Exception
    {
        if ( applicationContext != null )
        {
            applicationContext.close();
        }
    }

    public static String getBasedir()
    {
        return PlexusToSpringUtils.getBasedir();
    }

    public String getTestConfiguration()
    {
        return getTestConfiguration( getClass() );
    }

    public static String getTestConfiguration( Class clazz )
    {
        String s = clazz.getName().replace( '.', '/' );

        return s.substring( 0, s.indexOf( "$" ) ) + ".xml";
    }

    public Object lookup( Class role )
    {
        return lookup( role, null );
    }

    public Object lookup( Class role, String roleHint )
    {
        return lookup( role.getName(), roleHint );

    }

    public Object lookup( String role )
    {
        return lookup( role, null );
    }

    public Object lookup( String role, String roleHint )
    {
        return applicationContext.getBean( PlexusToSpringUtils.buildSpringId( role, roleHint ) );
    }

    public static File getTestFile( String path )
    {
        return new File( PlexusToSpringUtils.getBasedir(), path );
    }

    public static File getTestFile( String basedir,
                                    String path )
    {
        File basedirFile = new File( basedir );

        if ( !basedirFile.isAbsolute() )
        {
            basedirFile = getTestFile( basedir );
        }

        return new File( basedirFile, path );
    }

    public static String getTestPath( String path )
    {
        return getTestFile( path ).getAbsolutePath();
    }

    public static String getTestPath( String basedir,
                                      String path )
    {
        return getTestFile( basedir, path ).getAbsolutePath();
    }

    protected ConfigurableApplicationContext getApplicationContext()
    {
        return applicationContext;
    }
    
    protected void release( Object component )
        throws Exception
    {
        // nothing 
    }
    
    protected PlexusContainer getContainer()
    {
        return (PlexusContainer) applicationContext.getBean( "plexusContainer" );
    }
    
    protected InputStream getResourceAsStream( String resource )
    {
        return getClass().getResourceAsStream( resource );
    }
    
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy