Skip to content

Commit 38edc59

Browse files
committed
HV-2025 Remove OSGi testing and support
1 parent b4808e7 commit 38edc59

40 files changed

+64
-1953
lines changed

documentation/pom.xml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
<asciidoctor.examples-source-dir>${basedir}/src/test/java</asciidoctor.examples-source-dir>
2929
<asciidoctor.examples-resources-dir>${basedir}/src/test/resources</asciidoctor.examples-resources-dir>
3030
<asciidoctor.engine-source-dir>${basedir}/../engine/src/main/java</asciidoctor.engine-source-dir>
31-
<asciidoctor.osgi-integrationtest-source-dir>${basedir}/../osgi/integrationtest/src/test/java</asciidoctor.osgi-integrationtest-source-dir>
3231

3332
<html.meta.project-key>validator</html.meta.project-key>
3433
<html.google-analytics.id>G-282CVRCQHZ</html.google-analytics.id>
@@ -59,6 +58,11 @@
5958
<artifactId>hibernate-validator-cdi</artifactId>
6059
<scope>test</scope>
6160
</dependency>
61+
<dependency>
62+
<groupId>${project.groupId}</groupId>
63+
<artifactId>hibernate-validator-test-utils</artifactId>
64+
<scope>test</scope>
65+
</dependency>
6266
<dependency>
6367
<groupId>jakarta.enterprise</groupId>
6468
<artifactId>jakarta.enterprise.cdi-api</artifactId>
@@ -79,11 +83,6 @@
7983
<artifactId>groovy-jsr223</artifactId>
8084
<scope>test</scope>
8185
</dependency>
82-
<dependency>
83-
<groupId>org.osgi</groupId>
84-
<artifactId>org.osgi.core</artifactId>
85-
<scope>test</scope>
86-
</dependency>
8786
<dependency>
8887
<groupId>org.springframework</groupId>
8988
<artifactId>spring-expression</artifactId>
@@ -285,7 +284,6 @@
285284
<sourcedir>${asciidoctor.examples-source-dir}</sourcedir>
286285
<resourcesdir>${asciidoctor.examples-resources-dir}</resourcesdir>
287286
<engine-sourcedir>${asciidoctor.engine-source-dir}</engine-sourcedir>
288-
<osgi-integrationtest-sourcedir>${asciidoctor.osgi-integrationtest-source-dir}</osgi-integrationtest-sourcedir>
289287

290288
<hvVersion>${project.version}</hvVersion>
291289
<bvVersion>${version.jakarta.validation-api}</bvVersion>

documentation/src/main/asciidoc/ch09.asciidoc

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ its `ValidationProvider` implementation. In the case of Hibernate Validator, thi
8282

8383
Depending on your environment and its classloading specifics, provider discovery via the Java's
8484
service loader mechanism might not work. In this case, you can plug in a custom
85-
`ValidationProviderResolver` implementation which performs the provider retrieval. An example is OSGi,
86-
where you could implement a provider resolver which uses OSGi services for provider discovery.
85+
`ValidationProviderResolver` implementation which performs the provider retrieval.
8786

8887
To use a custom provider resolver, pass it via `providerResolver()` as shown in
8988
<<example-using-custom-validation-provider-resolver>>.
@@ -93,7 +92,7 @@ To use a custom provider resolver, pass it via `providerResolver()` as shown in
9392
====
9493
[source, JAVA, indent=0]
9594
----
96-
include::{sourcedir}/org/hibernate/validator/referenceguide/chapter09/OsgiServiceDiscoverer.java[tags=include]
95+
include::{sourcedir}/org/hibernate/validator/referenceguide/chapter09/CustomValidationProviderResolver.java[tags=include]
9796
----
9897
9998
[source, JAVA, indent=0]
@@ -436,7 +435,7 @@ For constraints like `@ScriptAssert` and `@ParameterScriptAssert`, it might be u
436435
how the script engines are initialized and how the script evaluators are built.
437436
This can be done by setting a custom implementation of `ScriptEvaluatorFactory`.
438437

439-
In particular, this is important for modular environments (e.g. OSGi), where user might face issues
438+
In particular, this is important for modular environments, where user might face issues
440439
with modular class loading and https://www.jcp.org/en/jsr/detail?id=223[JSR 223].
441440
It also allows to use any custom script engine, not necessarily based on the
442441
https://www.jcp.org/en/jsr/detail?id=223[JSR 223] (e.g. Spring Expression Language).
@@ -487,7 +486,7 @@ To solve this issue, the `MultiClassLoaderScriptEvaluatorFactory` class below ca
487486
====
488487
[source, JAVA, indent=0]
489488
----
490-
include::{osgi-integrationtest-sourcedir}/org/hibernate/validator/osgi/scripting/MultiClassLoaderScriptEvaluatorFactory.java[]
489+
include::{sourcedir}/org/hibernate/validator/referenceguide/chapter09/MultiClassLoaderScriptEvaluatorFactory.java[]
491490
----
492491
====
493492

@@ -496,33 +495,12 @@ and then declared with:
496495
====
497496
[source, JAVA, indent=0]
498497
----
499-
include::{osgi-integrationtest-sourcedir}/org/hibernate/validator/osgi/integrationtest/OsgiIntegrationTest.java[tags=scriptEvaluatorFactoryMultiClassLoaderScriptEvaluatorFactory]
498+
include::{sourcedir}/org/hibernate/validator/referenceguide/chapter09/BootstrappingTest.java[tags=scriptEvaluatorFactoryMultiClassLoaderScriptEvaluatorFactory]
500499
----
501500
====
502501

503502
This way, it is possible to pass multiple `ClassLoader` instances: typically the class loaders of the wanted ``ScriptEngine``s.
504503

505-
An alternative approach for OSGi environments can be to use the `OsgiScriptEvaluatorFactory` defined below:
506-
507-
====
508-
[source, JAVA, indent=0]
509-
----
510-
include::{osgi-integrationtest-sourcedir}/org/hibernate/validator/osgi/scripting/OsgiScriptEvaluatorFactory.java[]
511-
----
512-
====
513-
514-
and then declared with:
515-
516-
====
517-
[source, JAVA, indent=0]
518-
----
519-
include::{osgi-integrationtest-sourcedir}/org/hibernate/validator/osgi/integrationtest/OsgiIntegrationTest.java[tags=scriptEvaluatorFactoryOsgiScriptEvaluatorFactory]
520-
----
521-
====
522-
523-
It is designed specifically for OSGi environments and allows you to pass the `BundleContext` which will be used to search
524-
for `ScriptEngineFactory` as a parameter.
525-
526504
As already mentioned, you can also use script engines that are not based on https://www.jcp.org/en/jsr/detail?id=223[JSR 223].
527505

528506
For instance, to use the https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions[Spring Expression Language],

documentation/src/main/asciidoc/ch12.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,8 @@ There are several cases in which Hibernate Validator needs to load resources or
803803
By default, Hibernate Validator tries to load these resources via the current thread context class loader.
804804
If that's not successful, Hibernate Validator's own class loader will be tried as a fallback.
805805

806+
// Let's keep this mention of OSGi here in the docs, pointing the users,
807+
// to what they can do, if they are in the OSGi env.
806808
For cases where this strategy is not appropriate (e.g. modularized environments such as OSGi),
807809
you may provide a specific class loader for loading these resources when bootstrapping the validator factory:
808810

documentation/src/test/java/org/hibernate/validator/referenceguide/chapter09/BootstrappingTest.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package org.hibernate.validator.referenceguide.chapter09;
22

3+
import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertThat;
4+
import static org.hibernate.validator.testutil.ConstraintViolationAssert.pathWith;
5+
import static org.hibernate.validator.testutil.ConstraintViolationAssert.violationOf;
6+
37
import java.io.InputStream;
48
import java.time.Duration;
9+
import java.time.LocalDate;
510
import java.time.ZoneId;
611
import java.time.ZonedDateTime;
712

@@ -11,9 +16,12 @@
1116

1217
import org.hibernate.validator.HibernateValidator;
1318
import org.hibernate.validator.cfg.ConstraintMapping;
19+
import org.hibernate.validator.constraints.ScriptAssert;
1420

1521
import org.junit.Test;
1622

23+
import org.codehaus.groovy.jsr223.GroovyScriptEngineFactory;
24+
1725
public class BootstrappingTest {
1826

1927
@Test
@@ -54,7 +62,7 @@ public void byDefaultProvider() {
5462
public void providerResolver() {
5563
//tag::providerResolver[]
5664
ValidatorFactory validatorFactory = Validation.byDefaultProvider()
57-
.providerResolver( new OsgiServiceDiscoverer() )
65+
.providerResolver( new CustomValidationProviderResolver() )
5866
.configure()
5967
.buildValidatorFactory();
6068
Validator validator = validatorFactory.getValidator();
@@ -227,4 +235,41 @@ public void scriptEvaluatorFactoryProgrammatically() {
227235
Validator validator = validatorFactory.getValidator();
228236
//end::scriptEvaluatorFactoryProgrammatically[]
229237
}
238+
239+
@Test
240+
public void canUseScriptAssertConstraintWithMultiClassLoaderScriptEvaluatorFactory() {
241+
//tag::scriptEvaluatorFactoryMultiClassLoaderScriptEvaluatorFactory[]
242+
Validator validator = Validation.byProvider( HibernateValidator.class )
243+
.configure()
244+
.scriptEvaluatorFactory(
245+
new MultiClassLoaderScriptEvaluatorFactory( GroovyScriptEngineFactory.class.getClassLoader() )
246+
)
247+
.buildValidatorFactory()
248+
.getValidator();
249+
//end::scriptEvaluatorFactoryMultiClassLoaderScriptEvaluatorFactory[]
250+
251+
canUseScriptAssertConstraint( validator );
252+
}
253+
254+
private void canUseScriptAssertConstraint(Validator validator) {
255+
assertThat(
256+
validator.validate( new Event( LocalDate.of( 2017, 8, 8 ), LocalDate.of( 2016, 8, 8 ) ) )
257+
).containsOnlyViolations(
258+
violationOf( ScriptAssert.class ).withPropertyPath( pathWith().bean() )
259+
.withMessage( "start of event cannot be after the end" )
260+
);
261+
}
262+
263+
@ScriptAssert(lang = "groovy", script = "_this.start < _this.end", message = "start of event cannot be after the end")
264+
public class Event {
265+
266+
private final LocalDate start;
267+
268+
private final LocalDate end;
269+
270+
public Event(LocalDate start, LocalDate end) {
271+
this.start = start;
272+
this.end = end;
273+
}
274+
}
230275
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
import jakarta.validation.spi.ValidationProvider;
99

1010
//tag::include[]
11-
public class OsgiServiceDiscoverer implements ValidationProviderResolver {
11+
public class CustomValidationProviderResolver implements ValidationProviderResolver {
1212

1313
@Override
1414
public List<ValidationProvider<?>> getValidationProviders() {
1515
//...
16-
return null;
16+
//end::include[]
17+
List<ValidationProvider<?>> providers = null;
18+
//tag::include[]
19+
return providers;
1720
}
1821
}
1922
//end::include[]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* License: Apache License, Version 2.0
55
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
66
*/
7-
package org.hibernate.validator.osgi.scripting;
7+
package org.hibernate.validator.referenceguide.chapter09;
88

99
import javax.script.ScriptEngine;
1010
import javax.script.ScriptEngineManager;

0 commit comments

Comments
 (0)