This is an automated email from the ASF dual-hosted git repository. bdelacretaz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-adapter-annotations-it.git
commit 6d2b99e6acdd430e601e4dff318c9e7c636fee3c Author: Henry Kuijpers <[email protected]> AuthorDate: Sun Sep 20 23:38:10 2020 +0200 SLING-2938 - Extensive IT for Sling Adapter Annotations - testing generated service properties, adapter status page and the registration of the adapter factories --- .asf.yaml | 8 + .gitignore | 18 ++ CODE_OF_CONDUCT.md | 22 ++ CONTRIBUTING.md | 24 +++ Jenkinsfile | 20 ++ LICENSE | 202 ++++++++++++++++++ README.md | 9 + bnd.bnd | 1 + pom.xml | 234 +++++++++++++++++++++ .../adapters/AbstractNoOpAdapterFactory.java | 29 +++ .../testing/adapters/DeprecatedAdapterFactory.java | 30 +++ .../IntegerAndShortToLongAdapterFactory.java | 29 +++ .../adapters/InvalidEmptyAdapterFactory.java | 28 +++ .../InvalidNoAdaptablesAdapterFactory.java | 28 +++ .../adapters/InvalidNoAdaptersAdapterFactory.java | 28 +++ .../LongToIntegerIfFitsAdapterFactory.java | 32 +++ .../ShortToIntegerAndLongAdapterFactory.java | 26 +++ .../adapters/TextLengthIfFitsAdapterFactory.java | 34 +++ .../adapter/annotations/AdapterAnnotationsIT.java | 40 ++++ .../adapter/annotations/AdapterRegistrationIT.java | 123 +++++++++++ .../sling/adapter/annotations/AdapterStatusIT.java | 104 +++++++++ .../adapter/annotations/ServicePropertiesIT.java | 180 ++++++++++++++++ .../adapter/annotations/util/AppConstants.java | 26 +++ .../adapter/annotations/util/AppSlingClient.java | 48 +++++ .../sling/adapter/annotations/util/Util.java | 68 ++++++ src/test/provisioning/it-model.txt | 25 +++ 26 files changed, 1416 insertions(+) diff --git a/.asf.yaml b/.asf.yaml new file mode 100644 index 0000000..ae54e4b --- /dev/null +++ b/.asf.yaml @@ -0,0 +1,8 @@ +github: + description: "Apache Sling Adapter Annotations Integration Tests" + homepage: "https://sling.apache.org/" + labels: + - sling + - java + - annotations + - adapters \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..974f3bf --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +/target/ +/.project +/.settings/ +/.classpath +.idea +.metadata +.externalToolBuilders +maven-eclipse.xml +*.swp +*.iml +*.ipr +*.iws +*.bak +.vlt +.DS_Store +jcr.log +atlassian-ide-plugin.xml +.vscode/ \ No newline at end of file diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..4c8ab2c --- /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). \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..1003c04 --- /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. \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..b20ef4f --- /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() \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7a4a3ea --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + 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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..aa17a01 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +[](https://sling.apache.org) + + [](https://ci-builds.apache.org/job/Sling/job/modules/job/sling-org-apache-sling-adapter-annotations-it/job/master/) [](https://ci-builds.apache.org/job/Sling/job/modul [...] + +# Apache Sling Adapter Annotations IT + +This module is part of the [Apache Sling](https://sling.apache.org) project. + +Integration tests for OSGi DS 1.4 [component property type annotations](https://github.com/apache/sling-org-apache-sling-adapter-annotations) for Sling Adapters \ No newline at end of file diff --git a/bnd.bnd b/bnd.bnd new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/bnd.bnd @@ -0,0 +1 @@ + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..218fd8d --- /dev/null +++ b/pom.xml @@ -0,0 +1,234 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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</artifactId> + <version>34</version> + <relativePath/> + </parent> + + <artifactId>org.apache.sling.adapter.annotations.it</artifactId> + <version>1.0.0-SNAPSHOT</version> + + <name>Apache Sling Adapter Annotations IT</name> + <description> + Integration tests for OSGi DS 1.4 component property type annotations for Sling adapters + </description> + + <scm> + <connection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-adapter-annotations-it.git</connection> + <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-adapter-annotations-it.git</developerConnection> + <url>https://gitbox.apache.org/repos/asf?p=sling-org-apache-sling-adapter-annotations-it.git</url> + <tag>HEAD</tag> + </scm> + + <properties> + <sling.java.version>8</sling.java.version> + <http.host>localhost</http.host> + <!-- start with -DkeepITServerRunning=true to allow to rerun ITs or inspect the server after the ITs have been executed there --> + <keepITServerRunning>false</keepITServerRunning> + </properties> + + <build> + <plugins> + <plugin> + <groupId>biz.aQute.bnd</groupId> + <artifactId>bnd-maven-plugin</artifactId> + <version>5.0.0</version> + </plugin> + <plugin> + <!-- Find free ports to run our server --> + <groupId>org.codehaus.mojo</groupId> + <artifactId>build-helper-maven-plugin</artifactId> + <executions> + <execution> + <id>reserve-server-port</id> + <goals> + <goal>reserve-network-port</goal> + </goals> + <phase>pre-integration-test</phase> + <configuration> + <portNames> + <!-- used port name must be stored in property because it must be used for the base url --> + <portName>http.port</portName> + </portNames> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <!-- the Sling instance is provisioned from the model in src/main/provisioning/it-model.txt --> + <groupId>org.apache.sling</groupId> + <artifactId>slingstart-maven-plugin</artifactId> + <!-- for https://issues.apache.org/jira/browse/SLING-7662 --> + <version>1.8.2</version> + <extensions>true</extensions> + <executions> + <execution> + <id>customise-starter</id> + <goals> + <goal>prepare-package</goal> + </goals> + <configuration> + <modelDirectory>${project.basedir}/src/test/provisioning</modelDirectory> + <usePomDependencies>true</usePomDependencies> + </configuration> + </execution> + <execution> + <id>package-starter</id> + <goals> + <goal>package</goal> + </goals> + <configuration> + <attachArtifact>false</attachArtifact> + <modelDirectory>${project.basedir}/src/test/provisioning</modelDirectory> + <usePomDependencies>true</usePomDependencies> + </configuration> + </execution> + <execution> + <id>start-container-before-IT</id> + <goals> + <goal>start</goal> + </goals> + </execution> + <execution> + <id>stop-container-after-IT</id> + <goals> + <goal>stop</goal> + </goals> + <configuration> + <shouldBlockUntilKeyIsPressed>${keepITServerRunning}</shouldBlockUntilKeyIsPressed> + </configuration> + </execution> + </executions> + <configuration> + <servers> + <!-- this configuration applies to both 'start' and 'stop' --> + <server> + <id>singleinstance</id> + <port>${http.port}</port> + <vmOpts>${sling.vm.options}</vmOpts> + <stdOutFile>sling/logs/stdout.log</stdOutFile> + </server> + </servers> + <!-- this configuration only applies to 'prepare-package' and 'package' --> + <disableExtendingMavenClasspath>true</disableExtendingMavenClasspath> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-failsafe-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>integration-test</goal> + <goal>verify</goal> + </goals> + </execution> + </executions> + <configuration> + <systemPropertyVariables> + <baseUrl>http://${http.host}:${http.port}/</baseUrl> + <bundleFile>${project.build.directory}/${project.build.finalName}.jar</bundleFile> + <bundleSymbolicName>${project.artifactId}</bundleSymbolicName> + </systemPropertyVariables> + </configuration> + </plugin> + </plugins> + </build> + + <dependencies> + <!-- dependency under test --> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.adapter.annotations</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.service.component.annotations</artifactId> + <version>1.3.0</version><!-- to be compliant with DS 1.3 --> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.api</artifactId> + <version>2.20.0</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.adapter</artifactId> + <version>2.1.10</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>javax.servlet-api</artifactId> + <scope>provided</scope> + </dependency> + + <!-- testing dependencies --> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.servlets.resolver</artifactId> + <version>2.5.2</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.testing.clients</artifactId> + <version>2.0.3-SNAPSHOT</version><!-- Because of https://issues.apache.org/jira/browse/SLING-9750 --> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.testing.rules</artifactId> + <version>2.0.0</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>osgi.core</artifactId> + <scope>test</scope> + </dependency> + </dependencies> +</project> \ No newline at end of file diff --git a/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/AbstractNoOpAdapterFactory.java b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/AbstractNoOpAdapterFactory.java new file mode 100644 index 0000000..9c82cdd --- /dev/null +++ b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/AbstractNoOpAdapterFactory.java @@ -0,0 +1,29 @@ +/* + * 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.adapter.annotations.testing.adapters; + +import org.apache.sling.api.adapter.AdapterFactory; + +public abstract class AbstractNoOpAdapterFactory implements AdapterFactory { + public static final String INVALID_CONFIGURATION_MESSAGE = "Configuration is invalid"; + + @Override + @SuppressWarnings("squid:S00119") + public <AdapterType> AdapterType getAdapter(final Object adaptable, final Class<AdapterType> type) { + return null; + } +} diff --git a/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/DeprecatedAdapterFactory.java b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/DeprecatedAdapterFactory.java new file mode 100644 index 0000000..1da5b5c --- /dev/null +++ b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/DeprecatedAdapterFactory.java @@ -0,0 +1,30 @@ +/* + * 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.adapter.annotations.testing.adapters; + +import org.apache.sling.adapter.annotations.AdapterDeprecated; +import org.apache.sling.adapter.annotations.SlingAdapter; +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.adapter.AdapterFactory; +import org.apache.sling.api.resource.Resource; +import org.osgi.service.component.annotations.Component; + +@Component(service = AdapterFactory.class) +@SlingAdapter(adaptables = SlingHttpServletRequest.class, adapters = Resource.class) +@AdapterDeprecated // Just use request.getResource() +public class DeprecatedAdapterFactory extends AbstractNoOpAdapterFactory { +} diff --git a/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/IntegerAndShortToLongAdapterFactory.java b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/IntegerAndShortToLongAdapterFactory.java new file mode 100644 index 0000000..b848b53 --- /dev/null +++ b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/IntegerAndShortToLongAdapterFactory.java @@ -0,0 +1,29 @@ +/* + * 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.adapter.annotations.testing.adapters; + +import org.apache.sling.adapter.annotations.SlingAdapter; +import org.apache.sling.api.adapter.AdapterFactory; +import org.osgi.service.component.annotations.Component; + +@Component(service = AdapterFactory.class) +@SlingAdapter( + adaptables = {Integer.class, Short.class}, + adapters = Long.class +) +public class IntegerAndShortToLongAdapterFactory extends AbstractNoOpAdapterFactory { +} diff --git a/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/InvalidEmptyAdapterFactory.java b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/InvalidEmptyAdapterFactory.java new file mode 100644 index 0000000..3135e71 --- /dev/null +++ b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/InvalidEmptyAdapterFactory.java @@ -0,0 +1,28 @@ +/* + * 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.adapter.annotations.testing.adapters; + +import org.apache.sling.adapter.annotations.AdapterCondition; +import org.apache.sling.adapter.annotations.SlingAdapter; +import org.apache.sling.api.adapter.AdapterFactory; +import org.osgi.service.component.annotations.Component; + +@Component(service = AdapterFactory.class) +@SlingAdapter(adaptables = {}, adapters = {}) +@AdapterCondition(AbstractNoOpAdapterFactory.INVALID_CONFIGURATION_MESSAGE) +public class InvalidEmptyAdapterFactory extends AbstractNoOpAdapterFactory { +} diff --git a/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/InvalidNoAdaptablesAdapterFactory.java b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/InvalidNoAdaptablesAdapterFactory.java new file mode 100644 index 0000000..0c98d90 --- /dev/null +++ b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/InvalidNoAdaptablesAdapterFactory.java @@ -0,0 +1,28 @@ +/* + * 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.adapter.annotations.testing.adapters; + +import org.apache.sling.adapter.annotations.AdapterCondition; +import org.apache.sling.adapter.annotations.SlingAdapter; +import org.apache.sling.api.adapter.AdapterFactory; +import org.osgi.service.component.annotations.Component; + +@Component(service = AdapterFactory.class) +@SlingAdapter(adaptables = {}, adapters = Void.class) +@AdapterCondition(AbstractNoOpAdapterFactory.INVALID_CONFIGURATION_MESSAGE) +public class InvalidNoAdaptablesAdapterFactory extends AbstractNoOpAdapterFactory { +} diff --git a/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/InvalidNoAdaptersAdapterFactory.java b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/InvalidNoAdaptersAdapterFactory.java new file mode 100644 index 0000000..d7cb8ca --- /dev/null +++ b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/InvalidNoAdaptersAdapterFactory.java @@ -0,0 +1,28 @@ +/* + * 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.adapter.annotations.testing.adapters; + +import org.apache.sling.adapter.annotations.AdapterCondition; +import org.apache.sling.adapter.annotations.SlingAdapter; +import org.apache.sling.api.adapter.AdapterFactory; +import org.osgi.service.component.annotations.Component; + +@Component(service = AdapterFactory.class) +@SlingAdapter(adaptables = Void.class, adapters = {}) +@AdapterCondition(AbstractNoOpAdapterFactory.INVALID_CONFIGURATION_MESSAGE) +public class InvalidNoAdaptersAdapterFactory extends AbstractNoOpAdapterFactory { +} diff --git a/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/LongToIntegerIfFitsAdapterFactory.java b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/LongToIntegerIfFitsAdapterFactory.java new file mode 100644 index 0000000..f134243 --- /dev/null +++ b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/LongToIntegerIfFitsAdapterFactory.java @@ -0,0 +1,32 @@ +/* + * 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.adapter.annotations.testing.adapters; + +import org.apache.sling.adapter.annotations.AdapterCondition; +import org.apache.sling.adapter.annotations.SlingAdapter; +import org.apache.sling.api.adapter.AdapterFactory; +import org.osgi.service.component.annotations.Component; + +@Component(service = AdapterFactory.class) +@SlingAdapter( + adaptables = Long.class, + adapters = Integer.class +) +@AdapterCondition(LongToIntegerIfFitsAdapterFactory.CONDITION) +public class LongToIntegerIfFitsAdapterFactory extends AbstractNoOpAdapterFactory { + public static final String CONDITION = "If the value is small enough to fit in an integer."; +} diff --git a/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/ShortToIntegerAndLongAdapterFactory.java b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/ShortToIntegerAndLongAdapterFactory.java new file mode 100644 index 0000000..74d77e1 --- /dev/null +++ b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/ShortToIntegerAndLongAdapterFactory.java @@ -0,0 +1,26 @@ +/* + * 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.adapter.annotations.testing.adapters; + +import org.apache.sling.adapter.annotations.SlingAdapter; +import org.apache.sling.api.adapter.AdapterFactory; +import org.osgi.service.component.annotations.Component; + +@Component(service = AdapterFactory.class) +@SlingAdapter(adaptables = Short.class, adapters = {Integer.class, Long.class}) +public class ShortToIntegerAndLongAdapterFactory extends AbstractNoOpAdapterFactory { +} diff --git a/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/TextLengthIfFitsAdapterFactory.java b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/TextLengthIfFitsAdapterFactory.java new file mode 100644 index 0000000..0af1e61 --- /dev/null +++ b/src/main/java/org/apache/sling/adapter/annotations/testing/adapters/TextLengthIfFitsAdapterFactory.java @@ -0,0 +1,34 @@ +/* + * 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.adapter.annotations.testing.adapters; + +import org.apache.sling.adapter.annotations.AdapterCondition; +import org.apache.sling.adapter.annotations.SlingAdapter; +import org.apache.sling.api.adapter.AdapterFactory; +import org.osgi.service.component.annotations.Component; + +import java.math.BigInteger; + +@Component(service = AdapterFactory.class) +@SlingAdapter( + adaptables = {CharSequence.class, String.class}, + adapters = {Short.class, Integer.class, Long.class, BigInteger.class} +) +@AdapterCondition(TextLengthIfFitsAdapterFactory.CONDITION) +public class TextLengthIfFitsAdapterFactory extends AbstractNoOpAdapterFactory { + public static final String CONDITION = "If the text length fits in the requested type."; +} diff --git a/src/test/java/org/apache/sling/adapter/annotations/AdapterAnnotationsIT.java b/src/test/java/org/apache/sling/adapter/annotations/AdapterAnnotationsIT.java new file mode 100644 index 0000000..a24da5b --- /dev/null +++ b/src/test/java/org/apache/sling/adapter/annotations/AdapterAnnotationsIT.java @@ -0,0 +1,40 @@ +/* + * 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.adapter.annotations; + +import org.apache.sling.testing.clients.ClientException; +import org.junit.Test; + +public interface AdapterAnnotationsIT { + @Test + void testLongToIntegerIfFitsAdapterFactory() throws ClientException; + + @Test + void testTextLengthIfFitsAdapterFactory() throws ClientException; + + @Test + void testShortToIntegerAndLongAdapterFactory() throws ClientException; + + @Test + void testIntegerAndShortToLongAdapterFactory() throws ClientException; + + @Test + void testDeprecatedAdapterFactory() throws ClientException; + + @Test + void testInvalidAdapterFactories() throws ClientException; +} diff --git a/src/test/java/org/apache/sling/adapter/annotations/AdapterRegistrationIT.java b/src/test/java/org/apache/sling/adapter/annotations/AdapterRegistrationIT.java new file mode 100644 index 0000000..9f8a9fa --- /dev/null +++ b/src/test/java/org/apache/sling/adapter/annotations/AdapterRegistrationIT.java @@ -0,0 +1,123 @@ +/* + * 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.adapter.annotations; + +import com.google.common.collect.ImmutableMap; +import org.apache.sling.adapter.Adaption; +import org.apache.sling.adapter.annotations.util.AppSlingClient; +import org.apache.sling.adapter.annotations.util.Util; +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.adapter.AdapterFactory; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.testing.clients.ClientException; +import org.apache.sling.testing.clients.osgi.OsgiConsoleClient; +import org.apache.sling.testing.clients.osgi.ServiceInfo; +import org.apache.sling.testing.clients.osgi.ServicesInfo; +import org.apache.sling.testing.clients.util.JsonUtils; +import org.codehaus.jackson.JsonNode; +import org.junit.BeforeClass; +import org.junit.Test; +import org.osgi.framework.Constants; + +import java.io.IOException; +import java.math.BigInteger; +import java.net.URISyntaxException; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeoutException; +import java.util.function.UnaryOperator; + +import static org.hamcrest.CoreMatchers.hasItem; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; + +public class AdapterRegistrationIT implements AdapterAnnotationsIT { + private static Set<Map<String, Object>> registeredAdaptions; + + @BeforeClass + public static void setUpAdaptions() throws ClientException, InterruptedException, TimeoutException, URISyntaxException, IOException { + try (final OsgiConsoleClient client = AppSlingClient.newSlingClient().adaptTo(OsgiConsoleClient.class)) { + registeredAdaptions = new HashSet<>(); + final ServicesInfo services = new ServicesInfo(JsonUtils.getJsonNodeFromString( + client.doGet("/system/console/services.json").getContent())); + for (final ServiceInfo serviceInfo : services.forType(Adaption.class.getName())) { + final JsonNode serviceJson = JsonUtils.getJsonNodeFromString( + client.doGet("/system/console/services/" + serviceInfo.getId() + ".json").getContent()); + registeredAdaptions.add(Util.getNonDynamicPropertiesForService(serviceJson)); + } + } + } + + @Override + @Test + public void testLongToIntegerIfFitsAdapterFactory() { + assertAdaption(properties -> properties + .put(AdapterFactory.ADAPTABLE_CLASSES, Collections.singletonList(Long.class.getName())) + .put(AdapterFactory.ADAPTER_CLASSES, Collections.singletonList(Integer.class.getName()))); + } + + @Override + @Test + public void testTextLengthIfFitsAdapterFactory() { + assertAdaption(properties -> properties + .put(AdapterFactory.ADAPTABLE_CLASSES, Arrays.asList(CharSequence.class.getName(), String.class.getName())) + .put(AdapterFactory.ADAPTER_CLASSES, Arrays.asList( + Short.class.getName(), + Integer.class.getName(), + Long.class.getName(), + BigInteger.class.getName()))); + } + + @Override + @Test + public void testShortToIntegerAndLongAdapterFactory() { + assertAdaption(properties -> properties + .put(AdapterFactory.ADAPTABLE_CLASSES, Collections.singletonList(Short.class.getName())) + .put(AdapterFactory.ADAPTER_CLASSES, Arrays.asList(Integer.class.getName(), Long.class.getName()))); + } + + @Override + @Test + public void testIntegerAndShortToLongAdapterFactory() { + assertAdaption(properties -> properties + .put(AdapterFactory.ADAPTABLE_CLASSES, Arrays.asList(Integer.class.getName(), Short.class.getName())) + .put(AdapterFactory.ADAPTER_CLASSES, Collections.singletonList(Long.class.getName()))); + } + + @Override + @Test + public void testDeprecatedAdapterFactory() { + assertAdaption(properties -> properties + .put(AdapterFactory.ADAPTABLE_CLASSES, Collections.singletonList(SlingHttpServletRequest.class.getName())) + .put(AdapterFactory.ADAPTER_CLASSES, Collections.singletonList(Resource.class.getName()))); + } + + @Override + @Test + public void testInvalidAdapterFactories() { + assertFalse(registeredAdaptions.stream().anyMatch(properties -> properties.containsValue(Collections.singletonList(Void.class.getName())))); + } + + private static void assertAdaption(final UnaryOperator<ImmutableMap.Builder<String, Object>> properties) { + assertThat(registeredAdaptions, hasItem(properties.apply(ImmutableMap.<String, Object>builder() + .put(Constants.SERVICE_SCOPE, Constants.SCOPE_SINGLETON) + ).build())); + } +} diff --git a/src/test/java/org/apache/sling/adapter/annotations/AdapterStatusIT.java b/src/test/java/org/apache/sling/adapter/annotations/AdapterStatusIT.java new file mode 100644 index 0000000..4760138 --- /dev/null +++ b/src/test/java/org/apache/sling/adapter/annotations/AdapterStatusIT.java @@ -0,0 +1,104 @@ +/* + * 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.adapter.annotations; + +import org.apache.commons.lang3.StringUtils; +import org.apache.sling.adapter.annotations.testing.adapters.AbstractNoOpAdapterFactory; +import org.apache.sling.adapter.annotations.testing.adapters.LongToIntegerIfFitsAdapterFactory; +import org.apache.sling.adapter.annotations.testing.adapters.TextLengthIfFitsAdapterFactory; +import org.apache.sling.adapter.annotations.util.AppConstants; +import org.apache.sling.adapter.annotations.util.AppSlingClient; +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.testing.clients.ClientException; +import org.apache.sling.testing.clients.osgi.OsgiConsoleClient; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.IOException; +import java.math.BigInteger; +import java.net.URISyntaxException; +import java.util.concurrent.TimeoutException; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertThat; + +public class AdapterStatusIT implements AdapterAnnotationsIT { + private static String adaptersStatus; + + @BeforeClass + public static void setUpAdaptersStatus() throws ClientException, InterruptedException, TimeoutException, URISyntaxException, IOException { + try (final OsgiConsoleClient client = AppSlingClient.newSlingClient().adaptTo(OsgiConsoleClient.class)) { + adaptersStatus = client.doGet("/system/console/status-adapters.txt").getContent(); + adaptersStatus = StringUtils.replace(adaptersStatus, "\r", StringUtils.EMPTY); // Prevent platform-specific issues + } + } + + @Override + @Test + public void testLongToIntegerIfFitsAdapterFactory() { + assertAdapterDescriptor(Long.class, LongToIntegerIfFitsAdapterFactory.CONDITION, Integer.class); + } + + @Override + @Test + public void testShortToIntegerAndLongAdapterFactory() { + assertAdapterDescriptor(Short.class, null, Integer.class, Long.class); + } + + @Override + @Test + public void testIntegerAndShortToLongAdapterFactory() { + assertAdapterDescriptor(Integer.class, null, Long.class); + assertAdapterDescriptor(Short.class, null, Long.class); + } + + @Override + @Test + public void testTextLengthIfFitsAdapterFactory() { + assertAdapterDescriptor(CharSequence.class, TextLengthIfFitsAdapterFactory.CONDITION, Short.class, Integer.class, Long.class, BigInteger.class); + assertAdapterDescriptor(String.class, TextLengthIfFitsAdapterFactory.CONDITION, Short.class, Integer.class, Long.class, BigInteger.class); + } + + @Override + @Test + public void testDeprecatedAdapterFactory() { + // Deprecated status is not dumped on the status page + assertAdapterDescriptor(SlingHttpServletRequest.class, null, Resource.class); + } + + @Override + @Test + public void testInvalidAdapterFactories() { + assertThat(adaptersStatus, not(containsString("\nCondition: " + AbstractNoOpAdapterFactory.INVALID_CONFIGURATION_MESSAGE + "\n"))); + } + + private static void assertAdapterDescriptor(final Class<?> adaptable, final String condition, final Class<?>... adapters) { + final StringBuilder descriptor = new StringBuilder("\nAdaptable: ").append(adaptable.getName()).append("\n"); + if (condition != null) { + descriptor.append("Condition: ").append(condition).append("\n"); + } + descriptor.append("Providing Bundle: ").append(AppConstants.BUNDLE_SYMBOLIC_NAME).append("\n"); + descriptor.append("Available Adapters:\n"); + for (final Class<?> adapter : adapters) { + descriptor.append(" * ").append(adapter.getName()).append("\n"); + } + descriptor.append("\n"); + assertThat(adaptersStatus, containsString(descriptor.toString())); + } +} diff --git a/src/test/java/org/apache/sling/adapter/annotations/ServicePropertiesIT.java b/src/test/java/org/apache/sling/adapter/annotations/ServicePropertiesIT.java new file mode 100644 index 0000000..2d896d4 --- /dev/null +++ b/src/test/java/org/apache/sling/adapter/annotations/ServicePropertiesIT.java @@ -0,0 +1,180 @@ +/* + * 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.adapter.annotations; + +import com.google.common.collect.ImmutableMap; +import org.apache.sling.adapter.annotations.testing.adapters.AbstractNoOpAdapterFactory; +import org.apache.sling.adapter.annotations.testing.adapters.DeprecatedAdapterFactory; +import org.apache.sling.adapter.annotations.testing.adapters.InvalidNoAdaptablesAdapterFactory; +import org.apache.sling.adapter.annotations.testing.adapters.InvalidEmptyAdapterFactory; +import org.apache.sling.adapter.annotations.testing.adapters.InvalidNoAdaptersAdapterFactory; +import org.apache.sling.adapter.annotations.testing.adapters.ShortToIntegerAndLongAdapterFactory; +import org.apache.sling.adapter.annotations.testing.adapters.LongToIntegerIfFitsAdapterFactory; +import org.apache.sling.adapter.annotations.testing.adapters.IntegerAndShortToLongAdapterFactory; +import org.apache.sling.adapter.annotations.testing.adapters.TextLengthIfFitsAdapterFactory; +import org.apache.sling.adapter.annotations.util.AppSlingClient; +import org.apache.sling.adapter.annotations.util.Util; +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.adapter.AdapterFactory; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.testing.clients.ClientException; +import org.apache.sling.testing.clients.osgi.OsgiConsoleClient; +import org.apache.sling.testing.clients.util.JsonUtils; +import org.codehaus.jackson.JsonNode; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.osgi.framework.Constants; +import org.osgi.service.component.ComponentConstants; + +import java.io.IOException; +import java.math.BigInteger; +import java.net.URISyntaxException; +import java.util.Arrays; +import java.util.Collections; +import java.util.Map; +import java.util.concurrent.TimeoutException; +import java.util.function.UnaryOperator; + +import static org.junit.Assert.assertEquals; + +public class ServicePropertiesIT implements AdapterAnnotationsIT { + private static final String ADAPTER_CONDITION = "adapter.condition"; + private static final String ADAPTER_DEPRECATED = "adapter.deprecated"; + + private static OsgiConsoleClient client; + + @BeforeClass + public static void setUpOnce() throws InterruptedException, TimeoutException, ClientException, URISyntaxException { + client = AppSlingClient.newSlingClient().adaptTo(OsgiConsoleClient.class); + } + + @AfterClass + public static void tearDown() throws IOException { + if (client != null) { + client.close(); + } + } + + @Override + @Test + public void testLongToIntegerIfFitsAdapterFactory() throws ClientException { + assertProperties(LongToIntegerIfFitsAdapterFactory.class.getName(), properties -> properties + .put(AdapterFactory.ADAPTABLE_CLASSES, Collections.singletonList(Long.class.getName())) + .put(AdapterFactory.ADAPTER_CLASSES, Collections.singletonList(Integer.class.getName())) + .put(ADAPTER_CONDITION, LongToIntegerIfFitsAdapterFactory.CONDITION)); + } + + @Override + @Test + public void testTextLengthIfFitsAdapterFactory() throws ClientException { + assertProperties(TextLengthIfFitsAdapterFactory.class.getName(), properties -> properties + .put(AdapterFactory.ADAPTABLE_CLASSES, Arrays.asList(CharSequence.class.getName(), String.class.getName())) + .put(AdapterFactory.ADAPTER_CLASSES, Arrays.asList( + Short.class.getName(), + Integer.class.getName(), + Long.class.getName(), + BigInteger.class.getName() + )) + .put(ADAPTER_CONDITION, TextLengthIfFitsAdapterFactory.CONDITION)); + } + + @Override + @Test + public void testShortToIntegerAndLongAdapterFactory() throws ClientException { + assertProperties(ShortToIntegerAndLongAdapterFactory.class.getName(), properties -> properties + .put(AdapterFactory.ADAPTABLE_CLASSES, Collections.singletonList(Short.class.getName())) + .put(AdapterFactory.ADAPTER_CLASSES, Arrays.asList(Integer.class.getName(), Long.class.getName()))); + } + + @Override + @Test + public void testIntegerAndShortToLongAdapterFactory() throws ClientException { + assertProperties(IntegerAndShortToLongAdapterFactory.class.getName(), properties -> properties + .put(AdapterFactory.ADAPTABLE_CLASSES, Arrays.asList(Integer.class.getName(), Short.class.getName())) + .put(AdapterFactory.ADAPTER_CLASSES, Collections.singletonList(Long.class.getName()))); + } + + @Test + public void testInvalidMissingAdaptablesAndAdaptersAdapter() throws ClientException { + assertProperties(InvalidEmptyAdapterFactory.class.getName(), properties -> properties + .put(ADAPTER_CONDITION, AbstractNoOpAdapterFactory.INVALID_CONFIGURATION_MESSAGE)); + } + + @Test + public void testInvalidMissingAdaptablesAdapter() throws ClientException { + assertProperties(InvalidNoAdaptablesAdapterFactory.class.getName(), properties -> properties + .put(ADAPTER_CONDITION, AbstractNoOpAdapterFactory.INVALID_CONFIGURATION_MESSAGE) + .put(AdapterFactory.ADAPTER_CLASSES, Collections.singletonList(Void.class.getName()))); + } + + @Test + public void testInvalidMissingAdaptersAdapter() throws ClientException { + assertProperties(InvalidNoAdaptersAdapterFactory.class.getName(), properties -> properties + .put(ADAPTER_CONDITION, AbstractNoOpAdapterFactory.INVALID_CONFIGURATION_MESSAGE) + .put(AdapterFactory.ADAPTABLE_CLASSES, Collections.singletonList(Void.class.getName()))); + } + + @Override + @Test + public void testDeprecatedAdapterFactory() throws ClientException { + assertProperties(DeprecatedAdapterFactory.class.getName(), properties -> properties + .put(ADAPTER_DEPRECATED, true) + .put(AdapterFactory.ADAPTABLE_CLASSES, Collections.singletonList(SlingHttpServletRequest.class.getName())) + .put(AdapterFactory.ADAPTER_CLASSES, Collections.singletonList(Resource.class.getName()))); + } + + @Override + @Test + public void testInvalidAdapterFactories() throws ClientException { + assertProperties(InvalidEmptyAdapterFactory.class.getName(), properties -> properties + .put(ADAPTER_CONDITION, AbstractNoOpAdapterFactory.INVALID_CONFIGURATION_MESSAGE)); + assertProperties(InvalidNoAdaptablesAdapterFactory.class.getName(), properties -> properties + .put(ADAPTER_CONDITION, AbstractNoOpAdapterFactory.INVALID_CONFIGURATION_MESSAGE) + .put(AdapterFactory.ADAPTER_CLASSES, Collections.singletonList(Void.class.getName()))); + assertProperties(InvalidNoAdaptersAdapterFactory.class.getName(), properties -> properties + .put(ADAPTER_CONDITION, AbstractNoOpAdapterFactory.INVALID_CONFIGURATION_MESSAGE) + .put(AdapterFactory.ADAPTABLE_CLASSES, Collections.singletonList(Void.class.getName()))); + } + + private static void assertProperties(final String componentName, + final UnaryOperator<ImmutableMap.Builder<String, Object>> properties) throws ClientException { + final Map<String, Object> expected = properties.apply(ImmutableMap.<String, Object>builder() + .put(ComponentConstants.COMPONENT_NAME, componentName) + .put(Constants.SERVICE_SCOPE, Constants.SCOPE_BUNDLE)) + .build(); + assertEquals(expected, getNonDynamicPropertiesOfComponentService(componentName)); + } + + private static Map<String, Object> getNonDynamicPropertiesOfComponentService(final String nameOrId) throws ClientException { + final JsonNode componentJson = JsonUtils.getJsonNodeFromString( + client.doGet("/system/console/components/" + nameOrId + ".json").getContent()); + final JsonNode serviceJson = JsonUtils.getJsonNodeFromString( + client.doGet("/system/console/services/" + getServiceIdFromComponentJson(componentJson) + ".json").getContent()); + return Util.getNonDynamicPropertiesForService(serviceJson); + } + + private static int getServiceIdFromComponentJson(final JsonNode componentJson) { + final JsonNode props = componentJson.get("data").get(0).get("props"); + for (final JsonNode prop : props) { + if ("serviceId".equals(prop.get("key").getValueAsText())) { + return Integer.parseInt(prop.get("value").getValueAsText()); + } + } + throw new AssertionError("No service ID found"); + } +} diff --git a/src/test/java/org/apache/sling/adapter/annotations/util/AppConstants.java b/src/test/java/org/apache/sling/adapter/annotations/util/AppConstants.java new file mode 100644 index 0000000..9e987f6 --- /dev/null +++ b/src/test/java/org/apache/sling/adapter/annotations/util/AppConstants.java @@ -0,0 +1,26 @@ +/* + * 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.adapter.annotations.util; + +public class AppConstants { + public AppConstants() { + // Constants class + } + + public static final String BUNDLE_SYMBOLIC_NAME = System.getProperty("bundleSymbolicName"); + public static final String BUNDLE_FILE = System.getProperty("bundleFile"); +} diff --git a/src/test/java/org/apache/sling/adapter/annotations/util/AppSlingClient.java b/src/test/java/org/apache/sling/adapter/annotations/util/AppSlingClient.java new file mode 100644 index 0000000..63c058e --- /dev/null +++ b/src/test/java/org/apache/sling/adapter/annotations/util/AppSlingClient.java @@ -0,0 +1,48 @@ +/* + * 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.adapter.annotations.util; + +import org.apache.sling.testing.clients.ClientException; +import org.apache.sling.testing.clients.SlingClient; +import org.apache.sling.testing.clients.osgi.OsgiConsoleClient; +import org.apache.sling.testing.clients.util.poller.Polling; + +import java.io.File; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.concurrent.TimeoutException; + +public class AppSlingClient { + private static boolean bundleInstalledAndStarted; + + @SuppressWarnings("squid:S2095") // Caller will close the client + public static SlingClient newSlingClient() throws URISyntaxException, ClientException, TimeoutException, InterruptedException { + final SlingClient client = new SlingClient(new URI(System.getProperty("baseUrl")), "admin", "admin"); + + // client.waitExists() adds ".json" to the path, which is not desired, since that requests the Sling Default GET Servlet instead of Sling Starter HTML + new Polling(() -> client.doGet("/starter/index.html").getStatusLine().getStatusCode() == 200) + .poll(60_000, 500); + + if (!bundleInstalledAndStarted) { + final OsgiConsoleClient osgiConsoleClient = client.adaptTo(OsgiConsoleClient.class); + osgiConsoleClient.waitInstallBundle(new File(AppConstants.BUNDLE_FILE), true, -1, 10_000, 500); + osgiConsoleClient.waitBundleStarted(AppConstants.BUNDLE_SYMBOLIC_NAME, 10_000, 500); + bundleInstalledAndStarted = true; + } + return client; + } +} diff --git a/src/test/java/org/apache/sling/adapter/annotations/util/Util.java b/src/test/java/org/apache/sling/adapter/annotations/util/Util.java new file mode 100644 index 0000000..960a2af --- /dev/null +++ b/src/test/java/org/apache/sling/adapter/annotations/util/Util.java @@ -0,0 +1,68 @@ +/* + * 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.adapter.annotations.util; + +import org.codehaus.jackson.JsonNode; +import org.osgi.framework.Constants; +import org.osgi.service.component.ComponentConstants; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class Util { + private static final Set<String> DYNAMIC_PROPERTIES = new HashSet<>(Arrays.asList( + ComponentConstants.COMPONENT_ID, + Constants.SERVICE_BUNDLEID + )); + + public static Map<String, Object> getNonDynamicPropertiesForService(final JsonNode json) { + final JsonNode props = json.get("data").get(0).get("props"); + final Map<String, Object> properties = new LinkedHashMap<>(); + for (final JsonNode prop : props) { + final String name = prop.get("key").getTextValue(); + if (!DYNAMIC_PROPERTIES.contains(name)) { + properties.put(name, getPropertyValue(prop.get("value"))); + } + } + return properties; + } + + private static Object getPropertyValue(final JsonNode value) { + if (value.isBoolean()) { + return value.getBooleanValue(); + } + if (value.isNumber()) { + return value.getNumberValue(); + } + if (value.isTextual()) { + return value.getTextValue(); + } + if (value.isArray()) { + final List<String> items = new ArrayList<>(); + for (final JsonNode item : value) { + items.add(item.getTextValue()); + } + return items; + } + return null; + } +} diff --git a/src/test/provisioning/it-model.txt b/src/test/provisioning/it-model.txt new file mode 100644 index 0000000..5d3780e --- /dev/null +++ b/src/test/provisioning/it-model.txt @@ -0,0 +1,25 @@ +# +# 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. +# +[feature name=adapter-annotations-it] + +# Dependencies +[artifacts] + org.apache.sling/org.apache.sling.starter/11/slingstart + org.apache.sling/org.apache.sling.api + org.apache.sling/org.apache.sling.servlets.resolver \ No newline at end of file
