001package org.apache.maven.wagon;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.codehaus.plexus.PlexusTestCase;
023import org.apache.maven.wagon.repository.Repository;
024import org.apache.maven.wagon.authentication.AuthenticationInfo;
025
026/**
027 * Base class for command executor tests.
028 *
029 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
030 *
031 */
032public abstract class CommandExecutorTestCase
033    extends PlexusTestCase
034{
035    public void testErrorInCommandExecuted()
036        throws Exception
037    {
038        CommandExecutor exec = (CommandExecutor) lookup( CommandExecutor.ROLE );
039
040        Repository repository = getTestRepository();
041
042        AuthenticationInfo authenticationInfo = new AuthenticationInfo();
043        authenticationInfo.setUserName( System.getProperty( "user.name" ) );
044
045        exec.connect( repository, authenticationInfo );
046
047        try
048        {
049            exec.executeCommand( "fail" );
050            fail( "Command should have failed" );
051        }
052        catch ( CommandExecutionException e )
053        {
054            assertTrue( e.getMessage().trim().endsWith( "fail: command not found" ) );
055        }
056        finally
057        {
058            exec.disconnect();
059        }
060    }
061
062    public void testIgnoreFailuresInCommandExecuted()
063        throws Exception
064    {
065        CommandExecutor exec = (CommandExecutor) lookup( CommandExecutor.ROLE );
066
067        Repository repository = getTestRepository();
068
069        AuthenticationInfo authenticationInfo = new AuthenticationInfo();
070        authenticationInfo.setUserName( System.getProperty( "user.name" ) );
071
072        exec.connect( repository, authenticationInfo );
073
074        try
075        {
076            Streams streams = exec.executeCommand( "fail", true );
077            //expect no exception, and stderr has something.
078            assertTrue( streams.getErr().length() > 0 );
079        }
080        finally
081        {
082            exec.disconnect();
083        }
084    }
085
086    public void testExecuteSuccessfulCommand()
087        throws Exception
088    {
089        CommandExecutor exec = (CommandExecutor) lookup( CommandExecutor.ROLE );
090
091        Repository repository = getTestRepository();
092
093        AuthenticationInfo authenticationInfo = new AuthenticationInfo();
094        authenticationInfo.setUserName( System.getProperty( "user.name" ) );
095
096        exec.connect( repository, authenticationInfo );
097
098        try
099        {
100            exec.executeCommand( "ls" );
101        }
102        finally
103        {
104            exec.disconnect();
105        }
106    }
107
108    protected abstract Repository getTestRepository();
109}