Skip to content

Commit

Permalink
[MEAR-267] - Test for the case when unpacking of EJB JARs is turned on
Browse files Browse the repository at this point in the history
  • Loading branch information
mabrarov committed Oct 5, 2020
1 parent d7ec060 commit b5a54dc
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 92 deletions.
184 changes: 95 additions & 89 deletions src/test/java/org/apache/maven/plugins/ear/it/AbstractEarPluginIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,93 +131,44 @@ protected File executeMojo( final String projectName, final Properties propertie
return executeMojo( projectName, properties, true, true );
}

/**
* Executes the specified projects and asserts the given artifacts. Assert the deployment descriptors are valid
*
* @param projectName the project to test
* @param earModuleName the name of 1st level EAR module in multi-module project or null if project is single-module
* @param expectedArtifacts the list of artifacts to be found in the EAR archive
* @param artifactsDirectory whether the artifact is an exploded artifactsDirectory or not
* @param cleanBeforeExecute call clean plugin before execution
* @return the base directory of the project
*/
protected File doTestProject( final String projectName, final String earModuleName, final String[] expectedArtifacts,
final boolean[] artifactsDirectory, boolean cleanBeforeExecute )
throws VerificationException, IOException
{
final File baseDir = executeMojo( projectName, new Properties(), true, cleanBeforeExecute );
final File earDir = getEarModuleDirectory( baseDir, earModuleName );
assertEarArchive( earDir, projectName );
assertEarDirectory( earDir, projectName );

assertArchiveContent( earDir, projectName, expectedArtifacts, artifactsDirectory );

assertDeploymentDescriptors( earDir, projectName );

return baseDir;
}

/**
* Executes the specified projects and asserts the given artifacts. Asserts the deployment descriptors are valid.
* Asserts Class-Path entry of manifest of EAR modules.
*
* @param projectName the project to test
* @param earModuleName the name of 1st level EAR module in multi-module project or null if project is single-module
* @param expectedArtifacts the list of artifacts to be found in the EAR archive
* @param moduleArtifacts the list of artifacts representing EAR modules which manifest needs to be asserted
* @param artifactsDirectory whether the artifact is an exploded artifactsDirectory or not
* @param moduleArtifacts the list of artifacts representing EAR modules which manifest needs to be asserted or
* {@code null} if there is no need to validate Class-Path entry of EAR modules manifests
* @param moduleArtifactsDirectory whether the artifact from {@code moduleArtifacts} list is an exploded or not.
* Can be {@code null} if {@code moduleArtifacts} is {@code null}
* @param expectedClassPathElements the list of elements of Class-Path entry of manifest. Rows should match
* modules passed in {@code moduleArtifacts} parameter
* modules passed in {@code moduleArtifacts} parameter. Can be {@code null} if
* {@code moduleArtifacts} is {@code null}
* @param cleanBeforeExecute call clean plugin before execution
* @return the base directory of the project
*/
protected File doTestProject( final String projectName, final String earModuleName, final String[] expectedArtifacts,
final String[] moduleArtifacts, final String[][] expectedClassPathElements )
protected File doTestProject( final String projectName, final String earModuleName,
final String[] expectedArtifacts, boolean[] artifactsDirectory,
final String[] moduleArtifacts, boolean[] moduleArtifactsDirectory,
final String[][] expectedClassPathElements,
final boolean cleanBeforeExecute )
throws VerificationException, IOException
{
assertEquals( "Rows of expectedClassPathElements parameter should match items of moduleArtifacts parameter",
moduleArtifacts.length, expectedClassPathElements.length );

final File baseDir = doTestProject( projectName, earModuleName, expectedArtifacts, new boolean[expectedArtifacts.length], true );
final File baseDir = executeMojo( projectName, new Properties(), true, cleanBeforeExecute );

final File earFile = getEarArchive( getEarModuleDirectory( baseDir, earModuleName ), projectName );
for ( int i = 0; i != moduleArtifacts.length; ++i )
{
final String moduleArtifact = moduleArtifacts[i];
Assert.assertArrayEquals( "Wrong elements of Class-Path entry of module [" + moduleArtifact + "] manifest",
expectedClassPathElements[i], getClassPathElements( earFile, moduleArtifact ) );
}
final File earModuleDir = getEarModuleDirectory( baseDir, earModuleName );
assertEarArchive( earModuleDir, projectName );
assertEarDirectory( earModuleDir, projectName );
assertArchiveContent( earModuleDir, projectName, expectedArtifacts, artifactsDirectory );
assertDeploymentDescriptors( earModuleDir, projectName );
assertClassPathElements( earModuleDir, projectName, moduleArtifacts, moduleArtifactsDirectory,
expectedClassPathElements );

return baseDir;
}

/**
* Extracts elements of Class-Path entry of manifest of given EAR module.
*
* @param earFile the EAR file to investigate
* @param moduleArtifact the name of artifact in EAR representing EAR module
* @return elements of Class-Path entry of manifest of EAR module which is represented by
* {@code moduleArtifact} artifact in {@code earFile} file
*/
protected String[] getClassPathElements( final File earFile, final String moduleArtifact ) throws IOException
{
try ( JarFile earJarFile = new JarFile( earFile ) )
{
final ZipEntry moduleEntry = earJarFile.getEntry( moduleArtifact );
assertNotNull( "Artifact [" + moduleArtifact + "] should exist in EAR", moduleEntry );
try ( InputStream moduleInputStream = earJarFile.getInputStream( moduleEntry );
JarInputStream moduleJarInputStream = new JarInputStream( moduleInputStream ) )
{
final Manifest manifest = moduleJarInputStream.getManifest();
assertNotNull( "Artifact [" + moduleArtifact + "] of EAR should have manifest", manifest );
final String classPath = manifest.getMainAttributes().getValue( "Class-Path" );
if ( classPath == null )
{
return new String[0];
}
return classPath.split( " " );
}
}
}

/**
* Executes the specified projects and asserts the given artifacts. Assert the deployment descriptors are valid
*
Expand All @@ -230,39 +181,22 @@ protected File doTestProject( final String projectName, final String[] expectedA
final boolean[] artifactsDirectory )
throws VerificationException, IOException
{
return doTestProject( projectName, null, expectedArtifacts, artifactsDirectory, true );
return doTestProject( projectName, null, expectedArtifacts, artifactsDirectory, null, null, null, true );
}

/**
* Executes the specified projects and asserts the given artifacts as artifacts (non directory)
*
* @param projectName the project to test
* @param expectedArtifacts the list of artifacts to be found in the EAR archive
* @param testDeploymentDescriptors whether we should test deployment descriptors
* @return the base directory of the project
*/
private File doTestProject( final String projectName, final String[] expectedArtifacts,
boolean testDeploymentDescriptors )
protected File doTestProject( final String projectName, final String[] expectedArtifacts )
throws VerificationException, IOException
{
return doTestProject( projectName, expectedArtifacts, new boolean[expectedArtifacts.length] );
}

/**
* Executes the specified projects and asserts the given artifacts as artifacts (non directory). Assert the
* deployment descriptors are valid
*
* @param projectName the project to test
* @param expectedArtifacts the list of artifacts to be found in the EAR archive
* @return the base directory of the project
* @throws Exception Mojo exception in case of an error.
*/
protected File doTestProject( final String projectName, final String[] expectedArtifacts )
throws Exception
{
return doTestProject( projectName, expectedArtifacts, true );
}

protected void assertEarArchive( final File baseDir, final String projectName )
{
assertTrue( "EAR archive does not exist", getEarArchive( baseDir, projectName ).exists() );
Expand Down Expand Up @@ -476,4 +410,76 @@ public boolean accept( File dir, String name )
}
} );
}

private void assertClassPathElements( final File earDir, String projectName, String[] moduleArtifacts,
boolean[] moduleArtifactsDirectory, String[][] expectedClassPathElements )
throws IOException
{
if ( moduleArtifacts == null )
{
return;
}

assertNotNull( "moduleArtifactsDirectory should be provided if moduleArtifacts is provided",
moduleArtifactsDirectory );
assertTrue( "Size of moduleArtifactsDirectory should match size of moduleArtifacts parameter",
moduleArtifacts.length <= moduleArtifactsDirectory.length );
assertNotNull( "expectedClassPathElements should be provided if moduleArtifacts is provided",
expectedClassPathElements );
assertTrue( "Rows of expectedClassPathElements parameter should match items of moduleArtifacts parameter",
moduleArtifacts.length <= expectedClassPathElements.length );

final File earFile = getEarArchive( earDir, projectName );
for ( int i = 0; i != moduleArtifacts.length; ++i )
{
final String moduleArtifact = moduleArtifacts[i];
Assert.assertArrayEquals( "Wrong elements of Class-Path entry of module [" + moduleArtifact + "] manifest",
expectedClassPathElements[i], getClassPathElements( earFile, moduleArtifact, moduleArtifactsDirectory[i] ) );
}
}

/**
* Extracts elements of Class-Path entry of manifest of given EAR module.
*
* @param earFile the EAR file to investigate
* @param moduleArtifact the name of artifact in EAR representing EAR module
* @return elements of Class-Path entry of manifest of EAR module which is represented by
* {@code moduleArtifact} artifact in {@code earFile} file
*/
protected String[] getClassPathElements( final File earFile, final String moduleArtifact, final boolean directory )
throws IOException
{
final String classPath;
try ( JarFile earJarFile = new JarFile( earFile ) )
{
final ZipEntry moduleEntry = earJarFile.getEntry( moduleArtifact );
assertNotNull( "Artifact [" + moduleArtifact + "] should exist in EAR", moduleEntry );
if (directory)
{
final String manifestEntryName = moduleArtifact + "/META-INF/MANIFEST.MF";
final ZipEntry manifestEntry = earJarFile.getEntry( manifestEntryName );
assertNotNull( manifestEntryName + " manifest file should exist in EAR", manifestEntry );
try ( InputStream manifestInputStream = earJarFile.getInputStream( manifestEntry ) )
{
final Manifest manifest = new Manifest(manifestInputStream);
classPath = manifest.getMainAttributes().getValue( "Class-Path" );
}
}
else
{
try ( InputStream moduleInputStream = earJarFile.getInputStream( moduleEntry );
JarInputStream moduleJarInputStream = new JarInputStream( moduleInputStream ) )
{
final Manifest manifest = moduleJarInputStream.getManifest();
assertNotNull( "Artifact [" + moduleArtifact + "] of EAR should have manifest", manifest );
classPath = manifest.getMainAttributes().getValue( "Class-Path" );
}
}
}
if ( classPath == null )
{
return new String[0];
}
return classPath.split( " " );
}
}
16 changes: 13 additions & 3 deletions src/test/java/org/apache/maven/plugins/ear/it/EarMojoIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1009,8 +1009,11 @@ public void testProject089()
final String jarSampleThreeArtifact = "lib/eartest-jar-sample-three-with-deps-1.0.jar";
doTestProject( "project-089", "ear",
new String[] { warModuleArtifact, ejbModuleArtifact, jarSampleTwoArtifact, jarSampleThreeArtifact },
new boolean[] { false, false, false, false},
new String[] { warModuleArtifact, ejbModuleArtifact },
new String[][] { { jarSampleTwoArtifact, jarSampleThreeArtifact }, { jarSampleThreeArtifact, jarSampleTwoArtifact } } );
new boolean[] { false, false },
new String[][] { { jarSampleTwoArtifact, jarSampleThreeArtifact }, { jarSampleThreeArtifact, jarSampleTwoArtifact } },
true );
}

/**
Expand All @@ -1029,15 +1032,19 @@ public void testProject090()
final String jarSampleThreeArtifact = "lib/eartest-jar-sample-three-with-deps-1.0.jar";
doTestProject( "project-090", "ear",
new String[] { warModuleArtifact, ejbModuleArtifact, jarSampleTwoArtifact, jarSampleThreeArtifact },
new boolean[] { false, false, false, false },
new String[] { warModuleArtifact, ejbModuleArtifact },
new String[][] { { jarSampleTwoArtifact }, { jarSampleThreeArtifact, jarSampleTwoArtifact } } );
new boolean[] { false, false },
new String[][] { { jarSampleTwoArtifact }, { jarSampleThreeArtifact, jarSampleTwoArtifact } },
true );
}

/**
* Validates modification of Class-Path entry of EAR modules manifest when
* <ul>
* <li>skinnyWars option is turned off</li>
* <li>skipClassPathModification option is turned off</li>
* <li>unpacking of EJB JARs is turned on</li>
* </ul>
*/
public void testProject091()
Expand All @@ -1049,7 +1056,10 @@ public void testProject091()
final String jarSampleThreeArtifact = "eartest-jar-sample-three-with-deps-1.0.jar";
doTestProject( "project-091", "ear",
new String[] { warModuleArtifact, ejbModuleArtifact, jarSampleTwoArtifact, jarSampleThreeArtifact },
new boolean[] { false, true, false, false },
new String[] { warModuleArtifact, ejbModuleArtifact },
new String[][] { { "jar-sample-two-1.0.jar" }, { jarSampleThreeArtifact, jarSampleTwoArtifact } } );
new boolean[] { false, true },
new String[][] { { "jar-sample-two-1.0.jar" }, { jarSampleThreeArtifact, jarSampleTwoArtifact } },
true );
}
}
1 change: 1 addition & 0 deletions src/test/resources/projects/project-091/ear/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ under the License.
<version>@project.version@</version>
<configuration>
<version>6</version>
<unpackTypes>ejb</unpackTypes>
</configuration>
</plugin>
</plugins>
Expand Down

0 comments on commit b5a54dc

Please sign in to comment.