1 package org.apache.maven.scm.provider.git.gitexe.command;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
38
39 public class GitCommandLineUtilsAddTargetTest
40 {
41
42
43
44
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
58
59 @Test
60 public void testAddTargetWindows()
61 {
62 assumeTrue( runsOnWindows() );
63 final File workingDir = new File( "C:\\prj" );
64
65
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 }