This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-feature-launcher-maven-plugin.git

commit 9f2a5e0268aa993f3d725c53352ca664b3bd96cc
Author: Robert Munteanu <[email protected]>
AuthorDate: Thu Jun 25 16:08:11 2020 +0200

    SLING-9526 - Allow launching feature model applications in external 
processes, non-blocking
    
    Validation enhancements.
---
 .../sling/maven/feature/launcher/Launch.java       | 21 ++++++
 .../sling/maven/feature/launcher/StartMojo.java    | 16 ++--
 .../sling/maven/feature/launcher/LaunchTest.java   | 85 ++++++++++++++++++++++
 3 files changed, 114 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/sling/maven/feature/launcher/Launch.java 
b/src/main/java/org/apache/sling/maven/feature/launcher/Launch.java
index 0b98119..e691610 100644
--- a/src/main/java/org/apache/sling/maven/feature/launcher/Launch.java
+++ b/src/main/java/org/apache/sling/maven/feature/launcher/Launch.java
@@ -18,9 +18,13 @@
  */
 package org.apache.sling.maven.feature.launcher;
 
+import java.util.regex.Pattern;
+
 import org.apache.maven.model.Dependency;
 
 public class Launch {
+    
+    private static final Pattern ID_PATTERN = 
Pattern.compile("[a-zA-Z0-9_\\-\\.]+");
 
     private String id;
     private Dependency feature;
@@ -50,4 +54,21 @@ public class Launch {
         this.launcherArguments = launcherArguments;
     }
 
+    public void validate() {
+        if ( id == null || id.trim().isEmpty() ) 
+            throw new IllegalArgumentException("Missing id");
+        
+        if ( !ID_PATTERN.matcher(id).matches() )
+            throw new IllegalArgumentException("Invalid id '" + id + "'. 
Allowed characters are digits, numbers, '-','_' and '.'.");
+        
+        if ( feature == null )
+            throwInvalid("required field 'feature' is missing");
+        
+        if ( ! "slingosgifeature".equals(feature.getType()) )
+            throwInvalid("type must be 'slingosgifeature' but is '" + 
feature.getType()+"'");
+    }
+    
+    private void throwInvalid(String reason) {
+        throw new IllegalArgumentException("Invalid launch '" + id + "': " + 
reason);
+    }
 }
diff --git 
a/src/main/java/org/apache/sling/maven/feature/launcher/StartMojo.java 
b/src/main/java/org/apache/sling/maven/feature/launcher/StartMojo.java
index 5f6a87c..04e6a6a 100644
--- a/src/main/java/org/apache/sling/maven/feature/launcher/StartMojo.java
+++ b/src/main/java/org/apache/sling/maven/feature/launcher/StartMojo.java
@@ -110,8 +110,8 @@ public class StartMojo
             workDir.mkdirs();
             
             for ( Launch launch : launches ) {
-            
-                // TODO - validate it is actually a feature
+                launch.validate();
+
                 Artifact artifact = toArtifact(launch.getFeature());
                 
                 ArtifactResult result = 
resolver.resolveArtifact(repositorySession, new ArtifactRequest(artifact, null, 
null));
@@ -124,7 +124,7 @@ public class StartMojo
                 args.add("-f");
                 args.add(featureFile.getAbsolutePath());
                 args.add("-p");
-                args.add(launch.getId()); // TODO - validate launch id is a 
valid file name
+                args.add(launch.getId());
                 
                 for ( Map.Entry<String, String> frameworkProperty : 
launch.getLauncherArguments().getFrameworkProperties().entrySet() ) {
                     args.add("-D");
@@ -150,10 +150,8 @@ public class StartMojo
                         String line;
                         try {
                             while ( (line = reader.readLine()) != null ) {
-                                System.out.println(line);
-//                                getLog().info("Checking line '" + line);
+                                System.out.println(line); // NOSONAR - we pass 
through the subprocess stderr
                                 if ( line.contains("Framework started")) {
-//                                    getLog().info("STARTED!");
                                     latch.countDown();
                                     break;
                                 }
@@ -175,9 +173,11 @@ public class StartMojo
                 processes.startTracking(launch.getId(), process);
             }
 
-            // TODO - properly handle interrupted exception
-        } catch (ArtifactResolutionException | IOException | 
InterruptedException e) {
+        } catch (ArtifactResolutionException | IOException e) {
             throw new MojoExecutionException(e.getMessage(), e);
+        } catch ( InterruptedException e ) {
+            Thread.currentThread().interrupt();
+            throw new MojoExecutionException("Execution interrupted", e);
         }
     }
 
diff --git 
a/src/test/java/org/apache/sling/maven/feature/launcher/LaunchTest.java 
b/src/test/java/org/apache/sling/maven/feature/launcher/LaunchTest.java
new file mode 100644
index 0000000..4450b96
--- /dev/null
+++ b/src/test/java/org/apache/sling/maven/feature/launcher/LaunchTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.sling.maven.feature.launcher;
+
+import org.apache.maven.model.Dependency;
+import org.junit.Before;
+import org.junit.Test;
+
+public class LaunchTest {
+    
+    private Dependency validDep;
+    
+    @Before
+    public void prepare() {
+        validDep  = new Dependency();
+        validDep.setGroupId("org.apache.sling");
+        validDep.setArtifactId("org.apache.sling.starter");
+        validDep.setVersion("12");
+        validDep.setClassifier("oak_tar");
+        validDep.setType("slingosgifeature");
+    }
+
+    @Test
+    public void validLaunch() {
+        
+        Launch launch = new Launch();
+        launch.setFeature(validDep);
+        launch.setId("oak_TAR-12.1"); // covers all allowed character classes
+        launch.validate();
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void invalidLaunch_noId() {
+        
+        Launch launch = new Launch();
+        launch.setFeature(validDep);
+        launch.validate();
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void invalidLaunch_unsupportedType() {
+        
+        Dependency invalidDep = validDep.clone();
+        invalidDep.setType("jar");
+        
+        Launch launch = new Launch();
+        launch.setFeature(invalidDep);
+        launch.setId("oak_tar");
+        launch.validate();
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void invalidLaunch_noFeature() {
+        
+        Launch launch = new Launch();
+        launch.setId("no_feature");
+        launch.validate();
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void invalidLaunch_badId() {
+        
+        Launch launch = new Launch();
+        launch.setId("/feature");
+        launch.setFeature(validDep);
+        launch.validate();
+    }
+
+}

Reply via email to