maven-scm-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tryg...@apache.org
Subject cvs commit: maven-scm/maven-scm-providers/maven-scm-provider-svn/src/test/java/org/apache/maven/scm/provider/svn/command/checkin SvnCheckInCommandTest.java
Date Fri, 17 Dec 2004 19:53:41 GMT
trygvis     2004/12/17 11:53:41

  Modified:    maven-scm-providers/maven-scm-provider-svn/src/main/resources/META-INF/plexus
                        components.xml
  Added:       maven-scm-providers/maven-scm-provider-svn/src/main/java/org/apache/maven/scm/provider/svn/command/checkin
                        SvnCheckInCommand.java SvnCheckInConsumer.java
               maven-scm-providers/maven-scm-provider-svn/src/test/java/org/apache/maven/scm/provider/svn/command/checkin
                        SvnCheckInCommandTest.java
  Log:
  o Adding checkin support for SVN.
  
  Revision  Changes    Path
  1.1                  maven-scm/maven-scm-providers/maven-scm-provider-svn/src/main/java/org/apache/maven/scm/provider/svn/command/checkin/SvnCheckInCommand.java
  
  Index: SvnCheckInCommand.java
  ===================================================================
  package org.apache.maven.scm.provider.svn.command.checkin;
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import java.io.File;
  import java.io.IOException;
  
  import org.apache.maven.scm.ScmException;
  import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
  import org.apache.maven.scm.command.checkin.CheckInScmResult;
  import org.apache.maven.scm.provider.ScmProviderRepository;
  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
  
  import org.codehaus.plexus.util.FileUtils;
  import org.codehaus.plexus.util.StringUtils;
  import org.codehaus.plexus.util.cli.CommandLineException;
  import org.codehaus.plexus.util.cli.CommandLineUtils;
  import org.codehaus.plexus.util.cli.Commandline;
  
  /**
   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
   * @version $Id: SvnCheckInCommand.java,v 1.1 2004/12/17 19:53:41 trygvis Exp $
   */
  public class SvnCheckInCommand
      extends AbstractCheckInCommand
  {
      protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, File workingDirectory,
                                                        String message, String tag, File[]
files )
          throws ScmException
      {
          if ( files.length != 0 )
          {
              throw new ScmException( "This command can only commit entire working directories."
);
          }
  
          if ( !StringUtils.isEmpty( tag ) )
          {
              throw new ScmException( "This provider can't handle tags." );
          }
  
          File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null );
  
          try
          {
              FileUtils.fileWrite( messageFile.getAbsolutePath(), message );
          }
          catch( IOException ex )
          {
              return new CheckInScmResult( "Error while making a temporary file for the commit
message: " + ex.getMessage(), null, false );
          }
  
          Commandline cl = createCommandLine( (SvnScmProviderRepository) repo, workingDirectory,
messageFile );
  
          SvnCheckInConsumer consumer = new SvnCheckInConsumer( getLogger(), workingDirectory.getParentFile()
);
  
          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
  
          getLogger().info( "Working directory: " + workingDirectory.getAbsolutePath() );
          getLogger().info( "Command line: " + cl );
  
          int exitCode;
  
          try
          {
              exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
          }
          catch ( CommandLineException ex )
          {
              throw new ScmException( "Error while executing command.", ex );
          }
          finally
          {
              try
              {
                  FileUtils.forceDelete( messageFile );
              }
              catch( IOException ex )
              {
                  // ignore
              }
          }
  
          if ( exitCode != 0 )
          {
              return new CheckInScmResult( "The svn command failed.", stderr.getOutput(),
false );
          }
  
          return new CheckInScmResult( consumer.getCheckedInFiles() );
      }
  
      // ----------------------------------------------------------------------
      //
      // ----------------------------------------------------------------------
  
      public static Commandline createCommandLine( SvnScmProviderRepository repository, File
workingDirectory, File messageFile )
      {
          Commandline cl = new Commandline();
  
          cl.setExecutable( "svn" );
  
          cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
  
          cl.createArgument().setValue( "commit" );
  
          cl.createArgument().setValue( "--non-interactive" );
  
          if ( repository.getUser() != null )
          {
              cl.createArgument().setValue( "--username" );
  
              cl.createArgument().setValue( repository.getUser() );
          }
  
          if ( repository.getPassword() != null )
          {
              cl.createArgument().setValue( "--password" );
  
              cl.createArgument().setValue( repository.getPassword() );
          }
  
          cl.createArgument().setValue( "--file" );
  
          cl.createArgument().setValue( messageFile.getAbsolutePath() );
  
          return cl;
      }
  }
  
  
  
  1.1                  maven-scm/maven-scm-providers/maven-scm-provider-svn/src/main/java/org/apache/maven/scm/provider/svn/command/checkin/SvnCheckInConsumer.java
  
  Index: SvnCheckInConsumer.java
  ===================================================================
  package org.apache.maven.scm.provider.svn.command.checkin;
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import java.util.List;
  import java.util.ArrayList;
  import java.io.File;
  
  import org.apache.maven.scm.ScmFileStatus;
  import org.apache.maven.scm.ScmFile;
  
  import org.codehaus.plexus.logging.Logger;
  import org.codehaus.plexus.util.cli.StreamConsumer;
  
  /**
   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
   * @version $Id: SvnCheckInConsumer.java,v 1.1 2004/12/17 19:53:41 trygvis Exp $
   */
  public class SvnCheckInConsumer
      implements StreamConsumer
  {
      private final static String SENDING_TOKEN = "Sending        ";
  
      private final static String TRANSMITTING_TOKEN = "Transmitting file data";
  
      private final static String COMMITTED_REVISION_TOKEN = "Committed revision";
  
      private Logger logger;
  
      private File workingDirectory;
  
      private List checkedInFiles = new ArrayList();
  
      private int revision;
  
      // ----------------------------------------------------------------------
      //
      // ----------------------------------------------------------------------
  
      public SvnCheckInConsumer( Logger logger, File workingDirectory )
      {
          this.logger = logger;
  
          this.workingDirectory = workingDirectory;
      }
  
      // ----------------------------------------------------------------------
      // StreamConsumer Implementation
      // ----------------------------------------------------------------------
  
      public void consumeLine( String line )
      {
          if ( line.length() <= 3 )
          {
              logger.warn( "Unexpected input, the line must be at least three characters long.
Line: '" + line + "'." );
  
              return;
          }
  
          String statusString = line.substring( 0, 1 );
  
          String file = line.substring( 3 );
  
          if ( line.startsWith( COMMITTED_REVISION_TOKEN ) )
          {
              String revisionString = line.substring( COMMITTED_REVISION_TOKEN.length() +
1, line.length() - 1 );
  
              revision = parseInt( revisionString );
  
              return;
          }
          else if ( line.startsWith( SENDING_TOKEN ) )
          {
              file = line.substring( SENDING_TOKEN.length() );
          }
          else if ( line.startsWith( TRANSMITTING_TOKEN ) )
          {
              // ignore
          }
          else
          {
              logger.info( "Unknown file status: '" + statusString + "'." );
  
              return;
          }
  
          // If the file isn't a file; don't add it.
          if ( !new File( workingDirectory, file ).isFile() )
          {
              return;
          }
  
          checkedInFiles.add( new ScmFile( file, ScmFileStatus.CHECKED_IN ) );
      }
  
      public List getCheckedInFiles()
      {
          return checkedInFiles;
      }
  
      public int getRevision()
      {
          return revision;
      }
  
      // ----------------------------------------------------------------------
      //
      // ----------------------------------------------------------------------
  
      private int parseInt( String revisionString )
      {
          try
          {
              return Integer.parseInt( revisionString );
          }
          catch ( NumberFormatException ex )
          {
              return 0;
          }
      }
  }
  
  
  
  1.3       +6 -0      maven-scm/maven-scm-providers/maven-scm-provider-svn/src/main/resources/META-INF/plexus/components.xml
  
  Index: components.xml
  ===================================================================
  RCS file: /home/cvs/maven-scm/maven-scm-providers/maven-scm-provider-svn/src/main/resources/META-INF/plexus/components.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- components.xml	9 Nov 2004 16:07:17 -0000	1.2
  +++ components.xml	17 Dec 2004 19:53:41 -0000	1.3
  @@ -15,6 +15,12 @@
   
       <component>
         <role>org.apache.maven.scm.provider.svn.command.SvnCommand</role>
  +      <role-hint>check-in</role-hint>
  +      <implementation>org.apache.maven.scm.provider.svn.command.checkin.SvnCheckInCommand</implementation>
  +    </component>
  +
  +    <component>
  +      <role>org.apache.maven.scm.provider.svn.command.SvnCommand</role>
         <role-hint>check-out</role-hint>
         <implementation>org.apache.maven.scm.provider.svn.command.checkout.SvnCheckOutCommand</implementation>
       </component>
  
  
  
  1.1                  maven-scm/maven-scm-providers/maven-scm-provider-svn/src/test/java/org/apache/maven/scm/provider/svn/command/checkin/SvnCheckInCommandTest.java
  
  Index: SvnCheckInCommandTest.java
  ===================================================================
  package org.apache.maven.scm.provider.svn.command.checkin;
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import java.io.File;
  
  import org.apache.maven.scm.ScmTestCase;
  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
  import org.apache.maven.scm.repository.ScmRepository;
  
  import org.codehaus.plexus.util.cli.Commandline;
  
  /**
   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
   * @version $Id: SvnCheckInCommandTest.java,v 1.1 2004/12/17 19:53:41 trygvis Exp $
   */
  public class SvnCheckInCommandTest
      extends ScmTestCase
  {
      private File messageFile;
  
      private String messageFileString;
  
      public void setUp()
          throws Exception
      {
          super.setUp();
  
          messageFile = new File( "commit-message" );
  
          messageFileString = "--file \"" + messageFile.getAbsolutePath() + "\"";
      }
  
      public void testCommandLineWithEmptyTag()
          throws Exception
      {
          testCommandLine( "scm:svn:http://foo.com/svn/trunk",
                           "svn commit --non-interactive " + messageFileString );
      }
  
      public void testCommandLineWithoutTag()
          throws Exception
      {
          testCommandLine( "scm:svn:http://foo.com/svn/trunk",
                           "svn commit --non-interactive " + messageFileString );
      }
  
      public void testCommandLineTag()
          throws Exception
      {
          testCommandLine( "scm:svn:anonymous@http://foo.com/svn/trunk",
                           "svn commit --non-interactive --username anonymous " + messageFileString
);
      }
  
      public void testCommandLineWithUsernameAndTag()
          throws Exception
      {
          testCommandLine( "scm:svn:anonymous@http://foo.com/svn/trunk",
                           "svn commit --non-interactive --username anonymous " + messageFileString
);
      }
  
      // ----------------------------------------------------------------------
      //
      // ----------------------------------------------------------------------
  
      private void testCommandLine( String scmUrl, String commandLine )
          throws Exception
      {
          File workingDirectory = getTestFile( "target/svn-checkin-command-test" );
  
          ScmRepository repository = getScmManager().makeScmRepository( scmUrl );
  
          SvnScmProviderRepository svnRepository = (SvnScmProviderRepository) repository.getProviderRepository();
  
          Commandline cl = SvnCheckInCommand.createCommandLine( svnRepository, workingDirectory,
messageFile );
  
          assertEquals( commandLine, cl.toString() );
      }
  }
  
  
  

Mime
View raw message