View Javadoc
1   package org.apache.maven.scm.provider.git.gitexe.command;
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 static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assume.assumeTrue;
26  
27  import java.io.File;
28  import java.text.MessageFormat;
29  import java.util.Arrays;
30  import java.util.List;
31  
32  import org.codehaus.plexus.util.Os;
33  import org.codehaus.plexus.util.cli.Commandline;
34  import org.junit.Test;
35  
36  /**
37   * @author mfriedenhagen
38   */
39  public class GitCommandLineUtilsAddTargetTest
40  {
41  
42      /**
43       * Test of addTarget method, of class GitCommandLineUtils on Non-Windows
44       * systems.
45       */
46      @Test
47      public void testAddTargetNonWindows()
48      {
49          assumeTrue( !runsOnWindows() );
50          final File workingDir = new File( "/prj" );
51          final List<File> filesToAdd = Arrays.asList( new File( "/prj/pom.xml" ), new File( "/prj/mod1/pom.xml" ) );
52          final String expectedArguments = "[add, pom.xml, mod1/pom.xml]";
53          check( workingDir, filesToAdd, expectedArguments );
54      }
55  
56      /**
57       * Test of addTarget method, of class GitCommandLineUtils on Windows.
58       */
59      @Test
60      public void testAddTargetWindows()
61      {
62          assumeTrue( runsOnWindows() );
63          final File workingDir = new File( "C:\\prj" );
64          // Note that the second file has a lowercase drive letter, see
65          // https://jira.codehaus.org/browse/SCM-667
66          final List<File> filesToAdd = Arrays.asList( new File( "C:\\prj\\pom.xml" ),
67              new File( "c:\\prj\\mod1\\pom.xml" ) );
68          final String expectedArguments = "[add, pom.xml, mod1\\pom.xml]";
69          check( workingDir, filesToAdd, expectedArguments );
70      }
71  
72      private void check( final File workingDir, final List<File> filesToAdd, final String expectedArguments )
73      {
74          final Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDir, "add" );
75          GitCommandLineUtils.addTarget( cl, filesToAdd );
76          final String arguments = Arrays.toString( cl.getArguments() );
77          assertEquals( 3, cl.getArguments().length );
78          assertEquals( expectedArguments, arguments );
79      }
80  
81      @Test
82      public void testPasswordAnonymous()
83          throws Exception
84      {
85  
86          String commandLine = "git push https://user:password@foo.com/git/trunk refs/tags/my-tag-1";
87  
88          final Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( new File( "." ), commandLine );
89  
90          String[] commandLineArgs = cl.getShellCommandline();
91  
92          //
93          for ( int i = 0; i < commandLineArgs.length; i++ )
94          {
95              assertFalse( MessageFormat.format( "The target log message should not contain <{0}> but it contains <{1}>",
96                  AnonymousCommandLine.PASSWORD_PLACE_HOLDER, commandLineArgs[i] ),
97                  commandLineArgs[i].contains( AnonymousCommandLine.PASSWORD_PLACE_HOLDER ) );
98          }
99  
100         final String scmUrlFakeForTest = "https://user:".concat( AnonymousCommandLine.PASSWORD_PLACE_HOLDER ).concat(
101             "@foo.com/git/trunk" );
102 
103         assertTrue( MessageFormat.format( "The target log message should contain <{0}> but it contains <{1}>",
104             scmUrlFakeForTest, cl.toString() ), cl.toString().contains( scmUrlFakeForTest ) );
105     }
106 
107     private boolean runsOnWindows()
108     {
109         return Os.isFamily( Os.FAMILY_WINDOWS );
110     }
111 }