Coverage Report - org.apache.maven.scm.provider.cvslib.repository.CvsScmProviderRepository
 
Classes in this File Line Coverage Branch Coverage Complexity
CvsScmProviderRepository
97 %
47/48
86 %
19/22
2,091
 
 1  
 package org.apache.maven.scm.provider.cvslib.repository;
 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.provider.ScmProviderRepositoryWithHost;
 23  
 import org.apache.maven.scm.provider.cvslib.AbstractCvsScmProvider;
 24  
 
 25  
 /**
 26  
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 27  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 28  
  * @version $Id: CvsScmProviderRepository.java 1306867 2012-03-29 13:45:10Z olamy $
 29  
  */
 30  
 public class CvsScmProviderRepository
 31  
     extends ScmProviderRepositoryWithHost
 32  
 {
 33  
     /** */
 34  
     private String cvsroot;
 35  
 
 36  
     /** */
 37  
     private String transport;
 38  
 
 39  
     /** */
 40  
     private String path;
 41  
 
 42  
     /** */
 43  
     private String module;
 44  
 
 45  
     public CvsScmProviderRepository( String cvsroot, String transport, String user, String password, String host,
 46  
                                      String path, String module )
 47  
     {
 48  4
         this( cvsroot, transport, user, password, host, -1, path, module );
 49  4
     }
 50  
 
 51  
     public CvsScmProviderRepository( String cvsroot, String transport, String user, String password, String host,
 52  
                                      int port, String path, String module )
 53  21
     {
 54  21
         this.cvsroot = cvsroot;
 55  
 
 56  21
         this.transport = transport;
 57  
 
 58  21
         if ( user == null && AbstractCvsScmProvider.TRANSPORT_EXT.equals( transport ) )
 59  
         {
 60  0
             user = System.getProperty( "user.name" );
 61  
         }
 62  
 
 63  21
         setUser( user );
 64  
 
 65  21
         setPassword( password );
 66  
 
 67  21
         setHost( host );
 68  
 
 69  21
         setPort( port );
 70  
 
 71  21
         this.path = path;
 72  
 
 73  21
         this.module = module;
 74  21
     }
 75  
 
 76  
     /**
 77  
      * @return The cvs root
 78  
      */
 79  
     public String getCvsRoot()
 80  
     {
 81  16
         String root = getCvsRootForCvsPass();
 82  
 
 83  15
         return removeDefaultPortFromCvsRoot( root );
 84  
     }
 85  
 
 86  
     private String removeDefaultPortFromCvsRoot( String root )
 87  
     {
 88  17
         if ( root != null && root.indexOf( ":2401" ) > 0 )
 89  
         {
 90  13
             root = root.substring( 0, root.indexOf( ":2401" ) ) + ":" + root.substring( root.indexOf( ":2401" ) + 5 );
 91  
         }
 92  
 
 93  17
         return root;
 94  
     }
 95  
 
 96  
     /**
 97  
      * @return The cvs root stored in .cvspass
 98  
      */
 99  
     public String getCvsRootForCvsPass()
 100  
     {
 101  
         String result;
 102  24
         String transport = getTransport();
 103  24
         if ( AbstractCvsScmProvider.TRANSPORT_LOCAL.equals( transport ) )
 104  
         {
 105  3
             result = ":" + transport + ":" + cvsroot;
 106  
         }
 107  21
         else if ( getUser() != null )
 108  
         {
 109  20
             result = getCvsRootWithCorrectUser( getUser() );
 110  
         }
 111  
         else
 112  
         {
 113  1
             throw new IllegalArgumentException( "Username isn't defined." );
 114  
         }
 115  23
         return result;
 116  
     }
 117  
 
 118  
     /**
 119  
      * @return The subtype (like pserver).
 120  
      */
 121  
     public String getTransport()
 122  
     {
 123  80
         return transport;
 124  
     }
 125  
 
 126  
     /**
 127  
      * @return The path.
 128  
      */
 129  
     public String getPath()
 130  
     {
 131  9
         return path;
 132  
     }
 133  
 
 134  
     /**
 135  
      * @return The module name.
 136  
      */
 137  
     public String getModule()
 138  
     {
 139  10
         return module;
 140  
     }
 141  
 
 142  
     private String getCvsRootWithCorrectUser()
 143  
     {
 144  2
         return getCvsRootWithCorrectUser( null );
 145  
     }
 146  
 
 147  
     /**
 148  
      * @param user user name
 149  
      * @return
 150  
      */
 151  
     private String getCvsRootWithCorrectUser( String user )
 152  
     {
 153  
         //:transport:rest_of_cvsroot
 154  22
         int indexOfUsername = getTransport().length() + 2;
 155  
 
 156  22
         int indexOfAt = cvsroot.indexOf( '@' );
 157  
 
 158  22
         String userString = user == null ? "" : ":" + user;
 159  
 
 160  22
         if ( indexOfAt > 0 )
 161  
         {
 162  20
             cvsroot = ":" + getTransport() + userString + cvsroot.substring( indexOfAt );
 163  
         }
 164  
         else
 165  
         {
 166  2
             cvsroot = ":" + getTransport() + userString + "@" + cvsroot.substring( indexOfUsername );
 167  
         }
 168  
 
 169  22
         return cvsroot;
 170  
     }
 171  
 
 172  
     /** {@inheritDoc} */
 173  
     public String toString()
 174  
     {
 175  9
         StringBuilder sb = new StringBuilder();
 176  
 
 177  9
         if ( getUser() == null )
 178  
         {
 179  3
             if ( AbstractCvsScmProvider.TRANSPORT_LOCAL.equals( getTransport() ) )
 180  
             {
 181  1
                 sb.append( getCvsRoot() );
 182  
             }
 183  
             else
 184  
             {
 185  2
                 sb.append( removeDefaultPortFromCvsRoot( getCvsRootWithCorrectUser() ) );
 186  
             }
 187  
         }
 188  
         else
 189  
         {
 190  6
             sb.append( getCvsRoot() );
 191  
         }
 192  9
         sb.append( ":" );
 193  9
         sb.append( getModule() );
 194  
 
 195  
         /* remove first colon */
 196  9
         if ( sb.charAt( 0 ) == ':' )
 197  
         {
 198  9
             sb.deleteCharAt( 0 );
 199  
         }
 200  9
         return sb.toString();
 201  
     }
 202  
 
 203  
 }