You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2022/08/11 06:16:46 UTC

[sling-maven-plugin] 01/01: add test for adapter annotation metadata generator

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

kwin pushed a commit to branch feature/replace-scannotation
in repository https://gitbox.apache.org/repos/asf/sling-maven-plugin.git

commit 972fef2b1fad5845b068a7ccf56f85c22f345fc3
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Thu Aug 11 08:16:40 2022 +0200

    add test for adapter annotation metadata generator
---
 sling-maven-plugin/pom.xml                         |  6 +++
 .../bundlesupport/GenerateAdapterMetadataMojo.java |  8 +--
 .../GenerateAdapterMetadataMojoTest.java           | 62 ++++++++++++++++++++++
 .../bundlesupport/annotationtest/Adapter1.java     | 13 +++++
 .../bundlesupport/annotationtest/Adapter2.java     |  9 ++++
 5 files changed, 94 insertions(+), 4 deletions(-)

diff --git a/sling-maven-plugin/pom.xml b/sling-maven-plugin/pom.xml
index fe8e523..116f789 100644
--- a/sling-maven-plugin/pom.xml
+++ b/sling-maven-plugin/pom.xml
@@ -265,6 +265,12 @@
             <version>15.0</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.11.0</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
     
     <profiles>
diff --git a/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/GenerateAdapterMetadataMojo.java b/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/GenerateAdapterMetadataMojo.java
index fabf269..ecdb065 100644
--- a/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/GenerateAdapterMetadataMojo.java
+++ b/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/GenerateAdapterMetadataMojo.java
@@ -81,19 +81,19 @@ public class GenerateAdapterMetadataMojo extends AbstractMojo {
 
     /** The directory which to check for classes with adapter metadata annotations. */
     @Parameter(defaultValue = "${project.build.outputDirectory}")
-    private File buildOutputDirectory;
+    File buildOutputDirectory;
 
     /**
      * Name of the generated descriptor file.
      */
     @Parameter(property = "adapter.descriptor.name", defaultValue = "SLING-INF/adapters.json")
-    private String fileName;
+    String fileName;
 
     /**
      * The output directory in which to emit the descriptor file with name {@link GenerateAdapterMetadataMojo#fileName}.
      */
     @Parameter(defaultValue = "${project.build.outputDirectory}", required = true)
-    private File outputDirectory;
+    File outputDirectory;
 
     /**
      * The Maven project.
@@ -143,7 +143,7 @@ public class GenerateAdapterMetadataMojo extends AbstractMojo {
                     JsonWriter jsonWriter = Json.createWriter(writer)) {
                 jsonWriter.writeObject(JsonSupport.toJson(descriptor));
             }
-            addResource();
+            //addResource();
 
         } catch (IOException e) {
             throw new MojoExecutionException("Unable to generate metadata", e);
diff --git a/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/GenerateAdapterMetadataMojoTest.java b/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/GenerateAdapterMetadataMojoTest.java
new file mode 100644
index 0000000..bce04a9
--- /dev/null
+++ b/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/GenerateAdapterMetadataMojoTest.java
@@ -0,0 +1,62 @@
+package org.apache.sling.maven.bundlesupport;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import javax.json.Json;
+import javax.json.JsonObjectBuilder;
+import javax.json.JsonReader;
+
+import org.apache.commons.io.file.PathUtils;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.sling.maven.bundlesupport.annotationtest.Adapter1;
+import org.apache.sling.maven.bundlesupport.annotationtest.Adapter2;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+
+public class GenerateAdapterMetadataMojoTest {
+
+    @Rule
+    public TemporaryFolder tmpDirectory = new TemporaryFolder();
+
+    @Test
+    public void testExecute() throws MojoExecutionException, MojoFailureException, IOException {
+        GenerateAdapterMetadataMojo mojo = new GenerateAdapterMetadataMojo();
+        // copy classes in package "annotationtest" to classpath?
+        File classpathFolder = tmpDirectory.newFolder("test-classpath");
+        Path testClasspath = Paths.get(GenerateAdapterMetadataMojoTest.class.getProtectionDomain().getCodeSource().getLocation().getPath());
+        // only support directory right now
+        if (!Files.isDirectory(testClasspath)) {
+            throw new IllegalStateException("Only supposed to be called from a directory, not a jar file");
+        }
+        testClasspath.resolve("org/apache/sling/bundlesupport/annotationtest");
+        PathUtils.copyDirectory(testClasspath, classpathFolder.toPath());
+        mojo.buildOutputDirectory = classpathFolder;
+        mojo.outputDirectory = tmpDirectory.getRoot();
+        mojo.fileName = "output.json";
+        mojo.execute();
+
+        // check output file
+        Path outputFile = new File(tmpDirectory.getRoot(), "output.json").toPath();
+        assertTrue(Files.exists(outputFile));
+        JsonObjectBuilder expectedJsonObjectBuilder = Json.createObjectBuilder();
+        expectedJsonObjectBuilder.add("java.lang.Long", Json.createObjectBuilder().add("first condition", Adapter2.class.getName()));
+        expectedJsonObjectBuilder.add("java.lang.String", Json.createObjectBuilder().add("If the adaptable is a Adapter1.", Json.createArrayBuilder().add(Adapter1.class.getName()).add(Adapter2.class.getName())));
+        expectedJsonObjectBuilder.add("java.lang.Integer", Json.createObjectBuilder().add("If the adaptable is a Adapter1.", Adapter1.class.getName()));
+        
+        try (InputStream input = Files.newInputStream(outputFile);
+             JsonReader jsonReader = Json.createReader(input)) {
+            assertEquals(expectedJsonObjectBuilder.build(), jsonReader.readObject());
+        }
+    }
+}
diff --git a/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/annotationtest/Adapter1.java b/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/annotationtest/Adapter1.java
new file mode 100644
index 0000000..e0b3a5f
--- /dev/null
+++ b/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/annotationtest/Adapter1.java
@@ -0,0 +1,13 @@
+package org.apache.sling.maven.bundlesupport.annotationtest;
+
+import org.apache.sling.adapter.annotations.Adaptable;
+import org.apache.sling.adapter.annotations.Adaptables;
+import org.apache.sling.adapter.annotations.Adapter;
+
+@Adaptables({
+    @Adaptable( adaptableClass = String.class, adapters = { @Adapter(value = {Adapter1.class, Adapter2.class}) }),
+    @Adaptable( adaptableClass = Integer.class, adapters = { @Adapter(Adapter1.class) })
+})
+public class Adapter1 {
+
+}
diff --git a/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/annotationtest/Adapter2.java b/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/annotationtest/Adapter2.java
new file mode 100644
index 0000000..00505d3
--- /dev/null
+++ b/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/annotationtest/Adapter2.java
@@ -0,0 +1,9 @@
+package org.apache.sling.maven.bundlesupport.annotationtest;
+
+import org.apache.sling.adapter.annotations.Adaptable;
+import org.apache.sling.adapter.annotations.Adapter;
+
+@Adaptable(adaptableClass = Long.class, adapters = { @Adapter(condition = "first condition", value = Adapter2.class) })
+public class Adapter2 {
+
+}