Coverage Report - org.apache.maven.plugin.surefire.booterclient.ChecksumCalculator
 
Classes in this File Line Coverage Branch Coverage Complexity
ChecksumCalculator
0%
0/58
0%
0/22
2
 
 1  
 package org.apache.maven.plugin.surefire.booterclient;
 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.artifact.Artifact;
 23  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 24  
 import org.apache.maven.surefire.util.NestedRuntimeException;
 25  
 
 26  
 import java.io.File;
 27  
 import java.io.UnsupportedEncodingException;
 28  
 import java.security.MessageDigest;
 29  
 import java.security.NoSuchAlgorithmException;
 30  
 import java.util.ArrayList;
 31  
 import java.util.Iterator;
 32  
 import java.util.List;
 33  
 import java.util.Map;
 34  
 
 35  
 
 36  
 /**
 37  
  * @author Kristian Rosenvold
 38  
  */
 39  0
 public class ChecksumCalculator
 40  
 {
 41  
     private static final String HEX = "0123456789ABCDEF";
 42  
 
 43  0
     private final List checksumItems = new ArrayList();
 44  
 
 45  
     private void appendObject( Object item )
 46  
     {
 47  0
         checksumItems.add( item );
 48  0
     }
 49  
 
 50  
     public void add( boolean value )
 51  
     {
 52  0
         checksumItems.add( value ? Boolean.TRUE : Boolean.FALSE );
 53  0
     }
 54  
 
 55  
     public void add( int value )
 56  
     {
 57  0
         checksumItems.add( new Integer( value ) );
 58  0
     }
 59  
 
 60  
     public void add( Map map )
 61  
     {
 62  0
         if ( map != null )
 63  
         {
 64  0
             appendObject( map.toString() );
 65  
         }
 66  0
     }
 67  
 
 68  
     public void add( String string )
 69  
     {
 70  0
         appendObject( string );
 71  0
     }
 72  
 
 73  
     public void add( File workingDirectory )
 74  
     {
 75  0
         appendObject( workingDirectory );
 76  0
     }
 77  
 
 78  
     public void add( ArtifactRepository localRepository )
 79  
     {
 80  0
         appendObject( localRepository );
 81  0
     }
 82  
 
 83  
     public void add( List items )
 84  
     {
 85  0
         if ( items != null )
 86  
         {
 87  0
             int size = items.size();
 88  
             Object item;
 89  0
             for ( int i = 0; i < size; i++ )
 90  
             {
 91  0
                 item = items.get( i );
 92  0
                 appendObject( item );
 93  
             }
 94  0
         }
 95  
         else
 96  
         {
 97  0
             appendObject( null );
 98  
         }
 99  
 
 100  0
     }
 101  
 
 102  
     public void add( File[] fileList )
 103  
     {
 104  0
         if ( fileList != null )
 105  
         {
 106  0
             int size = fileList.length;
 107  0
             for ( int i = 0; i < size; i++ )
 108  
             {
 109  0
                 appendObject( fileList[i] );
 110  
             }
 111  0
         }
 112  
         else
 113  
         {
 114  0
             appendObject( null );
 115  
         }
 116  0
     }
 117  
 
 118  
     public void add( Artifact artifact )
 119  
     {
 120  0
         appendObject( artifact != null ? artifact.getId() : null );
 121  0
     }
 122  
 
 123  
     public void add( Boolean aBoolean )
 124  
     {
 125  0
         appendObject( aBoolean );
 126  0
     }
 127  
 
 128  
     private static String asHexString( byte[] bytes )
 129  
     {
 130  0
         if ( bytes == null )
 131  
         {
 132  0
             return null;
 133  
         }
 134  0
         int size = bytes.length;
 135  0
         final StringBuilder result = new StringBuilder( 2 * size );
 136  
         byte b;
 137  0
         for ( int i = 0; i < size; i++ )
 138  
         {
 139  0
             b = bytes[i];
 140  0
             result.append( HEX.charAt( ( b & 0xF0 ) >> 4 ) ).append( HEX.charAt( ( b & 0x0F ) ) );
 141  
         }
 142  0
         return result.toString();
 143  
     }
 144  
 
 145  
     private String getConfig()
 146  
     {
 147  0
         StringBuilder result = new StringBuilder();
 148  
         Object item;
 149  0
         for ( Iterator iter = checksumItems.iterator(); iter.hasNext(); )
 150  
         {
 151  0
             item = iter.next();
 152  0
             result.append( item != null ? item.toString() : "null" );
 153  
         }
 154  0
         return result.toString();
 155  
     }
 156  
 
 157  
     public String getSha1()
 158  
     {
 159  
         try
 160  
         {
 161  0
             MessageDigest md = MessageDigest.getInstance( "SHA-1" );
 162  0
             String configValue = getConfig();
 163  0
             md.update( configValue.getBytes( "iso-8859-1" ), 0, configValue.length() );
 164  0
             byte[] sha1hash = md.digest();
 165  0
             return asHexString( sha1hash );
 166  
         }
 167  0
         catch ( NoSuchAlgorithmException e )
 168  
         {
 169  0
             throw new NestedRuntimeException( e );
 170  
         }
 171  0
         catch ( UnsupportedEncodingException e )
 172  
         {
 173  0
             throw new NestedRuntimeException( e );
 174  
         }
 175  
     }
 176  
 
 177  
 }