Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

fix for the @Extension problem from ABDERA-351 #1

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,16 @@ protected void writeExtensions(Object source,
ObjectContext valueContext = new ObjectContext(value, source, accessor);
Extension extension = valueContext.getAnnotation(Extension.class);
boolean simple = extension != null ? extension.simple() : false;
Serializer ser = context.getSerializer(valueContext);
Serializer ser;
if (simple) {
final QName qname = getQName(accessor);
ser = new SimpleElementSerializer(qname);
} else {
ser = context.getSerializer(valueContext);
}
if (ser == null) {
if (simple) {
QName qname = getQName(accessor);
ser = new SimpleElementSerializer(qname);
} else {
ser = context.getSerializer(valueContext);
if (ser == null) {
QName qname = getQName(accessor);
ser = new ExtensionSerializer(qname);
}
}
final QName qname = getQName(accessor);
ser = new ExtensionSerializer(qname);
}
ser.serialize(value, valueContext, context);
}
Expand Down Expand Up @@ -249,7 +247,7 @@ protected static QName getQName(AccessibleObject accessor) {
protected static QName getQName(Extension extension) {
QName qname = null;
if (extension != null) {
if (isUndefined(extension.prefix()) && isUndefined(extension.ns()) && isUndefined(extension.name())) {
if (!isUndefined(extension.prefix()) && !isUndefined(extension.ns()) && !isUndefined(extension.name())) {
qname = new QName(extension.ns(), extension.name(), extension.prefix());
} else if (isUndefined(extension.prefix()) && !isUndefined(extension.ns())
&& !isUndefined(extension.name())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.apache.abdera.test.ext.serializer;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import org.apache.abdera.Abdera;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Element;

public final class AbderaTestHelper {

public static final <T extends Element> T deserialize(final ByteArrayOutputStream serialized) {
final ByteArrayInputStream in = new ByteArrayInputStream(serialized.toByteArray());
final Document<T> doc = Abdera.getInstance().getParser().parse(in);
return doc.getRoot();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package org.apache.abdera.test.ext.serializer;

import java.io.ByteArrayOutputStream;
import javax.xml.namespace.QName;

import org.apache.abdera.Abdera;
import org.apache.abdera.ext.serializer.ConventionSerializationContext;
import org.apache.abdera.ext.serializer.annotation.Entry;
import org.apache.abdera.ext.serializer.annotation.Extension;
import org.apache.abdera.model.Element;
import org.apache.abdera.model.ExtensibleElement;
import org.apache.abdera.writer.StreamWriter;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public final class ExtensionAnnotationTest {

private static final String PREFIX = "foo";

private static final String NS = "http://example.org/foo";

private static final String EXT_NAME = "ext";

private static final QName EXT_QNAME = new QName(NS, EXT_NAME, PREFIX);

/**
* Expected serialized result:
* <?xml version='1.0'?>
* <entry xmlns="http://www.w3.org/2005/Atom">
* <foo:ext xmlns:s="http://example.org/foo">
* foo
* </foo:ext>
* </entry>
*/
@Entry
public static final class EntryWithSimpleExtension {

private final String value;

public EntryWithSimpleExtension(final String value) {
this.value = value;
}

@Extension(prefix = PREFIX, ns = NS, name = EXT_NAME, simple = true)
public String getSimpleExtension() {
return value;
}

}

/**
* Expected serialized result:
* <?xml version='1.0'?>
* <entry xmlns="http://www.w3.org/2005/Atom">
* <foo:ext xmlns:s="http://example.org/foo">
* <foo:ext xmlns:s="http://example.org/foo">
* foo
* </foo:ext>
* </foo:ext>
* </entry>
*/
@Entry
public static final class EntryWithExtensionNestingSimpleExtension {

@Extension(prefix = PREFIX, ns = NS, name = EXT_NAME)
public ExtensionNestingSimpleExtension getExtension() {
return new ExtensionNestingSimpleExtension("foo");
}

}

public static final class ExtensionNestingSimpleExtension {

private final String value;

public ExtensionNestingSimpleExtension(final String value) {
this.value = value;
}

@Extension(prefix = PREFIX, ns = NS, name = EXT_NAME, simple = true)
public String getNestedExtension() {
return value;
}

}

/**
* Expected serialized result:
* <?xml version='1.0'?>
* <entry xmlns="http://www.w3.org/2005/Atom">
* <foo:ext xmlns:s="http://example.org/foo">
* <foo:ext xmlns:s="http://example.org/foo">
* <foo:ext xmlns:s="http://example.org/foo">
* foo
* </foo:ext>
* </foo:ext>
* </foo:ext>
* </entry>
*/
@Entry
public static final class EntryWithExtensionNestingExtensionNestingSimpleExtension {

@Extension(prefix = PREFIX, ns = NS, name = EXT_NAME)
public ExtensionNestingExtensionNestingSimpleExtension getExtension() {
return new ExtensionNestingExtensionNestingSimpleExtension("foo");
}

}

public static final class ExtensionNestingExtensionNestingSimpleExtension {

private final String value;

public ExtensionNestingExtensionNestingSimpleExtension(final String value) {
this.value = value;
}

@Extension(prefix = PREFIX, ns = NS, name = EXT_NAME)
public ExtensionNestingSimpleExtension getExtensionNestingSimpleExtension() {
return new ExtensionNestingSimpleExtension(value);
}

}

private final Abdera abdera = Abdera.getInstance();

@Test
public void shouldGenerateSimpleExtension() {
// given
final EntryWithSimpleExtension source = new EntryWithSimpleExtension("foo");

// when
final StreamWriter streamWriter = abdera.newStreamWriter();
final ByteArrayOutputStream serialized = new ByteArrayOutputStream();
streamWriter.setOutputStream(serialized).setAutoIndent(true);
final ConventionSerializationContext context = new ConventionSerializationContext(streamWriter);
streamWriter.startDocument();
context.serialize(source);
streamWriter.endDocument();

// then
final org.apache.abdera.model.Entry entry = AbderaTestHelper.deserialize(serialized);
final ExtensibleElement extension = entry.getExtension(EXT_QNAME);
assertNotNull(extension);
assertEquals("foo", extension.getText().trim());
}

@Test
public void shouldGenerateExtensionNestingSimpleExtension() {
// given
final EntryWithExtensionNestingSimpleExtension source = new EntryWithExtensionNestingSimpleExtension();

// when
final StreamWriter streamWriter = abdera.newStreamWriter();
final ByteArrayOutputStream serialized = new ByteArrayOutputStream();
streamWriter.setOutputStream(serialized).setAutoIndent(true);
final ConventionSerializationContext context = new ConventionSerializationContext(streamWriter);
streamWriter.startDocument();
context.serialize(source);
streamWriter.endDocument();

// then
final org.apache.abdera.model.Entry entry = AbderaTestHelper.deserialize(serialized);
final ExtensibleElement extension = entry.getExtension(EXT_QNAME);
assertNotNull(extension);
final Element simple = extension.getExtension(EXT_QNAME);
assertNotNull(simple);
assertEquals("foo", simple.getText().trim());
}

@Test
public void shouldGenerateExtensionNestingExtensionNestingSimpleExtension() {
// given
final EntryWithExtensionNestingExtensionNestingSimpleExtension source =
new EntryWithExtensionNestingExtensionNestingSimpleExtension();

// when
final StreamWriter streamWriter = abdera.newStreamWriter();
final ByteArrayOutputStream serialized = new ByteArrayOutputStream();
streamWriter.setOutputStream(serialized).setAutoIndent(true);
final ConventionSerializationContext context = new ConventionSerializationContext(streamWriter);
streamWriter.startDocument();
context.serialize(source);
streamWriter.endDocument();

// then
final org.apache.abdera.model.Entry entry = AbderaTestHelper.deserialize(serialized);
final ExtensibleElement extension = entry.getExtension(EXT_QNAME);
assertNotNull(extension);
final ExtensibleElement nested = extension.getExtension(EXT_QNAME);
assertNotNull(nested);
final Element simple = nested.getExtension(EXT_QNAME);
assertNotNull(simple);
assertEquals("foo", simple.getText().trim());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ public static void main(String[] args) {
JUnitCore runner = new JUnitCore();
runner.addListener(new TextListener(System.out));
runner.run(SerializerTest.class);
runner.run(ExtensionAnnotationTest.class);
}
}