Coverage Report - org.apache.maven.shared.release.util.ReleaseUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
ReleaseUtil
89%
74/83
80%
37/46
3,231
 
 1  
 package org.apache.maven.shared.release.util;
 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 java.io.File;
 23  
 import java.io.IOException;
 24  
 import java.io.Reader;
 25  
 import java.util.List;
 26  
 import java.util.Locale;
 27  
 
 28  
 import org.apache.commons.lang.StringUtils;
 29  
 import org.apache.maven.project.MavenProject;
 30  
 import org.apache.maven.shared.release.ReleaseExecutionException;
 31  
 import org.apache.maven.shared.release.config.ReleaseDescriptor;
 32  
 import org.codehaus.plexus.util.FileUtils;
 33  
 import org.codehaus.plexus.util.IOUtil;
 34  
 import org.codehaus.plexus.util.ReaderFactory;
 35  
 
 36  
 /**
 37  
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 38  
  * @version $Id: ReleaseUtil.java 1348252 2012-06-08 21:34:01Z rfscholte $
 39  
  */
 40  
 public class ReleaseUtil
 41  
 {
 42  
     public static final String RELEASE_POMv4 = "release-pom.xml";
 43  
 
 44  
     public static final String POMv4 = "pom.xml";
 45  
 
 46  2
     private static final String FS = File.separator;
 47  
 
 48  
     /**
 49  
      * The line separator to use.
 50  
      */
 51  2
     public static final String LS = System.getProperty( "line.separator" );
 52  
 
 53  
     private ReleaseUtil()
 54  0
     {
 55  
         // noop
 56  0
     }
 57  
 
 58  
     public static MavenProject getRootProject( List<MavenProject> reactorProjects )
 59  
     {
 60  598
         MavenProject project = reactorProjects.get( 0 );
 61  598
         for ( MavenProject currentProject : reactorProjects )
 62  
         {
 63  1340
             if ( currentProject.isExecutionRoot() )
 64  
             {
 65  0
                 project = currentProject;
 66  0
                 break;
 67  
             }
 68  
         }
 69  
 
 70  598
         return project;
 71  
     }
 72  
 
 73  
     public static File getStandardPom( MavenProject project )
 74  
     {
 75  1182
         if ( project == null )
 76  
         {
 77  2
             return null;
 78  
         }
 79  
 
 80  1180
         File pom = project.getFile();
 81  
 
 82  1180
         if ( pom == null )
 83  
         {
 84  0
             return null;
 85  
         }
 86  
 
 87  1180
         File releasePom = getReleasePom( project );
 88  1180
         if ( pom.equals( releasePom ) )
 89  
         {
 90  0
             pom = new File( pom.getParent(), POMv4 );
 91  
         }
 92  
 
 93  1180
         return pom;
 94  
     }
 95  
 
 96  
     public static File getReleasePom( MavenProject project )
 97  
     {
 98  1692
         if ( project == null )
 99  
         {
 100  2
             return null;
 101  
         }
 102  
 
 103  1690
         File pom = project.getFile();
 104  
 
 105  1690
         if ( pom == null )
 106  
         {
 107  0
             return null;
 108  
         }
 109  
 
 110  1690
         return new File( pom.getParent(), RELEASE_POMv4 );
 111  
     }
 112  
 
 113  
     /**
 114  
      * Gets the string contents of the specified XML file. Note: In contrast to an XML processor, the line separators in
 115  
      * the returned string will be normalized to use the platform's native line separator. This is basically to save
 116  
      * another normalization step when writing the string contents back to an XML file.
 117  
      *
 118  
      * @param file The path to the XML file to read in, must not be <code>null</code>.
 119  
      * @return The string contents of the XML file.
 120  
      * @throws IOException If the file could not be opened/read.
 121  
      */
 122  
     public static String readXmlFile( File file )
 123  
         throws IOException
 124  
     {
 125  80
         return readXmlFile( file, LS );
 126  
     }
 127  
 
 128  
     public static String readXmlFile( File file, String ls )
 129  
         throws IOException
 130  
     {
 131  632
         Reader reader = null;
 132  
         try
 133  
         {
 134  632
             reader = ReaderFactory.newXmlReader( file );
 135  632
             return normalizeLineEndings( IOUtil.toString( reader ), ls );
 136  
         }
 137  
         finally
 138  
         {
 139  632
             IOUtil.close( reader );
 140  
         }
 141  
     }
 142  
 
 143  
     /**
 144  
      * Normalizes the line separators in the specified string.
 145  
      *
 146  
      * @param text      The string to normalize, may be <code>null</code>.
 147  
      * @param separator The line separator to use for normalization, typically "\n" or "\r\n", must not be
 148  
      *                  <code>null</code>.
 149  
      * @return The input string with normalized line separators or <code>null</code> if the string was <code>null</code>
 150  
      *         .
 151  
      */
 152  
     public static String normalizeLineEndings( String text, String separator )
 153  
     {
 154  1532
         String norm = text;
 155  1532
         if ( text != null )
 156  
         {
 157  1532
             norm = text.replaceAll( "(\r\n)|(\n)|(\r)", separator );
 158  
         }
 159  1532
         return norm;
 160  
     }
 161  
 
 162  
     public static ReleaseDescriptor createBasedirAlignedReleaseDescriptor( ReleaseDescriptor releaseDescriptor,
 163  
                                                                            List<MavenProject> reactorProjects )
 164  
         throws ReleaseExecutionException
 165  
     {
 166  
         String basedir;
 167  
         try
 168  
         {
 169  18
             basedir = getCommonBasedir( reactorProjects );
 170  
         }
 171  0
         catch ( IOException e )
 172  
         {
 173  0
             throw new ReleaseExecutionException( "Exception occurred while calculating common basedir: "
 174  
                 + e.getMessage(), e );
 175  18
         }
 176  
 
 177  18
         int parentLevels =
 178  
             getBaseWorkingDirectoryParentCount( basedir,
 179  
                                                 FileUtils.normalize( releaseDescriptor.getWorkingDirectory() ) );
 180  
 
 181  18
         String url = releaseDescriptor.getScmSourceUrl();
 182  18
         url = realignScmUrl( parentLevels, url );
 183  
 
 184  18
         ReleaseDescriptor descriptor = new ReleaseDescriptor();
 185  18
         descriptor.setWorkingDirectory( basedir );
 186  18
         descriptor.setScmSourceUrl( url );
 187  18
         return descriptor;
 188  
     }
 189  
 
 190  
     public static String getCommonBasedir( List<MavenProject> reactorProjects )
 191  
         throws IOException
 192  
     {
 193  542
         return getCommonBasedir( reactorProjects, FS );
 194  
     }
 195  
 
 196  
     public static String getCommonBasedir( List<MavenProject> reactorProjects, String separator )
 197  
         throws IOException
 198  
     {
 199  566
         String[] baseDirs = new String[reactorProjects.size()];
 200  566
         int idx = 0;
 201  566
         for ( MavenProject p : reactorProjects )
 202  
         {
 203  1772
             String dir = p.getBasedir().getCanonicalPath();
 204  
 
 205  
             // always end with separator so that we know what is a path and what is a partial directory name in the
 206  
             // next call
 207  1772
             if ( !dir.endsWith( separator ) )
 208  
             {
 209  1772
                 dir = dir + separator;
 210  
             }
 211  1772
             baseDirs[idx++] = dir;
 212  1772
         }
 213  
 
 214  566
         String basedir = StringUtils.getCommonPrefix( baseDirs );
 215  
 
 216  566
         int separatorPos = basedir.lastIndexOf( separator );
 217  566
         if ( !basedir.endsWith( separator ) && separatorPos >= 0 )
 218  
         {
 219  4
             basedir = basedir.substring( 0, separatorPos );
 220  
         }
 221  
 
 222  566
         if ( basedir.endsWith( separator ) && basedir.length() > 1 )
 223  
         {
 224  562
             basedir = basedir.substring( 0, basedir.length() - 1 );
 225  
         }
 226  
 
 227  566
         return basedir;
 228  
     }
 229  
 
 230  
     public static int getBaseWorkingDirectoryParentCount( String basedir, String workingDirectory )
 231  
     {
 232  96
         int num = 0;
 233  
 
 234  
         // we can safely assume case-insensitivity as we are just backtracking, not comparing. This helps with issues
 235  
         // on Windows with C: vs c:
 236  96
         workingDirectory = workingDirectory.toLowerCase( Locale.ENGLISH );
 237  96
         basedir = basedir.toLowerCase( Locale.ENGLISH );
 238  
 
 239  
         // MRELEASE-663
 240  
         // For Windows is does matter if basedir ends with a file-separator or not to be able to compare.
 241  
         // Using the parent of a dummy file makes it possible to compare them OS-independent
 242  96
         File workingDirectoryFile = new File( workingDirectory, ".tmp" ).getParentFile();
 243  96
         File basedirFile = new File( basedir, ".tmp" ).getParentFile();
 244  
 
 245  96
         if ( !workingDirectoryFile.equals( basedirFile ) && workingDirectory.startsWith( basedir ) )
 246  
         {
 247  
             do
 248  
             {
 249  42
                 workingDirectoryFile = workingDirectoryFile.getParentFile();
 250  42
                 num++;
 251  
             }
 252  42
             while ( !workingDirectoryFile.equals( basedirFile ) );
 253  
         }
 254  96
         return num;
 255  
     }
 256  
 
 257  
     public static String realignScmUrl( int parentLevels, String url )
 258  
     {
 259  204
         if ( !StringUtils.isEmpty( url ) )
 260  
         {
 261  204
             int index = url.length();
 262  204
             String suffix = "";
 263  204
             if ( url.endsWith( "/" ) )
 264  
             {
 265  20
                 index--;
 266  20
                 suffix = "/";
 267  
             }
 268  256
             for ( int i = 0; i < parentLevels && index > 0; i++ )
 269  
             {
 270  52
                 index = url.lastIndexOf( '/', index - 1 );
 271  
             }
 272  
 
 273  204
             if ( index > 0 )
 274  
             {
 275  200
                 url = url.substring( 0, index ) + suffix;
 276  
             }
 277  
         }
 278  204
         return url;
 279  
     }
 280  
 
 281  
     public static boolean isSymlink( File file )
 282  
         throws IOException
 283  
     {
 284  192
         return !file.getAbsolutePath().equals( file.getCanonicalPath() );
 285  
     }
 286  
 }