Coverage Report - org.apache.maven.surefire.booter.Classpath
 
Classes in this File Line Coverage Branch Coverage Complexity
Classpath
54%
42/77
47%
19/40
2,5
 
 1  
 package org.apache.maven.surefire.booter;
 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.surefire.util.UrlUtils;
 23  
 
 24  
 import java.io.File;
 25  
 import java.net.MalformedURLException;
 26  
 import java.net.URL;
 27  
 import java.util.ArrayList;
 28  
 import java.util.Collection;
 29  
 import java.util.Collections;
 30  
 import java.util.Iterator;
 31  
 import java.util.LinkedHashSet;
 32  
 import java.util.List;
 33  
 
 34  
 /**
 35  
  * An ordered list of classpath elements with set behaviour
 36  
  *
 37  
  * A Classpath is immutable and thread safe.
 38  
  *
 39  
  * Immutable and thread safe
 40  
  *
 41  
  * @author Kristian Rosenvold
 42  
  */
 43  
 public class Classpath implements Iterable<String>
 44  
 {
 45  
 
 46  
     private final List<String> unmodifiableElements;
 47  
 
 48  
     public static Classpath join( Classpath firstClasspath, Classpath secondClasspath )
 49  
     {
 50  3
         LinkedHashSet<String> accumulated =  new LinkedHashSet<String>(  );
 51  3
         if (firstClasspath != null) firstClasspath.addTo( accumulated );
 52  3
         if (secondClasspath != null) secondClasspath.addTo( accumulated );
 53  3
         return new Classpath( accumulated );
 54  
     }
 55  
 
 56  
 
 57  
     private void addTo(Collection<String> c){
 58  4
         c.addAll( unmodifiableElements );
 59  4
     }
 60  
 
 61  
     private Classpath()
 62  15
     {
 63  15
         this.unmodifiableElements = Collections.emptyList();
 64  15
     }
 65  
 
 66  
 
 67  
     public Classpath( Classpath other, String additionalElement )
 68  21
     {
 69  21
         ArrayList<String> elems = new ArrayList<String>( other.unmodifiableElements );
 70  21
         elems.add( additionalElement );
 71  21
         this.unmodifiableElements = Collections.unmodifiableList( elems );
 72  21
     }
 73  
 
 74  
     public Classpath( Iterable<String> elements )
 75  6
     {
 76  6
         List<String> newCp = new ArrayList<String>(  );
 77  6
         for ( String element : elements )
 78  
         {
 79  7
             newCp.add( element );
 80  7
         }
 81  6
         this.unmodifiableElements = Collections.unmodifiableList( newCp );
 82  6
     }
 83  
 
 84  
     public static Classpath emptyClasspath()
 85  
     {
 86  15
         return new Classpath();
 87  
     }
 88  
 
 89  
     public Classpath addClassPathElementUrl( String path )
 90  
     {
 91  24
         if ( path == null )
 92  
         {
 93  2
             throw new IllegalArgumentException( "Null is not a valid class path element url." );
 94  
         }
 95  22
         return !unmodifiableElements.contains( path ) ? new Classpath( this, path ) : this;
 96  
     }
 97  
 
 98  
     public List<String> getClassPath()
 99  
     {
 100  9
         return unmodifiableElements;
 101  
     }
 102  
 
 103  
     public List<URL> getAsUrlList()
 104  
         throws MalformedURLException
 105  
     {
 106  1
         List<URL> urls = new ArrayList<URL>();
 107  1
         for ( String url : unmodifiableElements )
 108  
         {
 109  2
             File f = new File( url );
 110  2
             urls.add( UrlUtils.getURL( f ) );
 111  2
         }
 112  1
         return urls;
 113  
     }
 114  
 
 115  
     public void writeToSystemProperty( String propertyName )
 116  
     {
 117  2
         StringBuilder sb = new StringBuilder();
 118  2
         for ( String element : unmodifiableElements )
 119  
         {
 120  2
             sb.append( element ).append( File.pathSeparatorChar );
 121  2
         }
 122  2
         System.setProperty( propertyName, sb.toString() );
 123  2
     }
 124  
 
 125  
     public boolean equals( Object o )
 126  
     {
 127  2
         if ( this == o )
 128  
         {
 129  0
             return true;
 130  
         }
 131  2
         if ( o == null || getClass() != o.getClass() )
 132  
         {
 133  0
             return false;
 134  
         }
 135  
 
 136  2
         Classpath classpath = (Classpath) o;
 137  
 
 138  2
         return !( unmodifiableElements
 139  
             != null ? !unmodifiableElements.equals( classpath.unmodifiableElements ) : classpath.unmodifiableElements != null );
 140  
 
 141  
     }
 142  
 
 143  
     public ClassLoader createClassLoader( ClassLoader parent, boolean childDelegation, boolean enableAssertions,
 144  
                                           String roleName )
 145  
         throws SurefireExecutionException
 146  
     {
 147  
         try
 148  
         {
 149  0
             List urls = getAsUrlList();
 150  0
             IsolatedClassLoader classLoader = new IsolatedClassLoader( parent, childDelegation, roleName );
 151  0
             for ( Object url1 : urls )
 152  
             {
 153  0
                 URL url = (URL) url1;
 154  0
                 classLoader.addURL( url );
 155  0
             }
 156  0
             if ( parent != null )
 157  
             {
 158  0
                 parent.setDefaultAssertionStatus( enableAssertions );
 159  
             }
 160  0
             classLoader.setDefaultAssertionStatus( enableAssertions );
 161  0
             return classLoader;
 162  
         }
 163  0
         catch ( MalformedURLException e )
 164  
         {
 165  0
             throw new SurefireExecutionException( "When creating classloader", e );
 166  
         }
 167  
     }
 168  
 
 169  
 
 170  
     public int hashCode()
 171  
     {
 172  0
         return unmodifiableElements != null ? unmodifiableElements.hashCode() : 0;
 173  
     }
 174  
 
 175  
     public String getLogMessage( String descriptor )
 176  
     {
 177  0
         StringBuilder result = new StringBuilder();
 178  0
         result.append( descriptor ).append( " classpath:" );
 179  0
         for ( String element : unmodifiableElements )
 180  
         {
 181  0
             result.append( "  " ).append( element );
 182  0
         }
 183  0
         return result.toString();
 184  
     }
 185  
 
 186  
     public String getCompactLogMessage( String descriptor )
 187  
     {
 188  0
         StringBuilder result = new StringBuilder();
 189  0
         result.append( descriptor ).append( " classpath:" );
 190  0
         for ( String element : unmodifiableElements )
 191  
         {
 192  0
             result.append( "  " );
 193  0
             if ( element != null )
 194  
             {
 195  0
                 int pos = element.lastIndexOf( File.separatorChar );
 196  0
                 if ( pos >= 0 )
 197  
                 {
 198  0
                     result.append( element.substring( pos + 1 ) );
 199  
                 }
 200  
                 else
 201  
                 {
 202  0
                     result.append( element );
 203  
                 }
 204  
 
 205  0
             }
 206  
             else
 207  
             {
 208  0
                 result.append( element );
 209  
             }
 210  0
         }
 211  0
         return result.toString();
 212  
     }
 213  
 
 214  
     public Iterator<String> iterator()
 215  
     {
 216  0
         return unmodifiableElements.iterator();
 217  
     }
 218  
 }