forked from square/retrofit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.squareup.retrofit</groupId> | ||
<artifactId>retrofit-converters</artifactId> | ||
<version>1.2.3-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>converter-simplexml</artifactId> | ||
<name>Converter: SimpleXML</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.squareup.retrofit</groupId> | ||
<artifactId>retrofit</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.simpleframework</groupId> | ||
<artifactId>simple-xml</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.easytesting</groupId> | ||
<artifactId>fest-assert-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
64 changes: 64 additions & 0 deletions
64
retrofit-converters/simplexml/src/main/java/retrofit/converter/SimpleXMLConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package retrofit.converter; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStreamWriter; | ||
import java.lang.reflect.Type; | ||
|
||
import org.simpleframework.xml.Serializer; | ||
|
||
import retrofit.mime.TypedByteArray; | ||
import retrofit.mime.TypedInput; | ||
import retrofit.mime.TypedOutput; | ||
|
||
/** | ||
* A {@link Converter} which uses SimpleXML for reading and writing entities. | ||
* | ||
* @author Fabien Ric ([email protected]) | ||
*/ | ||
public class SimpleXMLConverter implements Converter { | ||
private static final String CHARSET = "UTF-8"; | ||
private static final String MIME_TYPE = "application/xml; charset=" + CHARSET; | ||
|
||
private Serializer serializer = null; | ||
|
||
public SimpleXMLConverter(Serializer serializer) { | ||
this.serializer = serializer; | ||
} | ||
|
||
@Override | ||
public Object fromBody(TypedInput body, Type type) throws ConversionException { | ||
try { | ||
return serializer.read((Class<?>) type, body.in()); | ||
} catch (Exception e) { | ||
throw new ConversionException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public TypedOutput toBody(Object source) { | ||
OutputStreamWriter osw = null; | ||
|
||
try { | ||
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
osw = new OutputStreamWriter(bos, CHARSET); | ||
serializer.write(source, osw); | ||
osw.flush(); | ||
return new TypedByteArray(MIME_TYPE, bos.toByteArray()); | ||
} catch (final NullPointerException e) { | ||
throw new AssertionError(e); | ||
} catch (IOException e) { | ||
throw new AssertionError(e); | ||
} catch (Exception e) { | ||
throw new AssertionError(e); | ||
} finally { | ||
try { | ||
if (osw != null) { | ||
osw.close(); | ||
} | ||
} catch (IOException e) { | ||
throw new AssertionError(e); | ||
} | ||
} | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
retrofit-converters/simplexml/src/test/java/retrofit/converter/SimpleXMLConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package retrofit.converter; | ||
|
||
import static org.fest.assertions.api.Assertions.assertThat; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
|
||
import org.junit.Test; | ||
import org.simpleframework.xml.Default; | ||
import org.simpleframework.xml.DefaultType; | ||
import org.simpleframework.xml.Element; | ||
import org.simpleframework.xml.core.Persister; | ||
import org.simpleframework.xml.stream.Format; | ||
import org.simpleframework.xml.stream.HyphenStyle; | ||
import org.simpleframework.xml.stream.Verbosity; | ||
|
||
import retrofit.mime.TypedByteArray; | ||
import retrofit.mime.TypedInput; | ||
import retrofit.mime.TypedOutput; | ||
|
||
public class SimpleXMLConverterTest { | ||
private static final String MIME_TYPE = "application/xml; charset=UTF-8"; | ||
|
||
private final MyObject obj = new MyObject("hello world", 10); | ||
private final String objAsXML = String.format( | ||
"<my-object><message>%s</message><count>%d</count></my-object>", | ||
obj.getMessage(), obj.getCount()); | ||
private final Converter converter = initConverter(); | ||
|
||
private static Converter initConverter() { | ||
|
||
Format format = new Format(0, null, new HyphenStyle(), Verbosity.HIGH); | ||
Persister persister = new Persister(format); | ||
return new SimpleXMLConverter(persister); | ||
} | ||
|
||
@Test | ||
public void serialize() throws Exception { | ||
final TypedOutput typedOutput = converter.toBody(obj); | ||
assertThat(typedOutput.mimeType()).isEqualTo(MIME_TYPE); | ||
assertThat(asString(typedOutput)).isEqualTo(objAsXML); | ||
} | ||
|
||
@Test | ||
public void deserialize() throws Exception { | ||
final TypedInput input = new TypedByteArray(MIME_TYPE, | ||
objAsXML.getBytes()); | ||
final MyObject result = (MyObject) converter.fromBody(input, | ||
MyObject.class); | ||
assertThat(result).isEqualTo(obj); | ||
} | ||
|
||
@Test(expected = ConversionException.class) | ||
public void deserializeWrongValue() throws Exception { | ||
final TypedInput input = new TypedByteArray(MIME_TYPE, | ||
"<myObject><foo/><bar/></myObject>".getBytes()); | ||
converter.fromBody(input, MyObject.class); | ||
|
||
} | ||
|
||
@Test | ||
public void deserializeWrongClass() throws Exception { | ||
final TypedInput input = new TypedByteArray(MIME_TYPE, | ||
objAsXML.getBytes()); | ||
Object result = converter.fromBody(input, String.class); | ||
assertThat(result).isNull(); | ||
} | ||
|
||
private String asString(TypedOutput typedOutput) throws Exception { | ||
final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | ||
typedOutput.writeTo(bytes); | ||
return new String(bytes.toByteArray()); | ||
} | ||
|
||
@Default(value = DefaultType.FIELD) | ||
static class MyObject { | ||
@Element | ||
private String message; | ||
@Element | ||
private int count; | ||
|
||
public MyObject() { | ||
} | ||
|
||
public MyObject(String message, int count) { | ||
this.message = message; | ||
this.count = count; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setCount(int count) { | ||
this.count = count; | ||
} | ||
|
||
public int getCount() { | ||
return count; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + count; | ||
result = prime * result | ||
+ ((message == null) ? 0 : message.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
MyObject other = (MyObject) obj; | ||
if (count != other.count) { | ||
return false; | ||
} | ||
if (message == null) { | ||
if (other.message != null) { | ||
return false; | ||
} | ||
} else if (!message.equals(other.message)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
} |