View Javadoc
1   package org.apache.maven.wagon;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.codehaus.plexus.PlexusTestCase;
23  import org.apache.maven.wagon.repository.Repository;
24  import org.apache.maven.wagon.authentication.AuthenticationInfo;
25  
26  /**
27   * Base class for command executor tests.
28   *
29   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
30   *
31   */
32  public abstract class CommandExecutorTestCase
33      extends PlexusTestCase
34  {
35      public void testErrorInCommandExecuted()
36          throws Exception
37      {
38          CommandExecutor exec = (CommandExecutor) lookup( CommandExecutor.ROLE );
39  
40          Repository repository = getTestRepository();
41  
42          AuthenticationInfo authenticationInfo = new AuthenticationInfo();
43          authenticationInfo.setUserName( System.getProperty( "user.name" ) );
44  
45          exec.connect( repository, authenticationInfo );
46  
47          try
48          {
49              exec.executeCommand( "fail" );
50              fail( "Command should have failed" );
51          }
52          catch ( CommandExecutionException e )
53          {
54              assertTrue( e.getMessage().trim().endsWith( "fail: command not found" ) );
55          }
56          finally
57          {
58              exec.disconnect();
59          }
60      }
61  
62      public void testIgnoreFailuresInCommandExecuted()
63          throws Exception
64      {
65          CommandExecutor exec = (CommandExecutor) lookup( CommandExecutor.ROLE );
66  
67          Repository repository = getTestRepository();
68  
69          AuthenticationInfo authenticationInfo = new AuthenticationInfo();
70          authenticationInfo.setUserName( System.getProperty( "user.name" ) );
71  
72          exec.connect( repository, authenticationInfo );
73  
74          try
75          {
76              Streams streams = exec.executeCommand( "fail", true );
77              //expect no exception, and stderr has something.
78              assertTrue( streams.getErr().length() > 0 );
79          }
80          finally
81          {
82              exec.disconnect();
83          }
84      }
85  
86      public void testExecuteSuccessfulCommand()
87          throws Exception
88      {
89          CommandExecutor exec = (CommandExecutor) lookup( CommandExecutor.ROLE );
90  
91          Repository repository = getTestRepository();
92  
93          AuthenticationInfo authenticationInfo = new AuthenticationInfo();
94          authenticationInfo.setUserName( System.getProperty( "user.name" ) );
95  
96          exec.connect( repository, authenticationInfo );
97  
98          try
99          {
100             exec.executeCommand( "ls" );
101         }
102         finally
103         {
104             exec.disconnect();
105         }
106     }
107 
108     protected abstract Repository getTestRepository();
109 }