Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
onacit committed Apr 28, 2024
1 parent a21f76b commit e169f0e
Show file tree
Hide file tree
Showing 28 changed files with 517 additions and 360 deletions.
34 changes: 24 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@
<artifactId>checkstyle</artifactId>
<version>${version.checkstyle}</version>
</dependency>
<dependency> <!-- only for the javadoc -->
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>${version.jakarta.validation-api}</version>
</dependency>
<!-- <dependency> &lt;!&ndash; only for the javadoc &ndash;&gt;-->
<!-- <groupId>jakarta.validation</groupId>-->
<!-- <artifactId>jakarta.validation-api</artifactId>-->
<!-- <version>${version.jakarta.validation-api}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
Expand Down Expand Up @@ -129,11 +129,11 @@
<version>${version.logback-classic}</version>
<scope>test</scope>
</dependency>
<dependency> <!-- only for the javadoc -->
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- <dependency> &lt;!&ndash; only for the javadoc &ndash;&gt;-->
<!-- <groupId>jakarta.validation</groupId>-->
<!-- <artifactId>jakarta.validation-api</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
Expand Down Expand Up @@ -899,6 +899,20 @@
</sources>
</configuration>
</execution>
<execution>
<id>add-test-resource</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>sub/hibernate-validator/documentation/src/test/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public abstract class AbstractBeanAssert<SELF extends AbstractBeanAssert<SELF, A
extends AbstractPropertyAssert<SELF, ACTUAL>
implements BeanAssert<SELF, ACTUAL> {

// ---------------------------------------------------------------------------------------------------- CONSTRUCTORS

/**
* Creates a new instance for verifying specified actual value.
*
Expand All @@ -57,6 +59,7 @@ protected AbstractBeanAssert(final ACTUAL actual, final Class<?> selfType) {
super(actual, selfType);
}

// -----------------------------------------------------------------------------------------------------------------
@Override
public final SELF isValid(final Consumer<? super Set<ConstraintViolation<ACTUAL>>> consumer) {
Objects.requireNonNull(consumer, "consumer is null");
Expand Down Expand Up @@ -85,6 +88,10 @@ public final SELF isValid(final Consumer<? super Set<ConstraintViolation<ACTUAL>

@Override
public final SELF isNotValid() {
if (true) {
return isNotValid(s -> {
});
}
isNotNull();
final Validator validator = delegate.getValidator();
final Class<?>[] groups = delegate.getGroups();
Expand All @@ -109,8 +116,7 @@ public SELF isNotValid(final Consumer<Set<ConstraintViolation<ACTUAL>>> consumer
final Validator validator = delegate.getValidator();
final Class<?>[] groups = delegate.getGroups();
delegate.setViolations(validator.validate(actual, groups));
final Set<ConstraintViolation<ACTUAL>> violations = delegate.getViolations();
assertThat(violations)
assertThat(delegate.getViolations())
.as("%nThe set of constraint violations resulted while validating%n"
+ "\tactual: %s%n"
+ "targeting%n"
Expand All @@ -120,7 +126,7 @@ public SELF isNotValid(final Consumer<Set<ConstraintViolation<ACTUAL>>> consumer
)
.withFailMessage("%nexpected to be not empty but empty")
.isNotEmpty();
consumer.accept(violations);
consumer.accept(delegate.getViolations());
return myself;
}

Expand All @@ -132,9 +138,8 @@ public final SELF hasValidProperty(final String propertyName,
isNotNull();
final Validator validator = delegate.getValidator();
final Class<?>[] groups = delegate.getGroups();
final Set<ConstraintViolation<ACTUAL>> violations = validator.validateProperty(actual, propertyName, groups);
delegate.acceptViolations(consumer);
assertThat(violations)
delegate.setViolations(validator.validateProperty(actual, propertyName, groups));
assertThat(delegate.getViolations())
.as("%nThe set of constraint violations resulted while validating%n"
+ "\tactual: %s%n"
+ "for its%n"
Expand All @@ -148,10 +153,11 @@ public final SELF hasValidProperty(final String propertyName,
.withFailMessage(() -> String.format(
"%nexpected to be empty but contains %1$d element(s)%n"
+ "%2$s",
violations.size(),
ValidationAssertMessages.format(violations)
delegate.getViolations().size(),
ValidationAssertMessages.format(delegate.getViolations())
))
.isEmpty();
consumer.accept(delegate.getViolations());
return myself;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public SELF hasMessage(final String expectedMessage) {

// ---------------------------------------------------------------------------------------------------- propertyPath
@Override
public <ASSERT extends AbstractPathAssert<?, ? extends AbstractPathAssert.AbstractNodeAssert<?>>> ASSERT extractingPropertyPath(final AssertFactory<? super Path, ? extends ASSERT> assertFactory) {
public <ASSERT extends AbstractPathAssert<?, ? extends AbstractPathAssert.AbstractNodeAssert<?>>>
ASSERT extractingPropertyPath(final AssertFactory<? super Path, ? extends ASSERT> assertFactory) {
return isNotNull()
.extracting(ConstraintViolation::getPropertyPath, assertFactory::createAssert);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public abstract class AbstractPathAssert<
extends AbstractIterableAssert<SELF, Path, Path.Node, NODE_ASSERT>
implements PathAssert<SELF> {

// -----------------------------------------------------------------------------------------------------------------
@SuppressWarnings({
"java:S101" // class _Abstract...
})
abstract static class _AbstractNodeAssert<
SELF extends _AbstractNodeAssert<SELF, ACTUAL>, ACTUAL extends Path.Node>
extends AbstractAssert<SELF, ACTUAL>
Expand Down Expand Up @@ -285,10 +289,12 @@ protected static <N extends Path.Node> N nodeAt(final Iterable<? extends Path.No
return node.as(nodeType);
}

// -----------------------------------------------------------------------------------------------------------------
protected AbstractPathAssert(final Path actual, final Class<?> selfType) {
super(actual, selfType);
}

// -----------------------------------------------------------------------------------------------------------------
@Override
public <A extends _AbstractNodeAssert<? extends A, ? extends N>, N extends Path.Node> A extractingNode(
final int index, final Class<N> nodeType, final AssertFactory<? super N, ? extends A> factory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

import static org.assertj.core.api.Assertions.assertThat;

@SuppressWarnings({
"java:S119" // <SELF ...>
})
public abstract class AbstractPropertyAssert<SELF extends AbstractPropertyAssert<SELF, ACTUAL>, ACTUAL>
extends AbstractValidationAssert<SELF, ACTUAL>
implements PropertyAssert<SELF, ACTUAL> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package com.github.jinahya.assertj.validation;

/*-
* #%L
* assertj-bean-validation
* %%
* Copyright (C) 2021 - 2024 Jinahya, Inc.
* %%
* 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.
* #L%
*/

final class AssertFactories {

// public static <NODE extends Path.Node, ASSERT extends PathAssert.NodeAssert<? extends ASSERT, NODE>> ASSERT nodeAssert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
*/

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AbstractCharacterAssert;
import org.assertj.core.api.AbstractClassAssert;
import org.assertj.core.api.AbstractObjectArrayAssert;
import org.assertj.core.api.Assert;
import org.assertj.core.api.AssertFactory;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.assertj.core.api.ObjectArrayAssert;
import org.assertj.core.api.ObjectAssertFactory;
Expand All @@ -48,7 +46,9 @@
"java:S119" // <SELF ...>
})
public interface ConstraintViolationAssert<
SELF extends ConstraintViolationAssert<SELF, ACTUAL, T>, ACTUAL extends ConstraintViolation<T>, T>
SELF extends ConstraintViolationAssert<SELF, ACTUAL, T>,
ACTUAL extends ConstraintViolation<T>,
T>
extends Assert<SELF, ACTUAL> {

// -------------------------------------------------------------------------------------------- constraintDescriptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
* @param <SELF> self type parameter
* @author Jin Kwon &lt;onacit_at_gmail.com&gt;
*/
@SuppressWarnings({
"java:S119" // <SELF ...>
})
public interface PathAssert<SELF extends PathAssert<SELF>>
extends Assert<SELF, Path>,
EnumerableAssert<SELF, Path.Node> {
Expand Down Expand Up @@ -247,6 +250,25 @@ default SELF doesNotHaveIndex() {

// --------------------------------------------------------------------------------------------------------- key

/**
* Returns a new assertion for verifying {@link Path.Node#getKey()} actual.key} value.
*
* @param factory an assertion factory.
* @return an assert for verifying {@link Path.Node#getKey() actual.key} value.
* @see #extractingKey()
*/
default <KEY, ASSERT extends AbstractObjectAssert<ASSERT, ? extends KEY>>
ASSERT extractingKey(AssertFactory<? super KEY, ? extends ASSERT> factory) {
return extractingKey(
a -> {
@SuppressWarnings({"unchecked"})
final KEY key = (KEY) a.getKey();
return key;
},
factory
);
}

/**
* Returns an assert for verifying {@link Path.Node#getKey()} actual.key} value.
*
Expand Down Expand Up @@ -297,6 +319,18 @@ default SELF hasKey(final Object expectedKey) {
return hasKeySatisfying(a -> a.isEqualTo(expectedKey));
}

/**
* Verifies that {@link Path.Node#getKey() actual.key} is {@code null}.
*
* @return this assertion object.
* @implSpec The default implementation invokes the {@link #hasKey(Object)} method with {@code null}, and
* returns the result.
* @see #hasKey(Object)
*/
default SELF doesNotHaveKey() {
return hasKey(null);
}

// -------------------------------------------------------------------------------------------------------- kind
<ASSERT extends AbstractComparableAssert<?, ElementKind>> ASSERT extractingKind(
AssertFactory<? super ElementKind, ? extends ASSERT> factory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

import javax.validation.ConstraintViolation;
import javax.validation.Path;
import javax.validation.metadata.ConstraintDescriptor;
import java.lang.annotation.Annotation;

Expand Down Expand Up @@ -65,7 +66,7 @@ public static <ACTUAL> AbstractPropertyAssert<?, ACTUAL> assertThatProperty(fina
* @param actual the constraint violation value to verify.
* @return a new assertion instance for {@code actual}.
*/
public static <T> ConstraintViolationAssert<?, ConstraintViolation<T>, T> assertThatConstraintViolation(
public static <T> AbstractConstraintViolationAssert<?, ConstraintViolation<T>, T> assertThatConstraintViolation(
final ConstraintViolation<T> actual) {
return new DefaultConstraintViolationAssert<>(actual);
}
Expand All @@ -75,15 +76,15 @@ static <T> AbstractIterableOfConstraintViolationsAssert<?, T> assertThatIterable
return new IterableOfConstraintViolationsAssert<>(actual);
}

// /**
// * Creates a new assertion object for verifying specified path value.
// *
// * @param actual the path value to verify.
// * @return a new assertion instance for {@code actual}.
// */
// public static AbstractPathAssert<?, ?> assertThatPath(final Path actual) {
// return new DefaultPathAssert(actual);
// }
/**
* Creates a new assertion object for verifying specified path value.
*
* @param actual the path value to verify.
* @return a new assertion instance for {@code actual}.
*/
public static AbstractPathAssert<?, ?> assertThatPath(final Path actual) {
return new DefaultPathAssert(actual);
}

/**
* Creates a new instance.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,36 @@
package com.github.jinahya.assertj.validation;public interface ValidationInstanceOfAssertFactories {
package com.github.jinahya.assertj.validation;

/*-
* #%L
* assertj-bean-validation
* %%
* Copyright (C) 2021 - 2024 Jinahya, Inc.
* %%
* 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.
* #L%
*/

import org.assertj.core.api.InstanceOfAssertFactory;

import javax.validation.ConstraintViolation;

public interface ValidationInstanceOfAssertFactories {

static <T>
InstanceOfAssertFactory<ConstraintViolation<T>, AbstractConstraintViolationAssert<?, ConstraintViolation<T>, T>>
constraintViolation() {
@SuppressWarnings("unchecked")
final Class<ConstraintViolation<T>> type = (Class<ConstraintViolation<T>>) (Object) ConstraintViolation.class;
return new InstanceOfAssertFactory<>(type, ValidationAssertions::assertThatConstraintViolation);
}
}
Loading

0 comments on commit e169f0e

Please sign in to comment.