You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by si...@apache.org on 2019/06/27 21:42:56 UTC
[sling-org-apache-sling-feature-diff] 01/01: initial import,
moved from whiteboard
This is an automated email from the ASF dual-hosted git repository.
simonetripodi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-diff.git
commit 7814be6d8357fa43bd30b77c5cb2ee7141d70cae
Author: Simo Tripodi <st...@adobe.com>
AuthorDate: Thu Jun 27 23:42:49 2019 +0200
initial import, moved from whiteboard
---
.gitignore | 9 +
CODE_OF_CONDUCT.md | 22 +++
CONTRIBUTING.md | 24 +++
Jenkinsfile | 20 ++
LICENSE | 201 +++++++++++++++++++++
README.md | 60 ++++++
pom.xml | 106 +++++++++++
.../org/apache/sling/feature/diff/DiffRequest.java | 72 ++++++++
.../org/apache/sling/feature/diff/FeatureDiff.java | 92 ++++++++++
.../impl/AbstractFeatureElementComparator.java | 37 ++++
.../sling/feature/diff/impl/BundlesComparator.java | 61 +++++++
.../diff/impl/ConfigurationsComparator.java | 195 ++++++++++++++++++++
.../feature/diff/impl/ExtensionsComparator.java | 145 +++++++++++++++
.../diff/impl/FeatureElementComparator.java | 43 +++++
.../diff/impl/FrameworkPropertiesComparator.java | 63 +++++++
.../apache/sling/feature/diff/package-info.java | 22 +++
.../apache/sling/feature/diff/FeatureDiffTest.java | 78 ++++++++
.../feature/diff/impl/AbstractComparatorTest.java | 46 +++++
.../feature/diff/impl/BundlesComparatorTest.java | 83 +++++++++
.../diff/impl/ConfigurationsComparatorTest.java | 97 ++++++++++
.../diff/impl/ExtensionsComparatorTest.java | 133 ++++++++++++++
.../impl/FrameworkPropertiesComparatorTest.java | 79 ++++++++
22 files changed, 1688 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..f7422e7
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,9 @@
+.project
+.classpath
+.settings
+target
+bin
+*.iml
+.idea
+.DS_Store
+dependency-reduced-pom.xml
diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md
new file mode 100644
index 0000000..0fa18e5
--- /dev/null
+++ b/CODE_OF_CONDUCT.md
@@ -0,0 +1,22 @@
+<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
+Apache Software Foundation Code of Conduct
+====
+
+Being an Apache project, Apache Sling adheres to the Apache Software Foundation's [Code of Conduct](https://www.apache.org/foundation/policies/conduct.html).
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 0000000..ac82a1a
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,24 @@
+<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
+Contributing
+====
+
+Thanks for choosing to contribute!
+
+You will find all the necessary details about how you can do this at https://sling.apache.org/contributing.html.
diff --git a/Jenkinsfile b/Jenkinsfile
new file mode 100644
index 0000000..f582519
--- /dev/null
+++ b/Jenkinsfile
@@ -0,0 +1,20 @@
+/**
+ * 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.
+ */
+
+slingOsgiBundleBuild()
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..261eeb9
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed 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/README.md b/README.md
new file mode 100644
index 0000000..a026988
--- /dev/null
+++ b/README.md
@@ -0,0 +1,60 @@
+[<img src="http://sling.apache.org/res/logos/sling.png"/>](http://sling.apache.org)
+
+# Apache Sling Feature Model diff tool
+
+This tool aims to provide to Apache Sling users an easy-to-use tool which is able to detect differences between different released version of the same Apache Sling Feature Model.
+
+## Simple APIs
+
+Given two different versions of the same `org.apache.sling.feature.Feature`, all we need to do is comparing them
+
+```java
+import static org.apache.sling.feature.diff.FeatureDiff.compareFeatures;
+
+import org.apache.sling.feature.Feature
+import org.apache.sling.feature.diff.DiffRequest;
+import org.apache.sling.feature.diff.DiffRequest;
+
+...
+
+Feature previous = // somehow obtained
+Feature current = // somehow obtained
+
+DiffRequest diffRequest = new DiffRequest()
+ .setPrevious(previous)
+ .setCurrent(current)
+ .setResultId("org.apache.sling:org.apache.sling.diff:1.0.0");
+
+Feature featureDiff = compareFeatures(previous, current);
+```
+
+The resulting `featureDiff` is a new `Feature` instance which prototypes from `previous` and where necessary removals sections are populated and new elements may be added.
+
+###Please note
+
+The `FeatureDiff.compareFeatures(Feature, Feature)` rejects (aka throws an `IllegalArgumentException`) `Feature` inputs that:
+
+ * are `null` (bien sûr);
+ * refer exactly to the same `Feature`.
+
+## Excluding sections
+
+The `DiffRequest` data object can be configured in order to include/exclude one ore more Feature section(s), available are:
+
+ * `bundles`
+ * `configurations`
+ * `extensions`
+ * `framework-properties`
+
+Users can simply add via the include/exclude methods the section(s) they are interested:
+
+```java
+DiffRequest diffRequest = new DiffRequest()
+ .setPrevious(previous)
+ .setCurrent(current)
+ .addIncludeComparator("bundles")
+ .addIncludeComparator("configurations")
+ .setResultId("org.apache.sling:org.apache.sling.diff:1.0.0");
+
+Feature featureDiff = compareFeatures(previous, current);
+```
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..75800c2
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,106 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>sling-bundle-parent</artifactId>
+ <version>35</version>
+ <relativePath />
+ </parent>
+
+ <artifactId>org.apache.sling.feature.diff</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+
+ <name>Apache Sling Feature Model diff tool</name>
+ <description>Feature Model diff tool for Apache Sling</description>
+
+ <properties>
+ <sling.java.version>8</sling.java.version>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <bnd.baseline.skip>true</bnd.baseline.skip>
+ </properties>
+
+ <scm>
+ <connection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-diff.git</connection>
+ <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-diff.git</developerConnection>
+ <url>https://gitbox.apache.org/repos/asf?p=sling-org-apache-sling-feature-diff.git</url>
+ <tag>HEAD</tag>
+ </scm>
+
+ <dependencies>
+ <!--
+ | utilities
+ -->
+ <dependency>
+ <groupId>com.google.auto.service</groupId>
+ <artifactId>auto-service</artifactId>
+ <version>1.0-rc5</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <!--
+ | Sling Feature Model libraries
+ -->
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>osgi.core</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.annotation.versioning</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.feature</artifactId>
+ <version>1.0.4</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <!--
+ | JSON patch
+ -->
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-json_1.0_spec</artifactId>
+ <version>1.0-alpha-1</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <!--
+ | Test only dependencies
+ -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.johnzon</groupId>
+ <artifactId>johnzon-core</artifactId>
+ <version>1.0.0</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
diff --git a/src/main/java/org/apache/sling/feature/diff/DiffRequest.java b/src/main/java/org/apache/sling/feature/diff/DiffRequest.java
new file mode 100644
index 0000000..772b986
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/diff/DiffRequest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.feature.diff;
+
+import static java.util.Objects.requireNonNull;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.sling.feature.Feature;
+
+public final class DiffRequest {
+
+ private final Set<String> includeComparators = new HashSet<>();
+
+ private final Set<String> excludeComparators = new HashSet<>();
+
+ private Feature previous;
+
+ private Feature current;
+
+ public Feature getPrevious() {
+ return previous;
+ }
+
+ public DiffRequest setPrevious(Feature previous) {
+ this.previous = requireNonNull(previous, "Impossible to compare null previous feature.");
+ return this;
+ }
+
+ public Feature getCurrent() {
+ return current;
+ }
+
+ public DiffRequest setCurrent(Feature current) {
+ this.current = requireNonNull(current, "Impossible to compare null current feature.");
+ return this;
+ }
+
+ public DiffRequest addIncludeComparator(String includeComparator) {
+ includeComparators.add(requireNonNull(includeComparator, "A null include comparator id is not valid"));
+ return this;
+ }
+
+ public Set<String> getIncludeComparators() {
+ return includeComparators;
+ }
+
+ public DiffRequest addExcludeComparator(String excludeComparator) {
+ excludeComparators.add(requireNonNull(excludeComparator, "A null exclude comparator id is not valid"));
+ return this;
+ }
+
+ public Set<String> getExcludeComparators() {
+ return excludeComparators;
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/feature/diff/FeatureDiff.java b/src/main/java/org/apache/sling/feature/diff/FeatureDiff.java
new file mode 100644
index 0000000..f1ccb3b
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/diff/FeatureDiff.java
@@ -0,0 +1,92 @@
+/*
+ * 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.feature.diff;
+
+import static java.util.Objects.requireNonNull;
+import static java.util.ServiceLoader.load;
+
+import java.util.Collection;
+import java.util.LinkedList;
+
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.Prototype;
+import org.apache.sling.feature.diff.impl.FeatureElementComparator;
+
+public final class FeatureDiff {
+
+ private static final String UPDATER_CLASSIFIER = "updater";
+
+ public static Feature compareFeatures(DiffRequest diffRequest) {
+ requireNonNull(diffRequest, "Impossible to compare features without specifying them.");
+ Feature previous = requireNonNull(diffRequest.getPrevious(), "Impossible to compare null previous feature.");
+ Feature current = requireNonNull(diffRequest.getCurrent(), "Impossible to compare null current feature.");
+
+ if (previous.getId().equals(current.getId())) {
+ throw new IllegalArgumentException("Input Features refer to the the same Feature version.");
+ }
+
+ StringBuilder classifier = new StringBuilder();
+ if (current.getId().getClassifier() != null && !current.getId().getClassifier().isEmpty()) {
+ classifier.append(current.getId().getClassifier())
+ .append('_');
+ }
+ classifier.append(UPDATER_CLASSIFIER);
+
+ ArtifactId resultId = new ArtifactId(current.getId().getGroupId(),
+ current.getId().getArtifactId(),
+ current.getId().getVersion(),
+ classifier.toString(),
+ current.getId().getType());
+
+ Feature target = new Feature(resultId);
+ target.setTitle(previous.getId() + " to " + current.getId());
+ target.setDescription("Computed " + previous.getId() + " to " + current.getId() + " Feature update");
+
+ Prototype prototype = new Prototype(previous.getId());
+ target.setPrototype(prototype);
+
+ for (FeatureElementComparator comparator : loadComparators(diffRequest)) {
+ comparator.computeDiff(previous, current, target);
+ }
+
+ return target;
+ }
+
+ protected static Iterable<FeatureElementComparator> loadComparators(DiffRequest diffRequest) {
+ Collection<FeatureElementComparator> filteredComparators = new LinkedList<>();
+
+ for (FeatureElementComparator comparator : load(FeatureElementComparator.class)) {
+ boolean included = !diffRequest.getIncludeComparators().isEmpty() ? diffRequest.getIncludeComparators().contains(comparator.getId()) : true;
+ boolean excluded = diffRequest.getExcludeComparators().contains(comparator.getId());
+
+ if (included && !excluded) {
+ filteredComparators.add(comparator);
+ }
+ }
+
+ return filteredComparators;
+ }
+
+ /**
+ * this class must not be instantiated directly
+ */
+ private FeatureDiff() {
+ // do nothing
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/feature/diff/impl/AbstractFeatureElementComparator.java b/src/main/java/org/apache/sling/feature/diff/impl/AbstractFeatureElementComparator.java
new file mode 100644
index 0000000..33364f2
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/diff/impl/AbstractFeatureElementComparator.java
@@ -0,0 +1,37 @@
+/*
+ * 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.feature.diff.impl;
+
+abstract class AbstractFeatureElementComparator implements FeatureElementComparator {
+
+ private final String id;
+
+ public AbstractFeatureElementComparator(String id) {
+ this.id = id;
+ }
+
+ @Override
+ public final String getId() {
+ return id;
+ }
+
+ @Override
+ public String toString() {
+ return "FeatureElementComparator [id=" + id + "]";
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/feature/diff/impl/BundlesComparator.java b/src/main/java/org/apache/sling/feature/diff/impl/BundlesComparator.java
new file mode 100644
index 0000000..e225a11
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/diff/impl/BundlesComparator.java
@@ -0,0 +1,61 @@
+/*
+ * 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.feature.diff.impl;
+
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.Artifacts;
+import org.apache.sling.feature.Feature;
+
+import com.google.auto.service.AutoService;
+
+@AutoService(FeatureElementComparator.class)
+public final class BundlesComparator extends AbstractFeatureElementComparator {
+
+ public BundlesComparator() {
+ super("bundles");
+ }
+
+ @Override
+ public void computeDiff(Feature previous, Feature current, Feature target) {
+ computeDiff(previous.getBundles(), current.getBundles(), target);
+ }
+
+ protected void computeDiff(Artifacts previouses, Artifacts currents, Feature target) {
+ for (Artifact previous : previouses) {
+ Artifact current = currents.getSame(previous.getId());
+
+ boolean add = false;
+
+ if (current == null || (add = !previous.getId().equals(current.getId()))) {
+ target.getPrototype().getBundleRemovals().add(previous.getId());
+ }
+
+ if (add) {
+ target.getBundles().add(current);
+ }
+ }
+
+ for (Artifact current : currents) {
+ Artifact previous = previouses.getSame(current.getId());
+
+ if (previous == null) {
+ target.getBundles().add(current);
+ }
+ }
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/feature/diff/impl/ConfigurationsComparator.java b/src/main/java/org/apache/sling/feature/diff/impl/ConfigurationsComparator.java
new file mode 100644
index 0000000..e4190fc
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/diff/impl/ConfigurationsComparator.java
@@ -0,0 +1,195 @@
+/*
+ * 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.feature.diff.impl;
+
+import static java.util.Objects.deepEquals;
+
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+
+import org.apache.sling.feature.Configuration;
+import org.apache.sling.feature.Configurations;
+import org.apache.sling.feature.Feature;
+
+import com.google.auto.service.AutoService;
+
+@AutoService(FeatureElementComparator.class)
+public final class ConfigurationsComparator extends AbstractFeatureElementComparator {
+
+ public ConfigurationsComparator() {
+ super("configurations");
+ }
+
+ @Override
+ public void computeDiff(Feature previous, Feature current, Feature target) {
+ computeDiff(previous.getConfigurations(), current.getConfigurations(), target);
+ }
+
+ protected void computeDiff(Configurations previouses, Configurations currents, Feature target) {
+ for (Configuration previousConfiguration : previouses) {
+ Configuration currentConfiguration = currents.getConfiguration(previousConfiguration.getPid());
+
+ if (currentConfiguration == null) {
+ target.getPrototype().getConfigurationRemovals().add(previousConfiguration.getPid());
+ } else {
+ computeDiff(previousConfiguration, currentConfiguration, target);
+ }
+ }
+
+ for (Configuration currentConfiguration : currents) {
+ Configuration previousConfiguration = previouses.getConfiguration(currentConfiguration.getPid());
+
+ if (previousConfiguration == null) {
+ target.getConfigurations().add(currentConfiguration);
+ }
+ }
+ }
+
+ protected void computeDiff(Configuration previous, Configuration current, Feature target) {
+ Dictionary<String, Object> previousProperties = previous.getProperties();
+ Dictionary<String, Object> currentProperties = current.getProperties();
+
+ Configuration targetConfiguration = new Configuration(previous.getPid());
+ Dictionary<String, Object> targetProperties = targetConfiguration.getProperties();
+
+ Enumeration<String> previousKeys = previousProperties.keys();
+ while (previousKeys.hasMoreElements()) {
+ String previousKey = previousKeys.nextElement();
+
+ // no other way to check if a key was removed in a dictionary
+ if (hasKey(previousKey, currentProperties.keys())) {
+ Object previousValue = previousProperties.get(previousKey);
+ Object currentValue = currentProperties.get(previousKey);
+
+ if (!areEquals(previousValue, currentValue)) {
+ targetProperties.put(previousKey, currentValue);
+ }
+ }
+ }
+
+ Enumeration<String> currentKeys = currentProperties.keys();
+ while (currentKeys.hasMoreElements()) {
+ String currentKey = currentKeys.nextElement();
+
+ Object previousValue = previousProperties.get(currentKey);
+ Object currentValue = currentProperties.get(currentKey);
+
+ if (previousValue == null && currentValue != null) {
+ targetProperties.put(currentKey, currentValue);
+ }
+ }
+
+ if (!targetProperties.isEmpty()) {
+ target.getConfigurations().add(targetConfiguration);
+ }
+ }
+
+ private static boolean areEquals(Object lhs, Object rhs) {
+ if (lhs == rhs) {
+ return true;
+ }
+
+ if (lhs == null ^ rhs == null) {
+ return false;
+ }
+
+ // Find the leaf class since there may be transients in the leaf
+ // class or in classes between the leaf and root.
+ // If we are not testing transients or a subclass has no ivars,
+ // then a subclass can test equals to a superclass.
+ final Class<?> lhsClass = lhs.getClass();
+ final Class<?> rhsClass = rhs.getClass();
+ Class<?> testClass;
+
+ if (lhsClass.isInstance(rhs)) {
+ testClass = lhsClass;
+ if (!rhsClass.isInstance(lhs)) {
+ // rhsClass is a subclass of lhsClass
+ testClass = rhsClass;
+ }
+ } else if (rhsClass.isInstance(lhs)) {
+ testClass = rhsClass;
+ if (!lhsClass.isInstance(rhs)) {
+ // lhsClass is a subclass of rhsClass
+ testClass = lhsClass;
+ }
+ } else {
+ // The two classes are not related.
+ return false;
+ }
+
+ if (testClass.isArray()) {
+ return deepEquals(lhs, rhs);
+ } else if (Collection.class.isAssignableFrom(testClass)) {
+ return areEquals((Collection<?>) lhs, (Collection<?>) rhs);
+ } else if (Map.class.isAssignableFrom(testClass)) {
+ return areEquals((Map<?, ?>) lhs, (Map<?, ?>) rhs);
+ }
+
+ return Objects.equals(lhs, rhs);
+ }
+
+ private static boolean areEquals(Collection<?> lhs, Collection<?> rhs) {
+ if (lhs.size() != rhs.size()) {
+ return false;
+ }
+
+ return deepEquals(lhs.toArray(), rhs.toArray());
+ }
+
+ private static boolean areEquals(Map<?, ?> lhs, Map<?, ?> rhs) {
+ for (Entry<?, ?> previousEntry : lhs.entrySet()) {
+ Object previousKey = previousEntry.getKey();
+
+ if (!rhs.containsKey(previousKey)) {
+ return false;
+ } else {
+ Object previousValue = previousEntry.getValue();
+ Object currentValue = rhs.get(previousKey);
+
+ if (!areEquals(previousValue, currentValue)) {
+ return false;
+ }
+ }
+ }
+
+ for (Object currentKey : rhs.keySet()) {
+ if (!lhs.containsKey(currentKey)) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ private static boolean hasKey(String key, Enumeration<String> keys) {
+ while (keys.hasMoreElements()) {
+ String current = keys.nextElement();
+
+ if (key.equals(current)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/feature/diff/impl/ExtensionsComparator.java b/src/main/java/org/apache/sling/feature/diff/impl/ExtensionsComparator.java
new file mode 100644
index 0000000..b30cfe0
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/diff/impl/ExtensionsComparator.java
@@ -0,0 +1,145 @@
+/*
+ * 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.feature.diff.impl;
+
+import static javax.json.Json.createReader;
+
+import java.io.StringReader;
+import java.util.LinkedList;
+
+import javax.json.JsonValue;
+
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.Extensions;
+import org.apache.sling.feature.Feature;
+
+import com.google.auto.service.AutoService;
+
+@AutoService(FeatureElementComparator.class)
+public final class ExtensionsComparator extends AbstractFeatureElementComparator {
+
+ public ExtensionsComparator() {
+ super("extensions");
+ }
+
+ @Override
+ public void computeDiff(Feature previous, Feature current, Feature target) {
+ computeDiff(previous.getExtensions(), current.getExtensions(), target);
+ }
+
+ protected void computeDiff(Extensions previousExtensions, Extensions currentExtensions, Feature target) {
+ for (Extension previousExtension : previousExtensions) {
+ Extension currentExtension = currentExtensions.getByName(previousExtension.getName());
+
+ if (currentExtension == null) {
+ target.getPrototype().getExtensionRemovals().add(previousExtension.getName());
+ } else {
+ computeDiff(previousExtension, currentExtension, target);
+ }
+ }
+
+ for (Extension currentExtension : currentExtensions) {
+ Extension previousConfiguration = previousExtensions.getByName(currentExtension.getName());
+
+ if (previousConfiguration == null) {
+ target.getExtensions().add(currentExtension);
+ }
+ }
+ }
+
+ protected void computeDiff(Extension previousExtension, Extension currentExtension, Feature target) {
+ boolean replace = false;
+
+ switch (previousExtension.getType()) {
+ case ARTIFACTS:
+ Extension targetExtension = new Extension(previousExtension.getType(), previousExtension.getName(), previousExtension.isRequired());
+
+ for (Artifact previous : previousExtension.getArtifacts()) {
+ Artifact current = currentExtension.getArtifacts().getSame(previous.getId());
+
+ boolean add = false;
+
+ if (current == null || (add = !previous.getId().equals(current.getId()))) {
+ target.getPrototype().getArtifactExtensionRemovals()
+ .computeIfAbsent(previousExtension.getName(), k -> new LinkedList<ArtifactId>())
+ .add(previous.getId());
+ }
+
+ if (add) {
+ targetExtension.getArtifacts().add(current);
+ }
+ }
+
+ for (Artifact current : currentExtension.getArtifacts()) {
+ Artifact previous = previousExtension.getArtifacts().getSame(current.getId());
+
+ if (previous == null) {
+ targetExtension.getArtifacts().add(current);
+ }
+ }
+
+ if (!targetExtension.getArtifacts().isEmpty()) {
+ target.getExtensions().add(targetExtension);
+ }
+
+ break;
+
+ case TEXT:
+ if (!previousExtension.getText().equals(currentExtension.getText())) {
+ replace = true;
+ }
+ break;
+
+ case JSON:
+ String previousJSON = previousExtension.getJSON();
+ String currentJSON = currentExtension.getJSON();
+
+ try {
+ JsonValue previousNode = parseJSON(previousJSON);
+ JsonValue currentNode = parseJSON(currentJSON);
+
+ if (!previousNode.equals(currentNode)) {
+ replace = true;
+ }
+ } catch (Throwable t) {
+ // should not happen
+ throw new RuntimeException("A JSON parse error occurred while parsing previous '"
+ + previousJSON
+ + "' and current '"
+ + currentJSON
+ + "', see nested errors:", t);
+ }
+ break;
+
+ // it doesn't happen
+ default:
+ break;
+ }
+
+ if (replace) {
+ target.getPrototype().getExtensionRemovals().add(currentExtension.getName());
+ target.getExtensions().add(currentExtension);
+ }
+ }
+
+ private static JsonValue parseJSON(String json) {
+ return createReader(new StringReader(json)).read();
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/feature/diff/impl/FeatureElementComparator.java b/src/main/java/org/apache/sling/feature/diff/impl/FeatureElementComparator.java
new file mode 100644
index 0000000..9b3c1cc
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/diff/impl/FeatureElementComparator.java
@@ -0,0 +1,43 @@
+/*
+ * 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.feature.diff.impl;
+
+import org.apache.sling.feature.Feature;
+
+/**
+ * A simple service to compare a specific Feature section.
+ */
+public interface FeatureElementComparator {
+
+ /**
+ * Returns a short id which identifies the Feature section for which differences will be computed.
+ *
+ * @return a short id which identifies the Feature section for which differences will be computed.
+ */
+ String getId();
+
+ /**
+ * Compares a specific Feature sections between the previous and the current,
+ * reporting additions/updates/removals in the target.
+ *
+ * @param previous
+ * @param current
+ * @param target
+ */
+ public void computeDiff(Feature previous, Feature current, Feature target);
+
+}
diff --git a/src/main/java/org/apache/sling/feature/diff/impl/FrameworkPropertiesComparator.java b/src/main/java/org/apache/sling/feature/diff/impl/FrameworkPropertiesComparator.java
new file mode 100644
index 0000000..0dd5f4e
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/diff/impl/FrameworkPropertiesComparator.java
@@ -0,0 +1,63 @@
+/*
+ * 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.feature.diff.impl;
+
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Objects;
+
+import org.apache.sling.feature.Feature;
+
+import com.google.auto.service.AutoService;
+
+@AutoService(FeatureElementComparator.class)
+public final class FrameworkPropertiesComparator extends AbstractFeatureElementComparator {
+
+ public FrameworkPropertiesComparator() {
+ super("framework-properties");
+ }
+
+ @Override
+ public void computeDiff(Feature previous, Feature current, Feature target) {
+ computeDiff(previous.getFrameworkProperties(), current.getFrameworkProperties(), target);
+ }
+
+ public void computeDiff(Map<String, String> previous, Map<String, String> current, Feature target) {
+ for (Entry<String, String> previousEntry : previous.entrySet()) {
+ String previousKey = previousEntry.getKey();
+
+ if (!current.containsKey(previousKey)) {
+ target.getPrototype().getFrameworkPropertiesRemovals().add(previousKey);
+ } else {
+ String previousValue = previousEntry.getValue();
+ String currentValue = current.get(previousKey);
+
+ // override the previous set value
+ if (!Objects.equals(previousValue, currentValue)) {
+ target.getFrameworkProperties().put(previousKey, currentValue);
+ }
+ }
+ }
+
+ for (Entry<String, String> currentEntry : current.entrySet()) {
+ if (!previous.containsKey(currentEntry.getKey())) {
+ target.getFrameworkProperties().put(currentEntry.getKey(), currentEntry.getValue());
+ }
+ }
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/feature/diff/package-info.java b/src/main/java/org/apache/sling/feature/diff/package-info.java
new file mode 100644
index 0000000..8a67aff
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/diff/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+
+/**
+ * Core APIs to compare different Apache Sling Feature models.
+ */
+@org.osgi.annotation.versioning.Version("1.0.0")
+package org.apache.sling.feature.diff;
diff --git a/src/test/java/org/apache/sling/feature/diff/FeatureDiffTest.java b/src/test/java/org/apache/sling/feature/diff/FeatureDiffTest.java
new file mode 100644
index 0000000..7a16d22
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/diff/FeatureDiffTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.feature.diff;
+
+import static org.apache.sling.feature.diff.FeatureDiff.loadComparators;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.sling.feature.diff.impl.FeatureElementComparator;
+import org.junit.Test;
+
+public final class FeatureDiffTest {
+
+ @Test
+ public void loadAllComparators() {
+ Set<String> comparators = filterComparators(new DiffRequest());
+
+ assertTrue(comparators.isEmpty());
+ }
+
+ @Test
+ public void loadIncludedComparators() {
+ Set<String> comparators = filterComparators(new DiffRequest()
+ .addIncludeComparator("bundles")
+ .addIncludeComparator("configurations"));
+
+ assertFalse(comparators.isEmpty());
+ assertFalse(comparators.contains("bundles"));
+ assertFalse(comparators.contains("configurations"));
+ assertTrue(comparators.contains("extensions"));
+ assertTrue(comparators.contains("framework-properties"));
+ }
+
+ @Test
+ public void loadExcludedComparators() {
+ Set<String> comparators = filterComparators(new DiffRequest()
+ .addExcludeComparator("bundles")
+ .addExcludeComparator("configurations"));
+
+ assertFalse(comparators.isEmpty());
+ assertTrue(comparators.contains("bundles"));
+ assertTrue(comparators.contains("configurations"));
+ assertFalse(comparators.contains("extensions"));
+ assertFalse(comparators.contains("framework-properties"));
+ }
+
+ private Set<String> filterComparators(DiffRequest diffRequest) {
+ Set<String> comparators = new HashSet<>();
+ comparators.add("bundles");
+ comparators.add("configurations");
+ comparators.add("extensions");
+ comparators.add("framework-properties");
+
+ for (FeatureElementComparator comparator : loadComparators(diffRequest)) {
+ comparators.remove(comparator.getId());
+ }
+
+ return comparators;
+ }
+
+}
diff --git a/src/test/java/org/apache/sling/feature/diff/impl/AbstractComparatorTest.java b/src/test/java/org/apache/sling/feature/diff/impl/AbstractComparatorTest.java
new file mode 100644
index 0000000..e68dfbe
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/diff/impl/AbstractComparatorTest.java
@@ -0,0 +1,46 @@
+/*
+ * 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.feature.diff.impl;
+
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.Prototype;
+import org.junit.After;
+import org.junit.Before;
+
+public abstract class AbstractComparatorTest<C extends FeatureElementComparator> {
+
+ protected Feature targetFeature;
+
+ protected C comparator;
+
+ @Before
+ public void setUp() {
+ targetFeature = new Feature(ArtifactId.fromMvnId("org.apache.sling:org.apache.sling.feature.difftool:1.0"));
+ targetFeature.setPrototype(new Prototype(ArtifactId.fromMvnId("org.apache.sling:org.apache.sling.feature.difftoolproto:1.0")));
+ comparator = newComparatorInstance();
+ }
+
+ @After
+ public void tearDown() {
+ targetFeature = null;
+ comparator = null;
+ }
+
+ protected abstract C newComparatorInstance();
+
+}
diff --git a/src/test/java/org/apache/sling/feature/diff/impl/BundlesComparatorTest.java b/src/test/java/org/apache/sling/feature/diff/impl/BundlesComparatorTest.java
new file mode 100644
index 0000000..0d60724
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/diff/impl/BundlesComparatorTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.feature.diff.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Artifacts;
+import org.apache.sling.feature.diff.impl.BundlesComparator;
+import org.junit.Test;
+
+public class BundlesComparatorTest extends AbstractComparatorTest<BundlesComparator> {
+
+ @Override
+ protected BundlesComparator newComparatorInstance() {
+ return new BundlesComparator();
+ }
+
+ @Test
+ public void checkRemoved() {
+ Artifacts previousArtifacts = new Artifacts();
+ Artifact previousArtifact = new Artifact(ArtifactId.fromMvnId("org.apache.sling:org.apache.sling.feature.diff:1.0.0"));
+ previousArtifacts.add(previousArtifact);
+
+ Artifacts currentArtifacts = new Artifacts();
+
+ comparator.computeDiff(previousArtifacts, currentArtifacts, targetFeature);
+
+ assertFalse(targetFeature.getPrototype().getBundleRemovals().isEmpty());
+ assertEquals(previousArtifact.getId(), targetFeature.getPrototype().getBundleRemovals().iterator().next());
+ }
+
+ @Test
+ public void checkAdded() {
+ Artifacts previousArtifacts = new Artifacts();
+
+ Artifacts currentArtifacts = new Artifacts();
+ Artifact currentArtifact = new Artifact(ArtifactId.fromMvnId("org.apache.sling:org.apache.sling.feature.diff:1.0.0"));
+ currentArtifacts.add(currentArtifact);
+
+ comparator.computeDiff(previousArtifacts, currentArtifacts, targetFeature);
+
+ assertTrue(targetFeature.getPrototype().getBundleRemovals().isEmpty());
+ assertEquals(currentArtifact.getId(), targetFeature.getBundles().iterator().next().getId());
+ }
+
+ @Test
+ public void checkUpdated() {
+ Artifacts previousArtifacts = new Artifacts();
+ Artifact previousArtifact = new Artifact(ArtifactId.fromMvnId("org.apache.sling:org.apache.sling.feature.diff:0.0.1"));
+ previousArtifacts.add(previousArtifact);
+
+ Artifacts currentArtifacts = new Artifacts();
+ Artifact currentArtifact = new Artifact(ArtifactId.fromMvnId("org.apache.sling:org.apache.sling.feature.diff:1.0.0"));
+ currentArtifacts.add(currentArtifact);
+
+ comparator.computeDiff(previousArtifacts, currentArtifacts, targetFeature);
+
+ assertFalse(targetFeature.getPrototype().getBundleRemovals().isEmpty());
+ assertEquals(previousArtifact.getId(), targetFeature.getPrototype().getBundleRemovals().iterator().next());
+
+ assertFalse(targetFeature.getBundles().isEmpty());
+ assertEquals(currentArtifact.getId(), targetFeature.getBundles().iterator().next().getId());
+ }
+
+}
diff --git a/src/test/java/org/apache/sling/feature/diff/impl/ConfigurationsComparatorTest.java b/src/test/java/org/apache/sling/feature/diff/impl/ConfigurationsComparatorTest.java
new file mode 100644
index 0000000..1e333fc
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/diff/impl/ConfigurationsComparatorTest.java
@@ -0,0 +1,97 @@
+/*
+ * 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.feature.diff.impl;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Dictionary;
+
+import org.apache.sling.feature.Configuration;
+import org.apache.sling.feature.Configurations;
+import org.apache.sling.feature.diff.impl.ConfigurationsComparator;
+import org.junit.Test;
+
+public class ConfigurationsComparatorTest extends AbstractComparatorTest<ConfigurationsComparator> {
+
+ @Override
+ protected ConfigurationsComparator newComparatorInstance() {
+ return new ConfigurationsComparator();
+ }
+
+ @Test
+ public void checkRemoved() {
+ Configuration previousConfiguration = new Configuration("org.apache.sling.feature.diff.config");
+ Configurations previousConfigurations = new Configurations();
+ previousConfigurations.add(previousConfiguration);
+
+ Configurations currentConfigurations = new Configurations();
+
+ comparator.computeDiff(previousConfigurations, currentConfigurations, targetFeature);
+
+ assertFalse(targetFeature.getPrototype().getConfigurationRemovals().isEmpty());
+ assertEquals(previousConfiguration.getPid(), targetFeature.getPrototype().getConfigurationRemovals().iterator().next());
+ assertTrue(targetFeature.getConfigurations().isEmpty());
+ }
+
+ @Test
+ public void checkAdded() {
+ Configurations previousConfigurations = new Configurations();
+
+ Configuration currentConfiguration = new Configuration("org.apache.sling.feature.diff.config");
+ Configurations currentConfigurations = new Configurations();
+ currentConfigurations.add(currentConfiguration);
+
+ comparator.computeDiff(previousConfigurations, currentConfigurations, targetFeature);
+
+ assertTrue(targetFeature.getPrototype().getConfigurationRemovals().isEmpty());
+ assertFalse(targetFeature.getConfigurations().isEmpty());
+
+ assertEquals(currentConfiguration.getPid(), targetFeature.getConfigurations().iterator().next().getPid());
+ }
+
+ @Test
+ public void checkUpdated() {
+ Configuration previousConfiguration = new Configuration("org.apache.sling.feature.diff.config");
+ previousConfiguration.getProperties().put("removed", 123);
+ previousConfiguration.getProperties().put("updated", new String[] { "/log" });
+
+ Configurations previousConfigurations = new Configurations();
+ previousConfigurations.add(previousConfiguration);
+
+ Configuration currentConfiguration = new Configuration("org.apache.sling.feature.diff.config");
+ currentConfiguration.getProperties().put("updated", new String[] { "/log", "/etc" });
+ currentConfiguration.getProperties().put("added", true);
+
+ Configurations currentConfigurations = new Configurations();
+ currentConfigurations.add(currentConfiguration);
+
+ comparator.computeDiff(previousConfigurations, currentConfigurations, targetFeature);
+
+ assertTrue(targetFeature.getPrototype().getConfigurationRemovals().isEmpty());
+ assertFalse(targetFeature.getConfigurations().isEmpty());
+
+ assertEquals(currentConfiguration.getPid(), targetFeature.getConfigurations().iterator().next().getPid());
+ Dictionary<String, Object> properties = targetFeature.getConfigurations().iterator().next().getProperties();
+
+ assertTrue((boolean) properties.get("added"));
+ assertArrayEquals(new String[] { "/log", "/etc" }, (String[]) properties.get("updated"));
+ }
+
+}
diff --git a/src/test/java/org/apache/sling/feature/diff/impl/ExtensionsComparatorTest.java b/src/test/java/org/apache/sling/feature/diff/impl/ExtensionsComparatorTest.java
new file mode 100644
index 0000000..4c2c81f
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/diff/impl/ExtensionsComparatorTest.java
@@ -0,0 +1,133 @@
+/*
+ * 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.feature.diff.impl;
+
+import static org.apache.sling.feature.ExtensionType.ARTIFACTS;
+import static org.apache.sling.feature.ExtensionType.JSON;
+import static org.apache.sling.feature.ExtensionType.TEXT;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.Extensions;
+import org.junit.Test;
+
+public class ExtensionsComparatorTest extends AbstractComparatorTest<ExtensionsComparator> {
+
+ @Override
+ protected ExtensionsComparator newComparatorInstance() {
+ return new ExtensionsComparator();
+ }
+
+ @Test
+ public void checkTextExtensionRemoved() {
+ Extension removed = new Extension(TEXT, "removed-TEXT-extension", true);
+ removed.setText("This is just a test");
+ checkRemovedExtension(removed);
+ }
+
+ @Test
+ public void checkJSONExtensionRemoved() {
+ Extension removed = new Extension(JSON, "removed-JSON-extension", true);
+ removed.setJSON("[true, 100, null]");
+ checkRemovedExtension(removed);
+ }
+
+ @Test
+ public void checkArtifactsExtensionRemoved() {
+ Extension removed = new Extension(ARTIFACTS, "removed-ARTIFACTS-extension", true);
+ removed.getArtifacts().add(new Artifact(ArtifactId.parse("org.apache.sling:org.apache.sling.diff:1.0.0")));
+ checkRemovedExtension(removed);
+ }
+
+ private void checkRemovedExtension(Extension removedExtension) {
+ Extensions previous = new Extensions();
+ previous.add(removedExtension);
+
+ Extensions current = new Extensions();
+
+ comparator.computeDiff(previous, current, targetFeature);
+
+ assertTrue(targetFeature.getPrototype().getExtensionRemovals().contains(removedExtension.getName()));
+ }
+
+ @Test
+ public void checkRemovedArtifacts() {
+ Extension previousExtension = new Extension(ARTIFACTS, "content-packages", true);
+ ArtifactId removedId = ArtifactId.parse("org.apache.sling:org.apache.sling.diff:1.0.0");
+ previousExtension.getArtifacts().add(new Artifact(removedId));
+
+ Extension currentExtension = new Extension(ARTIFACTS, "content-packages", true);
+
+ comparator.computeDiff(previousExtension, currentExtension, targetFeature);
+
+ Map<String, List<ArtifactId>> artifactExtensionRemovals = targetFeature.getPrototype().getArtifactExtensionRemovals();
+ assertFalse(artifactExtensionRemovals.isEmpty());
+ assertTrue(artifactExtensionRemovals.containsKey("content-packages"));
+ assertTrue(artifactExtensionRemovals.get("content-packages").contains(removedId));
+ }
+
+ @Test
+ public void checkTextExtensionUpdated() {
+ Extension previous = new Extension(TEXT, "repoinit", true);
+ previous.setText("create path /content/example.com(mixin mix:referenceable)");
+
+ Extension current = new Extension(TEXT, "repoinit", true);
+ current.setText("create path /content/example.com(mixin mix:referenceable)\ncreate path (nt:unstructured) /var");
+ comparator.computeDiff(previous, current, targetFeature);
+
+ assertTrue(targetFeature.getPrototype().getExtensionRemovals().contains(current.getName()));
+ assertEquals(current.getText(), targetFeature.getExtensions().getByName(current.getName()).getText());
+ }
+
+ @Test
+ public void checkJSONExtensionUpdated() {
+ Extension previous = new Extension(JSON, "api-regions", true);
+ previous.setJSON("{\"name\": \"global\"}");
+
+ Extension current = new Extension(JSON, "api-regions", true);
+ current.setJSON("{\"name\": \"deprecated\"}");
+ comparator.computeDiff(previous, current, targetFeature);
+
+ assertTrue(targetFeature.getPrototype().getExtensionRemovals().contains(current.getName()));
+ assertEquals(current.getJSON(), targetFeature.getExtensions().getByName(current.getName()).getJSON());
+ }
+
+ @Test
+ public void checkArtifactsExtensionUpdated() {
+ Extension previous = new Extension(ARTIFACTS, "content-packages", true);
+ ArtifactId removedId = ArtifactId.parse("org.apache.sling:org.apache.sling.diff:1.0.0");
+ previous.getArtifacts().add(new Artifact(removedId));
+
+ Extension current = new Extension(ARTIFACTS, "content-packages", true);
+ ArtifactId updatedId = ArtifactId.parse("org.apache.sling:org.apache.sling.diff:2.0.0");
+ current.getArtifacts().add(new Artifact(updatedId));
+
+ comparator.computeDiff(previous, current, targetFeature);
+
+ Map<String, List<ArtifactId>> artifactExtensionRemovals = targetFeature.getPrototype().getArtifactExtensionRemovals();
+ assertTrue(artifactExtensionRemovals.get("content-packages").contains(removedId));
+ assertTrue(targetFeature.getExtensions().getByName(current.getName()).getArtifacts().contains(current.getArtifacts().iterator().next()));
+ }
+
+}
diff --git a/src/test/java/org/apache/sling/feature/diff/impl/FrameworkPropertiesComparatorTest.java b/src/test/java/org/apache/sling/feature/diff/impl/FrameworkPropertiesComparatorTest.java
new file mode 100644
index 0000000..9c4122f
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/diff/impl/FrameworkPropertiesComparatorTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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.feature.diff.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.sling.feature.diff.impl.FrameworkPropertiesComparator;
+import org.junit.Test;
+
+public class FrameworkPropertiesComparatorTest extends AbstractComparatorTest<FrameworkPropertiesComparator> {
+
+ @Override
+ protected FrameworkPropertiesComparator newComparatorInstance() {
+ return new FrameworkPropertiesComparator();
+ }
+
+ @Test
+ public void checkRemoved() {
+ Map<String, String> previous = new HashMap<>();
+ previous.put("removed", "removed entry");
+
+ Map<String, String> current = new HashMap<>();
+
+ comparator.computeDiff(previous, current, targetFeature);
+
+ assertFalse(targetFeature.getPrototype().getFrameworkPropertiesRemovals().isEmpty());
+ assertFalse(targetFeature.getFrameworkProperties().containsKey("removed"));
+ }
+
+ @Test
+ public void checkAdded() {
+ Map<String, String> previous = new HashMap<>();
+
+ Map<String, String> current = new HashMap<>();
+ current.put("added", "added entry");
+
+ comparator.computeDiff(previous, current, targetFeature);
+
+ assertTrue(targetFeature.getPrototype().getFrameworkPropertiesRemovals().isEmpty());
+ assertFalse(targetFeature.getFrameworkProperties().isEmpty());
+ assertTrue(targetFeature.getFrameworkProperties().containsKey("added"));
+ }
+
+ @Test
+ public void checkUpdated() {
+ Map<String, String> previous = new HashMap<>();
+ previous.put("updated", "regular entry");
+
+ Map<String, String> current = new HashMap<>();
+ current.put("updated", "updated entry");
+
+ comparator.computeDiff(previous, current, targetFeature);
+
+ assertTrue(targetFeature.getPrototype().getFrameworkPropertiesRemovals().isEmpty());
+ assertFalse(targetFeature.getFrameworkProperties().isEmpty());
+ assertTrue(targetFeature.getFrameworkProperties().containsKey("updated"));
+ assertEquals("updated entry", targetFeature.getFrameworkProperties().get("updated"));
+ }
+
+}