View Javadoc
1   package org.apache.maven.scm.provider.git.gitexe.command.changelog;
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.apache.maven.scm.ScmBranch;
23  import org.apache.maven.scm.ScmRevision;
24  import org.apache.maven.scm.ScmTestCase;
25  import org.apache.maven.scm.ScmVersion;
26  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
27  import org.apache.maven.scm.repository.ScmRepository;
28  import org.codehaus.plexus.util.StringUtils;
29  import org.codehaus.plexus.util.cli.Commandline;
30  
31  import java.io.File;
32  import java.util.Calendar;
33  import java.util.Date;
34  
35  /**
36   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
37   *
38   */
39  public class GitChangeLogCommandTest
40      extends ScmTestCase
41  {
42      private File workingDirectory;
43      
44      public void setUp() throws Exception
45      {
46          super.setUp();
47          
48          workingDirectory = getTestFile( "target/git-update-command-test" );
49      }
50  
51      public void testCommandLineNoDates()
52          throws Exception
53      {
54          testCommandLine( "scm:git:http://foo.com/git", null, (Date) null, (Date) null, 40,
55                           "git whatchanged --date=iso --max-count=40"
56                           + " -- " + StringUtils.quoteAndEscape( workingDirectory.getPath(), '"' ) );
57      }
58  
59      public void testCommandLineNoDatesLimitedCount()
60          throws Exception
61      {
62          testCommandLine( "scm:git:http://foo.com/git", null, (Date) null, (Date) null,
63                           "git whatchanged --date=iso"
64                           + " -- " + StringUtils.quoteAndEscape( workingDirectory.getPath(), '"' ) );
65      }
66  
67      public void testCommandLineWithDates()
68          throws Exception
69      {
70          Date startDate = getDate( 2003, Calendar.SEPTEMBER, 10, GMT_TIME_ZONE );
71          Date endDate = getDate( 2007, Calendar.OCTOBER, 10, GMT_TIME_ZONE );
72  
73          testCommandLine( "scm:git:http://foo.com/git", null, startDate, endDate,
74                           "git whatchanged \"--since=2003-09-10 00:00:00 +0000\" \"--until=2007-10-10 00:00:00 +0000\" --date=iso" 
75                           + " -- " + StringUtils.quoteAndEscape( workingDirectory.getPath(), '"' ) );
76      }
77  
78      public void testCommandLineStartDateOnly()
79          throws Exception
80      {
81          Date startDate = getDate( 2003, Calendar.SEPTEMBER, 10, 1, 1, 1, GMT_TIME_ZONE );
82  
83          testCommandLine( "scm:git:http://foo.com/git", null, startDate, null,
84                           "git whatchanged \"--since=2003-09-10 01:01:01 +0000\" --date=iso" 
85                           + " -- " + StringUtils.quoteAndEscape( workingDirectory.getPath(), '"' ) );
86      }
87  
88      public void testCommandLineDateFormat()
89          throws Exception
90      {
91          Date startDate = getDate( 2003, Calendar.SEPTEMBER, 10, 1, 1, 1, GMT_TIME_ZONE );
92          Date endDate = getDate( 2005, Calendar.NOVEMBER, 13, 23, 23, 23, GMT_TIME_ZONE );
93  
94          testCommandLine( "scm:git:http://foo.com/git", null, startDate, endDate,
95                           "git whatchanged \"--since=2003-09-10 01:01:01 +0000\" \"--until=2005-11-13 23:23:23 +0000\" --date=iso"
96                           + " -- " + StringUtils.quoteAndEscape( workingDirectory.getPath(), '"' ) );
97      }
98  
99      public void testCommandLineDateVersionRanges()
100         throws Exception
101     {
102         Date startDate = getDate( 2003, Calendar.SEPTEMBER, 10, 1, 1, 1, GMT_TIME_ZONE );
103         Date endDate = getDate( 2005, Calendar.NOVEMBER, 13, 23, 23, 23, GMT_TIME_ZONE );
104     
105         testCommandLine( "scm:git:http://foo.com/git", null, startDate, endDate, new ScmRevision( "1" ), new ScmRevision( "10" ),
106                          "git whatchanged \"--since=2003-09-10 01:01:01 +0000\" \"--until=2005-11-13 23:23:23 +0000\" --date=iso 1..10"
107                          + " -- " + StringUtils.quoteAndEscape( workingDirectory.getPath(), '"' ) );
108     }
109     
110     public void testCommandLineEndDateOnly()
111         throws Exception
112     {
113         Date endDate = getDate( 2003, Calendar.NOVEMBER, 10, GMT_TIME_ZONE );
114 
115         // Only specifying end date should print no dates at all
116         testCommandLine( "scm:git:http://foo.com/git", null, null, endDate,
117                          "git whatchanged \"--until=2003-11-10 00:00:00 +0000\" --date=iso"
118                          + " -- " + StringUtils.quoteAndEscape( workingDirectory.getPath(), '"' ) );
119     }
120 
121     public void testCommandLineWithBranchNoDates()
122         throws Exception
123     {
124         testCommandLine( "scm:git:http://foo.com/git", new ScmBranch( "my-test-branch" ), (Date) null, (Date) null, 
125                          "git whatchanged --date=iso my-test-branch"
126                          + " -- " + StringUtils.quoteAndEscape( workingDirectory.getPath(), '"' ) );
127     }
128 
129 
130     public void testCommandLineWithStartVersion()
131         throws Exception
132     {
133         testCommandLine( "scm:git:http://foo.com/git", null, new ScmRevision( "1" ), null, 
134                          "git whatchanged --date=iso 1.."
135                          + " -- " + StringUtils.quoteAndEscape( workingDirectory.getPath(), '"' ) );
136     }
137 
138     public void testCommandLineWithStartVersionAndEndVersion()
139         throws Exception
140     {
141         testCommandLine( "scm:git:http://foo.com/git", null, new ScmRevision( "1" ), new ScmRevision( "10" ), 
142                          "git whatchanged --date=iso 1..10"
143                          + " -- " + StringUtils.quoteAndEscape( workingDirectory.getPath(), '"' ) );
144     }
145 
146     public void testCommandLineWithStartVersionAndEndVersionEquals()
147         throws Exception
148     {
149         testCommandLine( "scm:git:http://foo.com/git", null, new ScmRevision( "1" ), new ScmRevision( "1" ), 
150                          "git whatchanged --date=iso 1..1"
151                          + " -- " + StringUtils.quoteAndEscape( workingDirectory.getPath(), '"' ) );
152     }
153 
154     public void testCommandLineWithStartVersionAndEndVersionAndBranch()
155         throws Exception
156     {
157         testCommandLine( "scm:git:http://foo.com/git", new ScmBranch( "my-test-branch" ), new ScmRevision( "1" ), new ScmRevision( "10" ), 
158                          "git whatchanged --date=iso 1..10 my-test-branch"
159                          + " -- " + StringUtils.quoteAndEscape( workingDirectory.getPath(), '"' ) );
160     }
161 
162     // ----------------------------------------------------------------------
163     // private helper functions
164     // ----------------------------------------------------------------------
165 
166     private void testCommandLine( String scmUrl, ScmBranch branch, Date startDate, Date endDate, String commandLine )
167         throws Exception
168     {
169         testCommandLine( scmUrl, branch, startDate, endDate, null, commandLine );
170     }
171 
172     private void testCommandLine( String scmUrl, ScmBranch branch, Date startDate, Date endDate, Integer limit, String commandLine )
173         throws Exception
174     {
175         ScmRepository repository = getScmManager().makeScmRepository( scmUrl );
176 
177         GitScmProviderRepository gitRepository = (GitScmProviderRepository) repository.getProviderRepository();
178 
179         Commandline cl = GitChangeLogCommand.createCommandLine( gitRepository, workingDirectory, branch, startDate,
180                                                                 endDate, null, null, limit );
181 
182         assertCommandLine( commandLine, workingDirectory, cl );
183     }
184 
185     private void testCommandLine( String scmUrl, ScmBranch branch, ScmVersion startVersion, ScmVersion endVersion, String commandLine )
186         throws Exception
187     {
188         ScmRepository repository = getScmManager().makeScmRepository( scmUrl );
189 
190         GitScmProviderRepository gitRepository = (GitScmProviderRepository) repository.getProviderRepository();
191 
192         Commandline cl = GitChangeLogCommand.createCommandLine( gitRepository, workingDirectory, branch, null, null,
193                                                                 startVersion, endVersion );
194 
195         assertCommandLine( commandLine, workingDirectory, cl );
196     }
197 
198     private void testCommandLine( String scmUrl, ScmBranch branch, Date startDate, Date endDate, 
199                                   ScmVersion startVersion, ScmVersion endVersion, String commandLine )
200         throws Exception
201     {
202         ScmRepository repository = getScmManager().makeScmRepository( scmUrl );
203     
204         GitScmProviderRepository gitRepository = (GitScmProviderRepository) repository.getProviderRepository();
205     
206         Commandline cl = GitChangeLogCommand.createCommandLine( gitRepository, workingDirectory, branch, startDate, endDate,
207                                                                 startVersion, endVersion );
208     
209         assertCommandLine( commandLine, workingDirectory, cl );
210     }
211 }