diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 0000000..e47338e --- /dev/null +++ b/.github/workflows/maven.yml @@ -0,0 +1,41 @@ +# +# Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) +# +# 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. +# +# Modifications copyright (C) 2020 Frank Jakop +# + +name: Java CI + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + java: [ 11 ] + name: Test with Java ${{ matrix.java }} + steps: + - uses: actions/checkout@v1 + - name: Set up Java + uses: actions/setup-java@v2.3.0 + with: + java-version: ${{ matrix.java }} + distribution: adopt + - name: Build with Maven + run: mvn -B package sonar:sonar -Pcoverage-per-test --file pom.xml + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/.gitignore b/.gitignore index 8054231..92cef4c 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ # IDEA files /annotation-validator.iml +.idea/ diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 37d7680..0000000 --- a/.travis.yml +++ /dev/null @@ -1,21 +0,0 @@ -language: java -sudo: false -install: true - -addons: - sonarqube: - organization: "arxes-tolina" - token: - secure: "Ja+klUuZ2M0ODpfbnHKt3QBLmz6UwWDmDgVMFn8NVNhYrg5tydU0NTYfTaIAlysXt5jLgQHy78jOZhcHlWrS6sEy4tRZKvkFce/BxND9/ulO5+9tqy4vYKbcY90HrT/BAdMJyXZ+DoZKT9DvCNN7jxtOrvK9OaNIiWm+goeaevRtkALCUqkGwM/hCdbHdVrv//VrLxj5WjFclWJAdvBU3TTaxE3pvTvogPSYtIJUvHo/qcKA1U7eoU/6KMq7+CVJZHff0KMth+tqB8SNhv8eCQDVfneRjtCEmUoCDDMmtBf4uc/uc0AtdTCYW3KyfyjrsOFrGm2/JxGkJ334vcQFHyPUwSnICyD+xqZbiSgi76UmA6CjYiEn+4/jyvoE0kQV0h3uYhAxxj8ShfXh88Jeh1DXL9S1Oe2W0gRhLms2winz2uz+zvEpGBfy9Q2mzwd7gWoofxfI0AnB2d0rb9IR7l88GspN8N4dvHk8aB552jRMTngCqsH8u14ab2bnJPfl1XEZmED/TxlwGOCdHCC3qLcDmc+B7t2k7V2HUjwjeQ+vEfs6aQkWYJY2ujSJmujpgxOebIC4TuvHpiuyR4kuzYGc1RJC4s7UfpJ9u5rwfmsT+l1X/AwauyW7lRPDuqainIokDEJaAh+Oi4cipI5Q4mKMfd1m/NK/cKyWsG2j/Hg=" - -jdk: - - oraclejdk8 - -script: - # JaCoCo is used to have code coverage, the agent has to be activated - - mvn clean verify sonar:sonar -Pcoverage-per-test - -cache: - directories: - - '$HOME/.m2/repository' - - '$HOME/.sonar/cache' diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f30f18..9143f89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## [2.0.2] - 2021-09-02 +### Changed +- Multiple dependencies updated + +## [2.0.1] - 2020-09-09 +### Changed +- Moved to new coordinates de.jakop.validation:annotation-validator +- Multiple dependencies updated + ## [2.0] - 2018-03-13 ### Changed - Dependencies updated: assertj-core, commons-lang3, jsr305, spring-core @@ -9,4 +18,4 @@ - Support for Annotations that use @AliasFor without attributes ### Changed -- Fixed a false positive in @AliasFor validation \ No newline at end of file +- Fixed a false positive in @AliasFor validation diff --git a/README.md b/README.md index 5cb3c31..41ec095 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -[![build status](https://travis-ci.org/arxes-tolina/annotation-validator.svg?branch=master)](https://github.com/arxes-tolina/annotation-validator/commits/master) -[![quality status](https://sonarcloud.io//api/badges/gate?key=de.tolina.common.validation%3Aannotation-validator)](https://sonarcloud.io/dashboard?id=de.tolina.common.validation%3Aannotation-validator) +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=fjakop_annotation-validator&metric=alert_status)](https://sonarcloud.io/dashboard?id=fjakop_annotation-validator) # Usage with Maven @@ -7,9 +6,9 @@ Add the following dependency to your pom.xml ``` - de.tolina.common.validation + de.jakop.validation annotation-validator - 1.0 + 2.0.2 test ``` @@ -35,7 +34,7 @@ class AnnotatedTestClass { If you would like to check the class to be annotated only with `@MyAnnotation` and only have the configured params use the following ``` -import static de.tolina.common.validation.AnnotationDefinition.*; +import static AnnotationDefinition.*; ... @@ -48,7 +47,7 @@ validate().exactly() // If you would like to check the class to be annotated only with `@MyAnnotation` but also want to consider default values for not configured params use the following ``` -import static de.tolina.common.validation.AnnotationDefinition.*; +import static AnnotationDefinition.*; ... @@ -75,4 +74,4 @@ validate() // .param("value", TEST)) // .forMethod(AnnotatedTestClass.class.getMethod("methodWithAnnotations")); -``` \ No newline at end of file +``` diff --git a/pom.xml b/pom.xml index 69add42..f5869d6 100644 --- a/pom.xml +++ b/pom.xml @@ -1,322 +1,338 @@ - - - - 4.0.0 - - de.tolina.common.validation - annotation-validator - 2.0 - jar - - Annotation Validator - - - Check classes, methods or fields for annotations with specific parameters. - - - 2016 - - - arxes-tolina GmbH - https://www.arxes-tolina.de - - - - - The Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - - - - - - Ron Peters - ron.peters@arxes-tolina.de - - - Matthias Hartmann - matthias.hartmann@signavio.com - - - Frank Jakop - - - - http://github.com/arxes-tolina/annotation-validator - - - scm:git:ssh://git@github.com/arxes-tolina/annotation-validator.git - scm:git:ssh://git@github.com/arxes-tolina/annotation-validator.git - https://github.com/arxes-tolina/annotation-validator - - - - - UTF-8 - 1.8 - ${java.version} - ${java.version} - - 0.7.5.201505241946 - 3.0 - 1.5 - 2.6 - 2.9.1 - 0.4 - 2.5.3 - 3.0.1 - 1.6.7 - 2.12.4 - 4.2 - - - - - org.assertj - assertj-core - 3.9.1 - - - - org.apache.commons - commons-lang3 - 3.7 - - - - com.google.code.findbugs - jsr305 - 3.0.2 - - - - junit - junit - 4.12 - test - - - - org.hamcrest - hamcrest-library - 1.3 - test - - - org.springframework - spring-core - 5.0.4.RELEASE - test - - - - - - - - com.soebes.maven.plugins.mlv - maven-license-verifier-plugin - ${maven-license-verifier-plugin.version} - - src/main/resources/licenses.xml - true - true - true - - - - - verify - - - - - - - com.mycila - license-maven-plugin - ${license-maven-plugin.version} - - true - true -
com/mycila/maven/plugin/license/templates/APACHE-2.txt
- - ${project.organization.name} - entwicklung@arxes-tolina.de - - - **/README.md - src/test/resources/** - src/main/resources/** - -
- - - - check - - - -
- - - org.apache.maven.plugins - maven-jar-plugin - ${maven-jar-plugin.version} - - - - test-jar - - - - - - org.apache.maven.plugins - maven-source-plugin - ${maven-source-plugin.version} - - - attach-sources - - jar-no-fork - - - - - - - org.sonatype.plugins - nexus-staging-maven-plugin - ${nexus-staging-maven-plugin.version} - true - - ossrh - https://oss.sonatype.org/ - false - - - -
-
- - - - release - - - - org.apache.maven.plugins - maven-release-plugin - ${maven-release-plugin.version} - - true - @{project.version} - false - release - deploy - - - - - org.apache.maven.plugins - maven-javadoc-plugin - ${maven-javadoc-plugin.version} - - - attach-javadocs - - jar - - - - - - - org.apache.maven.plugins - maven-gpg-plugin - ${maven-gpg-plugin.version} - - gpg - - - - sign-artifacts - verify - - sign - - - - - - - - - - coverage-per-test - - - - org.jacoco - jacoco-maven-plugin - ${jacoco-maven-plugin.version} - - - pre-test - - prepare-agent - - - - - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - - listener - org.sonar.java.jacoco.JUnitListener - - - - - - - - - org.sonarsource.java - sonar-jacoco-listeners - ${sonar-jacoco-listeners.version} - test - - - - - - - - ossrh - https://oss.sonatype.org/content/repositories/snapshots - - -
+ + + + 4.0.0 + + de.jakop.validation + annotation-validator + 2.0.3-SNAPSHOT + jar + + Annotation Validator + + + Check classes, methods or fields for annotations with specific parameters. + + + 2020 + + + + The Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + + + Frank Jakop + + + + https://github.com/fjakop/annotation-validator + + + scm:git:https://github.com/fjakop/annotation-validator.git + scm:git:https://github.com/fjakop/annotation-validator.git + https://github.com/fjakop/annotation-validator + + + + + UTF-8 + 1.8 + ${java.version} + ${java.version} + + 0.8.5 + 3.0 + 3.0.0-M3 + 1.6 + 3.2.0 + 3.2.0 + 0.4 + 3.0.0-M1 + 3.2.1 + 1.6.8 + 3.0.0-M5 + 5.14.0.18788 + + fjakop_annotation-validator + fjakop-github + https://sonarcloud.io + + + + + + org.assertj + assertj-core + 3.20.2 + + + + org.apache.commons + commons-lang3 + 3.12.0 + + + + com.google.code.findbugs + jsr305 + 3.0.2 + + + + junit + junit + 4.13.2 + test + + + + org.hamcrest + hamcrest + 2.2 + test + + + org.springframework + spring-core + 5.3.9 + test + + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + ${maven-enforcer-plugin.version} + + + enforce-maven + + enforce + + + + + 3.3.9 + + + + + + + + com.soebes.maven.plugins.mlv + maven-license-verifier-plugin + ${maven-license-verifier-plugin.version} + + + org.hamcrest:hamcrest:jar:2.2 + + src/main/resources/licenses.xml + true + true + true + + + + + verify + + + + + + + com.mycila + license-maven-plugin + ${license-maven-plugin.version} + + true + true +
APACHE-2-with-modifications.txt
+ + Frank Jakop + + + + **/README.md + src/test/resources/** + src/main/resources/** + +
+ + + + check + + + +
+ + + org.apache.maven.plugins + maven-jar-plugin + ${maven-jar-plugin.version} + + + + test-jar + + + + + + org.apache.maven.plugins + maven-source-plugin + ${maven-source-plugin.version} + + + attach-sources + + jar-no-fork + + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + ${nexus-staging-maven-plugin.version} + true + + ossrh + https://oss.sonatype.org/ + false + + + +
+
+ + + + release + + + + org.apache.maven.plugins + maven-release-plugin + ${maven-release-plugin.version} + + true + @{project.version} + false + release + deploy + + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + + attach-javadocs + + jar + + + + + + + org.apache.maven.plugins + maven-gpg-plugin + ${maven-gpg-plugin.version} + + gpg + + + + sign-artifacts + verify + + sign + + + + + + + + + + coverage-per-test + + + + org.jacoco + jacoco-maven-plugin + ${jacoco-maven-plugin.version} + + + pre-test + + prepare-agent + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + listener + org.sonar.java.jacoco.JUnitListener + + + + + + + + + org.sonarsource.java + sonar-jacoco-listeners + ${sonar-jacoco-listeners.version} + test + + + + + + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + +
diff --git a/src/main/java/de/tolina/common/validation/AnnotationDefinition.java b/src/main/java/de/jakop/validation/annotations/AnnotationDefinition.java similarity index 95% rename from src/main/java/de/tolina/common/validation/AnnotationDefinition.java rename to src/main/java/de/jakop/validation/annotations/AnnotationDefinition.java index 9a1321b..c97ed07 100644 --- a/src/main/java/de/tolina/common/validation/AnnotationDefinition.java +++ b/src/main/java/de/jakop/validation/annotations/AnnotationDefinition.java @@ -1,92 +1,94 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -import java.lang.annotation.Annotation; -import java.util.ArrayList; -import java.util.List; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -/** - * Combines an Annotation Class with optional {@link AnnotationMethodDefinition}s - */ -public class AnnotationDefinition { - private Class annotation; - private List annotationMethodDefinitions; - - private AnnotationDefinition(@Nonnull final Class annotation) { - this.annotation = annotation; - annotationMethodDefinitions = new ArrayList<>(); - } - - /** - * Describes an Annotation type - * - * @param annotationType - Type of the Annotation - */ - @Nonnull - public static AnnotationDefinition type(@Nonnull final Class annotationType) { - return new AnnotationDefinition(annotationType); - } - - /** - * Describes an Annotation parameter - * - * @param method Name of the Method that represents the Parameter - * @param values Values of the parameter - */ - @Nonnull - public AnnotationDefinition param(@Nonnull final String method, @Nullable final Object... values) { - final AnnotationMethodDefinition annotationMethodDefinition = new AnnotationMethodDefinition(method, values); - annotationMethodDefinitions.add(annotationMethodDefinition); - return AnnotationDefinition.this; - } - - @Nonnull - Class getAnnotation() { - return annotation; - } - - @Nonnull - List getAnnotationMethodDefinitions() { - return annotationMethodDefinitions; - } - - /** - * Holds Method names and Parameters - */ - static class AnnotationMethodDefinition { - private String method; - private Object[] values; - - private AnnotationMethodDefinition(@Nonnull final String method, @Nullable final Object[] values) { - this.method = method; - this.values = values; - } - - @Nonnull - String getMethod() { - return method; - } - - @Nullable - Object[] getValues() { - return values; - } - } -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +import java.lang.annotation.Annotation; +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +/** + * Combines an Annotation Class with optional {@link AnnotationMethodDefinition}s + */ +public class AnnotationDefinition { + private Class annotation; + private List annotationMethodDefinitions; + + private AnnotationDefinition(@Nonnull final Class annotation) { + this.annotation = annotation; + annotationMethodDefinitions = new ArrayList<>(); + } + + /** + * Describes an Annotation type + * + * @param annotationType - Type of the Annotation + */ + @Nonnull + public static AnnotationDefinition type(@Nonnull final Class annotationType) { + return new AnnotationDefinition(annotationType); + } + + /** + * Describes an Annotation parameter + * + * @param method Name of the Method that represents the Parameter + * @param values Values of the parameter + */ + @Nonnull + public AnnotationDefinition param(@Nonnull final String method, @Nullable final Object... values) { + final AnnotationMethodDefinition annotationMethodDefinition = new AnnotationMethodDefinition(method, values); + annotationMethodDefinitions.add(annotationMethodDefinition); + return AnnotationDefinition.this; + } + + @Nonnull + Class getAnnotation() { + return annotation; + } + + @Nonnull + List getAnnotationMethodDefinitions() { + return annotationMethodDefinitions; + } + + /** + * Holds Method names and Parameters + */ + static class AnnotationMethodDefinition { + private String method; + private Object[] values; + + private AnnotationMethodDefinition(@Nonnull final String method, @Nullable final Object[] values) { + this.method = method; + this.values = values; + } + + @Nonnull + String getMethod() { + return method; + } + + @Nullable + Object[] getValues() { + return values; + } + } +} diff --git a/src/main/java/de/tolina/common/validation/AnnotationValidation.java b/src/main/java/de/jakop/validation/annotations/AnnotationValidation.java similarity index 95% rename from src/main/java/de/tolina/common/validation/AnnotationValidation.java rename to src/main/java/de/jakop/validation/annotations/AnnotationValidation.java index c7d3435..68d1fab 100644 --- a/src/main/java/de/tolina/common/validation/AnnotationValidation.java +++ b/src/main/java/de/jakop/validation/annotations/AnnotationValidation.java @@ -1,481 +1,482 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -import org.apache.commons.lang3.ClassUtils; -import org.apache.commons.lang3.StringUtils; -import org.assertj.core.api.SoftAssertionError; -import org.assertj.core.api.SoftAssertions; -import org.assertj.core.util.Lists; -import org.assertj.core.util.VisibleForTesting; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.lang.annotation.Annotation; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.*; - -import static de.tolina.common.validation.ValidationMode.*; -import static java.lang.String.format; -import static org.assertj.core.api.Assertions.assertThat; - - -/** - * API for {@link AnnotationValidator} - */ -public class AnnotationValidation { - - @VisibleForTesting - HashSet paramBlacklist; - private List annotationDefinitions; - private ValidationMode validationMode; - private Annotation[] allAnnotations; - private static final String - ACCESS_OR_INVOCATION_EXCEPTION_MESSAGE = "Could not access/invoke aliased method for '%s'."; - - - AnnotationValidation( - @Nonnull final HashSet parametersBlacklist) { - validationMode = DEFAULT; - paramBlacklist = parametersBlacklist; - annotationDefinitions = new ArrayList<>(); - } - - - /** - * Adds an {@link AnnotationDefinition} to the Validator - * - * @param annotationDefinition the Annotation - * @return the AnnotationValidator - */ - @Nonnull - public AnnotationValidation annotation( - @Nonnull final AnnotationDefinition annotationDefinition) { - annotationDefinitions.add(annotationDefinition); - return this; - } - - - /** - * Validates that no other Annotations are defined and only the defined params are present. - * - * @return the AnnotationValidator - */ - @Nonnull - public AnnotationValidation exactly() { - validationMode = EXACTLY; - return this; - } - - - /** - * Validates that no other Annotations are defined considering default values for undefined params. - * - * @return the AnnotationValidator - */ - @Nonnull - public AnnotationValidation only() { - validationMode = ONLY; - return this; - } - - - /** - * Validates Annotations of the given Class and checks that: - *
- all given Annotations are found - *
- no other Annotations are on the given Class - *
- Annotations are in correct order - * - * @param annotatedClass Class to be validated - */ - public void forClass( - @Nonnull final Class annotatedClass) { - forClassOrMethodOrField(annotatedClass); - } - - - public void forConstructor(@Nonnull final Constructor annotatedContructor) { - forClassOrMethodOrField(annotatedContructor); - } - - - /** - * Validates Annotations of the given Method and checks that: - *
- all given Annotations are found - *
- no other Annotations are on the given Method - *
- Annotations are in correct order - * - * @param annotatedMethod Method to be validated - */ - public void forMethod( - @Nonnull final Method annotatedMethod) { - forClassOrMethodOrField(annotatedMethod); - } - - - /** - * Validates Annotations of the given Field and checks that: - *
- all given Annotations are found - *
- no other Annotations are on the given Filed - *
- Annotations are in correct order - * - * @param annotatedField Field to be validated - */ - public void forField( - @Nonnull final Field annotatedField) { - forClassOrMethodOrField(annotatedField); - } - - - /** - * Validates the configured Annotations - * - * @param annotatedObject can be a Class, a Method or a Field - */ - private void forClassOrMethodOrField( - @Nonnull final Object annotatedObject) { - final SoftAssertions softly = new SoftAssertions(); - final List annotationsList = new ArrayList<>(); - allAnnotations = getAllAnnotationsFor(annotatedObject); - - for (final AnnotationDefinition annotationDefinition : annotationDefinitions) { - // check if annotation is present - final Optional foundAnnotation = - findAnnotationFor(annotationDefinition.getAnnotation()); - - softly.assertThat(foundAnnotation) - .as("Expected Annotation %s not found", annotationDefinition.getAnnotation().getName()) - .isPresent(); - - foundAnnotation.ifPresent(annotation -> { - annotationsList.add(annotation.annotationType().getName()); - - // check all methods defined in annotation definition against current annotation's methods - final List validatedMethods = - validateAllMethodsOfAnnotationDefinition(softly, annotationDefinition, annotation); - - // check if there are undefined methods in annotation definition present in annotation - checkForUndefinedMethodsInAnnotation(softly, annotation, validatedMethods); - }); - } - - softly.assertThat(validationMode == DEFAULT && annotationDefinitions.isEmpty()) - .as("Please add at least one Annotation to assert or enable strict validation.") - .isFalse(); - - if (validationMode != DEFAULT) { - softly.assertThat(allAnnotations) - .extracting(annotation -> annotation.annotationType().getName()) - .containsExactlyElementsOf(annotationsList); - } - try { - softly.assertAll(); - } catch (final SoftAssertionError sae) { - throw new SoftAssertionErrorWithObjectDetails(sae.getErrors(), annotatedObject); - } - } - - - private void addIfNotPresent( - @Nonnull final Collection collection, - @Nonnull final Annotation[] annotations) { - for (final Annotation annotation : annotations) { - if (!collection.contains(annotation)) { - collection.add(annotation); - } - } - } - - - private void checkForUndefinedMethodsInAnnotation( - @Nonnull final SoftAssertions softly, - @Nonnull final Annotation annotation, - @Nonnull final List validatedMethods) { - final Method[] allMethods = annotation.annotationType().getDeclaredMethods(); - - for (final Method declaredMethod : allMethods) { - // we do not want these to be checked - final boolean isBlacklisted = paramBlacklist.contains(declaredMethod.getName()); - // skip already validated methods - final boolean isAlreadyValidated = validatedMethods.contains(declaredMethod.getName()); - - if (isBlacklisted || isAlreadyValidated) { - continue; - } - - // all methods in current annotation which are not defined in annotation definition or blacklist are to be reported as error - final Object methodResult; - try { - methodResult = declaredMethod.invoke(annotation); - - if (validationMode != EXACTLY) { - Object defaultValue = declaredMethod.getDefaultValue(); - - softly.assertThat(methodResult) - .as("Unexpected value for Method '%s' found.", declaredMethod.getName()) - .isEqualTo(defaultValue); - } else { - if (Object[].class.isInstance(methodResult)) { - softly.assertThat((Object[]) methodResult) - .as("Unexpected values for %s found.", declaredMethod.getName()).isNullOrEmpty(); - } else { - final String description = "Unexpected value for Method '%s' found."; - if (methodResult instanceof String) { - softly.assertThat((String) methodResult).as(description, declaredMethod.getName()) - .isNullOrEmpty(); - } else { - softly.assertThat(methodResult).as(description, declaredMethod.getName()).isNull(); - } - } - } - } catch (IllegalAccessException | InvocationTargetException e) { - softly.fail(format(ACCESS_OR_INVOCATION_EXCEPTION_MESSAGE, declaredMethod.getName())); - } - } - } - - - private boolean equalParamTypes( - @Nonnull final Class[] typesOne, - @Nonnull final Class[] typesTwo) { - if (typesOne.length == typesTwo.length) { - for (int i = 0; i < typesOne.length; i++) { - if (typesOne[i] != typesTwo[i]) { - return false; - } - } - return true; - } - return false; - } - - - /** - * Calls dependent on the type of the given Object: - */ - @Nonnull - private Optional findAnnotationFor( - @Nonnull final Class annotation) { - return Arrays.stream(allAnnotations) - .filter(annotationFound -> annotationFound.annotationType().getName().equals(annotation.getName())) - .findAny(); - } - - - /** - * Calls dependent on the type of the given Object: - */ - @Nullable - private Annotation[] getAllAnnotationsFor( - @Nonnull final Object annotated) { - if (annotated instanceof Field) { - return ((Field) annotated).getAnnotations(); - } - - if (annotated instanceof Constructor) { - return ((Constructor) annotated).getAnnotations(); - } - - if (annotated instanceof Method) { - final Method annotatedMethod = (Method) annotated; - final Class declaringClass = annotatedMethod.getDeclaringClass(); - final List> allClasses = new ArrayList<>(); - allClasses.add(declaringClass); - allClasses.addAll(ClassUtils.getAllSuperclasses(declaringClass)); - - final ArrayList allAnnotations = new ArrayList<>(); - - for (final Class aClass : allClasses) { - final ArrayList allMethods = new ArrayList<>(); - allMethods.addAll(Arrays.asList(aClass.getDeclaredMethods())); - - final List> interfaces = ClassUtils.getAllInterfaces(aClass); - - for (final Class anInterface : interfaces) { - allMethods.addAll(Arrays.asList(anInterface.getDeclaredMethods())); - } - - allMethods.stream() - .filter(method -> isSameMethod(method, annotatedMethod)) - .forEachOrdered(method -> addIfNotPresent(allAnnotations, method.getAnnotations())); - } - - return allAnnotations.toArray(new Annotation[]{}); - } - - final Class annotatedClass = (Class) annotated; - final List> allClasses = new ArrayList<>(); - allClasses.add(annotatedClass); - allClasses.addAll(ClassUtils.getAllSuperclasses(annotatedClass)); - - final ArrayList allAnnotations = new ArrayList<>(); - - for (final Class aClass : allClasses) { - addIfNotPresent(allAnnotations, aClass.getAnnotations()); - final List> interfaces = ClassUtils.getAllInterfaces(aClass); - for (final Class anInterface : interfaces) { - addIfNotPresent(allAnnotations, anInterface.getAnnotations()); - } - } - - return allAnnotations.toArray(new Annotation[]{}); - } - - - private boolean isSameMethod( - @Nonnull final Method one, - @Nonnull final Method two) { - return Objects.equals(one.getName(), two.getName()) && equalParamTypes(one.getParameterTypes(), - two.getParameterTypes()); - } - - - private List validateAllMethodsOfAnnotationDefinition( - @Nonnull final SoftAssertions softly, - @Nonnull final AnnotationDefinition annotationDefinition, - @Nonnull final Annotation annotation) { - final List validatedMethods = Lists.newArrayList(); - - // check all methods defined in annotation definition - for (final AnnotationDefinition.AnnotationMethodDefinition annotationMethodDefinition : annotationDefinition - .getAnnotationMethodDefinitions()) { - final String methodName = annotationMethodDefinition.getMethod(); - final Object[] expectedValues = annotationMethodDefinition.getValues(); - - Method actualMethod = null; - try { - actualMethod = annotation.annotationType().getMethod(methodName); - } catch (final NoSuchMethodException e) { - softly.assertThat(actualMethod).as("Method %s not found.", methodName).isNotNull(); - continue; - } - - // check if this annotation's actualMethod is an alias - Method aliasMethod = null; - Optional aliasForAnnotation = getAliasNameIfPresent(actualMethod); - if (aliasForAnnotation.isPresent()) { - try { - aliasMethod = getAliasMethod(annotation, aliasForAnnotation.get(), methodName); - } catch (NoSuchMethodException e) { - softly.assertThat(aliasMethod) - .as("Referenced alias method %s not found.", aliasForAnnotation.get()) - .isNotNull(); - continue; - } catch (IllegalAccessException | InvocationTargetException e) { - softly.fail(format(ACCESS_OR_INVOCATION_EXCEPTION_MESSAGE, actualMethod.getName())); - } - } - - - // check that actual actualMethod in annotation has defined return types - final Object actualMethodResult; - final Object aliasMethodResult; - Object assertableResult = null; - try { - actualMethodResult = actualMethod.invoke(annotation); - assertableResult = actualMethodResult; - - assertMethodResult(actualMethodResult, expectedValues); - } catch (IllegalAccessException | InvocationTargetException e) { - softly.fail(format(ACCESS_OR_INVOCATION_EXCEPTION_MESSAGE, actualMethod.getName())); - } catch (AssertionError e) { - if (aliasForAnnotation.isPresent()) { - try { - aliasMethodResult = aliasMethod.invoke(annotation); - - assertMethodResult(aliasMethodResult, expectedValues); - - assertableResult = aliasMethodResult; - } catch (IllegalAccessException | InvocationTargetException e1) { - softly.fail(ACCESS_OR_INVOCATION_EXCEPTION_MESSAGE, aliasMethod.getName()); - } catch (AssertionError e1) { - // noop - } - } - } - - if (Object[].class.isInstance(assertableResult)) { - // this produces readable descriptions on its own - // all and only defined values must be returned in defined order - softly.assertThat((Object[]) assertableResult).containsExactlyElementsOf(Arrays.asList(expectedValues)); - } else { - // this produces readable descriptions on its own - softly.assertThat(assertableResult).isEqualTo(expectedValues[0]); - } - - validatedMethods.add(actualMethod.getName()); - if (aliasForAnnotation.isPresent()) { - validatedMethods.add(aliasMethod.getName()); - } - } - - return validatedMethods; - } - - - private Method getAliasMethod(Annotation originalAnnotation, Annotation aliasForAnnotation, String methodName) - throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Method annotationMethod = aliasForAnnotation.annotationType().getDeclaredMethod("annotation"); - Class annotationValue = - (Class) annotationMethod.invoke(aliasForAnnotation); - - String aliasMethodName; - if (!annotationValue.equals(annotationMethod.getDefaultValue())) { - aliasMethodName = aliasForAnnotation - .annotationType() - .getDeclaredMethod("attribute") - .invoke(aliasForAnnotation) - .toString(); - - if (StringUtils.isEmpty(aliasMethodName)) { - aliasMethodName = methodName; - } - - return annotationValue.getDeclaredMethod(aliasMethodName); - } else { - aliasMethodName = aliasForAnnotation - .annotationType() - .getDeclaredMethod("value") - .invoke(aliasForAnnotation) - .toString(); - - return originalAnnotation.annotationType().getDeclaredMethod(aliasMethodName); - } - } - - - private void assertMethodResult(Object actualValues, Object[] expectedValues) { - if (Object[].class.isInstance(actualValues)) { - // this produces readable descriptions on its own - // all and only defined values must be returned in defined order - assertThat((Object[]) actualValues).containsExactlyElementsOf(Arrays.asList(expectedValues)); - } else { - // this produces readable descriptions on its own - assertThat(actualValues).isEqualTo(expectedValues[0]); - } - } - - - private Optional getAliasNameIfPresent(Method method) { - return Arrays.stream(method.getDeclaredAnnotations()) - .filter(annotationFound -> annotationFound.annotationType().getName().endsWith("AliasFor")) - .findAny(); - } - -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +import org.apache.commons.lang3.ClassUtils; +import org.apache.commons.lang3.StringUtils; +import org.assertj.core.api.SoftAssertionError; +import org.assertj.core.api.SoftAssertions; +import org.assertj.core.util.Lists; +import org.assertj.core.util.VisibleForTesting; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; + +import static java.lang.String.format; +import static org.assertj.core.api.Assertions.assertThat; + + +/** + * API for {@link AnnotationValidator} + */ +public class AnnotationValidation { + + @VisibleForTesting + HashSet paramBlacklist; + private List annotationDefinitions; + private ValidationMode validationMode; + private Annotation[] allAnnotations; + private static final String + ACCESS_OR_INVOCATION_EXCEPTION_MESSAGE = "Could not access/invoke aliased method for '%s'."; + + + AnnotationValidation( + @Nonnull final HashSet parametersBlacklist) { + validationMode = ValidationMode.DEFAULT; + paramBlacklist = parametersBlacklist; + annotationDefinitions = new ArrayList<>(); + } + + + /** + * Adds an {@link AnnotationDefinition} to the Validator + * + * @param annotationDefinition the Annotation + * @return the AnnotationValidator + */ + @Nonnull + public AnnotationValidation annotation( + @Nonnull final AnnotationDefinition annotationDefinition) { + annotationDefinitions.add(annotationDefinition); + return this; + } + + + /** + * Validates that no other Annotations are defined and only the defined params are present. + * + * @return the AnnotationValidator + */ + @Nonnull + public AnnotationValidation exactly() { + validationMode = ValidationMode.EXACTLY; + return this; + } + + + /** + * Validates that no other Annotations are defined considering default values for undefined params. + * + * @return the AnnotationValidator + */ + @Nonnull + public AnnotationValidation only() { + validationMode = ValidationMode.ONLY; + return this; + } + + + /** + * Validates Annotations of the given Class and checks that: + *
- all given Annotations are found + *
- no other Annotations are on the given Class + *
- Annotations are in correct order + * + * @param annotatedClass Class to be validated + */ + public void forClass( + @Nonnull final Class annotatedClass) { + forClassOrMethodOrField(annotatedClass); + } + + + public void forConstructor(@Nonnull final Constructor annotatedContructor) { + forClassOrMethodOrField(annotatedContructor); + } + + + /** + * Validates Annotations of the given Method and checks that: + *
- all given Annotations are found + *
- no other Annotations are on the given Method + *
- Annotations are in correct order + * + * @param annotatedMethod Method to be validated + */ + public void forMethod( + @Nonnull final Method annotatedMethod) { + forClassOrMethodOrField(annotatedMethod); + } + + + /** + * Validates Annotations of the given Field and checks that: + *
- all given Annotations are found + *
- no other Annotations are on the given Filed + *
- Annotations are in correct order + * + * @param annotatedField Field to be validated + */ + public void forField( + @Nonnull final Field annotatedField) { + forClassOrMethodOrField(annotatedField); + } + + + /** + * Validates the configured Annotations + * + * @param annotatedObject can be a Class, a Method or a Field + */ + private void forClassOrMethodOrField( + @Nonnull final Object annotatedObject) { + final SoftAssertions softly = new SoftAssertions(); + final List annotationsList = new ArrayList<>(); + allAnnotations = getAllAnnotationsFor(annotatedObject); + + for (final AnnotationDefinition annotationDefinition : annotationDefinitions) { + // check if annotation is present + final Optional foundAnnotation = + findAnnotationFor(annotationDefinition.getAnnotation()); + + softly.assertThat(foundAnnotation) + .as("Expected Annotation %s not found", annotationDefinition.getAnnotation().getName()) + .isPresent(); + + foundAnnotation.ifPresent(annotation -> { + annotationsList.add(annotation.annotationType().getName()); + + // check all methods defined in annotation definition against current annotation's methods + final List validatedMethods = + validateAllMethodsOfAnnotationDefinition(softly, annotationDefinition, annotation); + + // check if there are undefined methods in annotation definition present in annotation + checkForUndefinedMethodsInAnnotation(softly, annotation, validatedMethods); + }); + } + + softly.assertThat(validationMode == ValidationMode.DEFAULT && annotationDefinitions.isEmpty()) + .as("Please add at least one Annotation to assert or enable strict validation.") + .isFalse(); + + if (validationMode != ValidationMode.DEFAULT) { + softly.assertThat(allAnnotations) + .extracting(annotation -> annotation.annotationType().getName()) + .containsExactlyElementsOf(annotationsList); + } + try { + softly.assertAll(); + } catch (final SoftAssertionError sae) { + throw new SoftAssertionErrorWithObjectDetails(sae.getErrors(), annotatedObject); + } + } + + + private void addIfNotPresent( + @Nonnull final Collection collection, + @Nonnull final Annotation[] annotations) { + for (final Annotation annotation : annotations) { + if (!collection.contains(annotation)) { + collection.add(annotation); + } + } + } + + + private void checkForUndefinedMethodsInAnnotation( + @Nonnull final SoftAssertions softly, + @Nonnull final Annotation annotation, + @Nonnull final List validatedMethods) { + final Method[] allMethods = annotation.annotationType().getDeclaredMethods(); + + for (final Method declaredMethod : allMethods) { + // we do not want these to be checked + final boolean isBlacklisted = paramBlacklist.contains(declaredMethod.getName()); + // skip already validated methods + final boolean isAlreadyValidated = validatedMethods.contains(declaredMethod.getName()); + + if (isBlacklisted || isAlreadyValidated) { + continue; + } + + // all methods in current annotation which are not defined in annotation definition or blacklist are to be reported as error + final Object methodResult; + try { + methodResult = declaredMethod.invoke(annotation); + + if (validationMode != ValidationMode.EXACTLY) { + Object defaultValue = declaredMethod.getDefaultValue(); + + softly.assertThat(methodResult) + .as("Unexpected value for Method '%s' found.", declaredMethod.getName()) + .isEqualTo(defaultValue); + } else { + if (Object[].class.isInstance(methodResult)) { + softly.assertThat((Object[]) methodResult) + .as("Unexpected values for %s found.", declaredMethod.getName()).isNullOrEmpty(); + } else { + final String description = "Unexpected value for Method '%s' found."; + if (methodResult instanceof String) { + softly.assertThat((String) methodResult).as(description, declaredMethod.getName()) + .isNullOrEmpty(); + } else { + softly.assertThat(methodResult).as(description, declaredMethod.getName()).isNull(); + } + } + } + } catch (IllegalAccessException | InvocationTargetException e) { + softly.fail(format(ACCESS_OR_INVOCATION_EXCEPTION_MESSAGE, declaredMethod.getName())); + } + } + } + + + private boolean equalParamTypes( + @Nonnull final Class[] typesOne, + @Nonnull final Class[] typesTwo) { + if (typesOne.length == typesTwo.length) { + for (int i = 0; i < typesOne.length; i++) { + if (typesOne[i] != typesTwo[i]) { + return false; + } + } + return true; + } + return false; + } + + + /** + * Calls dependent on the type of the given Object: + */ + @Nonnull + private Optional findAnnotationFor( + @Nonnull final Class annotation) { + return Arrays.stream(allAnnotations) + .filter(annotationFound -> annotationFound.annotationType().getName().equals(annotation.getName())) + .findAny(); + } + + + /** + * Calls dependent on the type of the given Object: + */ + @Nullable + private Annotation[] getAllAnnotationsFor( + @Nonnull final Object annotated) { + if (annotated instanceof Field) { + return ((Field) annotated).getAnnotations(); + } + + if (annotated instanceof Constructor) { + return ((Constructor) annotated).getAnnotations(); + } + + if (annotated instanceof Method) { + final Method annotatedMethod = (Method) annotated; + final Class declaringClass = annotatedMethod.getDeclaringClass(); + final List> allClasses = new ArrayList<>(); + allClasses.add(declaringClass); + allClasses.addAll(ClassUtils.getAllSuperclasses(declaringClass)); + + final ArrayList allAnnotations = new ArrayList<>(); + + for (final Class aClass : allClasses) { + final ArrayList allMethods = new ArrayList<>(); + allMethods.addAll(Arrays.asList(aClass.getDeclaredMethods())); + + final List> interfaces = ClassUtils.getAllInterfaces(aClass); + + for (final Class anInterface : interfaces) { + allMethods.addAll(Arrays.asList(anInterface.getDeclaredMethods())); + } + + allMethods.stream() + .filter(method -> isSameMethod(method, annotatedMethod)) + .forEachOrdered(method -> addIfNotPresent(allAnnotations, method.getAnnotations())); + } + + return allAnnotations.toArray(new Annotation[]{}); + } + + final Class annotatedClass = (Class) annotated; + final List> allClasses = new ArrayList<>(); + allClasses.add(annotatedClass); + allClasses.addAll(ClassUtils.getAllSuperclasses(annotatedClass)); + + final ArrayList allAnnotations = new ArrayList<>(); + + for (final Class aClass : allClasses) { + addIfNotPresent(allAnnotations, aClass.getAnnotations()); + final List> interfaces = ClassUtils.getAllInterfaces(aClass); + for (final Class anInterface : interfaces) { + addIfNotPresent(allAnnotations, anInterface.getAnnotations()); + } + } + + return allAnnotations.toArray(new Annotation[]{}); + } + + + private boolean isSameMethod( + @Nonnull final Method one, + @Nonnull final Method two) { + return Objects.equals(one.getName(), two.getName()) && equalParamTypes(one.getParameterTypes(), + two.getParameterTypes()); + } + + + private List validateAllMethodsOfAnnotationDefinition( + @Nonnull final SoftAssertions softly, + @Nonnull final AnnotationDefinition annotationDefinition, + @Nonnull final Annotation annotation) { + final List validatedMethods = Lists.newArrayList(); + + // check all methods defined in annotation definition + for (final AnnotationDefinition.AnnotationMethodDefinition annotationMethodDefinition : annotationDefinition + .getAnnotationMethodDefinitions()) { + final String methodName = annotationMethodDefinition.getMethod(); + final Object[] expectedValues = annotationMethodDefinition.getValues(); + + Method actualMethod = null; + try { + actualMethod = annotation.annotationType().getMethod(methodName); + } catch (final NoSuchMethodException e) { + softly.assertThat(actualMethod).as("Method %s not found.", methodName).isNotNull(); + continue; + } + + // check if this annotation's actualMethod is an alias + Method aliasMethod = null; + Optional aliasForAnnotation = getAliasNameIfPresent(actualMethod); + if (aliasForAnnotation.isPresent()) { + try { + aliasMethod = getAliasMethod(annotation, aliasForAnnotation.get(), methodName); + } catch (NoSuchMethodException e) { + softly.assertThat(aliasMethod) + .as("Referenced alias method %s not found.", aliasForAnnotation.get()) + .isNotNull(); + continue; + } catch (IllegalAccessException | InvocationTargetException e) { + softly.fail(format(ACCESS_OR_INVOCATION_EXCEPTION_MESSAGE, actualMethod.getName())); + } + } + + + // check that actual actualMethod in annotation has defined return types + final Object actualMethodResult; + final Object aliasMethodResult; + Object assertableResult = null; + try { + actualMethodResult = actualMethod.invoke(annotation); + assertableResult = actualMethodResult; + + assertMethodResult(actualMethodResult, expectedValues); + } catch (IllegalAccessException | InvocationTargetException e) { + softly.fail(format(ACCESS_OR_INVOCATION_EXCEPTION_MESSAGE, actualMethod.getName())); + } catch (AssertionError e) { + if (aliasForAnnotation.isPresent()) { + try { + aliasMethodResult = aliasMethod.invoke(annotation); + + assertMethodResult(aliasMethodResult, expectedValues); + + assertableResult = aliasMethodResult; + } catch (IllegalAccessException | InvocationTargetException e1) { + softly.fail(ACCESS_OR_INVOCATION_EXCEPTION_MESSAGE, aliasMethod.getName()); + } catch (AssertionError e1) { + // noop + } + } + } + + if (Object[].class.isInstance(assertableResult)) { + // this produces readable descriptions on its own + // all and only defined values must be returned in defined order + softly.assertThat((Object[]) assertableResult).containsExactlyElementsOf(Arrays.asList(expectedValues)); + } else { + // this produces readable descriptions on its own + softly.assertThat(assertableResult).isEqualTo(expectedValues[0]); + } + + validatedMethods.add(actualMethod.getName()); + if (aliasForAnnotation.isPresent()) { + validatedMethods.add(aliasMethod.getName()); + } + } + + return validatedMethods; + } + + + private Method getAliasMethod(Annotation originalAnnotation, Annotation aliasForAnnotation, String methodName) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Method annotationMethod = aliasForAnnotation.annotationType().getDeclaredMethod("annotation"); + Class annotationValue = + (Class) annotationMethod.invoke(aliasForAnnotation); + + String aliasMethodName; + if (!annotationValue.equals(annotationMethod.getDefaultValue())) { + aliasMethodName = aliasForAnnotation + .annotationType() + .getDeclaredMethod("attribute") + .invoke(aliasForAnnotation) + .toString(); + + if (StringUtils.isEmpty(aliasMethodName)) { + aliasMethodName = methodName; + } + + return annotationValue.getDeclaredMethod(aliasMethodName); + } else { + aliasMethodName = aliasForAnnotation + .annotationType() + .getDeclaredMethod("value") + .invoke(aliasForAnnotation) + .toString(); + + return originalAnnotation.annotationType().getDeclaredMethod(aliasMethodName); + } + } + + + private void assertMethodResult(Object actualValues, Object[] expectedValues) { + if (Object[].class.isInstance(actualValues)) { + // this produces readable descriptions on its own + // all and only defined values must be returned in defined order + assertThat((Object[]) actualValues).containsExactlyElementsOf(Arrays.asList(expectedValues)); + } else { + // this produces readable descriptions on its own + assertThat(actualValues).isEqualTo(expectedValues[0]); + } + } + + + private Optional getAliasNameIfPresent(Method method) { + return Arrays.stream(method.getDeclaredAnnotations()) + .filter(annotationFound -> annotationFound.annotationType().getName().endsWith("AliasFor")) + .findAny(); + } + +} diff --git a/src/main/java/de/tolina/common/validation/AnnotationValidator.java b/src/main/java/de/jakop/validation/annotations/AnnotationValidator.java similarity index 93% rename from src/main/java/de/tolina/common/validation/AnnotationValidator.java rename to src/main/java/de/jakop/validation/annotations/AnnotationValidator.java index 18cac22..f78e58f 100644 --- a/src/main/java/de/tolina/common/validation/AnnotationValidator.java +++ b/src/main/java/de/jakop/validation/annotations/AnnotationValidator.java @@ -1,49 +1,51 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -import java.util.HashSet; - -import javax.annotation.Nonnull; - -/** - * Annotation Validation - */ -public class AnnotationValidator { - - private AnnotationValidator() { - // only via factory method - } - - /** - * Returns an AnnotationValidation - *
- uses SoftAssertions - *
- validates all given parameters for an Annotation and fails if there are differences - *
- respects parameter aliases - * - * @return ClassAnnotationValidator to validate for a specific Class - */ - @Nonnull - public static AnnotationValidation validate() { - final HashSet paramBlacklist = new HashSet<>(); - paramBlacklist.add("equals"); - paramBlacklist.add("toString"); - paramBlacklist.add("hashCode"); - paramBlacklist.add("annotationType"); - - return new AnnotationValidation(paramBlacklist); - } -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +import java.util.HashSet; + +import javax.annotation.Nonnull; + +/** + * Annotation Validation + */ +public class AnnotationValidator { + + private AnnotationValidator() { + // only via factory method + } + + /** + * Returns an AnnotationValidation + *
- uses SoftAssertions + *
- validates all given parameters for an Annotation and fails if there are differences + *
- respects parameter aliases + * + * @return ClassAnnotationValidator to validate for a specific Class + */ + @Nonnull + public static AnnotationValidation validate() { + final HashSet paramBlacklist = new HashSet<>(); + paramBlacklist.add("equals"); + paramBlacklist.add("toString"); + paramBlacklist.add("hashCode"); + paramBlacklist.add("annotationType"); + + return new AnnotationValidation(paramBlacklist); + } +} diff --git a/src/main/java/de/tolina/common/validation/SoftAssertionErrorWithObjectDetails.java b/src/main/java/de/jakop/validation/annotations/SoftAssertionErrorWithObjectDetails.java similarity index 93% rename from src/main/java/de/tolina/common/validation/SoftAssertionErrorWithObjectDetails.java rename to src/main/java/de/jakop/validation/annotations/SoftAssertionErrorWithObjectDetails.java index a978ea5..6293ad1 100644 --- a/src/main/java/de/tolina/common/validation/SoftAssertionErrorWithObjectDetails.java +++ b/src/main/java/de/jakop/validation/annotations/SoftAssertionErrorWithObjectDetails.java @@ -1,39 +1,41 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -/* - * (c) tolina GmbH, 2017 - */ -package de.tolina.common.validation; - -import java.util.List; - -import org.assertj.core.api.SoftAssertionError; - -final class SoftAssertionErrorWithObjectDetails extends SoftAssertionError { - private static final long serialVersionUID = 5527685338875086360L; - private final String annotatedObject; - - SoftAssertionErrorWithObjectDetails(final List errors, final Object annotatedObject) { - super(errors); - this.annotatedObject = annotatedObject.toString(); - } - - // if you are in a test loop and do not know witch object is under test, let's add this information to errormessage - @Override - public String getMessage() { - return "\nError on Validating " + annotatedObject + "\n" + super.getMessage(); - } -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +/* + * (c) tolina GmbH, 2017 + */ +package de.jakop.validation.annotations; + +import java.util.List; + +import org.assertj.core.api.SoftAssertionError; + +final class SoftAssertionErrorWithObjectDetails extends SoftAssertionError { + private static final long serialVersionUID = 5527685338875086360L; + private final String annotatedObject; + + SoftAssertionErrorWithObjectDetails(final List errors, final Object annotatedObject) { + super(errors); + this.annotatedObject = annotatedObject.toString(); + } + + // if you are in a test loop and do not know witch object is under test, let's add this information to errormessage + @Override + public String getMessage() { + return "\nError on Validating " + annotatedObject + "\n" + super.getMessage(); + } +} diff --git a/src/main/java/de/tolina/common/validation/ValidationMode.java b/src/main/java/de/jakop/validation/annotations/ValidationMode.java similarity index 88% rename from src/main/java/de/tolina/common/validation/ValidationMode.java rename to src/main/java/de/jakop/validation/annotations/ValidationMode.java index 228d47d..3ddb87d 100644 --- a/src/main/java/de/tolina/common/validation/ValidationMode.java +++ b/src/main/java/de/jakop/validation/annotations/ValidationMode.java @@ -1,21 +1,22 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ - -package de.tolina.common.validation; - -public enum ValidationMode { - DEFAULT, ONLY, EXACTLY -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +public enum ValidationMode { + DEFAULT, ONLY, EXACTLY +} diff --git a/src/main/resources/APACHE-2-with-modifications.txt b/src/main/resources/APACHE-2-with-modifications.txt new file mode 100644 index 0000000..e988120 --- /dev/null +++ b/src/main/resources/APACHE-2-with-modifications.txt @@ -0,0 +1,15 @@ +Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + +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. + +Modifications copyright (C) ${project.inceptionYear} ${owner} diff --git a/src/test/java/de/tolina/common/validation/AliasTestAnnotation.java b/src/test/java/de/jakop/validation/annotations/AliasTestAnnotation.java similarity index 78% rename from src/test/java/de/tolina/common/validation/AliasTestAnnotation.java rename to src/test/java/de/jakop/validation/annotations/AliasTestAnnotation.java index 1327929..27130fb 100644 --- a/src/test/java/de/tolina/common/validation/AliasTestAnnotation.java +++ b/src/test/java/de/jakop/validation/annotations/AliasTestAnnotation.java @@ -1,35 +1,35 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -import org.springframework.core.annotation.AliasFor; - -import java.lang.annotation.Retention; - -import static de.tolina.common.validation.TestEnum.TEST; -import static de.tolina.common.validation.TestEnum.TEST2; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -@Retention(RUNTIME) -@SuppressWarnings("javadoc") -@AnotherTestAnnotation -public @interface AliasTestAnnotation { - @AliasFor(annotation = AnotherTestAnnotation.class, attribute = "testEnum") - TestEnum referencedTestEnum() default TEST; - - @AliasFor(annotation = AnotherTestAnnotation.class) - TestEnum anotherValue() default TEST2; -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +import org.springframework.core.annotation.AliasFor; + +import java.lang.annotation.Retention; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Retention(RUNTIME) +@SuppressWarnings("javadoc") +@AnotherTestAnnotation +public @interface AliasTestAnnotation { + @AliasFor(annotation = AnotherTestAnnotation.class, attribute = "testEnum") + TestEnum referencedTestEnum() default TestEnum.TEST; + + @AliasFor(annotation = AnotherTestAnnotation.class) + TestEnum anotherValue() default TestEnum.TEST2; +} diff --git a/src/test/java/de/tolina/common/validation/AnnotatedAbstractTestClass.java b/src/test/java/de/jakop/validation/annotations/AnnotatedAbstractTestClass.java similarity index 92% rename from src/test/java/de/tolina/common/validation/AnnotatedAbstractTestClass.java rename to src/test/java/de/jakop/validation/annotations/AnnotatedAbstractTestClass.java index 2fe6f93..2f4a63d 100644 --- a/src/test/java/de/tolina/common/validation/AnnotatedAbstractTestClass.java +++ b/src/test/java/de/jakop/validation/annotations/AnnotatedAbstractTestClass.java @@ -1,31 +1,33 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -@AnnotatedAbstractTestClassAnnotation -@SuppressWarnings("javadoc") -abstract class AnnotatedAbstractTestClass implements AnnotatedTestInterface, AnnotatedTestInterfaceForAbstractClass { - @AnnotatedAbstractTestClassAnnotation - protected abstract void annotatedAbstractMethod(); - - protected void annotatedAbstractMethod(final String param) { - - }; - - protected String annotatedAbstractMethod(final Object param) { - return ""; - }; -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +@AnnotatedAbstractTestClassAnnotation +@SuppressWarnings("javadoc") +abstract class AnnotatedAbstractTestClass implements AnnotatedTestInterface, AnnotatedTestInterfaceForAbstractClass { + @AnnotatedAbstractTestClassAnnotation + protected abstract void annotatedAbstractMethod(); + + protected void annotatedAbstractMethod(final String param) { + + }; + + protected String annotatedAbstractMethod(final Object param) { + return ""; + }; +} diff --git a/src/test/java/de/tolina/common/validation/AnnotatedAbstractTestClassAnnotation.java b/src/test/java/de/jakop/validation/annotations/AnnotatedAbstractTestClassAnnotation.java similarity index 90% rename from src/test/java/de/tolina/common/validation/AnnotatedAbstractTestClassAnnotation.java rename to src/test/java/de/jakop/validation/annotations/AnnotatedAbstractTestClassAnnotation.java index da906c8..17e50eb 100644 --- a/src/test/java/de/tolina/common/validation/AnnotatedAbstractTestClassAnnotation.java +++ b/src/test/java/de/jakop/validation/annotations/AnnotatedAbstractTestClassAnnotation.java @@ -1,25 +1,27 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Retention; - -@Retention(RUNTIME) -@SuppressWarnings("javadoc") -@interface AnnotatedAbstractTestClassAnnotation { -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; + +@Retention(RUNTIME) +@SuppressWarnings("javadoc") +@interface AnnotatedAbstractTestClassAnnotation { +} diff --git a/src/test/java/de/tolina/common/validation/AnnotatedTestClass.java b/src/test/java/de/jakop/validation/annotations/AnnotatedTestClass.java similarity index 89% rename from src/test/java/de/tolina/common/validation/AnnotatedTestClass.java rename to src/test/java/de/jakop/validation/annotations/AnnotatedTestClass.java index b679ce4..343d4d1 100644 --- a/src/test/java/de/tolina/common/validation/AnnotatedTestClass.java +++ b/src/test/java/de/jakop/validation/annotations/AnnotatedTestClass.java @@ -1,73 +1,75 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -import static de.tolina.common.validation.TestEnum.TEST; -import static de.tolina.common.validation.TestEnum.TEST2; - -@TestAnnotation -@SuppressWarnings("javadoc") -class AnnotatedTestClass extends AnnotatedAbstractTestClass implements AnnotatedTestInterface { - - @TestAnnotation(testparameter = "testvalue") - private String fieldWithAnnotations; - - String fieldWithoutAnnotations; - - @TestAnnotation(testparameter = "testvalue", anotherTestParameter = "anotherTestValue") - @AnotherTestAnnotation(TEST2) - public void methodWithAnnotations() { - // noop - } - - @AliasTestAnnotation(referencedTestEnum = TEST2) - public void methodWithAliasAnnotations() { - // noop - } - - @AliasTestAnnotation(anotherValue = TEST) - public void methodWithAnOtherAliasAnnotations() { - // noop - } - - public void methodWithoutAnnotations() { - // noop - } - - @Override - public void annotatedInterfaceMethod() { - // noop - } - - @Override - public void annotatedInterfaceMethodForAbstractClass() { - // noop - } - - @Override - protected void annotatedAbstractMethod() { - // noop - } - - @AnotherTestAnnotation(TEST) - public void overloadedMethod(final String foo, final String bar) { - // noop - } - - public void overloadedMethod(final String foo, final int bar) { - // noop - } -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +import static de.jakop.validation.annotations.TestEnum.TEST; +import static de.jakop.validation.annotations.TestEnum.TEST2; + +@TestAnnotation +@SuppressWarnings("javadoc") +class AnnotatedTestClass extends AnnotatedAbstractTestClass implements AnnotatedTestInterface { + + @TestAnnotation(testparameter = "testvalue") + private String fieldWithAnnotations; + + String fieldWithoutAnnotations; + + @TestAnnotation(testparameter = "testvalue", anotherTestParameter = "anotherTestValue") + @AnotherTestAnnotation(TEST2) + public void methodWithAnnotations() { + // noop + } + + @AliasTestAnnotation(referencedTestEnum = TEST2) + public void methodWithAliasAnnotations() { + // noop + } + + @AliasTestAnnotation(anotherValue = TEST) + public void methodWithAnOtherAliasAnnotations() { + // noop + } + + public void methodWithoutAnnotations() { + // noop + } + + @Override + public void annotatedInterfaceMethod() { + // noop + } + + @Override + public void annotatedInterfaceMethodForAbstractClass() { + // noop + } + + @Override + protected void annotatedAbstractMethod() { + // noop + } + + @AnotherTestAnnotation(TEST) + public void overloadedMethod(final String foo, final String bar) { + // noop + } + + public void overloadedMethod(final String foo, final int bar) { + // noop + } +} diff --git a/src/test/java/de/tolina/common/validation/AnnotatedTestInterface.java b/src/test/java/de/jakop/validation/annotations/AnnotatedTestInterface.java similarity index 89% rename from src/test/java/de/tolina/common/validation/AnnotatedTestInterface.java rename to src/test/java/de/jakop/validation/annotations/AnnotatedTestInterface.java index a522076..d60607a 100644 --- a/src/test/java/de/tolina/common/validation/AnnotatedTestInterface.java +++ b/src/test/java/de/jakop/validation/annotations/AnnotatedTestInterface.java @@ -1,23 +1,25 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -@AnnotatedTestInterfaceAnnotation -@SuppressWarnings("javadoc") -interface AnnotatedTestInterface { - @AnnotatedTestInterfaceAnnotation - void annotatedInterfaceMethod(); -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +@AnnotatedTestInterfaceAnnotation +@SuppressWarnings("javadoc") +interface AnnotatedTestInterface { + @AnnotatedTestInterfaceAnnotation + void annotatedInterfaceMethod(); +} diff --git a/src/test/java/de/tolina/common/validation/AnnotatedTestInterfaceAnnotation.java b/src/test/java/de/jakop/validation/annotations/AnnotatedTestInterfaceAnnotation.java similarity index 90% rename from src/test/java/de/tolina/common/validation/AnnotatedTestInterfaceAnnotation.java rename to src/test/java/de/jakop/validation/annotations/AnnotatedTestInterfaceAnnotation.java index da2f893..1f9b160 100644 --- a/src/test/java/de/tolina/common/validation/AnnotatedTestInterfaceAnnotation.java +++ b/src/test/java/de/jakop/validation/annotations/AnnotatedTestInterfaceAnnotation.java @@ -1,25 +1,27 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Retention; - -@Retention(RUNTIME) -@SuppressWarnings("javadoc") -@interface AnnotatedTestInterfaceAnnotation { -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; + +@Retention(RUNTIME) +@SuppressWarnings("javadoc") +@interface AnnotatedTestInterfaceAnnotation { +} diff --git a/src/test/java/de/tolina/common/validation/AnnotatedTestInterfaceForAbstractClass.java b/src/test/java/de/jakop/validation/annotations/AnnotatedTestInterfaceForAbstractClass.java similarity index 90% rename from src/test/java/de/tolina/common/validation/AnnotatedTestInterfaceForAbstractClass.java rename to src/test/java/de/jakop/validation/annotations/AnnotatedTestInterfaceForAbstractClass.java index 3ac6d78..020304c 100644 --- a/src/test/java/de/tolina/common/validation/AnnotatedTestInterfaceForAbstractClass.java +++ b/src/test/java/de/jakop/validation/annotations/AnnotatedTestInterfaceForAbstractClass.java @@ -1,23 +1,25 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -@AnnotatedTestInterfaceForAbstractClassAnnotation -@SuppressWarnings("javadoc") -interface AnnotatedTestInterfaceForAbstractClass { - @AnnotatedTestInterfaceForAbstractClassAnnotation - void annotatedInterfaceMethodForAbstractClass(); -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +@AnnotatedTestInterfaceForAbstractClassAnnotation +@SuppressWarnings("javadoc") +interface AnnotatedTestInterfaceForAbstractClass { + @AnnotatedTestInterfaceForAbstractClassAnnotation + void annotatedInterfaceMethodForAbstractClass(); +} diff --git a/src/test/java/de/tolina/common/validation/AnnotatedTestInterfaceForAbstractClassAnnotation.java b/src/test/java/de/jakop/validation/annotations/AnnotatedTestInterfaceForAbstractClassAnnotation.java similarity index 90% rename from src/test/java/de/tolina/common/validation/AnnotatedTestInterfaceForAbstractClassAnnotation.java rename to src/test/java/de/jakop/validation/annotations/AnnotatedTestInterfaceForAbstractClassAnnotation.java index 01be516..9e369bf 100644 --- a/src/test/java/de/tolina/common/validation/AnnotatedTestInterfaceForAbstractClassAnnotation.java +++ b/src/test/java/de/jakop/validation/annotations/AnnotatedTestInterfaceForAbstractClassAnnotation.java @@ -1,25 +1,27 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Retention; - -@Retention(RUNTIME) -@SuppressWarnings("javadoc") -@interface AnnotatedTestInterfaceForAbstractClassAnnotation { -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; + +@Retention(RUNTIME) +@SuppressWarnings("javadoc") +@interface AnnotatedTestInterfaceForAbstractClassAnnotation { +} diff --git a/src/test/java/de/tolina/common/validation/AnnotationValidationTest.java b/src/test/java/de/jakop/validation/annotations/AnnotationValidationTest.java similarity index 64% rename from src/test/java/de/tolina/common/validation/AnnotationValidationTest.java rename to src/test/java/de/jakop/validation/annotations/AnnotationValidationTest.java index f6278cc..f48dce2 100644 --- a/src/test/java/de/tolina/common/validation/AnnotationValidationTest.java +++ b/src/test/java/de/jakop/validation/annotations/AnnotationValidationTest.java @@ -1,266 +1,266 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import java.lang.reflect.Method; - -import static de.tolina.common.validation.AnnotationDefinition.type; -import static de.tolina.common.validation.AnnotationValidator.validate; -import static de.tolina.common.validation.TestEnum.TEST; -import static de.tolina.common.validation.TestEnum.TEST2; - -/** - * Test for the {@link AnnotationValidator} - */ -public class AnnotationValidationTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - - @Test - public void testValidateAnnotatedClass_exactly_defaultsAreNotEvaluated() throws NoSuchMethodException { - validate().exactly() // - .annotation(type(TestAnnotation.class) // - .param("testparameter", "default") // - .param("anotherTestParameter", "one", "two")) // - .annotation(type(AnnotatedTestInterfaceAnnotation.class)) // - .annotation(type(AnnotatedTestInterfaceForAbstractClassAnnotation.class)) // - .annotation(type(AnnotatedAbstractTestClassAnnotation.class)) // - .forClass(AnnotatedTestClass.class); - } - - @Test - public void testValidateAnnotatedClass_only_defaultsAreEvaluated() throws NoSuchMethodException { - validate().only() // - .annotation(type(TestAnnotation.class)) // - .annotation(type(AnnotatedTestInterfaceAnnotation.class)) // - .annotation(type(AnnotatedTestInterfaceForAbstractClassAnnotation.class)) // - .annotation(type(AnnotatedAbstractTestClassAnnotation.class)) // - .forClass(AnnotatedTestClass.class); - } - - - @Test - public void testValidateAnnotatedClass_NotExactlyAndNoAnnotationsValidated() throws NoSuchMethodException { - thrown.expect(AssertionError.class); - thrown.expectMessage("Please add at least one Annotation to assert or enable strict validation."); - validate() // - .forClass(AnnotatedTestClass.class); - } - - - @Test - public void testValidateAnnotatedClass_NotExactly() throws NoSuchMethodException { - validate() // - .annotation(type(AnnotatedTestInterfaceAnnotation.class)) // - .forClass(AnnotatedTestClass.class); - } - - - @Test - public void testValidateAnnotatedClass_NoSuchAnnotationMethod() throws NoSuchMethodException { - thrown.expect(AssertionError.class); - thrown.expectMessage("Method noSuchMethod not found"); - - validate().exactly() // - .annotation(type(TestAnnotation.class) // - .param("noSuchMethod", "default")) // - .forClass(AnnotatedTestClass.class); - } - - - @Test - public void testValidateAnnotatedMethod_exactly_defaultsAreNotEvaluated() throws NoSuchMethodException { - validate().only() // - .annotation(type(TestAnnotation.class) // - .param("testparameter", "testvalue") // - .param("anotherTestParameter", "anotherTestValue")) // - .annotation(type(AnotherTestAnnotation.class) // - .param("testEnum", TEST2)) // - .forMethod(AnnotatedTestClass.class.getMethod("methodWithAnnotations")); - } - - - @Test - public void testValidateAnnotatedMethod_only_defaultsAreEvaluated() throws NoSuchMethodException { - validate().only() // - .annotation(type(TestAnnotation.class) // - .param("testparameter", "testvalue") // - .param("anotherTestParameter", "anotherTestValue")) // - .annotation(type(AnotherTestAnnotation.class) // - .param("testEnum", TEST2)) // - .forMethod(AnnotatedTestClass.class.getMethod("methodWithAnnotations")); - } - - - @Test - public void testValidateAnnotatedMethod_NotAllAnnotationMethodsDefinedInAnnotationDefinition() - throws NoSuchMethodException { - thrown.expect(AssertionError.class); - thrown.expectMessage("Unexpected value for Method 'testparameter' found"); - - validate() // - .annotation(type(TestAnnotation.class) // - .param("anotherTestParameter", "anotherTestValue")) // - .forMethod(AnnotatedTestClass.class.getMethod("methodWithAnnotations")); - } - - - @Test - public void testValidateAnnotatedMethod_NotAllAnnotationMethodsDefinedInAnnotationDefinition_NonStringValues() - throws NoSuchMethodException { - validate() // - .annotation(type(AnotherTestAnnotation.class).param("value", TEST2)) // - .forMethod(AnnotatedTestClass.class.getMethod("methodWithAnnotations")); - } - - - @Test - public void testValidateAnnotatedMethod_OverloadedMethod() throws NoSuchMethodException { - validate().only() // - .annotation(type(AnotherTestAnnotation.class) // - .param("testEnum", TEST) // - .param("value", TEST)) // - .forMethod(AnnotatedTestClass.class.getMethod("overloadedMethod", String.class, String.class)); - } - - - @Test - public void testValidateAnnotatedMethod_UseAlias() throws NoSuchMethodException { - validate().only() // - .annotation(type(AliasTestAnnotation.class) // - .param("referencedTestEnum", TEST2)) // - .forMethod(AnnotatedTestClass.class.getMethod("methodWithAliasAnnotations")); - } - - - @Test - public void testValidateAnnotatedMethod_UseAlias_WithoutAttribute() throws NoSuchMethodException { - validate().only() // - .annotation(type(AliasTestAnnotation.class) // - .param("anotherValue", TEST)) // - .forMethod(AnnotatedTestClass.class.getMethod("methodWithAnOtherAliasAnnotations")); - } - - - @Test - public void testValidateAnnotatedInterfaceMethod() throws NoSuchMethodException { - validate().exactly() // - .annotation(type(AnnotatedTestInterfaceAnnotation.class)) // - .forMethod(AnnotatedTestClass.class.getMethod("annotatedInterfaceMethod")); - } - - - @Test - public void testValidateAnnotatedInterfaceMethodFromSuperclass() throws NoSuchMethodException { - validate().exactly() // - .annotation(type(AnnotatedTestInterfaceForAbstractClassAnnotation.class)) // - .forMethod(AnnotatedTestClass.class.getMethod("annotatedInterfaceMethodForAbstractClass")); - } - - - @Test - public void testValidateAnnotatedAbstractMethodFromSuperclass() throws NoSuchMethodException { - validate().exactly() // - .annotation(type(AnnotatedAbstractTestClassAnnotation.class)) // - .forMethod(AnnotatedTestClass.class.getDeclaredMethod("annotatedAbstractMethod")); - } - - - @Test - public void testValidateAnnotatedField_exactly_defaultsAreNotEvaluated() throws NoSuchFieldException { - validate().exactly() // - .annotation(type(TestAnnotation.class) // - .param("testparameter", "testvalue") - .param("anotherTestParameter", "one", "two")) // - .forField(AnnotatedTestClass.class.getDeclaredField("fieldWithAnnotations")); - } - - - @Test - public void testValidateAnnotatedField_only_defaultsAreEvaluated() throws NoSuchFieldException { - validate().only() // - .annotation(type(TestAnnotation.class) // - .param("testparameter", "testvalue")) // - .forField(AnnotatedTestClass.class.getDeclaredField("fieldWithAnnotations")); - } - - - @Test - public void testValidateMethod() throws NoSuchMethodException { - validate().exactly()// - .forMethod(AnnotatedTestClass.class.getMethod("methodWithoutAnnotations")); - } - - - @Test - public void testValidateMethod_AnnotationNotPresent() throws NoSuchMethodException { - thrown.expect(AssertionError.class); - thrown.expectMessage("Expected Annotation de.tolina.common.validation.TestAnnotation not found"); - validate().exactly()// - .annotation(type(TestAnnotation.class)) // - .forMethod(AnnotatedTestClass.class.getMethod("methodWithoutAnnotations")); - } - - - @Test - public void testValidateMethod_NoSuchMethod() throws NoSuchMethodException { - thrown.expect(NoSuchMethodException.class); - validate().exactly()// - .forMethod(AnnotatedTestClass.class.getMethod("noSuchMethod")); - } - - - @Test - public void testValidateField() throws NoSuchFieldException { - validate().exactly().forField(AnnotatedTestClass.class.getDeclaredField("fieldWithoutAnnotations")); - } - - - @Test - public void testValidateLambdas() throws Exception { - TestInterface test1 = TestInterface::staticMethod; - Method annotatedMethod1 = test1.getClass().getMethod("method"); - validate().annotation(type(Deprecated.class)).forMethod(annotatedMethod1); - - TestInterface test2 = test1::defaultMethod; - Method annotatedMethod2 = test2.getClass().getMethod("defaultMethod"); - validate().annotation(type(Deprecated.class)).forMethod(annotatedMethod2); - } - - - interface TestInterface { - - @Deprecated - static void staticMethod() { - System.out.println("staticMethod"); - } - - @Deprecated - void method(); - - @Deprecated - default void defaultMethod() { - System.out.println("defaultMethod"); - - } - } -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.lang.reflect.Method; + +import static de.jakop.validation.annotations.TestEnum.TEST; +import static de.jakop.validation.annotations.TestEnum.TEST2; + +/** + * Test for the {@link AnnotationValidator} + */ +public class AnnotationValidationTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + + @Test + public void testValidateAnnotatedClass_exactly_defaultsAreNotEvaluated() throws NoSuchMethodException { + AnnotationValidator.validate().exactly() // + .annotation(AnnotationDefinition.type(TestAnnotation.class) // + .param("testparameter", "default") // + .param("anotherTestParameter", "one", "two")) // + .annotation(AnnotationDefinition.type(AnnotatedTestInterfaceAnnotation.class)) // + .annotation(AnnotationDefinition.type(AnnotatedTestInterfaceForAbstractClassAnnotation.class)) // + .annotation(AnnotationDefinition.type(AnnotatedAbstractTestClassAnnotation.class)) // + .forClass(AnnotatedTestClass.class); + } + + @Test + public void testValidateAnnotatedClass_only_defaultsAreEvaluated() throws NoSuchMethodException { + AnnotationValidator.validate().only() // + .annotation(AnnotationDefinition.type(TestAnnotation.class)) // + .annotation(AnnotationDefinition.type(AnnotatedTestInterfaceAnnotation.class)) // + .annotation(AnnotationDefinition.type(AnnotatedTestInterfaceForAbstractClassAnnotation.class)) // + .annotation(AnnotationDefinition.type(AnnotatedAbstractTestClassAnnotation.class)) // + .forClass(AnnotatedTestClass.class); + } + + + @Test + public void testValidateAnnotatedClass_NotExactlyAndNoAnnotationsValidated() throws NoSuchMethodException { + thrown.expect(AssertionError.class); + thrown.expectMessage("Please add at least one Annotation to assert or enable strict validation."); + AnnotationValidator.validate() // + .forClass(AnnotatedTestClass.class); + } + + + @Test + public void testValidateAnnotatedClass_NotExactly() throws NoSuchMethodException { + AnnotationValidator.validate() // + .annotation(AnnotationDefinition.type(AnnotatedTestInterfaceAnnotation.class)) // + .forClass(AnnotatedTestClass.class); + } + + + @Test + public void testValidateAnnotatedClass_NoSuchAnnotationMethod() throws NoSuchMethodException { + thrown.expect(AssertionError.class); + thrown.expectMessage("Method noSuchMethod not found"); + + AnnotationValidator.validate().exactly() // + .annotation(AnnotationDefinition.type(TestAnnotation.class) // + .param("noSuchMethod", "default")) // + .forClass(AnnotatedTestClass.class); + } + + + @Test + public void testValidateAnnotatedMethod_exactly_defaultsAreNotEvaluated() throws NoSuchMethodException { + AnnotationValidator.validate().only() // + .annotation(AnnotationDefinition.type(TestAnnotation.class) // + .param("testparameter", "testvalue") // + .param("anotherTestParameter", "anotherTestValue")) // + .annotation(AnnotationDefinition.type(AnotherTestAnnotation.class) // + .param("testEnum", TEST2)) // + .forMethod(AnnotatedTestClass.class.getMethod("methodWithAnnotations")); + } + + + @Test + public void testValidateAnnotatedMethod_only_defaultsAreEvaluated() throws NoSuchMethodException { + AnnotationValidator.validate().only() // + .annotation(AnnotationDefinition.type(TestAnnotation.class) // + .param("testparameter", "testvalue") // + .param("anotherTestParameter", "anotherTestValue")) // + .annotation(AnnotationDefinition.type(AnotherTestAnnotation.class) // + .param("testEnum", TEST2)) // + .forMethod(AnnotatedTestClass.class.getMethod("methodWithAnnotations")); + } + + + @Test + public void testValidateAnnotatedMethod_NotAllAnnotationMethodsDefinedInAnnotationDefinition() + throws NoSuchMethodException { + thrown.expect(AssertionError.class); + thrown.expectMessage("Unexpected value for Method 'testparameter' found"); + + AnnotationValidator.validate() // + .annotation(AnnotationDefinition.type(TestAnnotation.class) // + .param("anotherTestParameter", "anotherTestValue")) // + .forMethod(AnnotatedTestClass.class.getMethod("methodWithAnnotations")); + } + + + @Test + public void testValidateAnnotatedMethod_NotAllAnnotationMethodsDefinedInAnnotationDefinition_NonStringValues() + throws NoSuchMethodException { + AnnotationValidator.validate() // + .annotation(AnnotationDefinition.type(AnotherTestAnnotation.class).param("value", TEST2)) // + .forMethod(AnnotatedTestClass.class.getMethod("methodWithAnnotations")); + } + + + @Test + public void testValidateAnnotatedMethod_OverloadedMethod() throws NoSuchMethodException { + AnnotationValidator.validate().only() // + .annotation(AnnotationDefinition.type(AnotherTestAnnotation.class) // + .param("testEnum", TEST) // + .param("value", TEST)) // + .forMethod(AnnotatedTestClass.class.getMethod("overloadedMethod", String.class, String.class)); + } + + + @Test + public void testValidateAnnotatedMethod_UseAlias() throws NoSuchMethodException { + AnnotationValidator.validate().only() // + .annotation(AnnotationDefinition.type(AliasTestAnnotation.class) // + .param("referencedTestEnum", TEST2)) // + .forMethod(AnnotatedTestClass.class.getMethod("methodWithAliasAnnotations")); + } + + + @Test + public void testValidateAnnotatedMethod_UseAlias_WithoutAttribute() throws NoSuchMethodException { + AnnotationValidator.validate().only() // + .annotation(AnnotationDefinition.type(AliasTestAnnotation.class) // + .param("anotherValue", TEST)) // + .forMethod(AnnotatedTestClass.class.getMethod("methodWithAnOtherAliasAnnotations")); + } + + + @Test + public void testValidateAnnotatedInterfaceMethod() throws NoSuchMethodException { + AnnotationValidator.validate().exactly() // + .annotation(AnnotationDefinition.type(AnnotatedTestInterfaceAnnotation.class)) // + .forMethod(AnnotatedTestClass.class.getMethod("annotatedInterfaceMethod")); + } + + + @Test + public void testValidateAnnotatedInterfaceMethodFromSuperclass() throws NoSuchMethodException { + AnnotationValidator.validate().exactly() // + .annotation(AnnotationDefinition.type(AnnotatedTestInterfaceForAbstractClassAnnotation.class)) // + .forMethod(AnnotatedTestClass.class.getMethod("annotatedInterfaceMethodForAbstractClass")); + } + + + @Test + public void testValidateAnnotatedAbstractMethodFromSuperclass() throws NoSuchMethodException { + AnnotationValidator.validate().exactly() // + .annotation(AnnotationDefinition.type(AnnotatedAbstractTestClassAnnotation.class)) // + .forMethod(AnnotatedTestClass.class.getDeclaredMethod("annotatedAbstractMethod")); + } + + + @Test + public void testValidateAnnotatedField_exactly_defaultsAreNotEvaluated() throws NoSuchFieldException { + AnnotationValidator.validate().exactly() // + .annotation(AnnotationDefinition.type(TestAnnotation.class) // + .param("testparameter", "testvalue") + .param("anotherTestParameter", "one", "two")) // + .forField(AnnotatedTestClass.class.getDeclaredField("fieldWithAnnotations")); + } + + + @Test + public void testValidateAnnotatedField_only_defaultsAreEvaluated() throws NoSuchFieldException { + AnnotationValidator.validate().only() // + .annotation(AnnotationDefinition.type(TestAnnotation.class) // + .param("testparameter", "testvalue")) // + .forField(AnnotatedTestClass.class.getDeclaredField("fieldWithAnnotations")); + } + + + @Test + public void testValidateMethod() throws NoSuchMethodException { + AnnotationValidator.validate().exactly()// + .forMethod(AnnotatedTestClass.class.getMethod("methodWithoutAnnotations")); + } + + + @Test + public void testValidateMethod_AnnotationNotPresent() throws NoSuchMethodException { + thrown.expect(AssertionError.class); + thrown.expectMessage("Expected Annotation de.jakop.validation.annotations.TestAnnotation not found"); + AnnotationValidator.validate().exactly()// + .annotation(AnnotationDefinition.type(TestAnnotation.class)) // + .forMethod(AnnotatedTestClass.class.getMethod("methodWithoutAnnotations")); + } + + + @Test + public void testValidateMethod_NoSuchMethod() throws NoSuchMethodException { + thrown.expect(NoSuchMethodException.class); + AnnotationValidator.validate().exactly()// + .forMethod(AnnotatedTestClass.class.getMethod("noSuchMethod")); + } + + + @Test + public void testValidateField() throws NoSuchFieldException { + AnnotationValidator.validate().exactly().forField(AnnotatedTestClass.class.getDeclaredField("fieldWithoutAnnotations")); + } + + + @Test + public void testValidateLambdas() throws Exception { + TestInterface test1 = TestInterface::staticMethod; + Method annotatedMethod1 = test1.getClass().getMethod("method"); + AnnotationValidator.validate().annotation(AnnotationDefinition.type(Deprecated.class)).forMethod(annotatedMethod1); + + TestInterface test2 = test1::defaultMethod; + Method annotatedMethod2 = test2.getClass().getMethod("defaultMethod"); + AnnotationValidator.validate().annotation(AnnotationDefinition.type(Deprecated.class)).forMethod(annotatedMethod2); + } + + + interface TestInterface { + + @Deprecated + static void staticMethod() { + System.out.println("staticMethod"); + } + + @Deprecated + void method(); + + @Deprecated + default void defaultMethod() { + System.out.println("defaultMethod"); + + } + } +} diff --git a/src/test/java/de/tolina/common/validation/AnnotationValidatorTest.java b/src/test/java/de/jakop/validation/annotations/AnnotationValidatorTest.java similarity index 64% rename from src/test/java/de/tolina/common/validation/AnnotationValidatorTest.java rename to src/test/java/de/jakop/validation/annotations/AnnotationValidatorTest.java index c4b238d..c278cb4 100644 --- a/src/test/java/de/tolina/common/validation/AnnotationValidatorTest.java +++ b/src/test/java/de/jakop/validation/annotations/AnnotationValidatorTest.java @@ -1,31 +1,32 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -import static de.tolina.common.validation.AnnotationValidator.validate; -import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.junit.Assert.assertThat; - -import org.junit.Test; - -public class AnnotationValidatorTest { - @Test - public void testValidate_DefaultBlacklist() { - final AnnotationValidation validate = validate(); - assertThat(validate.paramBlacklist, containsInAnyOrder("equals", "toString", "hashCode", "annotationType")); - } - -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +import org.hamcrest.CoreMatchers; +import org.junit.Test; + +import static org.junit.Assert.assertThat; + +public class AnnotationValidatorTest { + @Test + public void testValidate_DefaultBlacklist() { + final AnnotationValidation validate = AnnotationValidator.validate(); + assertThat(validate.paramBlacklist, CoreMatchers.hasItems("equals", "toString", "hashCode", "annotationType")); + } + +} diff --git a/src/test/java/de/tolina/common/validation/AnotherTestAnnotation.java b/src/test/java/de/jakop/validation/annotations/AnotherTestAnnotation.java similarity index 83% rename from src/test/java/de/tolina/common/validation/AnotherTestAnnotation.java rename to src/test/java/de/jakop/validation/annotations/AnotherTestAnnotation.java index 68e9dd0..ee59645 100644 --- a/src/test/java/de/tolina/common/validation/AnotherTestAnnotation.java +++ b/src/test/java/de/jakop/validation/annotations/AnotherTestAnnotation.java @@ -1,37 +1,39 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -import org.springframework.core.annotation.AliasFor; - -import java.lang.annotation.Retention; - -import static de.tolina.common.validation.TestEnum.TEST; -import static de.tolina.common.validation.TestEnum.TEST2; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -@Retention(RUNTIME) -@SuppressWarnings("javadoc") -public @interface AnotherTestAnnotation { - - @AliasFor("testEnum") - TestEnum value() default TEST; - - @AliasFor("value") - TestEnum testEnum() default TEST; - - TestEnum anotherValue() default TEST2; -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +import org.springframework.core.annotation.AliasFor; + +import java.lang.annotation.Retention; + +import static de.jakop.validation.annotations.TestEnum.TEST; +import static de.jakop.validation.annotations.TestEnum.TEST2; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Retention(RUNTIME) +@SuppressWarnings("javadoc") +public @interface AnotherTestAnnotation { + + @AliasFor("testEnum") + TestEnum value() default TEST; + + @AliasFor("value") + TestEnum testEnum() default TEST; + + TestEnum anotherValue() default TEST2; +} diff --git a/src/test/java/de/tolina/common/validation/TestAnnotation.java b/src/test/java/de/jakop/validation/annotations/TestAnnotation.java similarity index 90% rename from src/test/java/de/tolina/common/validation/TestAnnotation.java rename to src/test/java/de/jakop/validation/annotations/TestAnnotation.java index 6f0ca9e..965bf10 100644 --- a/src/test/java/de/tolina/common/validation/TestAnnotation.java +++ b/src/test/java/de/jakop/validation/annotations/TestAnnotation.java @@ -1,28 +1,30 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Retention; - -@Retention(RUNTIME) -@SuppressWarnings("javadoc") -public @interface TestAnnotation { - String testparameter() default "default"; - - String[] anotherTestParameter() default { "one", "two" }; -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; + +@Retention(RUNTIME) +@SuppressWarnings("javadoc") +public @interface TestAnnotation { + String testparameter() default "default"; + + String[] anotherTestParameter() default { "one", "two" }; +} diff --git a/src/test/java/de/tolina/common/validation/TestEnum.java b/src/test/java/de/jakop/validation/annotations/TestEnum.java similarity index 88% rename from src/test/java/de/tolina/common/validation/TestEnum.java rename to src/test/java/de/jakop/validation/annotations/TestEnum.java index 100d154..9414aa7 100644 --- a/src/test/java/de/tolina/common/validation/TestEnum.java +++ b/src/test/java/de/jakop/validation/annotations/TestEnum.java @@ -1,21 +1,23 @@ -/** - * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) - * - * 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. - */ -package de.tolina.common.validation; - -@SuppressWarnings("javadoc") -public enum TestEnum { - TEST, TEST2 -} +/** + * Copyright © 2016 arxes-tolina GmbH (entwicklung@arxes-tolina.de) + * + * 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. + * + * Modifications copyright (C) 2020 Frank Jakop + */ +package de.jakop.validation.annotations; + +@SuppressWarnings("javadoc") +public enum TestEnum { + TEST, TEST2 +}