SLING-9197 - Align script file name analysis to the Sling conventions
diff --git a/src/main/java/org/apache/sling/scriptingbundle/maven/plugin/ResourceTypeFolderAnalyser.java b/src/main/java/org/apache/sling/scriptingbundle/maven/plugin/ResourceTypeFolderAnalyser.java
index 7cd6918..1e6645d 100644
--- a/src/main/java/org/apache/sling/scriptingbundle/maven/plugin/ResourceTypeFolderAnalyser.java
+++ b/src/main/java/org/apache/sling/scriptingbundle/maven/plugin/ResourceTypeFolderAnalyser.java
@@ -182,69 +182,56 @@
                 }
             }
             String[] parts = scriptFile.toString().split("\\.");
-            if (parts.length >= 2 && parts.length <= 4) {
-                String method = null;
-                String requestExtension = null;
-                String scriptExtension = parts[parts.length - 1];
-                String first = parts[0];
-                if (!first.equals(resourceTypeLabel)) {
-                    if (MetadataMojo.METHODS.contains(first)) {
-                        // method script
-                        method = first;
-                        if ("HEAD".equals(method) || "GET".equals(method)) {
-                            if (parts.length == 4) {
-                                selectors.add(parts[1]);
-                                requestExtension = parts[2];
-                            } else if (parts.length == 3) {
-                                String middlePart = parts[1];
-                                if (MimeTypeChecker.hasMimeType(middlePart)) {
-                                    requestExtension = middlePart;
-                                } else {
-                                    selectors.add(middlePart);
-                                }
-                            }
-                        } else {
-                            if (parts.length != 2) {
-                                log.warn(String.format("Script %s doesn't respect naming conventions. Only GET and HEAD scripts are " +
-                                        "allowed to use additional selectors or request extensions.", script.toString()));
-                                return;
-                            }
-                        }
-                    } else {
-                        // selector script
-                        if (parts.length == 3) {
-                            selectors.add(first);
-                            requestExtension = parts[1];
-                        } else if (parts.length == 2) {
-                            selectors.add(first);
-                        } else {
-                            log.warn(String.format("Script %s doesn't respect naming conventions. A selector script can contain at max " +
-                                    "the request extension, but no other selectors."));
-                            return;
-                        }
-                    }
-                } else {
-                    // main script
-                    if (parts.length > 3) {
-                        log.warn(String.format("Script %s doesn't respect naming conventions. The main script can contain at max " +
-                                "the request extension, but no other selectors."));
-                        return;
-                    }
-                    if (parts.length == 3) {
-                        requestExtension = parts[parts.length - 2];
-                    }
-                }
-                String scriptEngine = scriptEngineMappings.get(scriptExtension);
-                if (StringUtils.isNotEmpty(scriptEngine)) {
-                    providedCapabilities.add(ProvidedCapability.builder().withResourceType(resourceType).withScriptEngine(scriptEngine)
-                            .withVersion(version).withSelectors(selectors).withRequestExtension(requestExtension).withRequestMethod(method)
-                            .build());
-                } else {
-                    log.warn(String.format("Unknown script engine mapping for script %s. Skipping.", scriptFile.toString()));
-                }
-            } else {
+            if (parts.length < 2 || parts.length > 4) {
                 log.warn(String.format("Skipping script %s since it either does not target a script engine or it provides too many " +
                         "selector parts in its name.", script.toString()));
+                return;
+            }
+            String name = parts[0];
+            String scriptEngineExtension = parts[parts.length - 1];
+            String scriptEngine = scriptEngineMappings.get(scriptEngineExtension);
+            if (StringUtils.isNotEmpty(scriptEngine)) {
+                String requestExtension = null;
+                String requestMethod = null;
+                if (parts.length == 2 && !resourceTypeLabel.equals(name)) {
+                    if (MetadataMojo.METHODS.contains(name)) {
+                        requestMethod = name;
+                    } else if (MimeTypeChecker.hasMimeType(name)) {
+                        requestExtension = name;
+                    } else {
+                        selectors.add(name);
+                    }
+                }
+                if (parts.length == 3) {
+                    String middle = parts[1];
+                    if (MetadataMojo.METHODS.contains(middle)) {
+                        requestMethod = middle;
+                    } else if (MimeTypeChecker.hasMimeType(middle)) {
+                        requestExtension = middle;
+                    }
+                    if (!resourceTypeLabel.equals(name)) {
+                        selectors.add(name);
+                    }
+                }
+                if (parts.length == 4){
+                    requestExtension = parts[1];
+                    requestMethod = parts[2];
+                    if (!resourceTypeLabel.equals(name)) {
+                        selectors.add(name);
+                    }
+                }
+                providedCapabilities.add(
+                        ProvidedCapability.builder()
+                                .withResourceType(resourceType)
+                                .withVersion(version)
+                                .withSelectors(selectors)
+                                .withRequestExtension(requestExtension)
+                                .withRequestMethod(requestMethod)
+                                .withScriptEngine(scriptEngine)
+                                .build()
+                );
+            } else {
+                log.warn(String.format("Cannot find a script engine mapping for script %s.", script.toString()));
             }
         } else {
             log.warn(String.format("Skipping path %s since it has 0 elements.", script.toString()));
diff --git a/src/test/java/org/apache/sling/scriptingbundle/maven/plugin/MetadataMojoTest.java b/src/test/java/org/apache/sling/scriptingbundle/maven/plugin/MetadataMojoTest.java
index 0ed437f..b8f9f97 100644
--- a/src/test/java/org/apache/sling/scriptingbundle/maven/plugin/MetadataMojoTest.java
+++ b/src/test/java/org/apache/sling/scriptingbundle/maven/plugin/MetadataMojoTest.java
@@ -34,7 +34,7 @@
 import org.osgi.framework.VersionRange;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 public class MetadataMojoTest {
 
@@ -91,7 +91,7 @@
                         , "depth2", "100")).build(),
                 ProvidedCapability.builder().withResourceType("org.apache.sling.foobar").withScriptEngine("htl").withRequestMethod("GET").build(),
                 ProvidedCapability.builder().withResourceType("org.apache.sling.foobar").withScriptEngine("htl").withRequestMethod("GET").withSelectors(Arrays.asList("test")).build(),
-                ProvidedCapability.builder().withResourceType("org.apache.sling.foobar").withScriptEngine("htl").withRequestMethod("GET").withRequestExtension("txt").build(),
+                ProvidedCapability.builder().withResourceType("org.apache.sling.foobar").withScriptEngine("htl").withRequestMethod("GET").withSelectors(Arrays.asList("test")).withRequestExtension("txt").build(),
                 ProvidedCapability.builder().withResourceType("org.apache.sling.foobar").withScriptEngine("htl").withSelectors(Arrays.asList("test")).withRequestExtension("txt").build(),
 
                 // sling
@@ -99,9 +99,15 @@
         ));
         Set<ProvidedCapability> provided = new HashSet<>(capabilities.getProvidedCapabilities());
         assertEquals(pExpected.size(), provided.size());
+        StringBuilder missingProvided = new StringBuilder();
         for (ProvidedCapability capability : pExpected) {
             boolean removed = provided.remove(capability);
-            assertTrue(String.format("Did not find expected provided capability %s.", capability), removed);
+            if (!removed) {
+                missingProvided.append("Missing capability: ").append(capability.toString()).append(System.lineSeparator());
+            }
+        }
+        if (missingProvided.length() > 0) {
+            fail(missingProvided.toString());
         }
 
         Set<RequiredCapability> rExpected = new HashSet<>(Arrays.asList(
@@ -111,9 +117,15 @@
         ));
         Set<RequiredCapability> required = new HashSet<>(capabilities.getRequiredCapabilities());
         assertEquals(rExpected.size(), required.size());
+        StringBuilder missingRequired = new StringBuilder();
         for (RequiredCapability capability : rExpected) {
             boolean removed = required.remove(capability);
-            assertTrue(String.format("Did not find expected required capability %s.", capability), removed);
+            if (!removed) {
+                missingRequired.append("Missing required capability: ").append(capability.toString()).append(System.lineSeparator());
+            }
+        }
+        if (missingRequired.length() > 0) {
+            fail(missingRequired.toString());
         }
     }
 
diff --git a/src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/GET.txt.html b/src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/GET.txt.html
deleted file mode 100644
index 2853663..0000000
--- a/src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/GET.txt.html
+++ /dev/null
@@ -1,18 +0,0 @@
-<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-  ~ 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.
-  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
diff --git a/src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/GET.test.html b/src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/test.GET.html
similarity index 100%
rename from src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/GET.test.html
rename to src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/test.GET.html
diff --git a/src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/GET.test.html b/src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/test.txt.GET.html
similarity index 100%
copy from src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/GET.test.html
copy to src/test/resources/project-1/src/main/scripts/org.apache.sling.foobar/test.txt.GET.html