View Javadoc
1   package org.apache.maven.scm.provider.perforce.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.ScmTestCase;
23  import org.apache.maven.scm.ScmRevision;
24  import org.apache.maven.scm.ScmVersion;
25  import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
26  import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
27  import org.apache.maven.scm.repository.ScmRepository;
28  import org.codehaus.plexus.util.cli.Commandline;
29  
30  import java.io.File;
31  import java.text.SimpleDateFormat;
32  import java.util.Date;
33  
34  /**
35   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
36   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
37   *
38   */
39  public class PerforceChangeLogCommandTest
40      extends ScmTestCase
41  {
42      private static final File workingDirectory = getTestFile( "target/perforce-changelog-command-test" );
43  
44      private static final String cmdPrefix = "p4 -d " + workingDirectory.getAbsolutePath();
45      
46      @Override
47      protected void tearDown()
48          throws Exception
49      {
50          // Some tests don't expect this property, so when tests are executed in random order these might fail
51          System.clearProperty( PerforceScmProvider.DEFAULT_CLIENTSPEC_PROPERTY );
52      }
53      
54      public void testGetCommandLine()
55          throws Exception
56      {
57          testCommandLine( "scm:perforce://depot/projects/pathname", cmdPrefix + " changes -t ..." );
58      }
59  
60      public void testGetCommandLineWithHost()
61          throws Exception
62      {
63          testCommandLine( "scm:perforce:a:username@//depot/projects/pathname",
64                           cmdPrefix + " -p a -u username changes -t ..." );
65      }
66  
67      public void testGetCommandLineWithHostAndPort()
68          throws Exception
69      {
70          System.setProperty( PerforceScmProvider.DEFAULT_CLIENTSPEC_PROPERTY, "foo" );
71          testCommandLine( "scm:perforce:myhost:1234:username@//depot/projects/pathname",
72                           cmdPrefix + " -p myhost:1234 -u username -c foo changes -t ..." );
73      }
74  
75      // ----------------------------------------------------------------------
76      //
77      // ----------------------------------------------------------------------
78  
79      private void testCommandLine( String scmUrl, String commandLine )
80          throws Exception
81      {
82          ScmRepository repository = getScmManager().makeScmRepository( scmUrl );
83  
84          PerforceScmProviderRepository repo = (PerforceScmProviderRepository) repository.getProviderRepository();
85  
86          Commandline cl = PerforceChangeLogCommand.createCommandLine( repo, workingDirectory,
87              System.getProperty( PerforceScmProvider.DEFAULT_CLIENTSPEC_PROPERTY ),
88               null, null, null, null, null );
89  
90          assertCommandLine( commandLine, null, cl );
91      }
92  
93      public void testGetCommandLineWithStartAndEndDates()
94          throws Exception
95      {
96          System.setProperty( PerforceScmProvider.DEFAULT_CLIENTSPEC_PROPERTY, "foo" );
97          SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
98          testCommandLineDates( cmdPrefix + " -c foo changes -t ...@2008/07/15:00:00:00,2008/07/16:00:00:00", sdf.parse("2008/07/15 00:00:00"), sdf.parse("2008/07/16 00:00:00") );
99      }
100 
101     public void testGetCommandLineWithStartAndEndChangelists()
102         throws Exception
103     {
104         System.setProperty( PerforceScmProvider.DEFAULT_CLIENTSPEC_PROPERTY, "foo" );
105         testCommandLineRevs( cmdPrefix + " -c foo changes -t ...@123456,234567", new ScmRevision( "123456" ), new ScmRevision( "234567" ) );
106     }
107 
108     private void testCommandLineRevs( String commandLine, ScmVersion version1, ScmVersion version2 )
109         throws Exception
110     {
111         ScmRepository repository = getScmManager().makeScmRepository( "scm:perforce://depot/projects/pathname");
112 
113         PerforceScmProviderRepository repo = (PerforceScmProviderRepository) repository.getProviderRepository();
114 
115         Commandline cl = PerforceChangeLogCommand.createCommandLine( repo, workingDirectory, System.getProperty(
116             PerforceScmProvider.DEFAULT_CLIENTSPEC_PROPERTY ), null, null, null, version1, version2 );
117 
118         assertCommandLine( commandLine, null, cl );
119     }
120 
121     private void testCommandLineDates( String commandLine, Date date1, Date date2 )
122         throws Exception
123     {
124         ScmRepository repository = getScmManager().makeScmRepository( "scm:perforce://depot/projects/pathname");
125 
126         PerforceScmProviderRepository repo = (PerforceScmProviderRepository) repository.getProviderRepository();
127 
128         Commandline cl = PerforceChangeLogCommand.createCommandLine( repo, workingDirectory, System.getProperty(
129             PerforceScmProvider.DEFAULT_CLIENTSPEC_PROPERTY ), null, date1, date2, null, null );
130 
131         assertCommandLine( commandLine, null, cl );
132     }
133 }