_converter(XmlAdapter,?> adapter, boolean forSerialization)
{
JavaType[] pt = getTypeFactory().findTypeParameters(adapter.getClass(), XmlAdapter.class);
// Order of type parameters for Converter is reverse between serializer, deserializer,
// whereas JAXB just uses single ordering
if (forSerialization) {
return new AdapterConverter(adapter, pt[1], pt[0], forSerialization);
}
return new AdapterConverter(adapter, pt[0], pt[1], forSerialization);
}
}
JaxbAnnotationModule.java 0000664 0000000 0000000 00000005412 12373261211 0044026 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/main/java/com/fasterxml/jackson/module/jaxb package com.fasterxml.jackson.module.jaxb;
import com.fasterxml.jackson.databind.module.SimpleModule;
/**
* Module that can be registered to add support for JAXB annotations.
* It does basically equivalent of
*
* objectMapper.setAnnotationIntrospector(...);
*
* with combination of {@link JaxbAnnotationIntrospector} and existing
* default introspector(s) (if any), depending on configuration
* (by default, JAXB annotations are used as {@link Priority#PRIMARY}
* annotations).
*/
public class JaxbAnnotationModule extends SimpleModule
{
private static final long serialVersionUID = 1L;
/**
* Enumeration that defines how we use JAXB Annotations: either
* as "primary" annotations (before any other already configured
* introspector -- most likely default JacksonAnnotationIntrospector) or
* as "secondary" annotations (after any other already configured
* introspector(s)).
*
* Default choice is PRIMARY
*
* Note that if you want to use JAXB annotations as the only annotations,
* you must directly set annotation introspector by calling
* {@link com.fasterxml.jackson.databind.ObjectMapper#setAnnotationIntrospector}.
*/
public enum Priority {
PRIMARY, SECONDARY;
}
/**
* Priority to use when registering annotation introspector: default
* value is {@link Priority#PRIMARY}.
*/
protected Priority _priority = Priority.PRIMARY;
/*
/**********************************************************
/* Life cycle
/**********************************************************
*/
public JaxbAnnotationModule()
{
super(PackageVersion.VERSION);
}
@Override
public void setupModule(SetupContext context)
{
JaxbAnnotationIntrospector intr = new JaxbAnnotationIntrospector(context.getTypeFactory());
switch (_priority) {
case PRIMARY:
context.insertAnnotationIntrospector(intr);
break;
case SECONDARY:
context.appendAnnotationIntrospector(intr);
break;
}
}
/*
/**********************************************************
/* Configuration
/**********************************************************
*/
/**
* Method for defining whether JAXB annotations should be added
* as primary or secondary annotations (compared to already registered
* annotations).
*
* NOTE: method MUST be called before registering the module -- calling
* afterwards will not have any effect on previous registrations.
*/
public JaxbAnnotationModule setPriority(Priority p) {
_priority = p;
return this;
}
public Priority getPriority() { return _priority; }
}
PackageVersion.java.in 0000664 0000000 0000000 00000001107 12373261211 0043244 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/main/java/com/fasterxml/jackson/module/jaxb package @package@;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.Versioned;
import com.fasterxml.jackson.core.util.VersionUtil;
/**
* Automatically generated from PackageVersion.java.in during
* packageVersion-generate execution of maven-replacer-plugin in
* pom.xml.
*/
public final class PackageVersion implements Versioned {
public final static Version VERSION = VersionUtil.parseVersion(
"@projectversion@", "@projectgroupid@", "@projectartifactid@");
@Override
public Version version() {
return VERSION;
}
}
0000775 0000000 0000000 00000000000 12373261211 0040176 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/main/java/com/fasterxml/jackson/module/jaxb/deser DataHandlerJsonDeserializer.java 0000664 0000000 0000000 00000003035 12373261211 0046406 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/main/java/com/fasterxml/jackson/module/jaxb/deser package com.fasterxml.jackson.module.jaxb.deser;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayInputStream;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
/**
* @author Ryan Heaton
*/
public class DataHandlerJsonDeserializer
extends StdScalarDeserializer
{
private static final long serialVersionUID = 1L;
public DataHandlerJsonDeserializer() { super(DataHandler.class); }
@Override
public DataHandler deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
final byte[] value = jp.getBinaryValue();
return new DataHandler(new DataSource() {
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(value);
}
@Override
public OutputStream getOutputStream() throws IOException {
throw new IOException();
}
@Override
public String getContentType() {
return "application/octet-stream";
}
@Override
public String getName() {
return "json-binary-data";
}
});
}
}
DomElementJsonDeserializer.java 0000664 0000000 0000000 00000006773 12373261211 0046304 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/main/java/com/fasterxml/jackson/module/jaxb/deser package com.fasterxml.jackson.module.jaxb.deser;
import java.io.IOException;
import java.util.Iterator;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
/**
* @author Ryan Heaton
*/
public class DomElementJsonDeserializer
extends StdDeserializer
{
private static final long serialVersionUID = 1L;
private final DocumentBuilder builder;
public DomElementJsonDeserializer()
{
super(Element.class);
try {
DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
bf.setNamespaceAware(true);
builder = bf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new RuntimeException();
}
}
public DomElementJsonDeserializer(DocumentBuilder builder)
{
super(Element.class);
this.builder = builder;
}
@Override
public Element deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
Document document = builder.newDocument();
JsonNode root = jp.readValueAsTree();
return fromNode(document, root);
}
protected Element fromNode(Document document, JsonNode jsonNode)
throws IOException
{
String ns = jsonNode.get("namespace") != null ? jsonNode.get("namespace").asText() : null;
String name = jsonNode.get("name") != null ? jsonNode.get("name").asText() : null;
if (name == null) {
throw new JsonMappingException("No name for DOM element was provided in the JSON object.");
}
Element element = document.createElementNS(ns, name);
JsonNode attributesNode = jsonNode.get("attributes");
if (attributesNode != null && attributesNode instanceof ArrayNode) {
Iterator atts = attributesNode.elements();
while (atts.hasNext()) {
JsonNode node = atts.next();
ns = node.get("namespace") != null ? node.get("namespace").asText() : null;
name = node.get("name") != null ? node.get("name").asText() : null;
String value = node.get("$") != null ? node.get("$").asText() : null;
if (name != null) {
element.setAttributeNS(ns, name, value);
}
}
}
JsonNode childsNode = jsonNode.get("children");
if (childsNode != null && childsNode instanceof ArrayNode) {
Iterator els = childsNode.elements();
while (els.hasNext()) {
JsonNode node = els.next();
name = node.get("name") != null ? node.get("name").asText() : null;
String value = node.get("$") != null ? node.get("$").asText() : null;
if (value != null) {
element.appendChild(document.createTextNode(value));
}
else if (name != null) {
element.appendChild(fromNode(document, node));
}
}
}
return element;
}
}
package-info.java 0000664 0000000 0000000 00000000567 12373261211 0042273 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/main/java/com/fasterxml/jackson/module/jaxb /**
* Package that contains support for using JAXB annotations for
* configuring Jackson data-binding aspects.
*
* Usage is by registering {@link com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule}:
*
* ObjectMapper mapper = new ObjectMapper();
* mapper.registerModule(new JaxbAnnotationModule());
*
*/
package com.fasterxml.jackson.module.jaxb;
0000775 0000000 0000000 00000000000 12373261211 0037665 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/main/java/com/fasterxml/jackson/module/jaxb/ser DataHandlerJsonSerializer.java 0000664 0000000 0000000 00000005010 12373261211 0045557 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/main/java/com/fasterxml/jackson/module/jaxb/ser package com.fasterxml.jackson.module.jaxb.ser;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import javax.activation.DataHandler;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonArrayFormatVisitor;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class DataHandlerJsonSerializer extends StdSerializer
{
public DataHandlerJsonSerializer() { super(DataHandler.class); }
@Override
public void serialize(DataHandler value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException
{
final ByteArrayOutputStream out = new ByteArrayOutputStream();
/* for copy-through, a small buffer should suffice: ideally
* we might want to reuse a generic byte buffer, but for now
* there's no serializer context to hold them.
*
* Also: it'd be nice not to have buffer all data, but use a
* streaming output. But currently JsonGenerator won't allow
* that.
*/
byte[] buffer = new byte[1024 * 4];
InputStream in = value.getInputStream();
int len = in.read(buffer);
while (len > 0) {
out.write(buffer, 0, len);
len = in.read(buffer);
}
in.close();
jgen.writeBinary(out.toByteArray());
}
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
throws JsonMappingException
{
if (visitor != null) {
JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
if (v2 != null) {
v2.itemsFormat(JsonFormatTypes.STRING);
}
}
}
@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint)
{
ObjectNode o = createSchemaNode("array", true);
ObjectNode itemSchema = createSchemaNode("string"); //binary values written as strings?
o.set("items", itemSchema);
return o;
}
}
DomElementJsonSerializer.java 0000664 0000000 0000000 00000006223 12373261211 0045450 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/main/java/com/fasterxml/jackson/module/jaxb/ser package com.fasterxml.jackson.module.jaxb.ser;
import java.io.IOException;
import java.lang.reflect.Type;
import org.w3c.dom.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
public class DomElementJsonSerializer
extends StdSerializer
{
public DomElementJsonSerializer() { super(Element.class); }
@Override
public void serialize(Element value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException
{
jgen.writeStartObject();
jgen.writeStringField("name", value.getTagName());
if (value.getNamespaceURI() != null) {
jgen.writeStringField("namespace", value.getNamespaceURI());
}
NamedNodeMap attributes = value.getAttributes();
if (attributes != null && attributes.getLength() > 0) {
jgen.writeArrayFieldStart("attributes");
for (int i = 0; i < attributes.getLength(); i++) {
Attr attribute = (Attr) attributes.item(i);
jgen.writeStartObject();
jgen.writeStringField("$", attribute.getValue());
jgen.writeStringField("name", attribute.getName());
String ns = attribute.getNamespaceURI();
if (ns != null) {
jgen.writeStringField("namespace", ns);
}
jgen.writeEndObject();
}
jgen.writeEndArray();
}
NodeList children = value.getChildNodes();
if (children != null && children.getLength() > 0) {
jgen.writeArrayFieldStart("children");
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
switch (child.getNodeType()) {
case Node.CDATA_SECTION_NODE:
case Node.TEXT_NODE:
jgen.writeStartObject();
jgen.writeStringField("$", child.getNodeValue());
jgen.writeEndObject();
break;
case Node.ELEMENT_NODE:
serialize((Element) child, jgen, provider);
break;
}
}
jgen.writeEndArray();
}
jgen.writeEndObject();
}
// Improved in 2.3; was missing from 2.2
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
throws JsonMappingException
{
if (visitor != null) {
visitor.expectStringFormat(typeHint);
}
}
@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint)
throws JsonMappingException
{
// Since 2.3: should be more like String type really, not structure
return createSchemaNode("string", true);
}
}
jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/main/resources/ 0000775 0000000 0000000 00000000000 12373261211 0031620 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/main/resources/META-INF/ 0000775 0000000 0000000 00000000000 12373261211 0032760 5 ustar 00root root 0000000 0000000 LICENSE 0000664 0000000 0000000 00000000501 12373261211 0033702 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/main/resources/META-INF This copy of Jackson JSON processor databind module is licensed under the
Apache (Software) License, version 2.0 ("the License").
See the License for details about distribution rights, and the
specific rights regarding derivate works.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
NOTICE 0000664 0000000 0000000 00000001471 12373261211 0033610 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/main/resources/META-INF # Jackson JSON processor
Jackson is a high-performance, Free/Open Source JSON processing library.
It was originally written by Tatu Saloranta (tatu.saloranta@iki.fi), and has
been in development since 2007.
It is currently developed by a community of developers, as well as supported
commercially by FasterXML.com.
## Licensing
Jackson core and extension components may be licensed under different licenses.
To find the details that apply to this artifact see the accompanying LICENSE file.
For more information, including possible other licensing options, contact
FasterXML.com (http://fasterxml.com).
## Credits
A list of contributors may be found from CREDITS file, which is included
in some artifacts (usually source distributions); but is always available
from the source code management (SCM) system project uses.
0000775 0000000 0000000 00000000000 12373261211 0034524 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/main/resources/META-INF/services com.fasterxml.jackson.databind.Module 0000664 0000000 0000000 00000000067 12373261211 0043654 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/main/resources/META-INF/services com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule
jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/ 0000775 0000000 0000000 00000000000 12373261211 0027641 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/ 0000775 0000000 0000000 00000000000 12373261211 0030562 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/ 0000775 0000000 0000000 00000000000 12373261211 0031340 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/ 0000775 0000000 0000000 00000000000 12373261211 0033345 5 ustar 00root root 0000000 0000000 0000775 0000000 0000000 00000000000 12373261211 0034716 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson 0000775 0000000 0000000 00000000000 12373261211 0036203 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module 0000775 0000000 0000000 00000000000 12373261211 0037127 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb BaseJaxbTest.java 0000664 0000000 0000000 00000004260 12373261211 0042313 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb package com.fasterxml.jackson.module.jaxb;
import java.io.IOException;
import java.util.Map;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
public abstract class BaseJaxbTest
extends junit.framework.TestCase
{
protected BaseJaxbTest() { }
/*
/**********************************************************************
/* Factory methods
/**********************************************************************
*/
protected ObjectMapper getJaxbMapper()
{
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector intr = new JaxbAnnotationIntrospector(mapper.getTypeFactory());
mapper.setAnnotationIntrospector(intr);
return mapper;
}
protected ObjectMapper getJaxbAndJacksonMapper()
{
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector intr = new AnnotationIntrospectorPair(new JaxbAnnotationIntrospector(
mapper.getTypeFactory()), new JacksonAnnotationIntrospector());
mapper.setAnnotationIntrospector(intr);
return mapper;
}
/*
/**********************************************************************
/* Helper methods
/**********************************************************************
*/
@SuppressWarnings("unchecked")
protected Map writeAndMap(ObjectMapper m, Object value)
throws IOException
{
String str = m.writeValueAsString(value);
return (Map) m.readValue(str, Map.class);
}
protected Map writeAndMap(Object value)
throws IOException
{
return writeAndMap(new ObjectMapper(), value);
}
protected String serializeAsString(ObjectMapper m, Object value)
throws IOException
{
return m.writeValueAsString(value);
}
protected String serializeAsString(Object value)
throws IOException
{
return serializeAsString(new ObjectMapper(), value);
}
}
TestVersions.java 0000664 0000000 0000000 00000001644 12373261211 0042447 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb package com.fasterxml.jackson.module.jaxb;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.Versioned;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
public class TestVersions extends BaseJaxbTest
{
public void testVersions()
{
assertVersion(new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()));
}
/*
/**********************************************************
/* Helper methods
/**********************************************************
*/
private void assertVersion(Versioned vers)
{
Version v = vers.version();
assertFalse("Should find version information (got "+v+")", v.isUknownVersion());
Version exp = PackageVersion.VERSION;
assertEquals(exp.toFullString(), v.toFullString());
assertEquals(exp, v);
}
}
0000775 0000000 0000000 00000000000 12373261211 0040732 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/adapters EntryType.java 0000664 0000000 0000000 00000000734 12373261211 0043544 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/adapters package com.fasterxml.jackson.module.jaxb.adapters;
/**
* @author Ryan Heaton
*/
public class EntryType {
private K key;
private V value;
public EntryType() {
}
public EntryType(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}
MapAdapter.java 0000664 0000000 0000000 00000001631 12373261211 0043614 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/adapters package com.fasterxml.jackson.module.jaxb.adapters;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class MapAdapter extends XmlAdapter, Map>
{
@Override
public MapType marshal(Map v) throws Exception
{
final List> theEntries = new LinkedList>();
for (final Map.Entry anEntry : v.entrySet()) {
theEntries.add(new EntryType(anEntry.getKey(), anEntry.getValue()));
}
return new MapType(theEntries);
}
@Override
public Map unmarshal(MapType v) throws Exception
{
final Map theMap = new HashMap();
for (final EntryType anEntry : v.getEntries()) {
theMap.put(anEntry.getKey(), anEntry.getValue());
}
return theMap;
}
} MapType.java 0000664 0000000 0000000 00000001006 12373261211 0043151 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/adapters package com.fasterxml.jackson.module.jaxb.adapters;
import com.fasterxml.jackson.module.jaxb.adapters.EntryType;
import java.util.List;
/**
* @author Ryan Heaton
*/
public class MapType {
public List> entries;
public MapType() {
}
public MapType(List> theEntries) {
this.entries = theEntries;
}
public List> getEntries() {
return entries;
}
public void setEntries(List> entries) {
this.entries = entries;
}
}
TestAdaptedMapType.java 0000664 0000000 0000000 00000006071 12373261211 0045303 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/adapters package com.fasterxml.jackson.module.jaxb.adapters;
import java.io.*;
import java.util.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
/**
* Tests for verifying JAXB adapter handling for {@link java.util.Map}
* types.
*/
public class TestAdaptedMapType extends BaseJaxbTest
{
static class ObjectContainingAMap
{
private Map myMap;
@XmlJavaTypeAdapter(MapAdapter.class)
public Map getMyMap() {
return myMap;
}
public void setMyMap(Map myMap) {
this.myMap = myMap;
}
}
static class StringMapWrapper {
@XmlJavaTypeAdapter(StringMapAdapter.class)
public Map values = new LinkedHashMap();
}
static class StringMapAdapter extends XmlAdapter, Map>
{
@Override
public Map marshal(Map input)
{
LinkedHashMap result = new LinkedHashMap();
for (Map.Entry entry : input.entrySet()) {
result.put(entry.getKey(), "M-"+entry.getValue());
}
return result;
}
@Override
public Map unmarshal(Map input)
{
LinkedHashMap result = new LinkedHashMap();
for (Map.Entry entry : input.entrySet()) {
result.put(entry.getKey(), "U-"+entry.getValue());
}
return result;
}
}
/*
/**********************************************************
/* Tests
/**********************************************************
*/
public void testJacksonAdaptedMapType() throws IOException
{
ObjectContainingAMap obj = new ObjectContainingAMap();
obj.setMyMap(new LinkedHashMap());
obj.getMyMap().put("this", "that");
obj.getMyMap().put("how", "here");
ObjectMapper mapper = getJaxbMapper();
// Ok, first serialize:
String json = mapper.writeValueAsString(obj);
obj = mapper.readValue(json, ObjectContainingAMap.class);
Map map = obj.getMyMap();
assertNotNull(map);
assertEquals(2, map.size());
assertEquals("here", map.get("how"));
}
public void testStringMaps() throws IOException
{
ObjectMapper mapper = getJaxbMapper();
StringMapWrapper map = mapper.readValue("{\"values\":{\"a\":\"b\"}}", StringMapWrapper.class);
assertNotNull(map.values);
assertEquals(1, map.values.size());
assertEquals("U-b", map.values.get("a"));
// and then out again
String json = mapper.writeValueAsString(map);
assertEquals("{\"values\":{\"a\":\"M-U-b\"}}", json);
}
}
TestAdapters.java 0000664 0000000 0000000 00000012640 12373261211 0044203 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/adapters package com.fasterxml.jackson.module.jaxb.adapters;
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
/**
* Unit tests for checking that JAXB type adapters work (to some
* degree, anyway).
* Related to issues [JACKSON-288], [JACKSON-411]
*
* @author tatu
*/
public class TestAdapters extends BaseJaxbTest
{
public static class SillyAdapter extends XmlAdapter
{
public SillyAdapter() { }
@Override
public Date unmarshal(String date) throws Exception {
return new Date(29L);
}
@Override
public String marshal(Date date) throws Exception {
return "XXX";
}
}
static class Bean
{
@XmlJavaTypeAdapter(SillyAdapter.class)
public Date value;
public Bean() { }
public Bean(long l) { value = new Date(l); }
}
// For [JACKSON-288]
static class Bean288 {
public List persons;
public Bean288() { }
public Bean288(String str) {
persons = new ArrayList();
persons.add(new Person(str));
}
}
static class Person
{
public String name;
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(DateAdapter.class)
protected Calendar date;
public Person() { }
public Person(String n) {
name = n;
date = Calendar.getInstance();
date.setTime(new Date(0L));
}
}
public static class DateAdapter
extends XmlAdapter
{
public DateAdapter() { }
@Override
public Calendar unmarshal(String value) {
return (javax.xml.bind.DatatypeConverter.parseDateTime(value));
}
@Override
public String marshal(Calendar value) {
if (value == null) {
return null;
}
return (javax.xml.bind.DatatypeConverter.printDateTime(value));
}
}
// [JACKSON-656]
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Paging", propOrder = { "numFound" })
public static class Bean656 {
@XmlElement(type = String.class)
@XmlJavaTypeAdapter(Adapter1.class)
@XmlSchemaType(name = "long")
protected Long numFound;
public Long getNumFound() {
return numFound;
}
public void setNumFound(Long value) {
this.numFound = value;
}
}
public static class Adapter1 extends XmlAdapter {
@Override
public Long unmarshal(String value) {
return ((long) javax.xml.bind.DatatypeConverter.parseLong(value));
}
@Override
public String marshal(Long value) {
if (value == null) {
return null;
}
return (javax.xml.bind.DatatypeConverter.printLong((long) (long) value));
}
}
// [Issue-10]: Infinite recursion in "self" adapters
public static class IdentityAdapter extends XmlAdapter {
@Override
public IdentityAdapterBean unmarshal(IdentityAdapterBean b) {
b.value += "U";
return b;
}
@Override
public IdentityAdapterBean marshal(IdentityAdapterBean b) {
if (b != null) {
b.value += "M";
}
return b;
}
}
@XmlJavaTypeAdapter(IdentityAdapter.class)
static class IdentityAdapterBean
{
public String value;
public IdentityAdapterBean() { }
public IdentityAdapterBean(String s) { value = s; }
}
static class IdentityAdapterPropertyBean
{
@XmlJavaTypeAdapter(IdentityAdapter.class)
public String value;
public IdentityAdapterPropertyBean() { }
public IdentityAdapterPropertyBean(String s) { value = s; }
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
public void testSimpleAdapterSerialization() throws Exception
{
Bean bean = new Bean(123L);
assertEquals("{\"value\":\"XXX\"}", getJaxbMapper().writeValueAsString(bean));
}
public void testSimpleAdapterDeserialization() throws Exception
{
Bean bean = getJaxbMapper().readValue("{\"value\":\"abc\"}", Bean.class);
assertNotNull(bean.value);
assertEquals(29L, bean.value.getTime());
}
// [JACKSON-288]
public void testDateAdapter() throws Exception
{
Bean288 input = new Bean288("test");
ObjectMapper mapper = getJaxbMapper();
String json = mapper.writeValueAsString(input);
Bean288 output = mapper.readValue(json, Bean288.class);
assertNotNull(output);
}
// [JACKSON-656]
public void testJackson656() throws Exception
{
Bean656 bean = new Bean656();
bean.setNumFound(3232l);
ObjectMapper mapper = getJaxbMapper();
String json = mapper.writeValueAsString(bean);
assertEquals("{\"numFound\":\"3232\"}", json);
}
}
TestAdaptersForContainers.java 0000664 0000000 0000000 00000010564 12373261211 0046703 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/adapters package com.fasterxml.jackson.module.jaxb.adapters;
import java.util.*;
import java.util.Map.Entry;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
import com.fasterxml.jackson.module.jaxb.introspect.TestJaxbAnnotationIntrospector.KeyValuePair;
/**
* Unit tests to check that {@link XmlAdapter}s also work with
* container types (Lists, Maps)
*/
public class TestAdaptersForContainers extends BaseJaxbTest
{
// Support for Maps
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public static class ParentJAXBBean
{
@XmlJavaTypeAdapter(JAXBMapAdapter.class)
private Map params = new HashMap();
public Map getParams() {
return params;
}
public void setParams(Map params) {
this.params = params;
}
}
public static class JAXBMapAdapter extends XmlAdapter,Map> {
@Override
public List marshal(Map arg0) throws Exception {
List keyValueList = new ArrayList();
for(Entry entry : arg0.entrySet()) {
KeyValuePair keyValuePair = new KeyValuePair();
keyValuePair.setKey(entry.getKey());
keyValuePair.setValue(entry.getValue());
keyValueList.add(keyValuePair);
}
return keyValueList;
}
@Override
public Map unmarshal(List arg0) throws Exception
{
HashMap hashMap = new HashMap();
for (int i = 0; i < arg0.size(); i++) {
hashMap.put(arg0.get(i).getKey(), arg0.get(i).getValue());
}
return hashMap;
}
}
// [JACKSON-722]
public static class SillyAdapter extends XmlAdapter
{
public SillyAdapter() { }
@Override
public Date unmarshal(String date) throws Exception {
return new Date(29L);
}
@Override
public String marshal(Date date) throws Exception {
return "XXX";
}
}
static class Wrapper {
@XmlJavaTypeAdapter(SillyAdapter.class)
public List values;
public Wrapper() { }
public Wrapper(long l) {
values = new ArrayList();
values.add(new Date(l));
}
}
/*
/**********************************************************
/* Unit tests, Lists
/**********************************************************
*/
public void testAdapterForList() throws Exception
{
Wrapper w = new Wrapper(123L);
assertEquals("{\"values\":[\"XXX\"]}", getJaxbMapper().writeValueAsString(w));
}
public void testSimpleAdapterDeserialization() throws Exception
{
Wrapper w = getJaxbMapper().readValue("{\"values\":[\"abc\"]}", Wrapper.class);
assertNotNull(w);
assertNotNull(w.values);
assertEquals(1, w.values.size());
assertEquals(29L, w.values.get(0).getTime());
}
/*
/**********************************************************
/* Unit tests, Map-related
/**********************************************************
*/
public void testAdapterForBeanWithMap() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
ParentJAXBBean parentJaxbBean = new ParentJAXBBean();
HashMap params = new HashMap();
params.put("sampleKey", "sampleValue");
parentJaxbBean.setParams(params);
String json = mapper.writeValueAsString(parentJaxbBean);
// uncomment to see what the json looks like.
//System.out.println(json);
//now make sure it gets deserialized correctly.
ParentJAXBBean readEx = mapper.readValue(json, ParentJAXBBean.class);
assertEquals("sampleValue", readEx.getParams().get("sampleKey"));
}
}
TestIdentityAdapters.java 0000664 0000000 0000000 00000005331 12373261211 0045714 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/adapters package com.fasterxml.jackson.module.jaxb.adapters;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
/**
* Failing unit tests related to Adapter handling.
*/
public class TestIdentityAdapters extends BaseJaxbTest
{
// [Issue-10]: Infinite recursion in "self" adapters
public static class IdentityAdapter extends XmlAdapter {
@Override
public IdentityAdapterBean unmarshal(IdentityAdapterBean b) {
return new IdentityAdapterBean(b.value + "U");
}
@Override
public IdentityAdapterBean marshal(IdentityAdapterBean b) {
return new IdentityAdapterBean(b.value + "M");
}
}
public static class IdentityStringAdapter extends XmlAdapter {
@Override
public String unmarshal(String b) {
return b + "U";
}
@Override
public String marshal(String b) {
return b + "M";
}
}
@XmlJavaTypeAdapter(IdentityAdapter.class)
static class IdentityAdapterBean
{
public String value;
protected IdentityAdapterBean() { }
public IdentityAdapterBean(String s) { value = s; }
}
static class IdentityAdapterPropertyBean
{
@XmlJavaTypeAdapter(IdentityStringAdapter.class)
public String value;
public IdentityAdapterPropertyBean() { }
public IdentityAdapterPropertyBean(String s) { value = s; }
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
// [Issue-10]
public void testIdentityAdapterForClass() throws Exception
{
IdentityAdapterBean input = new IdentityAdapterBean("A");
ObjectMapper mapper = getJaxbMapper();
String json = mapper.writeValueAsString(input);
assertEquals("{\"value\":\"AM\"}", json);
IdentityAdapterBean result = mapper.readValue(json, IdentityAdapterBean.class);
assertEquals("AMU", result.value);
}
// [Issue-10]
public void testIdentityAdapterForProperty() throws Exception
{
IdentityAdapterPropertyBean input = new IdentityAdapterPropertyBean("B");
ObjectMapper mapper = getJaxbMapper();
String json = mapper.writeValueAsString(input);
assertEquals("{\"value\":\"BM\"}", json);
IdentityAdapterPropertyBean result = mapper.readValue(json, IdentityAdapterPropertyBean.class);
assertEquals("BMU", result.value);
}
}
0000775 0000000 0000000 00000000000 12373261211 0040540 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/failing TestUnwrapping.java 0000664 0000000 0000000 00000004100 12373261211 0044370 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/failing package com.fasterxml.jackson.module.jaxb.failing;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
public class TestUnwrapping extends BaseJaxbTest
{
@XmlRootElement
static class Bean
{
@JsonUnwrapped
@XmlAnyElement(lax = true)
@XmlElementRefs( { @XmlElementRef(name = "a", type = A.class),
@XmlElementRef(name = "b", type = B.class) })
public R r;
public String name;
public Bean() { }
}
static class A {
public int count;
public A() { }
public A(int count) {
this.count = count;
}
}
static class B {
public String type;
public B() { }
public B(String type) {
this.type = type;
}
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
// not asserting anything
public void testXmlElementAndXmlElementRefs() throws Exception
{
Bean bean = new Bean ();
bean.r = new A(12);
bean.name = "test";
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector pair = new AnnotationIntrospectorPair(
new JacksonAnnotationIntrospector(),
new JaxbAnnotationIntrospector(mapper.getTypeFactory())
);
mapper.setAnnotationIntrospector(pair);
// mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector());
// mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector());
String json = mapper.writeValueAsString(bean);
// !!! TODO: verify
assertNotNull(json);
}
}
0000775 0000000 0000000 00000000000 12373261211 0037523 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/id TestXmlID.java 0000664 0000000 0000000 00000006702 12373261211 0042210 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/id package com.fasterxml.jackson.module.jaxb.id;
import java.util.*;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
/**
* Simple testing to verify that XmlID and XMLIDREF handling works
* to degree we can make it work.
*/
public class TestXmlID extends BaseJaxbTest
{
// From sample used in [http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html]
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
static class Company {
@XmlElement(name="employee")
protected List employees;
public Company() {
employees = new ArrayList();
}
}
@XmlAccessorType(XmlAccessType.FIELD)
static class Employee {
@XmlAttribute
@XmlID
protected String id;
@XmlAttribute
protected String name;
@XmlIDREF
protected Employee manager;
@XmlElement(name="report")
@XmlIDREF
protected List reports;
public Employee() {
reports = new ArrayList();
}
}
/*
/**********************************************************
/* Test methods
/**********************************************************
*/
public void testSimpleRefs() throws Exception
{
final ObjectMapper mapper = getJaxbMapper();
Company company = new Company();
Employee employee1 = new Employee();
employee1.id = "1";
employee1.name = "Jane Doe";
company.employees.add(employee1);
Employee employee2 = new Employee();
employee2.id = "2";
employee2.name = "John Smith";
employee2.manager = employee1;
employee1.reports.add(employee2);
company.employees.add(employee2);
Employee employee3 = new Employee();
employee3.id = "3";
employee3.name = "Anne Jones";
employee3.manager = employee1;
employee1.reports.add(employee3);
company.employees.add(employee3);
String json = mapper.writeValueAsString(company);
// this is the easy part actually...
assertNotNull(json);
// then try bringing back
Company result = mapper.readValue(json, Company.class);
assertNotNull(result);
assertEquals(3, company.employees.size());
assertEquals("Jane Doe", company.employees.get(0).name);
assertEquals("1", company.employees.get(0).id);
assertEquals("John Smith", company.employees.get(1).name);
assertEquals("2", company.employees.get(1).id);
assertEquals("Anne Jones", company.employees.get(2).name);
assertEquals("3", company.employees.get(2).id);
// then actual references:
final Employee resEmpl1 = company.employees.get(0);
final Employee resEmpl2 = company.employees.get(1);
final Employee resEmpl3 = company.employees.get(2);
assertEquals(2, resEmpl1.reports.size());
// Jane has John and Anne as reports:
assertSame(resEmpl2, resEmpl1.reports.get(0));
assertSame(resEmpl3, resEmpl1.reports.get(1));
assertEquals(0, resEmpl2.reports.size());
assertEquals(0, resEmpl3.reports.size());
// and they have her as manager
assertSame(resEmpl1, resEmpl2.manager);
assertSame(resEmpl1, resEmpl3.manager);
}
}
TestXmlID2.java 0000664 0000000 0000000 00000011236 12373261211 0042270 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/id package com.fasterxml.jackson.module.jaxb.id;
import java.util.*;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
// Reproduction of [Issue-9]
public class TestXmlID2 extends BaseJaxbTest
{
@XmlRootElement(name = "department")
@XmlAccessorType(XmlAccessType.FIELD)
static class Department {
@XmlElement
@XmlID
public Long id;
public String name;
@XmlIDREF
public List employees = new ArrayList();
protected Department() { }
public Department(Long id) {
this.id = id;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setEmployees(List employees) {
this.employees = employees;
}
}
@XmlRootElement(name = "user")
@XmlAccessorType(XmlAccessType.FIELD)
static class User
{
@XmlElement @XmlID
public Long id;
public String username;
public String email;
@XmlIDREF
public Department department;
protected User() { }
public User(Long id) {
this.id = id;
}
public void setId(Long id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setEmail(String email) {
this.email = email;
}
public void setDepartment(Department department) {
this.department = department;
}
}
private List getUserList()
{
List resultList = new ArrayList();
List users = new java.util.ArrayList();
User user1, user2, user3;
Department dep;
user1 = new User(11L);
user1.setUsername("11");
user1.setEmail("11@test.com");
user2 = new User(22L);
user2.setUsername("22");
user2.setEmail("22@test.com");
user3 = new User(33L);
user3.setUsername("33");
user3.setEmail("33@test.com");
dep = new Department(9L);
dep.setName("department9");
user1.setDepartment(dep);
users.add(user1);
user2.setDepartment(dep);
users.add(user2);
dep.setEmployees(users);
resultList.clear();
resultList.add(user1);
resultList.add(user2);
resultList.add(user3);
return resultList;
}
public void testIdWithJacksonRules() throws Exception
{
String expected = "[{\"id\":11,\"username\":\"11\",\"email\":\"11@test.com\","
+"\"department\":{\"id\":9,\"name\":\"department9\",\"employees\":["
+"11,{\"id\":22,\"username\":\"22\",\"email\":\"22@test.com\","
+"\"department\":9}]}},22,{\"id\":33,\"username\":\"33\",\"email\":\"33@test.com\",\"department\":null}]";
ObjectMapper mapper = new ObjectMapper();
// true -> ignore XmlIDREF annotation
mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(mapper.getTypeFactory(), true));
// first, with default settings (first NOT as id)
List users = getUserList();
String json = mapper.writeValueAsString(users);
assertEquals(expected, json);
List result = mapper.readValue(json, new TypeReference>() { });
assertEquals(3, result.size());
assertEquals(Long.valueOf(11), result.get(0).id);
assertEquals(Long.valueOf(22), result.get(1).id);
assertEquals(Long.valueOf(33), result.get(2).id);
}
public void testIdWithJaxbRules() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
// but then also variant where ID is ALWAYS used for XmlID / XmlIDREF
mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(mapper.getTypeFactory()));
List users = getUserList();
final String json = mapper.writeValueAsString(users);
String expected = "[{\"id\":11,\"username\":\"11\",\"email\":\"11@test.com\",\"department\":9}"
+",{\"id\":22,\"username\":\"22\",\"email\":\"22@test.com\",\"department\":9}"
+",{\"id\":33,\"username\":\"33\",\"email\":\"33@test.com\",\"department\":null}]";
assertEquals(expected, json);
// However, there is no way to resolve those back, without some external mechanism...
}
}
0000775 0000000 0000000 00000000000 12373261211 0041321 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/introspect Content.java 0000664 0000000 0000000 00000011712 12373261211 0043600 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/introspect package com.fasterxml.jackson.module.jaxb.introspect;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.*;
import org.w3c.dom.Element;
/**
* Represents an atom:content element.
*
* Per RFC4287:
*
*
* The "atom:content" element either contains or links to the content of
* the entry. The content of atom:content is Language-Sensitive.
*
* atomInlineTextContent =
* element atom:content {
* atomCommonAttributes,
* attribute type { "text" | "html" }?,
* (text)*
* }
*
* atomInlineXHTMLContent =
* element atom:content {
* atomCommonAttributes,
* attribute type { "xhtml" },
* xhtmlDiv
* }
* atomInlineOtherContent =
* element atom:content {
* atomCommonAttributes,
* attribute type { atomMediaType }?,
* (text|anyElement)*
* }
*
* atomOutOfLineContent =
* element atom:content {
* atomCommonAttributes,
* attribute type { atomMediaType }?,
* attribute src { atomUri },
* empty
* }
*
* atomContent = atomInlineTextContent
* | atomInlineXHTMLContent
* | atomInlineOtherContent
* | atomOutOfLineContent
*
*
*
* @author Bill Burke
* @version $Revision: 1666 $
*/
@XmlRootElement(name = "content")
@XmlAccessorType(XmlAccessType.PROPERTY)
class Content extends CommonAttributes
{
private String type;
private MediaType mediaType;
private String text;
private Element element;
private URI src;
private List value;
protected Object jaxbObject;
@XmlAnyElement
@XmlMixed
public List getValue() { return value; }
public void setValue(List value) { this.value = value; }
@XmlAttribute
public URI getSrc() { return src; }
public void setSrc(URI src) { this.src = src; }
/**
* Mime type of the content
*/
@XmlTransient
public MediaType getType()
{
if (mediaType == null)
{
if (type.equals("html")) mediaType = MediaType.TEXT_HTML_TYPE;
else if (type.equals("text")) mediaType = MediaType.TEXT_PLAIN_TYPE;
else if (type.equals("xhtml")) mediaType = MediaType.APPLICATION_XHTML_XML_TYPE;
else mediaType = MediaType.valueOf(type);
}
return mediaType;
}
public void setType(MediaType type)
{
mediaType = type;
if (type.equals(MediaType.TEXT_PLAIN_TYPE)) this.type = "text";
else if (type.equals(MediaType.TEXT_HTML_TYPE)) this.type = "html";
else if (type.equals(MediaType.APPLICATION_XHTML_XML_TYPE)) this.type = "xhtml";
else this.type = type.toString();
}
@XmlAttribute(name = "type")
public String getRawType() { return type; }
public void setRawType(String type) { this.type = type; }
@XmlTransient
public String getText()
{
if (value == null) return null;
if (value.size() == 0) return null;
if (text != null) return text;
StringBuffer buf = new StringBuffer();
for (Object obj : value)
{
if (obj instanceof String) buf.append(obj.toString());
}
text = buf.toString();
return text;
}
public void setText(String text)
{
if (value == null) value = new ArrayList();
if (this.text != null && value != null) value.clear();
this.text = text;
value.add(text);
}
/**
* Get content as an XML Element if the content is XML. Otherwise, this will just return null.
*/
@XmlTransient
public Element getElement()
{
if (value == null) return null;
if (element != null) return element;
for (Object obj : value)
{
if (obj instanceof Element)
{
element = (Element) obj;
return element;
}
}
return null;
}
/**
* Set the content to an XML Element
*
* @param element
*/
public void setElement(Element element)
{
if (value == null) value = new ArrayList();
if (this.element != null && value != null) value.clear();
this.element = element;
value.add(element);
}
}
@XmlAccessorType(XmlAccessType.PROPERTY)
class CommonAttributes
{
private String language;
private URI base;
private Map,?> extensionAttributes = new HashMap();
@XmlAttribute(name = "lang", namespace = "http://www.w3.org/XML/1998/namespace")
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
@XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace")
public URI getBase() {
return base;
}
public void setBase(URI base) {
this.base = base;
}
@XmlAnyAttribute
public Map,?> getExtensionAttributes() {
return extensionAttributes;
}
}
TestAccessType.java 0000664 0000000 0000000 00000005210 12373261211 0045065 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/introspect package com.fasterxml.jackson.module.jaxb.introspect;
import java.util.Date;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
/**
* Unit test(s) written for [JACKSON-303]; we should be able to detect setter
* even though it is not annotated, because there is matching annotated getter.
*/
public class TestAccessType
extends BaseJaxbTest
{
@XmlRootElement(name = "model")
@XmlAccessorType(XmlAccessType.NONE)
public static class SimpleNamed
{
protected String name;
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "LoggedActivity")
public static class Bean288
{
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(MyAdapter.class)
@XmlSchemaType(name = "date")
public Date date;
}
public static class MyAdapter
extends XmlAdapter
{
@Override
public String marshal(Date arg) throws Exception {
return "String="+arg.getTime();
}
@Override
public Date unmarshal(String arg0) throws Exception {
return new Date(Long.parseLong(arg0));
}
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
public void testXmlElementTypeDeser() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
SimpleNamed originalModel = new SimpleNamed();
originalModel.setName("Foobar");
String json = mapper.writeValueAsString(originalModel);
SimpleNamed result = null;
try {
result = mapper.readValue(json, SimpleNamed.class);
} catch (Exception ie) {
fail("Failed to deserialize '"+json+"': "+ie.getMessage());
}
if (!"Foobar".equals(result.name)) {
fail("Failed, JSON == '"+json+"')");
}
}
public void testForJackson288() throws Exception
{
final long TIMESTAMP = 12345678L;
ObjectMapper mapper = getJaxbMapper();
Bean288 bean = mapper.readValue("{\"date\":"+TIMESTAMP+"}", Bean288.class);
assertNotNull(bean);
Date d = bean.date;
assertNotNull(d);
assertEquals(TIMESTAMP, d.getTime());
}
}
TestAnnotationPriority.java 0000664 0000000 0000000 00000002770 12373261211 0046706 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/introspect package com.fasterxml.jackson.module.jaxb.introspect;
import javax.xml.bind.annotation.XmlElement;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
/**
* Unit test(s) to verify that annotations from super classes and
* interfaces are properly used (for example, wrt [JACKSON-450])
*/
public class TestAnnotationPriority extends BaseJaxbTest
{
/*
/**********************************************************
/* Helper beans
/**********************************************************
*/
public interface Identifiable {
public String getId();
public void setId(String i);
}
static abstract class IdBase
{
protected String id;
protected IdBase(String id) { this.id = id; }
@XmlElement(name="name")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
static class IdBean extends IdBase implements Identifiable {
public IdBean(String id) { super(id); }
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
public void testInterfacesAndClasses() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
String json = mapper.writeValueAsString(new IdBean("foo"));
assertEquals("{\"name\":\"foo\"}", json);
}
}
TestIntrospectorPair.java 0000664 0000000 0000000 00000025077 12373261211 0046346 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/introspect package com.fasterxml.jackson.module.jaxb.introspect;
import java.util.*;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
/**
* Simple testing that AnnotationIntrospector.Pair
works as
* expected, when used with Jackson and JAXB-based introspector.
*
* @author Tatu Saloranta
*/
public class TestIntrospectorPair
extends BaseJaxbTest
{
final static AnnotationIntrospector _jacksonAI = new JacksonAnnotationIntrospector();
final static AnnotationIntrospector _jaxbAI = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
/*
/**********************************************************
/* Helper beans
/**********************************************************
*/
/**
* Simple test bean for verifying basic field detection and property
* naming annotation handling
*/
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
static class NamedBean
{
@JsonProperty
private String jackson = "1";
@XmlElement(name="jaxb")
protected String jaxb = "2";
@JsonProperty("bothJackson")
@XmlElement(name="bothJaxb")
private String bothString = "3";
public String notAGetter() { return "xyz"; }
}
/**
* Another bean for verifying details of property naming
*/
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
static class NamedBean2
{
@JsonProperty("")
@XmlElement(name="jaxb")
public String foo = "abc";
@JsonProperty("jackson")
@XmlElement()
public String getBar() { return "123"; }
// JAXB, alas, requires setters for all properties too
public void setBar(String v) { }
}
/**
* And a bean to check how "ignore" annotations work with
* various combinations of annotation introspectors
*/
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
static class IgnoreBean
{
@JsonIgnore
public int getNumber() { return 13; }
@XmlTransient
public String getText() { return "abc"; }
public boolean getAny() { return true; }
}
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
static class IgnoreFieldBean
{
@JsonIgnore public int number = 7;
@XmlTransient public String text = "123";
public boolean any = true;
}
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlRootElement(name="test", namespace="urn:whatever")
static class NamespaceBean
{
public String string;
}
// Testing [JACKSON-495]
static class CreatorBean {
@JsonCreator
public CreatorBean(@JsonProperty("name") String name) {
;
}
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
public void testSimple() throws Exception
{
ObjectMapper mapper;
AnnotationIntrospector pair;
Map result;
mapper = new ObjectMapper();
// first: test with Jackson/Jaxb pair (jackson having precedence)
pair = new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI);
mapper.setAnnotationIntrospector(pair);
result = writeAndMap(mapper, new NamedBean());
assertEquals(3, result.size());
assertEquals("1", result.get("jackson"));
assertEquals("2", result.get("jaxb"));
// jackson one should have priority
assertEquals("3", result.get("bothJackson"));
mapper = new ObjectMapper();
pair = new AnnotationIntrospectorPair(_jaxbAI, _jacksonAI);
mapper.setAnnotationIntrospector(pair);
result = writeAndMap(mapper, new NamedBean());
assertEquals(3, result.size());
assertEquals("1", result.get("jackson"));
assertEquals("2", result.get("jaxb"));
// JAXB one should have priority
assertEquals("3", result.get("bothJaxb"));
}
public void testNaming() throws Exception
{
ObjectMapper mapper;
AnnotationIntrospector pair;
Map result;
mapper = new ObjectMapper();
// first: test with Jackson/Jaxb pair (jackson having precedence)
pair = new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI);
mapper.setAnnotationIntrospector(pair);
result = writeAndMap(mapper, new NamedBean2());
assertEquals(2, result.size());
// order shouldn't really matter here...
assertEquals("123", result.get("jackson"));
assertEquals("abc", result.get("jaxb"));
mapper = new ObjectMapper();
pair = new AnnotationIntrospectorPair(_jaxbAI, _jacksonAI);
mapper.setAnnotationIntrospector(pair);
result = writeAndMap(mapper, new NamedBean2());
/* Hmmh. Not 100% sure what JAXB would dictate.... thus...
*/
assertEquals(2, result.size());
assertEquals("abc", result.get("jaxb"));
//assertEquals("123", result.get("jackson"));
}
public void testSimpleIgnore() throws Exception
{
// first: only Jackson introspector (default)
ObjectMapper mapper = new ObjectMapper();
Map result = writeAndMap(mapper, new IgnoreBean());
assertEquals(2, result.size());
assertEquals("abc", result.get("text"));
assertEquals(Boolean.TRUE, result.get("any"));
// Then JAXB only
mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(_jaxbAI);
// jackson one should have priority
result = writeAndMap(mapper, new IgnoreBean());
assertEquals(2, result.size());
assertEquals(Integer.valueOf(13), result.get("number"));
assertEquals(Boolean.TRUE, result.get("any"));
// then both, Jackson first
mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI));
result = writeAndMap(mapper, new IgnoreBean());
assertEquals(1, result.size());
assertEquals(Boolean.TRUE, result.get("any"));
// then both, JAXB first
mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(new AnnotationIntrospectorPair(_jaxbAI, _jacksonAI));
result = writeAndMap(mapper, new IgnoreBean());
assertEquals(1, result.size());
assertEquals(Boolean.TRUE, result.get("any"));
}
public void testSimpleFieldIgnore() throws Exception
{
ObjectMapper mapper;
// first: only Jackson introspector (default)
mapper = new ObjectMapper();
Map result = writeAndMap(mapper, new IgnoreFieldBean());
assertEquals(2, result.size());
assertEquals("123", result.get("text"));
assertEquals(Boolean.TRUE, result.get("any"));
// Then JAXB only
mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(_jaxbAI);
// jackson one should have priority
result = writeAndMap(mapper, new IgnoreFieldBean());
assertEquals(2, result.size());
assertEquals(Integer.valueOf(7), result.get("number"));
assertEquals(Boolean.TRUE, result.get("any"));
// then both, Jackson first
mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI));
result = writeAndMap(mapper, new IgnoreFieldBean());
assertEquals(1, result.size());
assertEquals(Boolean.TRUE, result.get("any"));
// then both, JAXB first
mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(new AnnotationIntrospectorPair(_jaxbAI, _jacksonAI));
result = writeAndMap(mapper, new IgnoreFieldBean());
assertEquals(1, result.size());
assertEquals(Boolean.TRUE, result.get("any"));
}
public void testSimpleOther() throws Exception
{
// Let's use Jackson+JAXB comb
AnnotationIntrospector ann = new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI);
AnnotatedClass testClass = AnnotatedClass.construct(NamedBean.class, ann, null);
//assertNull(ann.findSerializationInclusion(testClass, null));
JavaType type = TypeFactory.defaultInstance().constructType(Object.class);
assertNull(ann.findDeserializationType(testClass, type));
assertNull(ann.findDeserializationContentType(testClass, type));
assertNull(ann.findDeserializationKeyType(testClass, type));
assertNull(ann.findSerializationType(testClass));
assertNull(ann.findDeserializer(testClass));
assertNull(ann.findContentDeserializer(testClass));
assertNull(ann.findKeyDeserializer(testClass));
assertFalse(ann.hasCreatorAnnotation(testClass));
}
public void testRootName() throws Exception
{
// first: test with Jackson/Jaxb pair (jackson having precedence)
AnnotationIntrospector pair = new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI);
assertNull(pair.findRootName(AnnotatedClass.construct(NamedBean.class, pair, null)));
PropertyName name = pair.findRootName(AnnotatedClass.construct(NamespaceBean.class, pair, null));
assertNotNull(name);
assertEquals("test", name.getSimpleName());
assertEquals("urn:whatever", name.getNamespace());
// then reverse; should make no difference
pair = new AnnotationIntrospectorPair(_jaxbAI, _jacksonAI);
name = pair.findRootName(AnnotatedClass.construct(NamedBean.class, pair, null));
assertNull(name);
name = pair.findRootName(AnnotatedClass.construct(NamespaceBean.class, pair, null));
assertEquals("test", name.getSimpleName());
assertEquals("urn:whatever", name.getNamespace());
}
/**
* Test that will just use Jackson annotations, but did trigger [JACKSON-495] due to a bug
* in JAXB annotation introspector.
*/
public void testIssue495() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI));
CreatorBean bean = mapper.readValue("{\"name\":\"foo\"}", CreatorBean.class);
assertNotNull(bean);
}
}
TestJaxbAnnotationIntrospector.java 0000664 0000000 0000000 00000025123 12373261211 0050362 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/introspect package com.fasterxml.jackson.module.jaxb.introspect;
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.namespace.QName;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
/**
* Tests for verifying that JAXB annotation based introspector
* implementation works as expected
*
* @author Ryan Heaton
* @author Tatu Saloranta
*/
public class TestJaxbAnnotationIntrospector
extends BaseJaxbTest
{
/*
/****************************************************
/* Helper beans
/****************************************************
*/
public static enum EnumExample {
@XmlEnumValue("Value One")
VALUE1
}
public static class JaxbExample
{
private String attributeProperty;
private String elementProperty;
private List wrappedElementProperty;
private EnumExample enumProperty;
private QName qname;
private QName qname1;
private String propertyToIgnore;
@XmlJavaTypeAdapter(QNameAdapter.class)
public QName getQname()
{
return qname;
}
public void setQname(QName qname)
{
this.qname = qname;
}
public QName getQname1()
{
return qname1;
}
public void setQname1(QName qname1)
{
this.qname1 = qname1;
}
@XmlAttribute(name="myattribute")
public String getAttributeProperty()
{
return attributeProperty;
}
public void setAttributeProperty(String attributeProperty)
{
this.attributeProperty = attributeProperty;
}
@XmlElement(name="myelement")
public String getElementProperty()
{
return elementProperty;
}
public void setElementProperty(String elementProperty)
{
this.elementProperty = elementProperty;
}
@XmlElementWrapper(name="mywrapped")
public List getWrappedElementProperty()
{
return wrappedElementProperty;
}
public void setWrappedElementProperty(List wrappedElementProperty)
{
this.wrappedElementProperty = wrappedElementProperty;
}
public EnumExample getEnumProperty()
{
return enumProperty;
}
public void setEnumProperty(EnumExample enumProperty)
{
this.enumProperty = enumProperty;
}
@XmlTransient
public String getPropertyToIgnore()
{
return propertyToIgnore;
}
public void setPropertyToIgnore(String propertyToIgnore)
{
this.propertyToIgnore = propertyToIgnore;
}
}
public static class QNameAdapter extends XmlAdapter {
@Override
public QName unmarshal(String v) throws Exception
{
return QName.valueOf(v);
}
@Override
public String marshal(QName v) throws Exception
{
return (v == null) ? null : v.toString();
}
}
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public static class SimpleBean
{
@XmlElement
protected String jaxb = "1";
@XmlElement
protected String jaxb2 = "2";
@XmlElement(name="jaxb3")
private String oddName = "3";
public String notAGetter() { return "xyz"; }
@XmlTransient
public int foobar = 3;
}
@SuppressWarnings("unused")
@XmlAccessorType(XmlAccessType.FIELD)
public static class SimpleBean2 {
protected String jaxb = "1";
private String jaxb2 = "2";
@XmlElement(name="jaxb3")
private String oddName = "3";
}
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlRootElement(namespace="urn:class")
static class NamespaceBean
{
@XmlElement(namespace="urn:method")
public String string;
}
@XmlRootElement(name="test")
static class RootNameBean { }
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
public static class AlphaBean
{
public int c = 3;
public int a = 1;
public int b = 2;
}
public static class KeyValuePair {
private String key;
private String value;
public KeyValuePair() {}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
// Beans for [JACKSON-256]
@XmlRootElement
static class BeanWithNillable {
public Nillable X;
}
@XmlRootElement
static class Nillable {
@XmlElement (name="Z", nillable=true)
Integer Z;
}
/*
/****************************************************
/* Unit tests
/****************************************************
*/
private final ObjectMapper MAPPER = getJaxbMapper();
public void testDetection() throws Exception
{
Map result = writeAndMap(MAPPER, new SimpleBean());
assertEquals(3, result.size());
assertEquals("1", result.get("jaxb"));
assertEquals("2", result.get("jaxb2"));
assertEquals("3", result.get("jaxb3"));
result = writeAndMap(MAPPER, new SimpleBean2());
assertEquals(3, result.size());
assertEquals("1", result.get("jaxb"));
assertEquals("2", result.get("jaxb2"));
assertEquals("3", result.get("jaxb3"));
}
/**
* tests getting serializer/deserializer instances.
*/
public void testSerializeDeserializeWithJaxbAnnotations() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
// test expects that wrapper name be used...
mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
JaxbExample ex = new JaxbExample();
QName qname = new QName("urn:hi", "hello");
ex.setQname(qname);
QName qname1 = new QName("urn:hi", "hello1");
ex.setQname1(qname1);
ex.setAttributeProperty("attributeValue");
ex.setElementProperty("elementValue");
ex.setWrappedElementProperty(Arrays.asList("wrappedElementValue"));
ex.setEnumProperty(EnumExample.VALUE1);
ex.setPropertyToIgnore("ignored");
String json = mapper.writeValueAsString(ex);
// uncomment to see what the JSON looks like.
// System.out.println(json);
//make sure the json is written out correctly.
JsonNode node = mapper.readValue(json, JsonNode.class);
assertEquals(qname.toString(), node.get("qname").asText());
JsonNode attr = node.get("myattribute");
assertNotNull(attr);
assertEquals("attributeValue", attr.asText());
assertEquals("elementValue", node.get("myelement").asText());
assertTrue(node.has("mywrapped"));
assertEquals(1, node.get("mywrapped").size());
assertEquals("wrappedElementValue", node.get("mywrapped").get(0).asText());
assertEquals("Value One", node.get("enumProperty").asText());
assertNull(node.get("propertyToIgnore"));
//now make sure it gets deserialized correctly.
JaxbExample readEx = mapper.readValue(json, JaxbExample.class);
assertEquals(ex.qname, readEx.qname);
assertEquals(ex.qname1, readEx.qname1);
assertEquals(ex.attributeProperty, readEx.attributeProperty);
assertEquals(ex.elementProperty, readEx.elementProperty);
assertEquals(ex.wrappedElementProperty, readEx.wrappedElementProperty);
assertEquals(ex.enumProperty, readEx.enumProperty);
assertNull(readEx.propertyToIgnore);
}
public void testRootNameAccess() throws Exception
{
AnnotationIntrospector ai = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
// If no @XmlRootElement, should get null (unless pkg has etc)
assertNull(ai.findRootName(AnnotatedClass.construct(SimpleBean.class, ai, null)));
// With @XmlRootElement, but no name, empty String
PropertyName rootName = ai.findRootName(AnnotatedClass.construct(NamespaceBean.class, ai, null));
assertNotNull(rootName);
assertEquals("", rootName.getSimpleName());
assertEquals("urn:class", rootName.getNamespace());
// and otherwise explicit name
rootName = ai.findRootName(AnnotatedClass.construct(RootNameBean.class, ai, null));
assertNotNull(rootName);
assertEquals("test", rootName.getSimpleName());
assertNull(rootName.getNamespace());
}
// JAXB can specify that properties are to be written in alphabetic order...
public void testSerializationAlphaOrdering() throws Exception
{
assertEquals("{\"a\":1,\"b\":2,\"c\":3}", MAPPER.writeValueAsString(new AlphaBean()));
}
// Test for [JACKSON-256], thanks John.
public void testWriteNulls() throws Exception
{
BeanWithNillable bean = new BeanWithNillable();
bean.X = new Nillable();
assertEquals("{\"X\":{\"Z\":null}}", MAPPER.writeValueAsString(bean));
}
/**
* Additional simple tests to ensure we will retain basic namespace information
* now that it can be included
*
* @since 2.1
*/
public void testNamespaces() throws Exception
{
JaxbAnnotationIntrospector ai = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
AnnotatedClass ac = AnnotatedClass.construct(NamespaceBean.class, ai, null);
AnnotatedField af = _findField(ac, "string");
assertNotNull(af);
PropertyName pn = ai.findNameForDeserialization(af);
assertNotNull(pn);
// JAXB seems to assert field name instead of giving "use default"...
assertEquals("", pn.getSimpleName());
assertEquals("urn:method", pn.getNamespace());
}
private AnnotatedField _findField(AnnotatedClass ac, String name)
{
for (AnnotatedField af : ac.fields()) {
if (name.equals(af.getName())) {
return af;
}
}
return null;
}
}
TestJaxbAutoDetect.java 0000664 0000000 0000000 00000011774 12373261211 0045704 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/introspect package com.fasterxml.jackson.module.jaxb.introspect;
import java.io.*;
import java.math.BigDecimal;
import java.util.Map;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
/**
* Tests for verifying auto-detection settings with JAXB annotations.
*
* @author Tatu Saloranta
*/
public class TestJaxbAutoDetect extends BaseJaxbTest
{
/*
/**********************************************************
/* Helper beans
/**********************************************************
*/
/* Bean for testing problem [JACKSON-183]: with normal
* auto-detect enabled, 2 fields visible; if disabled, just 1.
* NOTE: should NOT include "XmlAccessorType", since it will
* have priority over global defaults
*/
static class Jackson183Bean {
public String getA() { return "a"; }
@XmlElement public String getB() { return "b"; }
// JAXB (or Bean introspection) mandates use of matching setters...
public void setA(String str) { }
public void setB(String str) { }
}
static class Identified
{
Object id;
@XmlAttribute(name="id")
public Object getIdObject() {
return id;
}
public void setId(Object id) { this.id = id; }
}
@XmlRootElement(name="bah")
public static class JaxbAnnotatedObject {
private BigDecimal number;
public JaxbAnnotatedObject() { }
public JaxbAnnotatedObject(String number) {
this.number = new BigDecimal(number);
}
@XmlElement
public void setNumber(BigDecimal number) {
this.number = number;
}
@XmlTransient
public BigDecimal getNumber() {
return number;
}
@XmlElement(name = "number")
public BigDecimal getNumberString() {
return number;
}
}
@SuppressWarnings("serial")
public static class DualAnnotationObjectMapper extends ObjectMapper {
public DualAnnotationObjectMapper() {
super();
AnnotationIntrospector primary = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
// make de/serializer use JAXB annotations first, then jackson ones
AnnotationIntrospector pair = new AnnotationIntrospectorPair(primary, secondary);
setAnnotationIntrospector(pair);
}
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
public void testAutoDetectDisable() throws IOException
{
ObjectMapper mapper = getJaxbMapper();
Jackson183Bean bean = new Jackson183Bean();
Map result;
// Ok: by default, should see 2 fields:
result = writeAndMap(mapper, bean);
assertEquals(2, result.size());
assertEquals("a", result.get("a"));
assertEquals("b", result.get("b"));
// But when disabling auto-detection, just one
mapper = getJaxbMapper();
mapper.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
result = writeAndMap(mapper, bean);
assertEquals(1, result.size());
assertNull(result.get("a"));
assertEquals("b", result.get("b"));
}
public void testIssue246() throws IOException
{
ObjectMapper mapper = getJaxbMapper();
Identified id = new Identified();
id.id = "123";
assertEquals("{\"id\":\"123\"}", mapper.writeValueAsString(id));
}
// [JACKSON-556]
public void testJaxbAnnotatedObject() throws Exception
{
JaxbAnnotatedObject original = new JaxbAnnotatedObject("123");
ObjectMapper mapper = new DualAnnotationObjectMapper();
String json = mapper.writeValueAsString(original);
assertFalse("numberString field in JSON", json.contains("numberString")); // kinda hack-y :)
JaxbAnnotatedObject result = mapper.readValue(json, JaxbAnnotatedObject.class);
assertEquals(new BigDecimal("123"), result.number);
}
/*
public void testJaxbAnnotatedObjectXML() throws Exception
{
JAXBContext ctxt = JAXBContext.newInstance(JaxbAnnotatedObject.class);
JaxbAnnotatedObject original = new JaxbAnnotatedObject("123");
StringWriter sw = new StringWriter();
ctxt.createMarshaller().marshal(original, sw);
String xml = sw.toString();
JaxbAnnotatedObject result = (JaxbAnnotatedObject) ctxt.createUnmarshaller().unmarshal(new StringReader(xml));
assertEquals(new BigDecimal("123"), result.number);
}
*/
}
TestJaxbFieldAccess.java 0000664 0000000 0000000 00000002410 12373261211 0045773 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/introspect package com.fasterxml.jackson.module.jaxb.introspect;
import java.io.IOException;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
public class TestJaxbFieldAccess extends BaseJaxbTest
{
/*
/**********************************************************
/* Helper beans
/**********************************************************
*/
@XmlAccessorType(XmlAccessType.FIELD)
static class Fields {
protected int x;
public Fields() { }
Fields(int x) { this.x = x; }
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
// Verify serialization wrt [JACKSON-202]
public void testFieldSerialization() throws IOException
{
ObjectMapper mapper = getJaxbMapper();
assertEquals("{\"x\":3}", serializeAsString(mapper, new Fields(3)));
}
// Verify deserialization wrt [JACKSON-202]
public void testFieldDeserialization() throws IOException
{
ObjectMapper mapper = getJaxbMapper();
Fields result = mapper.readValue("{ \"x\":3 }", Fields.class);
assertEquals(3, result.x);
}
}
TestPropertyNaming.java 0000664 0000000 0000000 00000002517 12373261211 0046007 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/introspect package com.fasterxml.jackson.module.jaxb.introspect;
import java.io.IOException;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
public class TestPropertyNaming
extends BaseJaxbTest
{
static class WithXmlValueNoOverride
{
@XmlValue
public int getFoobar() {
return 13;
}
}
static class WithXmlValueAndOverride
{
@XmlValue
@JsonProperty("number")
public int getFoobar() {
return 13;
}
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
// For [Issue#30]
public void testXmlValueDefault() throws IOException
{
ObjectMapper mapper = getJaxbAndJacksonMapper();
// default is 'value'
assertEquals("{\"value\":13}", mapper.writeValueAsString(new WithXmlValueNoOverride()));
}
// For [Issue#30]
public void testXmlValueOverride() throws IOException
{
ObjectMapper mapper = getJaxbAndJacksonMapper();
// default is 'value'
assertEquals("{\"number\":13}", mapper.writeValueAsString(new WithXmlValueAndOverride()));
}
}
TestPropertyOrdering.java 0000664 0000000 0000000 00000005327 12373261211 0046351 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/introspect package com.fasterxml.jackson.module.jaxb.introspect;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
public class TestPropertyOrdering
extends BaseJaxbTest
{
@XmlType(propOrder = {"cparty", "contacts"})
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
public class BeanFor268
{
private String cpartyDto = "dto";
private int[] contacts = new int[] { 1, 2, 3 };
@XmlElement(name="cparty")
public String getCpartyDto() { return cpartyDto; }
public void setCpartyDto(String cpartyDto) { this.cpartyDto = cpartyDto; }
@XmlElement(name="contacts")
public int[] getContact() { return contacts; }
public void setContact(int[] contacts) { this.contacts = contacts; }
}
/* Also, considering that JAXB actually seems to expect original
* names for property ordering, let's see that alternative
* annotation also works
* (see [JACKSON-268] for more details)
*/
@XmlType(propOrder = {"cpartyDto", "contacts"})
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
public class BeanWithImplicitNames
{
private String cpartyDto = "dto";
private int[] contacts = new int[] { 1, 2, 3 };
@XmlElement(name="cparty")
public String getCpartyDto() { return cpartyDto; }
public void setCpartyDto(String cpartyDto) { this.cpartyDto = cpartyDto; }
@XmlElement(name="contacts")
public int[] getContact() { return contacts; }
public void setContact(int[] contacts) { this.contacts = contacts; }
}
@XmlType(propOrder={"b", "a", "c"})
public static class AlphaBean2
{
public int c = 3;
public int a = 1;
public int b = 2;
}
/*
/**********************************************************
/* Tests
/**********************************************************
*/
public void testSerializationExplicitOrdering() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
assertEquals("{\"b\":2,\"a\":1,\"c\":3}", serializeAsString(mapper, new AlphaBean2()));
}
// Trying to reproduce [JACKSON-268]
public void testOrderingWithRename() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
assertEquals("{\"cparty\":\"dto\",\"contacts\":[1,2,3]}", mapper.writeValueAsString(new BeanFor268()));
}
public void testOrderingWithOriginalPropName() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
assertEquals("{\"cparty\":\"dto\",\"contacts\":[1,2,3]}",
mapper.writeValueAsString(new BeanWithImplicitNames()));
}
}
TestPropertyVisibility.java 0000664 0000000 0000000 00000004647 12373261211 0046733 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/introspect package com.fasterxml.jackson.module.jaxb.introspect;
import java.io.IOException;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
public class TestPropertyVisibility
extends BaseJaxbTest
{
@XmlAccessorType(XmlAccessType.NONE)
protected static class Bean354
{
protected String name = "foo";
@XmlElement
protected String getName() { return name; }
public void setName(String s) { name = s; }
}
// Note: full example would be "Content"; but let's use simpler demonstration here, easier to debug
@XmlAccessorType(XmlAccessType.PROPERTY)
static class Jackson539Bean
{
protected int type;
@XmlTransient
public String getType() {
throw new UnsupportedOperationException();
}
public void setType(String type) {
throw new UnsupportedOperationException();
}
@XmlAttribute(name = "type")
public int getRawType() {
return type;
}
public void setRawType(int type) {
this.type = type;
}
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
// Verify serialization wrt [JACKSON-354]
//
// NOTE: fails currently because we use Bean Introspector which only sees public methods -- need to rewrite
public void testJackson354Serialization() throws IOException
{
ObjectMapper mapper = getJaxbMapper();
assertEquals("{\"name\":\"foo\"}", mapper.writeValueAsString(new Bean354()));
}
// For [JACKSON-539]
public void testJacksonSerialization()
throws Exception
{
/* Earlier
Content content = new Content();
content.setRawType("application/json");
String json = mapper.writeValueAsString(content);
Content content2 = mapper.readValue(json, Content.class); // deserialize
assertNotNull(content2);
*/
Jackson539Bean input = new Jackson539Bean();
input.type = 123;
ObjectMapper mapper = getJaxbMapper();
String json = mapper.writeValueAsString(input);
Jackson539Bean result = mapper.readValue(json, Jackson539Bean.class);
assertNotNull(result);
assertEquals(123, result.type);
}
}
0000775 0000000 0000000 00000000000 12373261211 0040062 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/misc TestDeserializerCaching.java 0000664 0000000 0000000 00000005161 12373261211 0045467 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/misc package com.fasterxml.jackson.module.jaxb.misc;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.BeanDeserializer;
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
/**
* Unit test(s) for [JACKSON-472]
*/
public class TestDeserializerCaching extends BaseJaxbTest
{
/*
/**********************************************************
/* Helper beans
/**********************************************************
*/
static class MyBeanModule extends Module {
@Override public String getModuleName() {
return "MyBeanModule";
}
@Override public Version version() {
return Version.unknownVersion();
}
@Override public void setupModule(SetupContext context) {
context.addBeanDeserializerModifier(new MyBeanDeserializerModifier());
}
}
@SuppressWarnings("serial")
static class MyBeanDeserializer extends BeanDeserializer {
public MyBeanDeserializer(BeanDeserializer src) {
super(src);
}
}
static class MyBean {
public MyType value1;
public MyType value2;
public MyType value3;
}
static class MyType {
public String name;
public String value;
}
static class MyBeanDeserializerModifier extends BeanDeserializerModifier
{
static int count = 0;
@Override
public JsonDeserializer> modifyDeserializer(DeserializationConfig config,
BeanDescription beanDesc, JsonDeserializer> deserializer)
{
if (MyType.class.isAssignableFrom(beanDesc.getBeanClass())) {
count++;
return new MyBeanDeserializer((BeanDeserializer)deserializer);
}
return super.modifyDeserializer(config, beanDesc, deserializer);
}
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
public void testCaching() throws Exception
{
final String JSON = "{\"value1\" : {\"name\" : \"fruit\", \"value\" : \"apple\"},\n"
+"\"value2\" : {\"name\" : \"color\", \"value\" : \"red\"},\n"
+"\"value3\" : {\"name\" : \"size\", \"value\" : \"small\"}}"
;
ObjectMapper mapper = getJaxbMapper();
mapper.registerModule(new MyBeanModule());
mapper.readValue(JSON, MyBean.class);
assertEquals(1, MyBeanDeserializerModifier.count);
}
}
TestDomElementSerialization.java 0000664 0000000 0000000 00000006723 12373261211 0046364 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/misc package com.fasterxml.jackson.module.jaxb.misc;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
import com.fasterxml.jackson.module.jaxb.deser.DomElementJsonDeserializer;
import com.fasterxml.jackson.module.jaxb.ser.DomElementJsonSerializer;
/**
* @author Ryan Heaton
*/
public class TestDomElementSerialization extends BaseJaxbTest
{
/*
/**********************************************************
/* Helper classes
/**********************************************************
*/
@SuppressWarnings("serial")
private final static class DomModule extends SimpleModule
{
public DomModule()
{
super("DomModule", Version.unknownVersion());
addDeserializer(Element.class, new DomElementJsonDeserializer());
/* 19-Feb-2011, tatu: Note: since SimpleModule does not support "generic"
* serializers, need to add bit more code here.
*/
//testModule.addSerializer(new DomElementJsonSerializer());
}
@Override
public void setupModule(SetupContext context)
{
super.setupModule(context);
context.addSerializers(new DomSerializers());
}
}
private final static class DomSerializers extends Serializers.Base
{
@Override
public JsonSerializer> findSerializer(SerializationConfig config,
JavaType type, BeanDescription beanDesc)
{
if (Element.class.isAssignableFrom(type.getRawClass())) {
return new DomElementJsonSerializer();
}
return null;
}
}
/*
/**********************************************************
/* Test methods
/**********************************************************
*/
public void testBasicDomElementSerializationDeserialization() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new DomModule());
StringBuilder builder = new StringBuilder()
.append("")
.append("howdy ")
.append(" ");
DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
bf.setNamespaceAware(true);
Document document = bf.newDocumentBuilder().parse(new ByteArrayInputStream(builder.toString().getBytes("utf-8")));
StringWriter jsonElement = new StringWriter();
mapper.writeValue(jsonElement, document.getDocumentElement());
Element el = mapper.readValue(jsonElement.toString(), Element.class);
assertEquals(3, el.getAttributes().getLength());
assertEquals("value1", el.getAttributeNS(null, "att1"));
assertEquals("value2", el.getAttributeNS(null, "att2"));
assertEquals(1, el.getChildNodes().getLength());
assertEquals("childel", el.getChildNodes().item(0).getLocalName());
assertEquals("urn:hello", el.getChildNodes().item(0).getNamespaceURI());
assertEquals("howdy", el.getChildNodes().item(0).getTextContent());
}
}
TestElementWrapper.java 0000664 0000000 0000000 00000010103 12373261211 0044512 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/misc package com.fasterxml.jackson.module.jaxb.misc;
import java.util.*;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
/**
* Unit tests to verify handling of @XmlElementWrapper annotation.
*/
public class TestElementWrapper extends BaseJaxbTest
{
/*
/**********************************************************
/* Helper beans
/**********************************************************
*/
// Beans for [JACKSON-436]
static class Person {
@XmlElementWrapper(name="phones")
@XmlElement(type=Phone.class)
public Collection phone;
}
interface IPhone {
public String getNumber();
}
static class Phone implements IPhone
{
private String number;
public Phone() { }
public Phone(String number) { this.number = number; }
@Override
public String getNumber() { return number; }
public void setNumber(String number) { this.number = number; }
}
// [Issue#13]
static class Bean13
{
@XmlElementWrapper(name="wrap")
public int id;
}
// [Issue#25]: should also work with 'default' name
static class Bean25
{
@XmlElement(name="element")
// This would work
// @XmlElementWrapper(name="values")
@XmlElementWrapper
public List values;
public Bean25() { }
public Bean25(int... v0) {
values = new ArrayList();
for (int v : v0) {
values.add(v);
}
}
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
// [JACKSON-436]
public void testWrapperWithCollection() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
// for fun, force renaming with wrapper annotation, even for JSON
mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);
Collection phones = new HashSet();
phones.add(new Phone("555-6666"));
Person p = new Person();
p.phone = phones;
String json = mapper.writeValueAsString(p);
// as per
assertEquals("{\"phones\":[{\"number\":\"555-6666\"}]}", json);
// System.out.println("JSON == "+json);
Person result = mapper.readValue(json, Person.class);
assertNotNull(result.phone);
assertEquals(1, result.phone.size());
assertEquals("555-6666", result.phone.iterator().next().getNumber());
}
// [Issue#13]
public void testWrapperRenaming() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
// verify that by default feature is off:
assertFalse(mapper.isEnabled(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME));
Bean13 input = new Bean13();
input.id = 3;
assertEquals("{\"id\":3}", mapper.writeValueAsString(input));
// but if we create new instance, configure
mapper = getJaxbMapper();
mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);
assertEquals("{\"wrap\":3}", mapper.writeValueAsString(input));
}
// [Issue#25]
public void testWrapperDefaultName() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
mapper = getJaxbMapper();
mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);
Bean25 input = new Bean25(1, 2, 3);
final String JSON = "{\"values\":[1,2,3]}";
assertEquals(JSON, mapper.writeValueAsString(input));
// plus needs to come back ok as well
Bean25 result = mapper.readValue(JSON, Bean25.class);
assertNotNull(result);
assertNotNull(result.values);
assertEquals(3, result.values.size());
// and finally verify roundtrip as well
assertEquals(JSON, mapper.writeValueAsString(result));
}
}
TestJaxbNullProperties.java 0000664 0000000 0000000 00000002220 12373261211 0045355 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/misc package com.fasterxml.jackson.module.jaxb.misc;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
/**
* Unit tests to ensure that handling of writing of null properties (or not)
* works when using JAXB annotation introspector.
* Mostly to test out [JACKSON-309].
*/
public class TestJaxbNullProperties
extends BaseJaxbTest
{
/*
/**********************************************************
/* Helper beans
/**********************************************************
*/
public static class Bean
{
public String empty;
public String x = "y";
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
// Testing [JACKSON-309]
public void testNullProps() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
assertEquals("{\"x\":\"y\"}", mapper.writeValueAsString(new Bean()));
}
}
TestRootName.java 0000664 0000000 0000000 00000002001 12373261211 0043302 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/misc package com.fasterxml.jackson.module.jaxb.misc;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
public class TestRootName extends BaseJaxbTest
{
/*
/**********************************************************
/* Helper beans
/**********************************************************
*/
@XmlRootElement(name="rooty")
static class MyType
{
public int value = 37;
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
public void testRootName() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
assertEquals("{\"rooty\":{\"value\":37}}", mapper.writeValueAsString(new MyType()));
}
}
TestSchemaGeneration.java 0000664 0000000 0000000 00000004123 12373261211 0045001 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/misc package com.fasterxml.jackson.module.jaxb.misc;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.jsonschema.JsonSchema;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
/**
* Test(s) to see that JAXB annotations-based information is properly
* accessible and used by JSON Schema generation
*
* @author tatu
*/
@SuppressWarnings("deprecation")
public class TestSchemaGeneration extends BaseJaxbTest
{
@XmlAccessorType(XmlAccessType.FIELD)
protected static class Person {
public String firstName;
public String lastName;
@XmlElement(type=Address.class)
public IAddress address;
}
protected interface IAddress {
public String getCity();
public void setCity(String city);
}
protected static class Address implements IAddress {
private String city;
private String state;
@Override
public String getCity() { return city; }
@Override
public void setCity(String city) { this.city = city; }
public String getState() { return state; }
public void setState(String state) { this.state = state; }
}
/*
/**********************************************************
/* Tests
/**********************************************************
*/
/**
* Test for [JACKSON-415]
*
* @since 1.7
*/
public void testWithJaxb() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
JsonSchema jsonSchema = mapper.generateJsonSchema(Address.class);
ObjectNode root = jsonSchema.getSchemaNode();
// should find two properties ("city", "state"), not just one...
JsonNode itemsNode = root.findValue("properties");
assertNotNull("Missing 'state' field", itemsNode.get("state"));
assertNotNull("Missing 'city' field", itemsNode.get("city"));
assertEquals(2, itemsNode.size());
}
}
TestXmlAnyElementWithElementRef.java 0000664 0000000 0000000 00000003303 12373261211 0047111 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/misc package com.fasterxml.jackson.module.jaxb.misc;
import java.util.*;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
public class TestXmlAnyElementWithElementRef
extends BaseJaxbTest
{
static class Bean {
@XmlAnyElement(lax=true)
@XmlElementRefs({
@XmlElementRef(name="a", type=Name.class),
@XmlElementRef(name="b", type=Count.class)
})
public List others;
public Bean() { }
public Bean(Object ob) {
others = new ArrayList();
others.add(ob);
}
}
static class Name {
public String name;
}
static class Count {
public int count;
}
/*
/**********************************************************
/* Test methods
/**********************************************************
*/
// [JACKSON-254]: verify that things do work
public void testXmlAnyElementWithElementRef() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
Count count = new Count();
count.count = 8;
Bean value = new Bean(count);
// typed handling should be triggered by annotation on property, so
String json = mapper.writeValueAsString(value);
assertEquals("{\"others\":[{\"b\":{\"count\":8}}]}", json);
Bean result = mapper.readValue(json, Bean.class);
assertNotNull(result);
assertEquals(1, result.others.size());
Object resultOb = result.others.get(0);
assertSame(Count.class, resultOb.getClass());
assertEquals(8, ((Count) resultOb).count);
}
}
package-info.java 0000664 0000000 0000000 00000000627 12373261211 0043256 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/misc /**
* Package info can be used to add "package annotations", so here we are...
*/
@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters({
@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(
type = javax.xml.namespace.QName.class,
value = com.fasterxml.jackson.module.jaxb.introspect.TestJaxbAnnotationIntrospector.QNameAdapter.class
)
})
package com.fasterxml.jackson.module.jaxb.misc;
0000775 0000000 0000000 00000000000 12373261211 0040273 5 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/types TestCyclicTypes.java 0000664 0000000 0000000 00000003305 12373261211 0044232 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/types package com.fasterxml.jackson.module.jaxb.types;
import java.util.*;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
/**
* Simple unit tests to verify that it is possible to handle
* potentially cyclic structures, as long as object graph itself
* is not cyclic. This is the case for directed hierarchies like
* trees and DAGs.
*/
public class TestCyclicTypes
extends BaseJaxbTest
{
/*
/**********************************************************
/* Helper bean classes
/**********************************************************
*/
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
static class Bean
{
private Bean _next;
final String _name;
public Bean(Bean next, String name) {
_next = next;
_name = name;
}
public Bean getNext() { return _next; }
public String getName() { return _name; }
public void assignNext(Bean n) { _next = n; }
}
/*
/**********************************************************
/* Types
/**********************************************************
*/
/* Added to check for [JACKSON-171], i.e. that type its being
* cyclic is not a problem (instances are).
*/
public void testWithJAXB() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
Bean bean = new Bean(null, "abx");
Map results = writeAndMap(mapper, bean);
assertEquals(2, results.size());
assertEquals("abx", results.get("name"));
assertTrue(results.containsKey("next"));
assertNull(results.get("next"));
}
}
TestJaxbPolymorphic.java 0000664 0000000 0000000 00000015425 12373261211 0045117 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/types package com.fasterxml.jackson.module.jaxb.types;
import java.util.*;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
/**
* Tests for handling of type-related JAXB annotations
*
* @author Tatu Saloranta
* @author Ryan Heaton
*/
public class TestJaxbPolymorphic
extends BaseJaxbTest
{
/*
/**********************************************************
/* Helper beans
/**********************************************************
*/
static class Bean
{
@XmlElements({
@XmlElement(type=Buffalo.class, name="beefalot"),
@XmlElement(type=Whale.class, name="whale")
})
public Animal animal;
@XmlElementRefs({
@XmlElementRef(type=Emu.class),
@XmlElementRef(type=Cow.class)
})
public Animal other;
public Bean() { }
public Bean(Animal a) { animal = a; }
}
static class ArrayBean
{
@XmlElements({
@XmlElement(type=Buffalo.class, name="b"),
@XmlElement(type=Whale.class, name="w")
})
public Animal[] animals;
@XmlElementRefs({
@XmlElementRef(type=Emu.class),
@XmlElementRef(type=Cow.class)
})
public Animal[] otherAnimals;
public ArrayBean() { }
public ArrayBean(Animal... a) {
animals = a;
}
}
static abstract class Animal {
public String nickname;
protected Animal(String n) { nickname = n; }
}
static class Buffalo extends Animal {
public String hairColor;
public Buffalo() { this(null, null); }
public Buffalo(String name, String hc) {
super(name);
hairColor = hc;
}
}
static class Whale extends Animal {
public int weightInTons;
public Whale() { this(null, 0); }
public Whale(String n, int w) {
super(n);
weightInTons = w;
}
}
@XmlRootElement
static class Emu extends Animal {
public String featherColor;
public Emu() { this(null, null); }
public Emu(String n, String w) {
super(n);
featherColor = w;
}
}
@XmlRootElement (name="moo")
static class Cow extends Animal {
public int weightInPounds;
public Cow() { this(null, 0); }
public Cow(String n, int w) {
super(n);
weightInPounds = w;
}
}
@XmlSeeAlso({ BaseImpl.class })
abstract static class Base {
}
static class BaseImpl extends Base
{
public String name;
public BaseImpl() { }
public BaseImpl(String n) { name = n; }
}
static class ContainerForBase
{
@XmlElements({ })
// @XmlElement(type=BaseImpl.class, name="baseImpl"),
public Base[] stuff;
}
/*
/**********************************************************
/* Test methods
/**********************************************************
*/
private final ObjectMapper MAPPER = getJaxbMapper();
//First a simple test with non-collection field
@SuppressWarnings("unchecked")
public void testSinglePolymorphic() throws Exception
{
Bean input = new Bean(new Buffalo("Billy", "brown"));
String str = MAPPER.writeValueAsString(input);
// First: let's verify output looks like what we expect:
Map map = MAPPER.readValue(str, Map.class);
assertEquals(2, map.size());
Map map2 = (Map) map.get("animal");
assertNotNull(map2);
// second level, should have type info as WRAPPER_OBJECT
assertEquals(1, map2.size());
assertTrue(map2.containsKey("beefalot"));
Map map3 = (Map) map2.get("beefalot");
assertEquals(2, map3.size());
// good enough, let's deserialize
Bean result = MAPPER.readValue(str, Bean.class);
Animal a = result.animal;
assertNotNull(a);
assertEquals(Buffalo.class, a.getClass());
assertEquals("Billy", a.nickname);
assertEquals("brown", ((Buffalo) a).hairColor);
}
public void testPolymorphicArray() throws Exception
{
Animal a1 = new Buffalo("Bill", "grey");
Animal a2 = new Whale("moe", 3000);
ArrayBean input = new ArrayBean(a1, null, a2);
String str = MAPPER.writeValueAsString(input);
ArrayBean result = MAPPER.readValue(str, ArrayBean.class);
assertEquals(3, result.animals.length);
a1 = result.animals[0];
assertNull(result.animals[1]);
a2 = result.animals[2];
assertNotNull(a1);
assertNotNull(a2);
assertEquals(Buffalo.class, a1.getClass());
assertEquals(Whale.class, a2.getClass());
assertEquals("Bill", a1.nickname);
assertEquals("grey", ((Buffalo) a1).hairColor);
assertEquals("moe", a2.nickname);
assertEquals(3000, ((Whale)a2).weightInTons);
}
public void testPolymorphicArrayElementRef() throws Exception
{
Animal a1 = new Emu("Bill", "grey");
Animal a2 = new Cow("moe", 3000);
ArrayBean input = new ArrayBean();
input.otherAnimals = new Animal[]{a1, null, a2};
String str = MAPPER.writeValueAsString(input);
ArrayBean result = MAPPER.readValue(str, ArrayBean.class);
assertEquals(3, result.otherAnimals.length);
a1 = result.otherAnimals[0];
assertNull(result.otherAnimals[1]);
a2 = result.otherAnimals[2];
assertNotNull(a1);
assertNotNull(a2);
assertEquals(Emu.class, a1.getClass());
assertEquals(Cow.class, a2.getClass());
assertEquals("Bill", a1.nickname);
assertEquals("grey", ((Emu) a1).featherColor);
assertEquals("moe", a2.nickname);
assertEquals(3000, ((Cow)a2).weightInPounds);
}
// For [Issue#1]
public void testXmlSeeAlso() throws Exception
{
ContainerForBase input = new ContainerForBase();
input.stuff = new Base[] { new BaseImpl("xyz") };
String json = MAPPER.writeValueAsString(input);
// so far so good. But can we read it back?
ContainerForBase output = MAPPER.readValue(json, ContainerForBase.class);
assertNotNull(output);
assertNotNull(output.stuff);
assertEquals(1, output.stuff.length);
assertEquals(BaseImpl.class, output.stuff[0].getClass());
assertEquals("xyz", ((BaseImpl) output.stuff[0]).name);
}
}
TestJaxbPolymorphicLists.java 0000664 0000000 0000000 00000010613 12373261211 0046130 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/types package com.fasterxml.jackson.module.jaxb.types;
import java.util.*;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
import com.fasterxml.jackson.module.jaxb.types.TestJaxbPolymorphic.Animal;
import com.fasterxml.jackson.module.jaxb.types.TestJaxbPolymorphic.Buffalo;
import com.fasterxml.jackson.module.jaxb.types.TestJaxbPolymorphic.Cow;
import com.fasterxml.jackson.module.jaxb.types.TestJaxbPolymorphic.Emu;
import com.fasterxml.jackson.module.jaxb.types.TestJaxbPolymorphic.Whale;
/**
* Tests for handling of type-related JAXB annotations with collection (List)
* types.
*/
public class TestJaxbPolymorphicLists
extends BaseJaxbTest
{
/*
/**********************************************************
/* Helper beans
/**********************************************************
*/
static class ListBean
{
@XmlElements({
@XmlElement(type=Buffalo.class, name="beefalot"),
@XmlElement(type=Whale.class, name="whale")
})
public List animals;
@XmlElementRefs({
@XmlElementRef(type=Emu.class),
@XmlElementRef(type=Cow.class)
})
public List otherAnimals;
public ListBean() { }
public ListBean(Animal... a) {
animals = new ArrayList();
for (Animal an : a) {
animals.add(an);
}
}
}
static class Leopard extends Animal {
public Leopard() { super("Lez"); }
}
static class ShortListHolder {
@XmlElement(name="id", type=Short.class)
public List ids;
}
/*
/**********************************************************
/* Tests
/**********************************************************
*/
/**
* And then a test for collection types
*/
public void testPolymorphicList() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
ListBean input = new ListBean(new Whale("bluey", 150),
new Buffalo("Bob", "black")
);
String str = mapper.writeValueAsString(input);
// Let's assume it's ok, and try deserialize right away:
ListBean result = mapper.readValue(str, ListBean.class);
assertEquals(2, result.animals.size());
Animal a1 = result.animals.get(0);
assertNotNull(a1);
assertEquals(Whale.class, a1.getClass());
assertEquals("bluey", a1.nickname);
assertEquals(150, ((Whale)a1).weightInTons);
Animal a2 = result.animals.get(1);
assertNotNull(a2);
assertEquals(Buffalo.class, a2.getClass());
assertEquals("Bob", a2.nickname);
assertEquals("black", ((Buffalo) a2).hairColor);
}
/**
* And then a test for collection types using element ref(s)
*/
public void testPolymorphicListElementRef() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
ListBean input = new ListBean();
input.otherAnimals = Arrays.asList(
new Cow("bluey", 150),
new Emu("Bob", "black")
);
String str = mapper.writeValueAsString(input);
// Let's assume it's ok, and try deserialize right away:
ListBean result = mapper.readValue(str, ListBean.class);
assertEquals(2, result.otherAnimals.size());
Animal a1 = result.otherAnimals.get(0);
assertNotNull(a1);
assertEquals(Cow.class, a1.getClass());
assertEquals("bluey", a1.nickname);
assertEquals(150, ((Cow)a1).weightInPounds);
Animal a2 = result.otherAnimals.get(1);
assertNotNull(a2);
assertEquals(Emu.class, a2.getClass());
assertEquals("Bob", a2.nickname);
assertEquals("black", ((Emu) a2).featherColor);
}
// [JACKSON-348]
public void testShortList() throws Exception
{
ShortListHolder holder = getJaxbMapper().readValue("{\"id\":[1,2,3]}",
ShortListHolder.class);
assertNotNull(holder.ids);
assertEquals(3, holder.ids.size());
assertSame(Short.valueOf((short)1), holder.ids.get(0));
assertSame(Short.valueOf((short)2), holder.ids.get(1));
assertSame(Short.valueOf((short)3), holder.ids.get(2));
}
}
TestJaxbPolymorphicMaps.java 0000664 0000000 0000000 00000006556 12373261211 0045745 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/types package com.fasterxml.jackson.module.jaxb.types;
import java.util.*;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
import com.fasterxml.jackson.module.jaxb.types.TestJaxbPolymorphic.Animal;
import com.fasterxml.jackson.module.jaxb.types.TestJaxbPolymorphic.Buffalo;
import com.fasterxml.jackson.module.jaxb.types.TestJaxbPolymorphic.Cow;
import com.fasterxml.jackson.module.jaxb.types.TestJaxbPolymorphic.Emu;
import com.fasterxml.jackson.module.jaxb.types.TestJaxbPolymorphic.Whale;
/**
* Tests for handling of type-related JAXB annotations
*
* @since 1.5
*/
public class TestJaxbPolymorphicMaps
extends BaseJaxbTest
{
/*
/**********************************************************
/* Helper beans
/**********************************************************
*/
static class MapBean
{
@XmlElements({
@XmlElement(type=Buffalo.class, name="beefalot"),
@XmlElement(type=Whale.class, name="whale")
})
public Map animals;
@XmlElementRefs({
@XmlElementRef(type=Emu.class),
@XmlElementRef(type=Cow.class)
})
public Map otherAnimals;
public MapBean() {
animals = new HashMap();
otherAnimals = new HashMap();
}
public void add(Integer key, Animal value) { animals.put(key, value); }
public void addOther(Integer key, Animal value) { otherAnimals.put(key, value); }
}
/*
/**********************************************************
/* Tests
/**********************************************************
*/
public void testPolymorphicMap() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
Animal a = new Whale("Jaska", 3000);
Animal b = new Whale("Arska", 2000);
Animal c = new Whale("Pena", 1500);
MapBean input = new MapBean();
input.add(1, a);
input.add(2, b);
input.add(3, c);
String str = mapper.writeValueAsString(input);
MapBean result = mapper.readValue(str, MapBean.class);
Map map = result.animals;
assertEquals(3, map.size());
assertEquals("Jaska", ((Whale) map.get(Integer.valueOf(1))).nickname);
assertEquals("Arska", ((Whale) map.get(Integer.valueOf(2))).nickname);
assertEquals("Pena", ((Whale) map.get(Integer.valueOf(3))).nickname);
}
/*
public void testPolymorphicMapElementRefs() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
Animal a = new Cow("Jaska", 3000);
Animal b = new Cow("Arska", 2000);
Animal c = new Cow("Pena", 1500);
MapBean input = new MapBean();
input.addOther(1, a);
input.addOther(2, b);
input.addOther(3, c);
String str = mapper.writeValueAsString(input);
MapBean result = mapper.readValue(str, MapBean.class);
Map map = result.otherAnimals;
assertEquals(3, map.size());
assertEquals("Jaska", ((Cow) map.get(Integer.valueOf(1))).nickname);
assertEquals("Arska", ((Cow) map.get(Integer.valueOf(2))).nickname);
assertEquals("Pena", ((Cow) map.get(Integer.valueOf(3))).nickname);
}
*/
}
TestJaxbTypeCoercion.java 0000664 0000000 0000000 00000002365 12373261211 0045214 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/types package com.fasterxml.jackson.module.jaxb.types;
import javax.xml.bind.annotation.XmlElement;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
public class TestJaxbTypeCoercion extends BaseJaxbTest
{
/*
/**********************************************************
/* Helper beans
/**********************************************************
*/
/**
* Unit test related to [JACKSON-416]
*/
static class Jackson416Bean
{
@XmlElement(type=Jackson416Base.class)
public Jackson416Base value = new Jackson416Sub();
}
static class Jackson416Base
{
public String foo = "foo";
}
static class Jackson416Sub extends Jackson416Base
{
public String bar = "bar";
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
public void testIssue416() throws Exception
{
ObjectMapper mapper = getJaxbAndJacksonMapper();
Jackson416Bean bean = new Jackson416Bean();
String json = mapper.writeValueAsString(bean);
assertEquals("{\"value\":{\"foo\":\"foo\"}}", json);
}
}
TestJaxbTypes.java 0000664 0000000 0000000 00000016640 12373261211 0043716 0 ustar 00root root 0000000 0000000 jackson-module-jaxb-annotations-jackson-module-jaxb-annotations-2.4.2/src/test/java/com/fasterxml/jackson/module/jaxb/types package com.fasterxml.jackson.module.jaxb.types;
import java.util.*;
import javax.xml.bind.annotation.*;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
/**
* Tests for handling of type-related JAXB annotations
*/
public class TestJaxbTypes
extends BaseJaxbTest
{
static class AbstractWrapper {
@XmlElement(type=BeanImpl.class)
public AbstractBean wrapped;
public AbstractWrapper() { this(null); }
public AbstractWrapper(AbstractBean bean) { wrapped = bean; }
}
interface AbstractBean { }
@JsonPropertyOrder({ "a", "b" })
static class BeanImpl
implements AbstractBean
{
public int a;
protected String b;
public BeanImpl() { this(0, null); }
public BeanImpl(int a, String b) {
this.a = a;
this.b = b;
}
public String getB() { return b; }
public void setB(String b) { this.b = b; }
}
static class ListBean {
/* Note: here we rely on implicit linking between the field
* and accessors.
*/
@XmlElement(type=BeanImpl.class)
private List beans;
public ListBean() { }
public ListBean(AbstractBean ... beans) {
this.beans = Arrays.asList(beans);
}
public ListBean(List beans) {
this.beans = beans;
}
public List getBeans() { return beans; }
public void setBeans(List b) { beans = b; }
public int size() { return beans.size(); }
public BeanImpl get(int index) { return (BeanImpl) beans.get(index); }
}
/* And then mix'n match, to try end-to-end
*/
static class ComboBean
{
private AbstractBean bean;
public ListBean beans;
public ComboBean() { }
public ComboBean(AbstractBean bean, ListBean beans)
{
this.bean = bean;
this.beans = beans;
}
@XmlElement(type=BeanImpl.class)
public AbstractBean getBean() { return bean; }
public void setBean(AbstractBean bean) { this.bean = bean; }
}
/**
* Unit test for [JACKSON-250]
*/
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.PROPERTY)
@JsonTypeName("Name")
@XmlType
static class P2
{
String id;
public P2(String id) { this.id = id; }
public P2() { }
@XmlID
@XmlAttribute(name="id")
public String getId() { return id; }
public void setId(String id) { this.id = id; }
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
public void testXmlElementTypeDeser() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
AbstractWrapper wrapper = mapper.readValue("{\"wrapped\":{\"a\":13,\"b\":\"...\"}}", AbstractWrapper.class);
assertNotNull(wrapper);
BeanImpl bean = (BeanImpl) wrapper.wrapped;
assertEquals(13, bean.a);
assertEquals("...", bean.b);
}
public void testXmlElementTypeSer() throws Exception
{
ObjectMapper mapper = getJaxbAndJacksonMapper();
AbstractWrapper wrapper = new AbstractWrapper(new BeanImpl(-3, "c"));
assertEquals("{\"wrapped\":{\"a\":-3,\"b\":\"c\"}}",
mapper.writeValueAsString(wrapper));
}
public void testXmlElementListTypeDeser() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
ListBean listBean = mapper.readValue
("{\"beans\": [{\"a\":1,\"b\":\"a\"}, {\"a\":7,\"b\":\"b\" }]}",
ListBean.class);
assertNotNull(listBean);
List beans = listBean.beans;
assertNotNull(beans);
assertEquals(2, beans.size());
assertNotNull(beans.get(0));
assertNotNull(beans.get(1));
BeanImpl bean = (BeanImpl) beans.get(0);
assertEquals(1, bean.a);
assertEquals("a", bean.b);
bean = (BeanImpl) beans.get(1);
assertEquals(7, bean.a);
assertEquals("b", bean.b);
}
public void testXmlElementListArrayDeser() throws Exception
{
ObjectMapper mapper = getJaxbMapper();
ListBean[] listBeans = mapper.readValue
("[{\"beans\": [{\"a\":1,\"b\":\"a\"}, {\"a\":7,\"b\":\"b\" }]}]",
ListBean[].class);
assertNotNull(listBeans);
assertEquals(1, listBeans.length);
List beans = listBeans[0].beans;
assertNotNull(beans);
assertEquals(2, beans.size());
BeanImpl bean = (BeanImpl) beans.get(0);
assertEquals(1, bean.a);
assertEquals("a", bean.b);
bean = (BeanImpl) beans.get(1);
assertEquals(7, bean.a);
assertEquals("b", bean.b);
}
public void testXmlElementListTypeSer() throws Exception
{
// important: Jackson mapper so we can force ordering
ObjectMapper mapper = getJaxbAndJacksonMapper();
ListBean bean = new ListBean();
List beans = new ArrayList();
beans.add(new BeanImpl(1, "a"));
beans.add(new BeanImpl(2, "b"));
bean.beans = beans;
assertEquals("{\"beans\":[{\"a\":1,\"b\":\"a\"},{\"a\":2,\"b\":\"b\"}]}",
mapper.writeValueAsString(bean));
}
public void testRoundTrip() throws Exception
{
ComboBean input = new ComboBean(new BeanImpl(3, "abc"),
new ListBean(new BeanImpl(1, "a"),
new BeanImpl(2, "b"),
new BeanImpl(3, "c")));
ObjectMapper mapper = getJaxbMapper();
String str = mapper.writeValueAsString(input);
ComboBean result = mapper.readValue(str, ComboBean.class);
assertEquals(3, ((BeanImpl)result.bean).a);
assertEquals("abc", ((BeanImpl)result.bean).b);
assertEquals(3, result.beans.size());
assertEquals(1, (result.beans.get(0)).a);
assertEquals("a", (result.beans.get(0)).b);
assertEquals(2, (result.beans.get(1)).a);
assertEquals("b", (result.beans.get(1)).b);
assertEquals(3, (result.beans.get(2)).a);
assertEquals("c", (result.beans.get(2)).b);
}
public void testListWithDefaultTyping() throws Exception
{
Object input = new ListBean(new BeanImpl(1, "a"));
ObjectMapper mapper = getJaxbMapper();
mapper.enableDefaultTyping();
String str = mapper.writeValueAsString(input);
ListBean listBean = mapper.readValue(str, ListBean.class);
assertNotNull(listBean);
List beans = listBean.beans;
assertNotNull(beans);
assertEquals(1, beans.size());
assertNotNull(beans.get(0));
BeanImpl bean = (BeanImpl) beans.get(0);
assertEquals(1, bean.a);
assertEquals("a", bean.b);
}
public void testIssue250() throws Exception
{
ObjectMapper mapper = getJaxbAndJacksonMapper();
P2 bean = new P2("myId");
String str = mapper.writeValueAsString(bean);
assertEquals("{\"@type\":\"Name\",\"id\":\"myId\"}", str);
}
}