Skip to content

Commit

Permalink
Switch to JUnit5
Browse files Browse the repository at this point in the history
  • Loading branch information
rfscholte committed Oct 9, 2021
1 parent 671ba68 commit fba7d56
Show file tree
Hide file tree
Showing 18 changed files with 217 additions and 140 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ jobs:
java-version: ${{ matrix.java }}

- name: Build with Maven
run: mvn install javadoc:javadoc -e -B -V -Pno-tests-if-not-on-osx
run: mvn verify javadoc:javadoc -e -B -V -fae -Pno-tests-if-not-on-osx
4 changes: 2 additions & 2 deletions plexus-compiler-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<artifactId>plexus-utils</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
</dependencies>
</project>
4 changes: 4 additions & 0 deletions plexus-compiler-its/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<packaging>pom</packaging>

<name>Plexus Compiler It Tests</name>

<properties>
<junit.version>4.13.2</junit.version>
</properties>

<dependencies>
<dependency>
Expand Down
16 changes: 13 additions & 3 deletions plexus-compiler-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,19 @@
<artifactId>plexus-compiler-api</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>compile</scope>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>compile</scope> <!-- override default test scope -->
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-testing</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.21.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,49 @@
* SOFTWARE.
*/

import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.util.FileUtils;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.codehaus.plexus.testing.PlexusTest;
import org.codehaus.plexus.util.FileUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

/**
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
*/
@PlexusTest
public abstract class AbstractCompilerTckTest
extends PlexusTestCase
{
private static final String EOL = System.lineSeparator();

private String roleHint;
private final String roleHint;

private TestInfo testInfo;

@Inject
private Map<String, Compiler> compilers;

protected AbstractCompilerTckTest( String roleHint )
{
this.roleHint = roleHint;
}

@BeforeEach
final void setup( TestInfo testInfo )
{
this.testInfo = testInfo;
}

@Test
public void testDeprecation()
throws Exception
{
Expand All @@ -69,19 +90,18 @@ public void testDeprecation()
//
// ----------------------------------------------------------------------

assertEquals( 1, result.size() );
assertThat( result.size() ).isEqualTo( 1 );

CompilerMessage error = result.get( 0 );

System.out.println( error.getMessage() );

assertFalse( error.isError() );
assertThat( error.isError() ).isFalse();

assertTrue( error.getMessage().contains( "Date" ) );
assertThat( error.getMessage() ).contains( "Date" );

assertTrue( error.getMessage().contains( "deprecated" ) );
assertThat( error.getMessage() ).contains( "deprecated" );
}

@Test
public void testWarning()
throws Exception
{
Expand All @@ -105,15 +125,15 @@ public void testWarning()
//
// ----------------------------------------------------------------------

assertEquals( 1, result.size() );
assertThat( result.size() ).isEqualTo( 1 );

CompilerMessage error = result.get( 0 );

assertFalse( error.isError() );
assertThat( error.isError() ).isFalse();

assertTrue( error.getMessage().contains( "finally block does not complete normally" ) );
assertThat( error.getMessage() ).contains( "finally block does not complete normally" );
}

protected List<CompilerMessage> compile( CompilerConfiguration configuration )
throws Exception
{
Expand All @@ -134,23 +154,25 @@ protected List<CompilerMessage> compile( CompilerConfiguration configuration )
// Compile!
// ----------------------------------------------------------------------

Compiler compiler = (Compiler) lookup( Compiler.ROLE, roleHint );
List<CompilerMessage> result = getCompiler().performCompile( configuration ).getCompilerMessages();

List<CompilerMessage> result = compiler.performCompile( configuration ).getCompilerMessages();

assertNotNull( result );
assertThat( result ).isNotNull();

return result;
}

private Compiler getCompiler() {
return compilers.get( roleHint );
}

private File getCompilerOutput()
{
return getTestFile( "target/compiler-output/" + getName() );
return new File( "target/compiler-output/" + testInfo.getTestMethod().map( Method::getName ).orElseThrow( null ) );
}

private File getSrc()
{
return getTestFile( "target/compiler-src/" + getName() );
return new File( "target/compiler-src/" + testInfo.getTestMethod().map( Method::getName ).orElseThrow( null ) );
}

protected void writeFileWithDeprecatedApi( File path, String className )
Expand All @@ -160,7 +182,7 @@ protected void writeFileWithDeprecatedApi( File path, String className )

if ( !parent.exists() )
{
assertTrue( parent.mkdirs() );
assertThat( parent.mkdirs() ).isTrue();
}

String source = "import java.util.Date;" + EOL +
Expand All @@ -186,7 +208,7 @@ protected void writeFileWithWarning( File path, String className )

if ( !parent.exists() )
{
assertTrue( parent.mkdirs() );
assertThat( parent.mkdirs() ).isTrue();
}

String source = "public class " + className + "" + EOL +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,80 @@
* SOFTWARE.
*/

import static org.assertj.core.api.Assertions.assertThat;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.test.ArtifactTestCase;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.artifact.versioning.VersionRange;

import org.apache.maven.settings.Settings;
import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
import org.codehaus.plexus.testing.PlexusTest;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.StringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import javax.inject.Inject;

/**
*
*/
@PlexusTest
public abstract class AbstractCompilerTest
extends ArtifactTestCase
{
private boolean compilerDebug = false;

private boolean compilerDeprecationWarnings = false;

private boolean forceJavacCompilerUse = false;


@Inject
private Map<String, Compiler> compilers;

@Inject
private ArtifactRepositoryLayout repositoryLayout;

private ArtifactRepository localRepository;

protected abstract String getRoleHint();

@BeforeEach
final void setUpLocalRepo()
throws Exception
{
String localRepo = System.getProperty( "maven.repo.local" );

if ( localRepo == null )
{
File settingsFile = new File( System.getProperty( "user.home" ), ".m2/settings.xml" );
if ( settingsFile.exists() )
{
Settings settings = new SettingsXpp3Reader().read( ReaderFactory.newXmlReader( settingsFile ) );
localRepo = settings.getLocalRepository();
}
}
if ( localRepo == null )
{
localRepo = System.getProperty( "user.home" ) + "/.m2/repository";
}

localRepository = new DefaultArtifactRepository( "local", "file://" + localRepo, repositoryLayout );
}

protected void setCompilerDebug( boolean flag )
{
compilerDebug = flag;
Expand All @@ -69,6 +112,11 @@ public void setForceJavacCompilerUse( boolean forceJavacCompilerUse )
{
this.forceJavacCompilerUse = forceJavacCompilerUse;
}

protected final Compiler getCompiler()
{
return compilers.get( getRoleHint() );
}

protected List<String> getClasspath()
throws Exception
Expand All @@ -77,8 +125,8 @@ protected List<String> getClasspath()

File file = getLocalArtifactPath( "commons-lang", "commons-lang", "2.0", "jar" );

assertTrue( "test prerequisite: commons-lang library must be available in local repository, expected "
+ file.getAbsolutePath(), file.canRead() );
assertThat( file.canRead() ).as( "test prerequisite: commons-lang library must be available in local repository, expected "
+ file.getAbsolutePath() ).isTrue();

cp.add( file.getAbsolutePath() );

Expand All @@ -90,6 +138,7 @@ protected void configureCompilerConfig( CompilerConfiguration compilerConfig )

}

@Test
public void testCompilingSources()
throws Exception
{
Expand All @@ -100,9 +149,7 @@ public void testCompilingSources()
{
File outputDir = new File( compilerConfig.getOutputLocation() );

Compiler compiler = (Compiler) lookup( Compiler.ROLE, getRoleHint() );

messages.addAll( compiler.performCompile( compilerConfig ).getCompilerMessages() );
messages.addAll( getCompiler().performCompile( compilerConfig ).getCompilerMessages() );

if ( outputDir.isDirectory() )
{
Expand Down Expand Up @@ -134,8 +181,8 @@ public void testCompilingSources()
errors.add( error.getMessage() );
}

assertEquals( "Wrong number of compilation errors (" + numCompilerErrors + "/" + expectedErrors //
+ ") : " + displayLines( errors ), expectedErrors, numCompilerErrors );
assertThat( numCompilerErrors ).as( "Wrong number of compilation errors (" + numCompilerErrors + "/" + expectedErrors //
+ ") : " + displayLines( errors ) ).isEqualTo( expectedErrors );
}

int expectedWarnings = expectedWarnings();
Expand All @@ -157,12 +204,12 @@ public void testCompilingSources()
warnings.add( error.getMessage() );
}

assertEquals( "Wrong number (" + numCompilerWarnings + "/"
+ expectedWarnings + ") of compilation warnings: " + displayLines( warnings ), //
expectedWarnings, numCompilerWarnings);
assertThat( numCompilerWarnings ).as( "Wrong number ("
+ numCompilerWarnings + "/" + expectedWarnings + ") of compilation warnings: "
+ displayLines( warnings ) ).isEqualTo( expectedWarnings );
}

assertEquals( new TreeSet<>( normalizePaths( expectedOutputFiles() ) ), files );
assertThat( files ).isEqualTo( new TreeSet<>( normalizePaths( expectedOutputFiles() ) ) );
}

protected String displayLines( List<String> warnings)
Expand All @@ -179,7 +226,7 @@ protected String displayLines( List<String> warnings)
private List<CompilerConfiguration> getCompilerConfigurations()
throws Exception
{
String sourceDir = getBasedir() + "/src/test-input/src/main";
String sourceDir = "src/test-input/src/main";

List<String> filenames =
FileUtils.getFileNames( new File( sourceDir ), "**/*.java", null, false, true );
Expand All @@ -202,7 +249,7 @@ private List<CompilerConfiguration> getCompilerConfigurations()

compilerConfig.addSourceLocation( sourceDir );

compilerConfig.setOutputLocation( getBasedir() + "/target/" + getRoleHint() + "/classes-" + index );
compilerConfig.setOutputLocation( "target/" + getRoleHint() + "/classes-" + index );

FileUtils.deleteDirectory( compilerConfig.getOutputLocation() );

Expand Down Expand Up @@ -315,4 +362,8 @@ protected String getJavaVersion()
return javaVersion;
}

protected File getLocalArtifactPath( Artifact artifact )
{
return new File( localRepository.getBasedir(), localRepository.pathOf( artifact ) );
}
}
Loading

0 comments on commit fba7d56

Please sign in to comment.