Skip to content

Commit

Permalink
NCL5715 refactor - avoid running via reflection, improve naming and s…
Browse files Browse the repository at this point in the history
…implify.
  • Loading branch information
rnc committed Dec 17, 2020
1 parent 3172469 commit 7604a4a
Show file tree
Hide file tree
Showing 21 changed files with 102 additions and 181 deletions.
38 changes: 16 additions & 22 deletions cli/src/test/java/org/commonjava/maven/ext/cli/CliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void checkTargetMatches() throws Exception
File pom1 = temp.newFile();
File settings = writeSettings( temp.newFile() );

TestUtils.executeMethod( c, "createSession", new Object[] { pom1, settings } );
TestUtils.executeMethod( c, "createSession", new Class[] { File.class, File.class }, new Object[] { pom1, settings } );

assertEquals( "Session file should match", pom1,
( (ManipulationSession) FieldUtils.readField( c, "session", true ) ).getPom() );
Expand Down Expand Up @@ -125,8 +125,7 @@ public void checkLocalRepositoryWithDefaults() throws Exception
{
Cli c = new Cli();
File settings = writeSettings( temp.newFile() );

TestUtils.executeMethod( c, "run", new Object[] { new String[] { "-s", settings.toString() } } );
c.run( new String[] { "-s", settings.toString()} );

ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
MavenSession ms = (MavenSession) FieldUtils.readField( session, "mavenSession", true );
Expand Down Expand Up @@ -159,7 +158,7 @@ public void checkLocalRepositoryWithDefaultsAndModifiedUserSettings() throws Exc
Files.copy( tmpSettings, source );

Cli c = new Cli();
TestUtils.executeMethod( c, "run", new Object[] { new String[] {} } );
c.run( new String[]{} );

ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
MavenSession ms = (MavenSession) FieldUtils.readField( session, "mavenSession", true );
Expand Down Expand Up @@ -192,10 +191,9 @@ public void checkProfileActivation() throws Exception
Files.copy( Paths.get( INTEGRATION_TEST.toString(), "pom.xml" ), target.toPath(), StandardCopyOption.REPLACE_EXISTING );

Cli c = new Cli();
TestUtils.executeMethod( c, "run", new Object[] {
new String[] { "-d", "--settings=" + getClass().getResource( "/settings-test.xml" ).getFile(),
c.run( new String[] { "-d", "--settings=" + getClass().getResource( "/settings-test.xml" ).getFile(),
"-Dmaven.repo.local=" + folder.toString(), "-Prun-its", "--file",
target.getCanonicalPath() } } );
target.getCanonicalPath() } );

assertTrue( systemOutRule.getLog().contains( "Explicitly activating [run-its]" ) );
assertTrue( systemOutRule.getLog().contains( "Will not scan all profiles and returning active profiles of [run-its]" ) );
Expand All @@ -205,8 +203,7 @@ public void checkProfileActivation() throws Exception
public void checkLocalRepositoryWithSettings() throws Exception
{
Cli c = new Cli();
TestUtils.executeMethod( c, "run", new Object[] { new String[] {
"-settings=" + getClass().getResource( "/settings-test.xml" ).getFile() } } );
c.run( new String[] { "-settings=" + getClass().getResource( "/settings-test.xml").getFile() } );

ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
MavenSession ms = (MavenSession) FieldUtils.readField( session, "mavenSession", true );
Expand All @@ -219,7 +216,7 @@ public void checkLocalRepositoryWithExplicitMavenRepo() throws Exception
{
File folder = temp.newFolder();
Cli c = new Cli();
TestUtils.executeMethod( c, "run", new Object[] { new String[] { "-Dmaven.repo.local=" + folder.toString() } } );
c.run( new String[] { "-Dmaven.repo.local=" + folder.toString() } );

ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
MavenSession ms = (MavenSession) FieldUtils.readField( session, "mavenSession", true );
Expand All @@ -232,9 +229,9 @@ public void checkLocalRepositoryWithExplicitMavenRepoAndSettings() throws Except
{
File folder = temp.newFolder();
Cli c = new Cli();
TestUtils.executeMethod( c, "run", new Object[] { new String[] {
"--settings=" + getClass().getResource( "/settings-test.xml" ).getFile(),
"-Dmaven.repo.local=" + folder.toString() } } );
c.run( new String[]
{ "--settings=" + getClass().getResource("/settings-test.xml").getFile(),
"-Dmaven.repo.local=" + folder.toString() } );

ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
MavenSession ms = (MavenSession) FieldUtils.readField( session, "mavenSession", true );
Expand Down Expand Up @@ -397,31 +394,28 @@ public void checkLogLevels() throws Exception
FileFilterUtils.or( DirectoryFileFilter.DIRECTORY, FileFilterUtils.suffixFileFilter("xml")));

Cli c = new Cli();
TestUtils.executeMethod( c, "run", new Object[] {
new String[] { "--settings=" + getClass().getResource( "/settings-test.xml" ).getFile(),
c.run( new String[] { "--settings=" + getClass().getResource( "/settings-test.xml" ).getFile(),
"-Dmaven.repo.local=" + folder.toString(), "-Prun-its", "--file",
target.getCanonicalPath() } } );
target.getCanonicalPath() } );
assertTrue( systemOutRule.getLog().contains( "Running manipulator" ) );

systemOutRule.clearLog();

c = new Cli();
TestUtils.executeMethod( c, "run", new Object[] {
new String[] { "-q", "--settings=" + getClass().getResource( "/settings-test.xml" ).getFile(),
c.run( new String[] { "-q", "--settings=" + getClass().getResource( "/settings-test.xml" ).getFile(),
"-Dmaven.repo.local=" + folder.toString(), "-Prun-its", "--file",
target.getCanonicalPath() } } );
target.getCanonicalPath() } );

assertFalse( systemOutRule.getLog().contains( "Running manipulator" ) );

systemOutRule.clearLog();

c = new Cli();
TestUtils.executeMethod( c, "run", new Object[] {
new String[] { "-t", "--settings=" + getClass().getResource( "/settings-test.xml" ).getFile(),
c.run( new String[] { "-t", "--settings=" + getClass().getResource( "/settings-test.xml" ).getFile(),
"-DdependencyRelocations.commons-io:commons0io@newGroupId:newArtifactId=1.0",
"-DversionSuffix=rebuild-1",
"-Dmaven.repo.local=" + folder.toString(), "-Prun-its", "--file",
target.getCanonicalPath() } } );
target.getCanonicalPath() } );

assertTrue( systemOutRule.getLog().contains( "Wildcard map " ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,24 +160,6 @@ public static Object executeMethod( Object instance, String name, Class<?>[] typ
return m.invoke( instance, params );
}

/**
* Executes a method on an object instance. The name and parameters of
* the method are specified. The method will be executed and the value
* of it returned, even if the method would have private or protected access.
*/
public static void executeMethod( Object instance, String name, Object[] params ) throws Exception
{
// Fetch the Class types of all method parameters
Class<?>[] types = new Class[params.length];

for ( int i = 0; i < params.length; i++ )
{
types[i] = params[i].getClass();
}

executeMethod( instance, name, types, params );
}

/**
* Container object to allow a {@link ManipulationSession} to be created but also return the {@link ManipulationManager}
* and {@link MavenExecutionRequest}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestRule;

Expand All @@ -52,9 +51,6 @@ public class GroovyFunctionsTest
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog().muteForSuccessfulTests();

@Rule
public final TestRule restoreSystemProperties = new RestoreSystemProperties();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.commonjava.maven.ext.io.PomIO;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.rules.TemporaryFolder;

import java.io.File;
Expand All @@ -49,9 +48,6 @@ public class InitialGroovyManipulatorTest
@Rule
public TemporaryFolder tf = new TemporaryFolder();

@Rule
public final SystemOutRule systemRule = new SystemOutRule().enableLog().muteForSuccessfulTests();

@Test
public void shouldRemoveProjectInGroovyScript() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.jboss.byteman.contrib.bmunit.BMUnitRunner;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;

Expand All @@ -38,9 +37,6 @@
@RunWith(BMUnitRunner.class)
public class ModelResolverTest
{
@Rule
public final SystemOutRule systemRule = new SystemOutRule().enableLog().muteForSuccessfulTests();

@Rule
public TemporaryFolder temp = new TemporaryFolder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.commonjava.maven.ext.io.resolver.GalleyInfrastructure;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.rules.TemporaryFolder;

import java.io.File;
Expand All @@ -34,9 +33,6 @@

public class ResolverTest
{
@Rule
public final SystemOutRule systemRule = new SystemOutRule().enableLog().muteForSuccessfulTests();

@Rule
public TemporaryFolder temp = new TemporaryFolder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import org.commonjava.maven.ext.core.state.CommonState;
import org.commonjava.maven.ext.core.state.VersioningState;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

Expand All @@ -35,11 +33,6 @@
@RunWith(Parameterized.class)
public class CheckStrictValueAlternativesTest
{
private static final ManipulationSession session = new ManipulationSession();

@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog().muteForSuccessfulTests();

@Before
public void beforeTest() throws ManipulationException
{
Expand Down Expand Up @@ -91,10 +84,11 @@ public static Collection<Object[]> data()
});
}

private String source;
private String target;
private boolean result;
private boolean strictIgnoreSuffix;
private final String source;
private final String target;
private final boolean result;
private final boolean strictIgnoreSuffix;
private final ManipulationSession session = new ManipulationSession();

public CheckStrictValueAlternativesTest( String source, String target, boolean result, boolean strictIgnoreSuffix)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import org.commonjava.maven.ext.core.state.CommonState;
import org.commonjava.maven.ext.core.state.VersioningState;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

Expand All @@ -35,11 +33,6 @@
@RunWith(Parameterized.class)
public class CheckStrictValueTest
{
private static final ManipulationSession session = new ManipulationSession();

@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog().muteForSuccessfulTests();

@Before
public void beforeTest() throws ManipulationException
{
Expand Down Expand Up @@ -93,10 +86,12 @@ public static Collection<Object[]> data()
});
}

private String source;
private String target;
private boolean result;
private boolean strictIgnoreSuffix;
private final String source;
private final String target;
private final boolean result;
private final boolean strictIgnoreSuffix;
private final ManipulationSession session = new ManipulationSession();


public CheckStrictValueTest( String source, String target, boolean result, boolean strictIgnoreSuffix)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -40,12 +39,6 @@ public class ProfileActivationTest

private static final String RESOURCE_BASE = "";

@Rule
public final SystemOutRule systemRule = new SystemOutRule().enableLog().muteForSuccessfulTests();

@Rule
public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();

private List<Project> getProject() throws Exception
{
// Locate the PME project pom file. Use that to verify inheritance tracking.
Expand Down
Loading

0 comments on commit 7604a4a

Please sign in to comment.