Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Surefire 1593 #199

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -101,7 +101,7 @@ private File createJar( @Nonnull List<String> classPath, @Nonnull String startCl
{
file.deleteOnExit();
}
Path parent = file.getParentFile().toPath();
String parent = file.getParent();
FileOutputStream fos = new FileOutputStream( file );
try ( JarOutputStream jos = new JarOutputStream( fos ) )
{
Expand All @@ -116,10 +116,10 @@ private File createJar( @Nonnull List<String> classPath, @Nonnull String startCl
StringBuilder cp = new StringBuilder();
for ( Iterator<String> it = classPath.iterator(); it.hasNext(); )
{
File classPathElement = new File( it.next() );
String classPathElement = it.next();
String uri = toClasspathElementUri( parent, classPathElement, dumpLogDirectory );
cp.append( uri );
if ( classPathElement.isDirectory() && !uri.endsWith( "/" ) )
if ( new File( classPathElement ).isDirectory() && !uri.endsWith( "/" ) )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Files.isDirectory will be more consistent with the new use of Paths.
What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eolivelli
yes, and moreover we should change the method signature from this:
toClasspathElementUri( String parent, String classPathElement, File dumpLogDirectory )
to this:
toClasspathElementUri( Path parent, Path classPathElement, Path dumpLogDirectory )
Which requires calling dumpLogDir.toPath() in ForkStarter (Line 580) but pls do not go deeper changing File to Path in all project since it is not a matter of this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have Path parent created once before the loop.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the scope of this change which is about using Paths it seemed to me strange to add a line which is using the legacy API.
Btw the result will be correct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am totally with you. Let's wait for @jglick until he makes all changes.
off-topic: We have one JUnit5 issue and some easy issues to fix. I guess we can release a new release candidate next week,

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should change the method signature

No, I deliberately used String parameters so as to make it possible to write unit tests. Paths can (as far as I know) only be constructed using platform-specific methods, making the tests behave differently according to the machine running them.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Files.isDirectory will be more consistent with the new use of Paths.

I chose to make a more minimal change to solve the immediate regression, rather than rewriting the whole class to use NIO methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thst's correct. The Strings are already platform specific.

{
cp.append( '/' );
}
Expand All @@ -143,24 +143,40 @@ private File createJar( @Nonnull List<String> classPath, @Nonnull String startCl
}
}

private static String toClasspathElementUri( @Nonnull Path parent,
@Nonnull File classPathElement,
@Nonnull File dumpLogDirectory )
static String relativize( @Nonnull String parent, @Nonnull String child )
throws IllegalArgumentException
{
return Paths.get( parent )
.relativize( Paths.get( child ) )
.toString();
}

static String toAbsoluteUri( @Nonnull String absolutePath )
{
return Paths.get( absolutePath )
.toUri()
.toASCIIString();
}

static String toClasspathElementUri( @Nonnull String parent,
@Nonnull String classPathElement,
@Nonnull File dumpLogDirectory )
throws IOException
{
try
{
return new URI( null, parent.relativize( classPathElement.toPath() ).toString(), null )
.toASCIIString();
String relativeUriPath = relativize( parent, classPathElement )
.replace( '\\', '/' );

return new URI( null, relativeUriPath, null ).toASCIIString();
}
catch ( IllegalArgumentException e )
{
String error = "Boot Manifest-JAR contains absolute paths in classpath " + classPathElement.getPath();
String error = "Boot Manifest-JAR contains absolute paths in classpath " + classPathElement;
InPluginProcessDumpSingleton.getSingleton()
.dumpException( e, error, dumpLogDirectory );

return classPathElement.toURI()
.toASCIIString();
return toAbsoluteUri( classPathElement );
}
catch ( URISyntaxException e )
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package org.apache.maven.plugin.surefire.booterclient;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.File;
import java.net.URI;

import org.apache.maven.plugin.surefire.booterclient.output.InPluginProcessDumpSingleton;
import static org.fest.assertions.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

/**
* Unit tests for {@link JarManifestForkConfiguration}.
*/
@RunWith( PowerMockRunner.class )
@PrepareForTest( { JarManifestForkConfiguration.class, InPluginProcessDumpSingleton.class } )
public class JarManifestForkConfigurationTest
{
private static final String BASE_DIR = System.getProperty( "user.dir" );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will pollute the CWD when mvn is run using -f rather than defaulting the basedir.

If you want a temporary directory, I would suggest using the TemporaryFolder rule.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are files for testing purposes in target folder. Here we run the build mvn install in the root and not in a child. Even if you run this particular test in IntelliJIDEA, the user.dir is maven-surefire-common/pom.xml, so I think it is fine for this test but we can create the temporal folders in TEMP if you like, this does not harm the goal of the test.


@Test
public void relativeClasspathUnixSimple()
throws Exception
{
mockStatic( JarManifestForkConfiguration.class );
String parent = "/home/me/prj/target/surefire";
String classPathElement = "/home/me/.m2/repository/grp/art/1.0/art-1.0.jar";
when( JarManifestForkConfiguration.relativize( parent, classPathElement ) )
.thenReturn( "../../../.m2/repository/grp/art/1.0/art-1.0.jar" );
when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) )
.thenCallRealMethod();
File dump = new File( BASE_DIR, "target/test-dump" );
//noinspection ResultOfMethodCallIgnored
dump.mkdir();
assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, dump ) )
.isEqualTo( "../../../.m2/repository/grp/art/1.0/art-1.0.jar" );
}

@Test
public void relativeClasspathUnixTricky()
throws Exception
{
mockStatic( JarManifestForkConfiguration.class );
String parent = "/home/me/prj/target/surefire";
String classPathElement = "/the Maven repo/grp/art/1.0/art-1.0.jar";
when( JarManifestForkConfiguration.relativize( parent, classPathElement ) )
.thenReturn( "../../../../../the Maven repo/grp/art/1.0/art-1.0.jar" );
when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) )
.thenCallRealMethod();
File dump = new File( BASE_DIR, "target/test-dump" );
assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, dump ) )
.isEqualTo( "../../../../../the%20Maven%20repo/grp/art/1.0/art-1.0.jar" );
}

@Test
public void relativeClasspathWindowsSimple()
throws Exception
{
mockStatic( JarManifestForkConfiguration.class );
String parent = "C:\\Windows\\Temp\\surefire";
String classPathElement = "C:\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar";
when( JarManifestForkConfiguration.relativize( parent, classPathElement ) )
.thenReturn( "..\\..\\..\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" );
when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) )
.thenCallRealMethod();
File dump = new File( BASE_DIR, "target/test-dump" );
assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, dump ) )
.isEqualTo( "../../../Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" );
}

@Test
public void relativeClasspathWindowsTricky()
throws Exception
{
mockStatic( JarManifestForkConfiguration.class );
String parent = "C:\\Windows\\Temp\\surefire";
String classPathElement = "C:\\Test User\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar";
when( JarManifestForkConfiguration.relativize( parent, classPathElement ) )
.thenReturn( "..\\..\\..\\Test User\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" );
when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) )
.thenCallRealMethod();
File dump = new File( BASE_DIR, "target/test-dump" );
assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, dump ) )
.isEqualTo( "../../../Test%20User/me/.m2/repository/grp/art/1.0/art-1.0.jar" );
}

@Test
public void crossDriveWindows()
throws Exception
{
mockStatic( JarManifestForkConfiguration.class );
mockStatic( InPluginProcessDumpSingleton.class );
when( InPluginProcessDumpSingleton.getSingleton() ).thenReturn(mock( InPluginProcessDumpSingleton.class ));
String parent = "C:\\Windows\\Temp\\surefire";
String classPathElement = "X:\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar";
when( JarManifestForkConfiguration.relativize( parent, classPathElement ) )
.thenThrow( new IllegalArgumentException() );
when( JarManifestForkConfiguration.toAbsoluteUri( classPathElement ) )
.thenReturn( "file:///X:/Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" );
when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) )
.thenCallRealMethod();
File dump = new File( BASE_DIR, "target/test-dump" );
assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, dump ) )
.isEqualTo( "file:///X:/Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" );
}

@Test
public void shouldRelativizeOnRealPlatform()
throws Exception
{
File parentDir = new File( BASE_DIR, "target/test-parent" );
//noinspection ResultOfMethodCallIgnored
parentDir.mkdir();
File testDir = new File( BASE_DIR, "target/test with white spaces" );
//noinspection ResultOfMethodCallIgnored
testDir.mkdir();

String relativeTestDir =
JarManifestForkConfiguration.relativize( parentDir.getCanonicalPath(), testDir.getCanonicalPath() );

assertThat( relativeTestDir )
.isEqualTo( ".." + File.separator + "test with white spaces" );
}

@Test
public void shouldMakeAbsoluteUriOnRealPlatform()
throws Exception
{
File testDir = new File( BASE_DIR, "target/@2 test with white spaces" );
//noinspection ResultOfMethodCallIgnored
testDir.mkdir();

URI testDirUri = new URI( JarManifestForkConfiguration.toAbsoluteUri( testDir.getCanonicalPath() ) );

assertThat( testDirUri.getScheme() )
.isEqualTo( "file" );

assertThat( testDirUri.getRawPath() )
.isEqualTo( testDir.toURI().getRawPath() );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will likely fail when run on Windows using a project directory that is a UNC path rather than a drive letter: just as File.toURL had numerous problems corrected only by File.toUri, so File.toUri has some problems corrected by Path.toUri.

I would suggest dropping shouldRelativizeOnRealPlatform and shouldMakeAbsoluteUriOnRealPlatform. If you want unit tests, that is what I originally wrote. If you want something more realistic, demonstrating that the result is what was intended, shouldMakeRelativeUriOnRealPlatform is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I will use Path.toUri then, but I am wondering who wants to run Jenkins build on Surefire project that is located on shared network folder.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering who wants to run Jenkins build on Surefire project that is located on shared network folder

\\vboxsvr\linux perhaps?

}

@Test
public void shouldMakeRelativeUriOnRealPlatform()
throws Exception
{
File parentDir = new File( BASE_DIR, "target/test-parent" );
//noinspection ResultOfMethodCallIgnored
parentDir.mkdir();
File testDir = new File( BASE_DIR, "target/@3 test with white spaces" );
//noinspection ResultOfMethodCallIgnored
testDir.mkdir();

File dump = new File( BASE_DIR, "target/test-dump" );
//noinspection ResultOfMethodCallIgnored
dump.mkdir();

String parent = parentDir.getCanonicalPath();
String cpElement = testDir.getCanonicalPath();

String testDirUriPath = JarManifestForkConfiguration.toClasspathElementUri( parent, cpElement, dump );

assertThat( testDirUriPath )
.isEqualTo( "../@3%20test%20with%20white%20spaces" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.maven.plugin.surefire.booterclient.DefaultForkConfigurationTest;
import org.apache.maven.plugin.surefire.booterclient.ForkConfigurationTest;
import org.apache.maven.plugin.surefire.booterclient.ForkingRunListenerTest;
import org.apache.maven.plugin.surefire.booterclient.JarManifestForkConfigurationTest;
import org.apache.maven.plugin.surefire.booterclient.ModularClasspathForkConfigurationTest;
import org.apache.maven.plugin.surefire.booterclient.lazytestprovider.TestLessInputStreamBuilderTest;
import org.apache.maven.plugin.surefire.booterclient.lazytestprovider.TestProvidingInputStreamTest;
Expand Down Expand Up @@ -88,6 +89,7 @@ public static Test suite()
suite.addTest( new JUnit4TestAdapter( SurefireHelperTest.class ) );
suite.addTest( new JUnit4TestAdapter( AbstractSurefireMojoTest.class ) );
suite.addTest( new JUnit4TestAdapter( DefaultForkConfigurationTest.class ) );
suite.addTest( new JUnit4TestAdapter( JarManifestForkConfigurationTest.class ) );
suite.addTest( new JUnit4TestAdapter( ModularClasspathForkConfigurationTest.class ) );
if ( JAVA_RECENT.atLeast( JAVA_1_7 ) )
{
Expand Down