sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject [sling-whiteboard] 04/04: Update the comparison method for generated Application JSON
Date Thu, 15 Mar 2018 07:44:18 GMT
This is an automated email from the ASF dual-hosted git repository.

davidb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit 977d6bcbb2c7f11f2939c79458332ef96e2d4985
Author: David Bosschaert <david.bosschaert@gmail.com>
AuthorDate: Mon Mar 12 17:56:18 2018 +0000

    Update the comparison method for generated Application JSON
---
 featuremodel/feature-applicationbuilder/pom.xml    | 24 +++++-----
 .../feature/applicationbuilder/impl/Main.java      | 14 +++---
 .../impl/ApplicationBuilderTest.java               | 55 +++++++++++++++++-----
 3 files changed, 62 insertions(+), 31 deletions(-)

diff --git a/featuremodel/feature-applicationbuilder/pom.xml b/featuremodel/feature-applicationbuilder/pom.xml
index 917e103..9f470e2 100644
--- a/featuremodel/feature-applicationbuilder/pom.xml
+++ b/featuremodel/feature-applicationbuilder/pom.xml
@@ -104,6 +104,12 @@
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.feature.analyser</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+            <scope>provided</scope>
+        </dependency>        
+        <dependency>
+            <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.feature.resolver</artifactId>
             <version>0.0.1-SNAPSHOT</version>
             <scope>provided</scope>
@@ -133,6 +139,12 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.framework</artifactId>
+            <version>5.6.10</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.commons.osgi</artifactId>
             <version>2.4.0</version>
@@ -151,17 +163,5 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>org.apache.felix</groupId>
-            <artifactId>org.apache.felix.framework</artifactId>
-            <version>5.6.10</version>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.sling</groupId>
-            <artifactId>org.apache.sling.feature.analyser</artifactId>
-            <version>0.0.1-SNAPSHOT</version>
-            <scope>test</scope>
-        </dependency>        
     </dependencies>
 </project>
diff --git a/featuremodel/feature-applicationbuilder/src/main/java/org/apache/sling/feature/applicationbuilder/impl/Main.java
b/featuremodel/feature-applicationbuilder/src/main/java/org/apache/sling/feature/applicationbuilder/impl/Main.java
index db733cb..3235e81 100644
--- a/featuremodel/feature-applicationbuilder/src/main/java/org/apache/sling/feature/applicationbuilder/impl/Main.java
+++ b/featuremodel/feature-applicationbuilder/src/main/java/org/apache/sling/feature/applicationbuilder/impl/Main.java
@@ -16,12 +16,6 @@
  */
 package org.apache.sling.feature.applicationbuilder.impl;
 
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.util.Collections;
-import java.util.stream.Stream;
-
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.CommandLineParser;
 import org.apache.commons.cli.DefaultParser;
@@ -39,6 +33,12 @@ import org.apache.sling.feature.support.json.ApplicationJSONWriter;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.stream.Stream;
+
 public class Main {
 
     private static Logger LOGGER;
@@ -190,7 +190,7 @@ public class Main {
     }
 
     private static void writeApplication(final Application app, final String out) {
-        LOGGER.info("Writing application...");
+        LOGGER.info("Writing application: " + out);
         final File file = new File(out);
         try ( final FileWriter writer = new FileWriter(file)) {
             ApplicationJSONWriter.write(writer, app);
diff --git a/featuremodel/feature-applicationbuilder/src/test/java/org/apache/sling/feature/applicationbuilder/impl/ApplicationBuilderTest.java
b/featuremodel/feature-applicationbuilder/src/test/java/org/apache/sling/feature/applicationbuilder/impl/ApplicationBuilderTest.java
index 414d426..0c88957 100644
--- a/featuremodel/feature-applicationbuilder/src/test/java/org/apache/sling/feature/applicationbuilder/impl/ApplicationBuilderTest.java
+++ b/featuremodel/feature-applicationbuilder/src/test/java/org/apache/sling/feature/applicationbuilder/impl/ApplicationBuilderTest.java
@@ -36,6 +36,7 @@ import org.osgi.framework.Constants;
 
 import java.io.File;
 import java.io.FileReader;
+import java.io.StringReader;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.net.URL;
@@ -45,6 +46,12 @@ import java.util.Collections;
 import java.util.Comparator;
 import java.util.Map;
 
+import javax.json.Json;
+import javax.json.JsonStructure;
+import javax.json.JsonWriter;
+import javax.json.JsonWriterFactory;
+import javax.json.stream.JsonGenerator;
+
 import static org.junit.Assert.assertEquals;
 
 public class ApplicationBuilderTest {
@@ -80,16 +87,21 @@ public class ApplicationBuilderTest {
 
         try (FeatureResolver fr = new FrameworkResolver(am, getFrameworkProps())) {
             Application app = ApplicationBuilder.assemble(null, bc, fr, features);
-            String genApp = writeApplication(app);
+            String actualJSON = writeApplication(app);
 
-            String expected = "{\"features\":["
+            String expectedJSON = "{\"features\":["
                     + "\"org.apache.sling.test.features:featureB:1.0.0\","
                     + "\"org.apache.sling.test.features:featureA:1.0.0\"],"
                 + "\"bundles\":["
-                    + "{\"id\":\"commons-io:commons-io:2.6\",\"start-order\":\"10\"},"
-                    + "{\"id\":\"org.apache.felix:org.apache.felix.http.servlet-api:1.1.2\",\"start-order\":\"15\"},"
-                    + "{\"id\":\"commons-fileupload:commons-fileupload:1.3.3\",\"start-order\":\"16\"}]}";
-            assertEquals(expected, genApp);
+                    + "{\"id\":\"commons-io:commons-io:2.6\",\"start-level\":\"10\",\"start-order\":\"10\"},"
+                    + "{\"id\":\"org.apache.felix:org.apache.felix.http.servlet-api:1.1.2\",\"start-level\":\"15\",\"start-order\":\"15\"},"
+                    + "{\"id\":\"commons-fileupload:commons-fileupload:1.3.3\",\"start-level\":\"16\",\"start-order\":\"16\"}]}";
+
+            StringWriter expectedWriter = new StringWriter();
+            StringWriter actualWriter = new StringWriter();
+
+            canonicalize(expectedJSON, expectedWriter, actualJSON, actualWriter);
+            assertEquals(expectedWriter.toString(), actualWriter.toString());
         }
     }
 
@@ -112,15 +124,28 @@ public class ApplicationBuilderTest {
             String expected = "{\"features\":["
                     + "\"org.apache.sling.test.features:featureC:1.0.0\","
                     + "\"org.apache.sling.test.features:featureD:1.0.0\"],"
-                    + "\"bundles\":[{\"id\":\"org.slf4j:slf4j-api:1.7.25\",\"start-order\":\"6\"}]}";
-            assertEquals(expected, genApp);
+                    + "\"bundles\":[{\"id\":\"org.slf4j:slf4j-api:1.7.25\",\"start-level\":\"6\",\"start-order\":\"6\"}]}";
+
+            StringWriter expectedWriter = new StringWriter();
+            StringWriter actualWriter = new StringWriter();
+            canonicalize(expected, expectedWriter, genApp, actualWriter);
+
+            assertEquals(expectedWriter.toString(), actualWriter.toString());
         }
     }
 
-    private static String writeApplication(Application app) throws Exception {
-        Writer writer = new StringWriter();
-        ApplicationJSONWriter.write(writer, app);
-        return writer.toString();
+    // Turn JSON into pretty-formatted canoncical JSON that should be comparable using String
compare
+    private void canonicalize(String expected, StringWriter expectedWriter, String actual,
StringWriter actualWriter) {
+        JsonStructure es = Json.createReader(new StringReader(expected)).read();
+        JsonStructure ea = Json.createReader(new StringReader(actual)).read();
+
+        JsonWriterFactory writerFactory = Json.createWriterFactory(
+                Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true));
+        JsonWriter w = writerFactory.createWriter(expectedWriter);
+        w.write(es);
+
+        JsonWriter w2 = writerFactory.createWriter(actualWriter);
+        w2.write(ea);
     }
 
     private Feature readFeature(final String res,
@@ -135,6 +160,12 @@ public class ApplicationBuilderTest {
         }
     }
 
+    private static String writeApplication(Application app) throws Exception {
+        Writer writer = new StringWriter();
+        ApplicationJSONWriter.write(writer, app);
+        return writer.toString();
+    }
+
     private static class TestFeatureProvider implements FeatureProvider {
         @Override
         public Feature provide(ArtifactId id) {

-- 
To stop receiving notification emails like this one, please contact
davidb@apache.org.

Mime
View raw message