Coverage Report - org.apache.maven.surefire.booter.Classpath
 
Classes in this File Line Coverage Branch Coverage Complexity
Classpath
69%
41/59
56%
17/30
2,462
 
 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 java.io.File;
 23  
 import java.net.MalformedURLException;
 24  
 import java.net.URL;
 25  
 import java.util.ArrayList;
 26  
 import java.util.Collections;
 27  
 import java.util.Iterator;
 28  
 import java.util.List;
 29  
 import org.apache.maven.surefire.util.UrlUtils;
 30  
 
 31  
 /**
 32  
  * An ordered list of classpath elements with set behaviour
 33  
  *
 34  
  * @author Kristian Rosenvold
 35  
  */
 36  
 public class Classpath
 37  
 {
 38  1
     private static final JdkReflector jdkReflector = new JdkReflector();
 39  
 
 40  
     public static Classpath join( Classpath firstClasspath, Classpath secondClasspath )
 41  
     {
 42  3
         Classpath joinedClasspath = new Classpath();
 43  3
         joinedClasspath.addElementsOfClasspath( firstClasspath );
 44  3
         joinedClasspath.addElementsOfClasspath( secondClasspath );
 45  3
         return joinedClasspath;
 46  
     }
 47  
 
 48  
     private final List elements;
 49  
 
 50  
     public Classpath()
 51  22
     {
 52  22
         this.elements = new ArrayList();
 53  22
     }
 54  
 
 55  
     public Classpath( Classpath other )
 56  0
     {
 57  0
         this.elements = new ArrayList( other.elements );
 58  0
     }
 59  
 
 60  
     public Classpath( List elements )
 61  
     {
 62  3
         this();
 63  3
         addElements( elements );
 64  3
     }
 65  
 
 66  
     public void addClassPathElementUrl( String path )
 67  
     {
 68  32
         if ( path == null )
 69  
         {
 70  2
             throw new IllegalArgumentException( "Null is not a valid class path element url." );
 71  
         }
 72  30
         else if ( !elements.contains( path ) )
 73  
         {
 74  28
             elements.add( path );
 75  
         }
 76  30
     }
 77  
 
 78  
     private void addElements( List additionalElements )
 79  
     {
 80  7
         for ( Iterator it = additionalElements.iterator(); it.hasNext(); )
 81  
         {
 82  8
             String element = (String) it.next();
 83  8
             addClassPathElementUrl( element );
 84  8
         }
 85  7
     }
 86  
 
 87  
     private void addElementsOfClasspath( Classpath otherClasspath )
 88  
     {
 89  6
         if ( otherClasspath != null )
 90  
         {
 91  4
             addElements( otherClasspath.elements );
 92  
         }
 93  6
     }
 94  
 
 95  
     public List getClassPath()
 96  
     {
 97  9
         return Collections.unmodifiableList( elements );
 98  
     }
 99  
 
 100  
     public List getAsUrlList()
 101  
         throws MalformedURLException
 102  
     {
 103  1
         List urls = new ArrayList();
 104  1
         for ( Iterator i = elements.iterator(); i.hasNext(); )
 105  
         {
 106  2
             String url = (String) i.next();
 107  2
             File f = new File( url );
 108  2
             urls.add( UrlUtils.getURL( f ) );
 109  2
         }
 110  1
         return urls;
 111  
     }
 112  
 
 113  
     public void writeToSystemProperty( String propertyName )
 114  
     {
 115  2
         StringBuffer sb = new StringBuffer();
 116  2
         for ( Iterator i = elements.iterator(); i.hasNext(); )
 117  
         {
 118  2
             sb.append( (String) i.next() ).append( File.pathSeparatorChar );
 119  
         }
 120  2
         System.setProperty( propertyName, sb.toString() );
 121  2
     }
 122  
 
 123  
     public boolean equals( Object o )
 124  
     {
 125  2
         if ( this == o )
 126  
         {
 127  0
             return true;
 128  
         }
 129  2
         if ( o == null || getClass() != o.getClass() )
 130  
         {
 131  0
             return false;
 132  
         }
 133  
 
 134  2
         Classpath classpath = (Classpath) o;
 135  
 
 136  2
         return !( elements != null ? !elements.equals( classpath.elements ) : classpath.elements != null );
 137  
 
 138  
     }
 139  
 
 140  
     public ClassLoader createClassLoader( ClassLoader parent, boolean childDelegation, boolean enableAssertions,
 141  
                                           String roleName )
 142  
         throws SurefireExecutionException
 143  
     {
 144  
         try
 145  
         {
 146  0
             List urls = getAsUrlList();
 147  0
             IsolatedClassLoader classLoader = new IsolatedClassLoader( parent, childDelegation, roleName );
 148  0
             for ( Iterator iter = urls.iterator(); iter.hasNext(); )
 149  
             {
 150  0
                 URL url = (URL) iter.next();
 151  0
                 classLoader.addURL( url );
 152  0
             }
 153  0
             if ( parent != null )
 154  
             {
 155  0
                 jdkReflector.invokeAssertionStatusMethod( parent, enableAssertions );
 156  
             }
 157  0
             jdkReflector.invokeAssertionStatusMethod( classLoader, enableAssertions );
 158  0
             return classLoader;
 159  
         }
 160  0
         catch ( MalformedURLException e )
 161  
         {
 162  0
             throw new SurefireExecutionException( "When creating classloader", e );
 163  
         }
 164  
     }
 165  
 
 166  
 
 167  
     public int hashCode()
 168  
     {
 169  0
         return elements != null ? elements.hashCode() : 0;
 170  
     }
 171  
 }