Skip to content

Commit

Permalink
Merge branch 'master' of github.com:caelum/vraptor
Browse files Browse the repository at this point in the history
  • Loading branch information
lucascs committed Mar 22, 2011
2 parents aa95733 + 71bddbc commit 4acad53
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
Expand Down Expand Up @@ -221,10 +222,18 @@ public Serializer include(String... fields) {
private Class<?> getActualType(Type genericType) {
if (genericType instanceof ParameterizedType) {
ParameterizedType type = (ParameterizedType) genericType;

if (isCollection(type)) {
return (Class<?>) type.getActualTypeArguments()[0];
Type actualType = type.getActualTypeArguments()[0];

if (actualType instanceof TypeVariable<?>) {
return (Class<?>) type.getRawType();
}

return (Class<?>) actualType;
}
}

return (Class<?>) genericType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletResponse;

Expand Down Expand Up @@ -65,5 +67,32 @@ public void shouldIncludeCallbackPadding() {
private String result() {
return new String(stream.toByteArray());
}

public static class GenericWrapper<T> {

Collection<T> entityList;
Integer total;

public GenericWrapper(Collection<T> entityList, Integer total) {
this.entityList = entityList;
this.total = total;
}

}

@Test
public void shouldSerializeGenericClass() {
String expectedResult = "myCallback({\"genericWrapper\": {\"entityList\": [{\"street\": \"vergueiro street\"},{\"street\": \"vergueiro street\"}],\"total\": 2}})";

Collection<Address> entityList = new ArrayList<Address>();
entityList.add(new Address("vergueiro street"));
entityList.add(new Address("vergueiro street"));

GenericWrapper<Address> wrapper = new GenericWrapper<Address>(entityList, entityList.size());

serialization.withCallback("myCallback").from(wrapper).include("entityList").serialize();

assertThat(result(), is(equalTo(expectedResult)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,33 @@ public AdvancedOrder(Client client, double price, String comments, String notes)

}

public static class GenericWrapper<T> {

Collection<T> entityList;
Integer total;

public GenericWrapper(Collection<T> entityList, Integer total) {
this.entityList = entityList;
this.total = total;
}

}

@Test
public void shouldSerializeGenericClass() {
String expectedResult = "{\"genericWrapper\": {\"entityList\": [{\"name\": \"washington botelho\"},{\"name\": \"washington botelho\"}],\"total\": 2}}";

Collection<Client> entityList = new ArrayList<Client>();
entityList.add(new Client("washington botelho"));
entityList.add(new Client("washington botelho"));

GenericWrapper<Client> wrapper = new GenericWrapper<Client>(entityList, entityList.size());

serialization.from(wrapper).include("entityList").serialize();

assertThat(result(), is(equalTo(expectedResult)));
}

@Test
public void shouldSerializeAllBasicFields() {
String expectedResult = "{\"order\": {\"price\": 15.0,\"comments\": \"pack it nicely, please\"}}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -105,6 +106,33 @@ public AdvancedOrder(Client client, double price, String comments, String notes)

}

public static class GenericWrapper<T> {

Collection<T> entityList;
Integer total;

public GenericWrapper(Collection<T> entityList, Integer total) {
this.entityList = entityList;
this.total = total;
}

}

@Test
public void shouldSerializeGenericClass() {
String expectedResult = "<genericWrapper>\n <entityList class=\"list\">\n <client>\n <name>washington botelho</name>\n </client>\n <client>\n <name>washington botelho</name>\n </client>\n </entityList>\n <total>2</total>\n</genericWrapper>";

Collection<Client> entityList = new ArrayList<Client>();
entityList.add(new Client("washington botelho"));
entityList.add(new Client("washington botelho"));

GenericWrapper<Client> wrapper = new GenericWrapper<Client>(entityList, entityList.size());

serialization.from(wrapper).include("entityList").serialize();

assertThat(result(), is(equalTo(expectedResult)));
}

@Test
public void shouldSerializeMaps() {
String expectedResult = "<properties>\n <map>\n <entry>\n <string>test</string>\n <string>true</string>\n </entry>\n </map>\n</properties>";
Expand Down

0 comments on commit 4acad53

Please sign in to comment.