|
30 | 30 | import org.junit.Before;
|
31 | 31 | import org.junit.Test;
|
32 | 32 |
|
33 |
| -import static org.junit.Assert.assertEquals; |
34 |
| -import static org.junit.Assert.assertNotNull; |
| 33 | +import static org.junit.Assert.*; |
35 | 34 | import static org.junit.Assume.assumeFalse;
|
36 | 35 | import static org.junit.Assume.assumeTrue;
|
37 | 36 |
|
@@ -76,6 +75,57 @@ public void testModularJarWithMainClassAndVersion()
|
76 | 75 | "1.0.0", "com.example.app.Main", "com.example.app", "com.example.resources" );
|
77 | 76 | }
|
78 | 77 |
|
| 78 | + /* |
| 79 | + * Verify that when both module main class is set and the |
| 80 | + * manifest contains main class atribute, the manifest |
| 81 | + * value is overridden |
| 82 | + */ |
| 83 | + @Test |
| 84 | + public void testModularJarWithManifestAndModuleMainClass() |
| 85 | + throws Exception |
| 86 | + { |
| 87 | + assumeTrue( modulesAreSupported() ); |
| 88 | + |
| 89 | + archiver.addDirectory( new File( "src/test/resources/java-module-descriptor" ) ); |
| 90 | + Manifest manifest = new Manifest(); |
| 91 | + manifest.addConfiguredAttribute( |
| 92 | + new Manifest.Attribute( "Main-Class", "com.example.app.Main2" ) ); |
| 93 | + archiver.addConfiguredManifest( manifest ); |
| 94 | + archiver.setModuleMainClass( "com.example.app.Main" ); |
| 95 | + |
| 96 | + archiver.createArchive(); |
| 97 | + |
| 98 | + // Verify that the explicitly set module main class |
| 99 | + // overrides the manifest main |
| 100 | + assertModularJarFile( archiver.getDestFile(), |
| 101 | + null, "com.example.app.Main", "com.example.app", "com.example.resources" ); |
| 102 | + assertManifestMainClass( archiver.getDestFile(), "com.example.app.Main" ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Verify that when the module main class is not explicitly set, |
| 107 | + * the manifest main class attribute (if present) is used instead |
| 108 | + */ |
| 109 | + @Test |
| 110 | + public void testModularJarWithManifestMainClassAttribute() |
| 111 | + throws Exception |
| 112 | + { |
| 113 | + assumeTrue( modulesAreSupported() ); |
| 114 | + |
| 115 | + archiver.addDirectory( new File( "src/test/resources/java-module-descriptor" ) ); |
| 116 | + Manifest manifest = new Manifest(); |
| 117 | + manifest.addConfiguredAttribute( |
| 118 | + new Manifest.Attribute( "Main-Class", "com.example.app.Main2" ) ); |
| 119 | + archiver.addConfiguredManifest( manifest ); |
| 120 | + |
| 121 | + archiver.createArchive(); |
| 122 | + |
| 123 | + // Verify that the the manifest main class attribute is used as module main class |
| 124 | + assertModularJarFile( archiver.getDestFile(), |
| 125 | + null, "com.example.app.Main2", "com.example.app", "com.example.resources" ); |
| 126 | + assertManifestMainClass( archiver.getDestFile(), "com.example.app.Main2" ); |
| 127 | + } |
| 128 | + |
79 | 129 | /*
|
80 | 130 | * Verify that a modular JAR file is created even when no additional attributes are set.
|
81 | 131 | */
|
@@ -288,6 +338,23 @@ private void assertModuleDescriptor( InputStream moduleDescriptorInputStream,
|
288 | 338 | assertEquals( expectedPackagesSet, actualPackagesSet );
|
289 | 339 | }
|
290 | 340 |
|
| 341 | + private void assertManifestMainClass( File jarFile, String expectedMainClass ) |
| 342 | + throws Exception |
| 343 | + { |
| 344 | + try ( ZipFile resultingArchive = new ZipFile( jarFile ) ) |
| 345 | + { |
| 346 | + ZipEntry manifestEntry = resultingArchive.getEntry( "META-INF/MANIFEST.MF" ); |
| 347 | + InputStream manifestInputStream = resultingArchive.getInputStream( manifestEntry ); |
| 348 | + |
| 349 | + // Get the manifest main class attribute |
| 350 | + Manifest manifest = new Manifest( manifestInputStream ); |
| 351 | + String actualManifestMainClass = manifest.getMainAttributes().getValue( "Main-Class" ); |
| 352 | + |
| 353 | + assertEquals( expectedMainClass, actualManifestMainClass ); |
| 354 | + } |
| 355 | + |
| 356 | + } |
| 357 | + |
291 | 358 | /*
|
292 | 359 | * Returns true if the current version of Java does support modules.
|
293 | 360 | */
|
|
0 commit comments