desruisseaux commented on code in PR #508:
URL: https://github.com/apache/maven-jar-plugin/pull/508#discussion_r3892137468


##########
src/test/java/org/apache/maven/plugins/jar/ArchiveTest.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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.
+ */
+package org.apache.maven.plugins.jar;
+
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Unit tests for {@link Archive}, focused on the two behaviours that are 
otherwise only
+ * exercised by integration tests whose outcome depends on the (unspecified) 
filesystem
+ * directory-iteration order, and therefore pass on some platforms while 
failing on others.
+ */
+class ArchiveTest {
+
+    /**
+     * Creates an {@code Archive} suitable for a unit test. {@code 
forceCreation = true} skips the
+     * existing-JAR timestamp check, so the (here {@code null}) logger is 
never dereferenced.
+     */
+    private static Archive archive(String moduleName, Runtime.Version version, 
Path directory, boolean isReproducible) {
+        return new Archive(directory.resolve("out.jar"), moduleName, version, 
directory, true, isReproducible, null);
+    }
+
+    /**
+     * Creates a manifest with the main class attribute set to the given value.
+     *
+     * @param value value of the main class attribute
+     * @return a new manifest with the given attribute value
+     */
+    private static Manifest manifestWithMainClass(String value) {
+        Manifest m = new Manifest();
+        Attributes attributes = m.getMainAttributes();
+        attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
+        attributes.put(Attributes.Name.MAIN_CLASS, value);
+        return m;
+    }
+
+    /**
+     * Returns the value of the main class attribute.
+     *
+     * @param m the manifest from which to get the value
+     * @return the main class attribute value, or {@code null} if none
+     */
+    private static Object mainClassOf(Manifest m) {
+        return m.getMainAttributes().get(Attributes.Name.MAIN_CLASS);
+    }
+
+    /**
+     * Verifies that {@link Archive#setMainClass(Manifest)} takes ownership 
for a module-qualified
+     * {@code "module/Class"} main class.
+     *
+     * Note that the {@code "foo.bar/"} prefix in this test (the module name) 
is a Maven extension.
+     * The standard <abbr>JAR</abbr> specification accepts only the {@code 
"foo.MainFile"} class name.
+     */
+    @Test
+    void owningModuleClaimsMainClassAndRemovesItFromManifest() {
+        Archive owner = archive("foo.bar", null, Path.of("."), false);
+        Manifest m = manifestWithMainClass("foo.bar/foo.MainFile");
+        // The owner keeps the main class (emitted via --main-class) ...
+        assertTrue(owner.setMainClass(m));
+        // ... and the raw `module/Class` value is removed from the written 
manifest.
+        assertNull(mainClassOf(m));
+    }
+
+    /**
+     * Verifies that {@link Archive#setMainClass(Manifest)} does <em>not</em> 
take ownership fo
+     * a module-qualified {@code "module/Class"} main class when the module 
name does not match.
+     * Note that the {@code "foo.bar/"} prefix in this test (the module name) 
is a Maven extension.
+     * The standard <abbr>JAR</abbr> specification accepts only the {@code 
"foo.MainFile"} class name.
+     */
+    @Test
+    void nonOwningModuleRejectsMainClass() {
+        Archive nonOwner = archive("foo.bar.more", null, Path.of("."), false);
+        Manifest m = manifestWithMainClass("foo.bar/foo.MainFile");
+        assertFalse(nonOwner.setMainClass(m));
+        assertNull(mainClassOf(m));
+    }
+
+    /**
+     * Tests that which module keeps the main class must not depend on 
processing order.

Review Comment:
   Done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to