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-1909] Replace --add-exports with --add-opens #461

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -401,6 +401,15 @@ public class IntegrationTestMojo
@Parameter( property = "failsafe.useModulePath", defaultValue = "true" )
private boolean useModulePath;

/**
* When {@code true}, Surefire {@code --add-opens} the test packages at runtime, this enables deep reflection on
* test classes and may be required by test runners. By default, Failsafe {@code --add-exports} these packages.
*
* @since 3.0.0-M6
*/
@Parameter( property = "failsafe.useJpmsAddOpens", defaultValue = "false" )
private boolean useJpmsAddOpens;

/**
* This parameter configures the forked node. Currently, you can select the communication protocol, i.e. process
* pipes or TCP/IP sockets.
Expand Down Expand Up @@ -959,6 +968,18 @@ protected void setUseModulePath( boolean useModulePath )
this.useModulePath = useModulePath;
}

@Override
protected boolean useJpmsAddOpens()
{
return useJpmsAddOpens;
}

@Override
protected void setUseJpmsAddOpens( boolean useJpmsAddOpens )
{
this.useJpmsAddOpens = useJpmsAddOpens;
}

@Override
protected final List<File> suiteXmlFiles()
{
Expand Down
Expand Up @@ -884,6 +884,10 @@ protected abstract void handleSummary( RunResult summary, Exception firstForkExc

protected abstract void setUseModulePath( boolean useModulePath );

protected abstract boolean useJpmsAddOpens();

protected abstract void setUseJpmsAddOpens( boolean useJpmsAddOpens );

protected abstract String getEnableProcessChecker();

protected abstract ForkNodeFactory getForkNode();
Expand Down Expand Up @@ -2472,19 +2476,21 @@ private ForkConfiguration createForkConfiguration( @Nonnull Platform platform,
if ( canExecuteProviderWithModularPath( platform, resolvedJavaModularityResult ) )
{
return new ModularClasspathForkConfiguration( bootClasspath,
tmpDir,
getEffectiveDebugForkedProcess(),
getWorkingDirectory() != null ? getWorkingDirectory() : getBasedir(),
getProject().getModel().getProperties(),
getArgLine(),
getEnvironmentVariables(),
getExcludedEnvironmentVariables(),
getConsoleLogger().isDebugEnabled(),
getEffectiveForkCount(),
reuseForks,
platform,
getConsoleLogger(),
forkNode );
tmpDir,
getEffectiveDebugForkedProcess(),
getWorkingDirectory() != null
? getWorkingDirectory() : getBasedir(),
getProject().getModel().getProperties(),
getArgLine(),
getEnvironmentVariables(),
getExcludedEnvironmentVariables(),
getConsoleLogger().isDebugEnabled(),
getEffectiveForkCount(),
reuseForks,
platform,
getConsoleLogger(),
forkNode,
useJpmsAddOpens() );
}
else if ( getClassLoaderConfiguration().isManifestOnlyJarRequestedAndUsable() )
{
Expand Down
Expand Up @@ -55,6 +55,8 @@
public class ModularClasspathForkConfiguration
extends DefaultForkConfiguration
{
private final boolean useJpmsAddOpens;

@SuppressWarnings( "checkstyle:parameternumber" )
public ModularClasspathForkConfiguration( @Nonnull Classpath bootClasspath,
@Nonnull File tempDirectory,
Expand All @@ -69,11 +71,13 @@ public ModularClasspathForkConfiguration( @Nonnull Classpath bootClasspath,
boolean reuseForks,
@Nonnull Platform pluginPlatform,
@Nonnull ConsoleLogger log,
@Nonnull ForkNodeFactory forkNodeFactory )
@Nonnull ForkNodeFactory forkNodeFactory,
boolean useJpmsAddOpens )
{
super( bootClasspath, tempDirectory, debugLine, workingDirectory, modelProperties, argLine,
environmentVariables, excludedEnvironmentVariables, debug, forkCount, reuseForks, pluginPlatform, log,
forkNodeFactory );
this.useJpmsAddOpens = useJpmsAddOpens;
}

@Override
Expand Down Expand Up @@ -183,7 +187,7 @@ File createArgsFile( @Nonnull String moduleName, @Nonnull List<String> modulePat

for ( String pkg : packages )
{
args.append( "--add-exports" )
args.append( useJpmsAddOpens ? "--add-opens" : "--add-exports" )
.append( NL )
.append( moduleName )
.append( '/' )
Expand Down
Expand Up @@ -910,6 +910,18 @@ protected void setUseModulePath( boolean useModulePath )

}

@Override
protected boolean useJpmsAddOpens()
{
return false;
}

@Override
protected void setUseJpmsAddOpens( boolean useJpmsAddOpens )
{

}

@Override
protected String getEnableProcessChecker()
{
Expand Down
Expand Up @@ -1987,6 +1987,7 @@ public static class Mojo
private File mainBuildPath;
private File testClassesDirectory;
private boolean useModulePath;
private boolean useJpmsAddOpens;
private int failOnFlakeCount;
private String[] includeJUnit5Engines;
private String[] excludeJUnit5Engines;
Expand Down Expand Up @@ -2393,6 +2394,18 @@ protected void setUseModulePath( boolean useModulePath )
this.useModulePath = useModulePath;
}

@Override
protected boolean useJpmsAddOpens()
{
return useJpmsAddOpens;
}

@Override
protected void setUseJpmsAddOpens( boolean useJpmsAddOpens )
{
this.useJpmsAddOpens = useJpmsAddOpens;
}

@Override
protected String getEnableProcessChecker()
{
Expand Down
Expand Up @@ -763,6 +763,18 @@ protected void setUseModulePath( boolean useModulePath )

}

@Override
protected boolean useJpmsAddOpens()
{
return false;
}

@Override
protected void setUseJpmsAddOpens( boolean useJpmsAddOpens )
{

}

@Override
protected ForkNodeFactory getForkNode()
{
Expand Down
Expand Up @@ -145,7 +145,7 @@ public void testCliArgs() throws Exception

ModularClasspathForkConfiguration config = new ModularClasspathForkConfiguration( emptyClasspath(), basedir,
"", basedir, new Properties(), "arg1", Collections.<String, String>emptyMap(), new String[0], false, 1,
true, platform, new NullConsoleLogger(), mock( ForkNodeFactory.class ) );
true, platform, new NullConsoleLogger(), mock( ForkNodeFactory.class ), false );

assertThat( config.isDebug() ).isFalse();

Expand Down
Expand Up @@ -68,7 +68,7 @@ public void shouldCreateModularArgsFile() throws Exception
ModularClasspathForkConfiguration config = new ModularClasspathForkConfiguration( booter, tmp, "", pwd,
new Properties(), "",
Collections.<String, String>emptyMap(), new String[0], true, 1, true,
new Platform(), new NullConsoleLogger(), mock( ForkNodeFactory.class ) );
new Platform(), new NullConsoleLogger(), mock( ForkNodeFactory.class ), false );

File patchFile = new File( "target" + separatorChar + "test-classes" );
File descriptor = new File( tmp, "module-info.class" );
Expand Down Expand Up @@ -162,4 +162,47 @@ public void shouldCreateModularArgsFile() throws Exception
assertThat( line ).isEqualTo( argsFileLines.get( i ) );
}
}

@Test
@SuppressWarnings( "ResultOfMethodCallIgnored" )
public void shouldCreateModularArgsFileWithAddOpens() throws Exception
{
Classpath booter = new Classpath( asList( "booter.jar", "non-modular.jar" ) );
File target = new File( "target" ).getCanonicalFile();
File tmp = new File( target, "surefire" );
tmp.mkdirs();
File pwd = new File( "." ).getCanonicalFile();

ModularClasspathForkConfiguration config = new ModularClasspathForkConfiguration( booter, tmp, "", pwd,
new Properties(), "",
Collections.emptyMap(),
new String[0], true, 1,
true, new Platform(),
new NullConsoleLogger(),
mock( ForkNodeFactory.class ),
true );

File patchFile = new File( "target" + separatorChar + "test-classes" );
File descriptor = new File( tmp, "module-info.class" );
descriptor.createNewFile();
List<String> modulePath =
asList( "modular.jar", "target" + separatorChar + "classes" );
List<String> classPath = asList( "booter.jar", "non-modular.jar", patchFile.getPath() );
Collection<String> packages = singleton( "org.apache.abc" );
String startClassName = ForkedBooter.class.getName();

File jigsawArgsFile = config.createArgsFile( "abc", modulePath, classPath, packages, patchFile,
startClassName, true, Collections.<String[]>emptyList() );

assertThat( jigsawArgsFile )
.isNotNull();

List<String> argsFileLines = readAllLines( jigsawArgsFile.toPath(), UTF_8 );

assertThat( argsFileLines )
.hasSize( 13 );

assertThat( argsFileLines.get( 6 ) )
.isEqualTo( "--add-opens" );
}
}
Expand Up @@ -391,6 +391,15 @@ public class SurefirePlugin
@Parameter( property = "surefire.useModulePath", defaultValue = "true" )
private boolean useModulePath;

/**
* When {@code true}, Surefire {@code --add-opens} the test packages at runtime, this enables deep reflection on
* test classes and may be required by test runners. By default, Surefire {@code --add-exports} these packages.
*
* @since 3.0.0-M6
*/
@Parameter( property = "surefire.useJpmsAddOpens", defaultValue = "false" )
private boolean useJpmsAddOpens;

/**
* This parameter configures the forked node. Currently, you can select the communication protocol, i.e. process
* pipes or TCP/IP sockets.
Expand Down Expand Up @@ -884,6 +893,18 @@ protected void setUseModulePath( boolean useModulePath )
this.useModulePath = useModulePath;
}

@Override
protected boolean useJpmsAddOpens()
{
return useJpmsAddOpens;
}

@Override
protected void setUseJpmsAddOpens( boolean useJpmsAddOpens )
{
this.useJpmsAddOpens = useJpmsAddOpens;
}

@Override
protected final List<File> suiteXmlFiles()
{
Expand Down
@@ -0,0 +1,69 @@
package org.apache.maven.surefire.its;

/*
* 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.IOException;

import org.apache.maven.surefire.its.fixture.AbstractJava9PlusIT;
import org.junit.Test;

/**
*
*/
public class ModulePathWithAddOpensIT
extends AbstractJava9PlusIT
{
private String suffix;

@Test
public void testModulePath()
throws IOException
{
assumeJava9()
.debugLogging()
.executeTest()
.verifyErrorFreeLog()
.assertTestSuiteResults( 2 );
}

@Test
public void testModulePathWithSpaces()
throws IOException
{
suffix = " with spaces";
assumeJava9()
.debugLogging()
.executeTest()
.verifyErrorFreeLog()
.assertTestSuiteResults( 2 );
}

@Override
protected String getProjectDirectoryName()
{
return "modulepath-use-add-opens";
}

@Override
protected String getSuffix()
{
return suffix;
}
}
49 changes: 49 additions & 0 deletions surefire-its/src/test/resources/modulepath-use-add-opens/pom.xml
@@ -0,0 +1,49 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>foo</groupId>
<artifactId>app</artifactId>
<version>1.0.0-SNAPSHOT</version>

<name>app</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>${java.specification.version}</maven.compiler.release>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<useJpmsAddOpens>true</useJpmsAddOpens>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>