package com.perforce.maven.scm.provider.p4; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Properties; import org.apache.maven.settings.Settings; import org.codehaus.plexus.DefaultPlexusContainer; import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.context.Context; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher; import com.perforce.maven.scm.provider.p4.repository.P4ScmProviderRepository; public abstract class AbstractPlexusTestCase { protected PlexusContainer container; private static String basedirPath; @After public void tearDown() throws Exception { container.dispose(); container = null; } protected SecDispatcher secDispatcher; protected Settings settings; @Before public void setUp() throws Exception { InputStream configuration = null; try { configuration = getCustomConfiguration(); if ( configuration == null ) { configuration = getConfiguration(); } } catch ( Exception e ) { System.out.println( "Error with configuration:" ); System.out.println( "configuration = " + configuration ); Assert.fail( e.getMessage() ); } container = createContainerInstance(); container.addContextValue( "basedir", getBasedir() ); // this method was deprecated customizeContext(); customizeContext( getContext() ); boolean hasPlexusHome = getContext().contains( "plexus.home" ); if ( !hasPlexusHome ) { File f = getTestFile( "target/plexus-home" ); if ( !f.isDirectory() ) { f.mkdir(); } getContext().put( "plexus.home", f.getAbsolutePath() ); } if ( configuration != null ) { container.setConfigurationResource( new InputStreamReader( configuration ) ); } container.initialize(); container.start(); this.secDispatcher = (SecDispatcher) this.lookup( "org.sonatype.plexus.components.sec.dispatcher.SecDispatcher", "mng-4384" ); // this.settings = (Settings) this.lookup( "org.apache.maven.settings.Settings" ); } protected PlexusContainer createContainerInstance() { return new DefaultPlexusContainer(); } private Context getContext() { return container.getContext(); } // !!! this should probably take a context as a parameter so that the // user is not forced to do getContainer().addContextValue(..) // this would require a change to PlexusContainer in order to get // hold of the context ... // @deprecated use void customizeContext( Context context ) protected void customizeContext() throws Exception { } protected void customizeContext( Context context ) throws Exception { } protected InputStream getCustomConfiguration() throws Exception { return null; } protected PlexusContainer getContainer() { return container; } protected InputStream getConfiguration() throws Exception { return getConfiguration( null ); } protected InputStream getConfiguration( String subname ) throws Exception { String className = getClass().getName(); String base = className.substring( className.lastIndexOf( "." ) + 1 ); String config = null; if ( subname == null || subname.equals( "" ) ) { config = base + ".xml"; } else { config = base + "-" + subname + ".xml"; } InputStream configStream = getResourceAsStream( config ); return configStream; } protected InputStream getResourceAsStream( String resource ) { return getClass().getResourceAsStream( resource ); } protected ClassLoader getClassLoader() { return getClass().getClassLoader(); } // ---------------------------------------------------------------------- // Container access // ---------------------------------------------------------------------- protected Object lookup( String componentKey ) throws Exception { return getContainer().lookup( componentKey ); } protected Object lookup( String role, String id ) throws Exception { return getContainer().lookup( role, id ); } protected void release( Object component ) throws Exception { getContainer().release( component ); } // ---------------------------------------------------------------------- // Helper methods for sub classes // ---------------------------------------------------------------------- public static File getTestFile( String path ) { return new File( 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(); } public static String getBasedir() { if ( basedirPath != null ) { return basedirPath; } basedirPath = System.getProperty( "basedir" ); if ( basedirPath == null ) { basedirPath = new File( "" ).getAbsolutePath(); } return basedirPath; } // ///////////////////////////////////////////////////////////////////////////////////// private static Properties testProps = null; @BeforeClass public static void beforeClass() throws IOException { File testPropsFile = new File( System.getProperty( "user.home" ), ".p4maven/test.properties" ); if ( testPropsFile.exists() ) { InputStream is = new FileInputStream( testPropsFile ); testProps = new Properties(); testProps.load( is ); is.close(); } } protected P4ScmProviderRepository createP4ScmRepository() throws Exception { if ( testProps == null ) { return null; } String P4PORT = testProps.getProperty( "P4PORT" ); String P4PATH = testProps.getProperty( "P4VIEW" ); P4ScmProvider provider = new P4ScmProvider(); P4ScmProviderRepository repo = (P4ScmProviderRepository) provider.makeProviderScmRepository( P4PORT + ":" + P4PATH, ':' ); repo.setUser( testProps.getProperty( "P4USER" ) ); repo.setPassword( this.secDispatcher.decrypt( testProps.getProperty( "P4PASS" ) ) ); return repo; } protected boolean hasTestData() { return testProps != null; } protected File getCheckoutDirectory() { return new File( getBasedir(), "target/checkout" ); } protected File getBuildDirectory() { return new File( getBasedir(), "target" ); } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 10055 | Matt Attaway | Pull over numerous changes from @dantran's branch. | ||
//guest/dantran/p4maven/p4maven-provider/src/test/java/com/perforce/maven/scm/provider/p4/AbstractPlexusTestCase.java | |||||
#2 | 9533 | dantran | source format | ||
#1 | 9519 | dantran | folder rename to match with its artifactId | ||
//guest/dantran/p4maven/com.perforce.maven/src/test/java/com/perforce/maven/scm/provider/p4/AbstractPlexusTestCase.java | |||||
#3 | 9259 | dantran |
hookup with maven-scm-test. Pickup user/password from system properties |
||
#2 | 9250 | dantran | caching client across mutiple commands | ||
#1 | 9239 | dantran | back to 2014.6.1-SNAPSHOT, add missing file |