forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#31666 from ppalaga/i4192
Validate the default JAXBContext at build time only if it is really u…
- Loading branch information
Showing
11 changed files
with
407 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...yment/src/main/java/io/quarkus/jaxb/deployment/FilteredJaxbClassesToBeBoundBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.quarkus.jaxb.deployment; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import io.quarkus.builder.item.SimpleBuildItem; | ||
import io.quarkus.jaxb.deployment.utils.JaxbType; | ||
|
||
/** | ||
* List of classes to be bound in the JAXB context. Aggregates all classes passed via | ||
* {@link JaxbClassesToBeBoundBuildItem}. All class names excluded via {@code quarkus.jaxb.exclude-classes} are not | ||
* present in this list. | ||
*/ | ||
public final class FilteredJaxbClassesToBeBoundBuildItem extends SimpleBuildItem { | ||
|
||
private final List<Class<?>> classes; | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
private FilteredJaxbClassesToBeBoundBuildItem(List<Class<?>> classes) { | ||
this.classes = classes; | ||
} | ||
|
||
public List<Class<?>> getClasses() { | ||
return new ArrayList<>(classes); | ||
} | ||
|
||
public static class Builder { | ||
private final Set<String> classNames = new LinkedHashSet<>(); | ||
private final Set<String> classNameExcludes = new LinkedHashSet<>(); | ||
|
||
public Builder classNameExcludes(Collection<String> classNameExcludes) { | ||
for (String className : classNameExcludes) { | ||
this.classNameExcludes.add(className); | ||
} | ||
return this; | ||
} | ||
|
||
public Builder classNames(Collection<String> classNames) { | ||
for (String className : classNames) { | ||
this.classNames.add(className); | ||
} | ||
return this; | ||
} | ||
|
||
public FilteredJaxbClassesToBeBoundBuildItem build() { | ||
final List<Class<?>> classes = classNames.stream() | ||
.filter(className -> !this.classNameExcludes.contains(className)) | ||
.map(FilteredJaxbClassesToBeBoundBuildItem::getClassByName) | ||
.filter(JaxbType::isValidType) | ||
.collect(Collectors.toList()); | ||
|
||
return new FilteredJaxbClassesToBeBoundBuildItem(classes); | ||
} | ||
} | ||
|
||
private static Class<?> getClassByName(String name) { | ||
try { | ||
return Class.forName(name, false, Thread.currentThread().getContextClassLoader()); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
9 changes: 7 additions & 2 deletions
9
...xb/deployment/src/main/java/io/quarkus/jaxb/deployment/JaxbClassesToBeBoundBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...nt/src/test/java/io/quarkus/jaxb/deployment/ConflictingModelClassesMarshalerOnlyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.quarkus.jaxb.deployment; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.enterprise.context.control.ActivateRequestContext; | ||
import jakarta.enterprise.inject.spi.DeploymentException; | ||
import jakarta.inject.Inject; | ||
import jakarta.xml.bind.JAXBContext; | ||
import jakarta.xml.bind.Marshaller; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.glassfish.jaxb.core.v2.runtime.IllegalAnnotationException; | ||
import org.glassfish.jaxb.runtime.v2.runtime.IllegalAnnotationsException; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
/** | ||
* Make sure that the validation of the default JAXB context fails if there conflicting model classes and there is only | ||
* a {@link Marshaller} injection point (which actually requires a {@link JAXBContext} bean to be available too). | ||
*/ | ||
public class ConflictingModelClassesMarshalerOnlyTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses( | ||
io.quarkus.jaxb.deployment.one.Model.class, | ||
io.quarkus.jaxb.deployment.two.Model.class)) | ||
.assertException(e -> { | ||
assertThat(e).isInstanceOf(DeploymentException.class); | ||
assertThat(e.getMessage()).isEqualTo("Failed to create or validate the default JAXBContext"); | ||
Throwable cause = e.getCause(); | ||
assertThat(cause).isInstanceOf(IllegalAnnotationsException.class); | ||
assertThat(cause.getMessage()).isEqualTo("1 counts of IllegalAnnotationExceptions"); | ||
List<IllegalAnnotationException> errors = ((IllegalAnnotationsException) cause).getErrors(); | ||
assertThat(errors.size()).isEqualTo(1); | ||
assertThat(errors.get(0).getMessage()).contains("Two classes have the same XML type name \"model\""); | ||
|
||
}); | ||
|
||
@Inject | ||
Marshaller marshaller; | ||
|
||
@Test | ||
@ActivateRequestContext | ||
public void shouldFail() { | ||
Assertions.fail("The application should fail at boot"); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...jaxb/deployment/src/test/java/io/quarkus/jaxb/deployment/ConflictingModelClassesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.quarkus.jaxb.deployment; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.enterprise.context.control.ActivateRequestContext; | ||
import jakarta.enterprise.inject.spi.DeploymentException; | ||
import jakarta.inject.Inject; | ||
import jakarta.xml.bind.JAXBContext; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.glassfish.jaxb.core.v2.runtime.IllegalAnnotationException; | ||
import org.glassfish.jaxb.runtime.v2.runtime.IllegalAnnotationsException; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
/** | ||
* Make sure that the validation of the default JAXB context fails if there conflicting model classes and there actually | ||
* is a {@link JAXBContext} injection point. | ||
*/ | ||
public class ConflictingModelClassesTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses( | ||
io.quarkus.jaxb.deployment.one.Model.class, | ||
io.quarkus.jaxb.deployment.two.Model.class)) | ||
.assertException(e -> { | ||
assertThat(e).isInstanceOf(DeploymentException.class); | ||
assertThat(e.getMessage()).isEqualTo("Failed to create or validate the default JAXBContext"); | ||
Throwable cause = e.getCause(); | ||
assertThat(cause).isInstanceOf(IllegalAnnotationsException.class); | ||
assertThat(cause.getMessage()).isEqualTo("1 counts of IllegalAnnotationExceptions"); | ||
List<IllegalAnnotationException> errors = ((IllegalAnnotationsException) cause).getErrors(); | ||
assertThat(errors.size()).isEqualTo(1); | ||
assertThat(errors.get(0).getMessage()).contains("Two classes have the same XML type name \"model\""); | ||
|
||
}); | ||
|
||
@Inject | ||
JAXBContext jaxbContext; | ||
|
||
@Test | ||
@ActivateRequestContext | ||
public void shouldFail() { | ||
Assertions.fail("The application should fail at boot"); | ||
} | ||
|
||
} |
Oops, something went wrong.