You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2020/02/14 00:11:28 UTC

[maven-surefire] branch maven2surefire-jvm-communication updated: covered Classpath

This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch maven2surefire-jvm-communication
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git


The following commit(s) were added to refs/heads/maven2surefire-jvm-communication by this push:
     new 3c90950  covered Classpath
3c90950 is described below

commit 3c90950c527bb61f08b6cbb02768858baf8c8bcf
Author: tibordigana <ti...@apache.org>
AuthorDate: Fri Feb 14 01:11:20 2020 +0100

    covered Classpath
---
 .../apache/maven/surefire/booter/Classpath.java    | 13 +---
 .../maven/surefire/booter/ClasspathTest.java       | 87 +++++++++++++++++++++-
 2 files changed, 88 insertions(+), 12 deletions(-)

diff --git a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/Classpath.java b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/Classpath.java
index 048f6fc..b2ed4e6 100644
--- a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/Classpath.java
+++ b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/Classpath.java
@@ -100,7 +100,7 @@ public final class Classpath implements Iterable<String>, Cloneable
         {
             throw new IllegalArgumentException( "Null is not a valid class path element url." );
         }
-        return !unmodifiableElements.contains( path ) ? new Classpath( this, path ) : this;
+        return unmodifiableElements.contains( path ) ? this : new Classpath( this, path );
     }
 
     @Nonnull
@@ -184,15 +184,8 @@ public final class Classpath implements Iterable<String>, Cloneable
         for ( String element : unmodifiableElements )
         {
             result.append( "  " );
-            if ( element != null )
-            {
-                int pos = element.lastIndexOf( File.separatorChar );
-                result.append( pos == -1 ? element : element.substring( pos + 1 ) );
-            }
-            else
-            {
-                result.append( (String) null );
-            }
+            int pos = element.lastIndexOf( File.separatorChar );
+            result.append( pos == -1 ? element : element.substring( pos + 1 ) );
         }
         return result.toString();
     }
diff --git a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ClasspathTest.java b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ClasspathTest.java
index 874010f..e9b8196 100644
--- a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ClasspathTest.java
+++ b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ClasspathTest.java
@@ -20,9 +20,12 @@ package org.apache.maven.surefire.booter;
  */
 
 import java.io.File;
+import java.net.URL;
+import java.util.Iterator;
 import java.util.List;
 
 import junit.framework.TestCase;
+import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
 
 import static org.apache.maven.surefire.booter.Classpath.emptyClasspath;
 
@@ -101,7 +104,7 @@ public class ClasspathTest
 
     private void assertClasspathConsistsOfElements( Classpath classpath, String[] elements )
     {
-        List classpathElements = classpath.getClassPath();
+        List<String> classpathElements = classpath.getClassPath();
         for ( String element : elements )
         {
             assertTrue( "The element '" + element + " is missing.", classpathElements.contains( element ) );
@@ -111,7 +114,7 @@ public class ClasspathTest
 
     private void assertEmptyClasspath( Classpath classpath )
     {
-        List classpathElements = classpath.getClassPath();
+        List<String> classpathElements = classpath.getClassPath();
         assertEquals( "Wrong number of classpath elements.", 0, classpathElements.size() );
     }
 
@@ -146,4 +149,84 @@ public class ClasspathTest
         }
         assertEmptyClasspath( classpath );
     }
+
+    public void testCloneShouldBeEqual()
+    {
+        Classpath classpath = Classpath.emptyClasspath()
+            .addClassPathElementUrl( DUMMY_URL_1 )
+            .addClassPathElementUrl( DUMMY_URL_2 );
+
+        assertEquals( classpath, classpath );
+        assertFalse( classpath.equals( null ) );
+
+        assertEquals( 2, classpath.getClassPath().size() );
+        assertEquals( classpath, classpath.clone() );
+        assertEquals( classpath.hashCode(), classpath.clone().hashCode() );
+    }
+
+    public void testIterator()
+    {
+        Classpath classpath = Classpath.emptyClasspath()
+            .addClassPathElementUrl( DUMMY_URL_1 )
+            .addClassPathElementUrl( DUMMY_URL_2 );
+        Iterator<String> it = classpath.iterator();
+        String url1 = it.hasNext() ? it.next() : null;
+        String url2 = it.hasNext() ? it.next() : null;
+        assertEquals( DUMMY_URL_1, url1 );
+        assertEquals( DUMMY_URL_2, url2 );
+    }
+
+    public void testLog()
+    {
+        Classpath classpath = Classpath.emptyClasspath()
+            .addClassPathElementUrl( DUMMY_URL_1 )
+            .addClassPathElementUrl( DUMMY_URL_2 );
+        String log = classpath.getLogMessage( "classpath:" );
+        assertEquals( "classpath:  " + DUMMY_URL_1 + "  " + DUMMY_URL_2, log );
+    }
+
+    public void testCompactLog()
+    {
+        Classpath classpath = Classpath.emptyClasspath()
+            .addClassPathElementUrl( "root" + File.separatorChar + DUMMY_URL_1 )
+            .addClassPathElementUrl( "root" + File.separatorChar + DUMMY_URL_2 );
+        String log = classpath.getCompactLogMessage( "classpath:" );
+        assertEquals( "classpath:  " + DUMMY_URL_1 + "  " + DUMMY_URL_2, log );
+    }
+
+    public void testLoadInNewClassLoader() throws Exception
+    {
+        Class<?> target = ConsoleLogger.class;
+        String thisPath = "/" + target.getName().replace( '.', '/' ) + ".class";
+        URL url = target.getResource( thisPath );
+        assertTrue( url.toString().endsWith( thisPath ) );
+        String oneClasspath = new URL( url.toString().replace( thisPath, "" ) ).getFile();
+        assertTrue( new File( oneClasspath ).exists() );
+        Classpath classpath = Classpath.emptyClasspath();
+        ClassLoader classLoader = classpath.addClassPathElementUrl( new File( oneClasspath ).getCanonicalPath() )
+            .createClassLoader( false, true, "" );
+        Class<?> cls = classLoader.loadClass( target.getName() );
+        assertNotNull( cls );
+        assertEquals( cls.getName(), target.getName() );
+        assertNotSame( cls, target );
+    }
+
+    public void testDontLoadInNewClassLoader()
+    {
+        Class<?> target = ConsoleLogger.class;
+        String thisPath = "/" + target.getName().replace( '.', '/' ) + ".class";
+        URL url = target.getResource( thisPath );
+        assertTrue( url.toString().endsWith( thisPath ) );
+        try
+        {
+            Classpath.emptyClasspath()
+                .addClassPathElementUrl( "\u0000" )
+                .createClassLoader( false, true, "" );
+            fail();
+        }
+        catch ( SurefireExecutionException e )
+        {
+            // expected
+        }
+    }
 }