Skip to content

Commit

Permalink
Increase JAXB extension test coverage
Browse files Browse the repository at this point in the history
Fixes #3896
  • Loading branch information
jamesnetherton committed Jul 6, 2022
1 parent 198dc71 commit ffd0c48
Show file tree
Hide file tree
Showing 25 changed files with 1,695 additions and 219 deletions.
11 changes: 11 additions & 0 deletions docs/modules/ROOT/pages/reference/extensions/jaxb.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ Or add the coordinates to your existing project:
----

Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications.

== Usage

=== Native mode `ObjectFactory` instantiation of non-JAXB annotated classes

When performing JAXB marshal operations with a custom `ObjectFactory` to instantiate POJO classes that do not have JAXB annotations,
you must register those POJO classes for reflection in order for them to be instantiated in native mode. E.g via the `@RegisterForReflection`
annotation or configuration property `quarkus.camel.native.reflection.include-patterns`.

Refer to the xref:user-guide/native-mode.adoc#reflection[Native mode] user guide for more information.

7 changes: 7 additions & 0 deletions extensions/jaxb/runtime/src/main/doc/usage.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=== Native mode `ObjectFactory` instantiation of non-JAXB annotated classes

When performing JAXB marshal operations with a custom `ObjectFactory` to instantiate POJO classes that do not have JAXB annotations,
you must register those POJO classes for reflection in order for them to be instantiated in native mode. E.g via the `@RegisterForReflection`
annotation or configuration property `quarkus.camel.native.reflection.include-patterns`.

Refer to the xref:user-guide/native-mode.adoc#reflection[Native mode] user guide for more information.
4 changes: 4 additions & 0 deletions integration-tests/jaxb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jsonb</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.component.jaxb.it;

import javax.json.Json;
import javax.json.JsonObject;

import org.apache.camel.quarkus.component.jaxb.it.model.Person;

public final class JaxbHelper {

private JaxbHelper() {
// Utility class
}

public static JsonObject personToJson(Person person) {
return Json.createObjectBuilder()
.add("firstName", person.getFirstName())
.add("lastName", person.getLastName())
.add("age", person.getAge())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.component.jaxb.it;

import java.util.Map;

import javax.inject.Named;
import javax.inject.Singleton;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;

import org.apache.camel.converter.jaxb.JaxbDataFormat;
import org.apache.camel.quarkus.component.jaxb.it.model.namespaced.NamespacedPerson;
import org.apache.camel.quarkus.component.jaxb.it.model.partial.PartClassPerson;
import org.apache.camel.quarkus.component.jaxb.it.model.pojo.PojoPerson;
import org.apache.camel.quarkus.component.jaxb.it.model.simple.SimplePerson;
import org.apache.camel.quarkus.component.jaxb.it.writer.CustomXmlStreamWriter;

public class JaxbProducers {

@Singleton
@Named("jaxbDefault")
public JaxbDataFormat defaultJaxbDataFormat() {
JaxbDataFormat dataFormat = new JaxbDataFormat();
dataFormat.setContextPath(SimplePerson.class.getPackageName());
dataFormat.setFragment(true);
dataFormat.setIgnoreJAXBElement(false);
dataFormat.setPrettyPrint(false);
dataFormat.setSchema("classpath:person.xsd");
dataFormat.setSchemaSeverityLevel(2);
return dataFormat;
}

@Singleton
@Named("jaxbWithNamespacePrefix")
public JaxbDataFormat jaxbDataFormatWithNamespacePrefix() {
JaxbDataFormat dataFormat = new JaxbDataFormat();
dataFormat.setContextPath(NamespacedPerson.class.getPackageName());
dataFormat.setNamespacePrefix(Map.of("https://example.com/a", "test"));
return dataFormat;
}

@Singleton
@Named("jaxbWithEncoding")
public JaxbDataFormat jaxbDataFormatWithCustomCharset() {
JaxbDataFormat dataFormat = new JaxbDataFormat();
dataFormat.setContextPath(SimplePerson.class.getPackageName());
dataFormat.setEncoding("ISO-8859-1");
dataFormat.setFilterNonXmlChars(true);
return dataFormat;
}

@Singleton
@Named("jaxbWithMustBeJAXBElementFalse")
public JaxbDataFormat jaxbDataFormatWithMustBeJAXBElementFalse() {
JaxbDataFormat dataFormat = new JaxbDataFormat();
dataFormat.setMustBeJAXBElement(false);
return dataFormat;
}

@Singleton
@Named("jaxbWithPartClass")
public JaxbDataFormat jaxbWithPartClass() {
JaxbDataFormat dataFormat = new JaxbDataFormat();
dataFormat.setContextPath(PartClassPerson.class.getPackageName());
dataFormat.setPartClass(PartClassPerson.class);
dataFormat.setPartNamespace(new QName(PartClassPerson.NAMESPACE, "person"));
return dataFormat;
}

@Singleton
@Named("jaxbWithIgnoreElement")
public JaxbDataFormat jaxbWithIgnoreElement() {
JaxbDataFormat dataFormat = new JaxbDataFormat();
dataFormat.setContextPath(PartClassPerson.class.getPackageName());
dataFormat.setIgnoreJAXBElement(false);
dataFormat.setPartClass(PartClassPerson.class);
dataFormat.setPartNamespace(new QName(PartClassPerson.NAMESPACE, "person"));
return dataFormat;
}

@Singleton
@Named("jaxbWithCustomProperties")
public JaxbDataFormat jaxbWithCustomProperties() {
String packages = String.format("%s:%s",
SimplePerson.class.getPackageName(),
NamespacedPerson.class.getPackageName());
JaxbDataFormat dataFormat = new JaxbDataFormat(packages);
dataFormat.setJaxbProviderProperties(Map.of(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE));
return dataFormat;
}

@Singleton
@Named("jaxbWithCustomStreamWriter")
public JaxbDataFormat jaxbWithCustomStreamWriter() {
JaxbDataFormat dataFormat = new JaxbDataFormat(SimplePerson.class.getPackageName());
dataFormat.setXmlStreamWriterWrapper(new CustomXmlStreamWriter());
return dataFormat;
}

@Singleton
@Named("jaxbWithoutObjectFactory")
public JaxbDataFormat jaxbWithoutObjectFactory() {
JaxbDataFormat dataFormat = new JaxbDataFormat(PojoPerson.class.getPackageName());
dataFormat.setObjectFactory(false);
return dataFormat;
}

@Singleton
@Named("jaxbWithNoNamespaceSchemaLocation")
public JaxbDataFormat jaxbWithNoNamespaceSchemaLocation() {
JaxbDataFormat dataFormat = new JaxbDataFormat(SimplePerson.class.getPackageName());
dataFormat.setNoNamespaceSchemaLocation("person-no-namespace.xsd");
return dataFormat;
}
}

0 comments on commit ffd0c48

Please sign in to comment.