Skip to content

Commit

Permalink
extracting nasty and buggy code to a new class, and testing it
Browse files Browse the repository at this point in the history
  • Loading branch information
lucascs committed Feb 23, 2012
1 parent 3c866b3 commit 9ee57ce
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 58 deletions.
@@ -0,0 +1,79 @@
package br.com.caelum.vraptor.serialization.xstream;

import static br.com.caelum.vraptor.serialization.xstream.VRaptorClassMapper.isPrimitive;
import static com.google.common.base.Objects.firstNonNull;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Map.Entry;

import net.vidageek.mirror.dsl.Mirror;

import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import com.thoughtworks.xstream.XStream;

public class OldAndProbablyBuggyConfigurer {

private final XStream xstream;

public OldAndProbablyBuggyConfigurer(XStream xstream) {
this.xstream = xstream;
}

public void configure(Serializee serializee) {
Multimap<Class<?>, String> excludesMap = LinkedListMultimap.create();
if (!serializee.isRecursive()) {
Class<?> type = serializee.getRootClass();
excludeNonPrimitiveFields(excludesMap, type);

for (Class<?> eType : firstNonNull(serializee.getElementTypes(), Collections.<Class<?>>emptySet())) {
excludeNonPrimitiveFields(excludesMap, eType);
}
}
for (Entry<String, Class<?>> exclude : serializee.getExcludes().entries()) {
parseExclude(exclude);
}
for (Entry<String, Class<?>> include : serializee.getIncludes().entries()) {
parseInclude(excludesMap, include);
}

for (Entry<Class<?>, String> exclude : excludesMap.entries()) {
xstream.omitField(exclude.getKey(), exclude.getValue());
}
}

private void parseExclude(Entry<String, Class<?>> exclude) {
xstream.omitField(exclude.getValue(), getNameFor(exclude.getKey()));
}


private void parseInclude(Multimap<Class<?>, String> excludesMap, Entry<String, Class<?>> include) {
Class<?> parentType = include.getValue();
String fieldName = getNameFor(include.getKey());
Field field = new Mirror().on(parentType).reflect().field(fieldName);
if (field == null) return;
Type genericType = field.getGenericType();
Class<?> fieldType = Serializee.getActualType(genericType);

if (!excludesMap.containsKey(fieldType)) {
excludeNonPrimitiveFields(excludesMap, fieldType);
}
excludesMap.remove(parentType, fieldName);
}

private String getNameFor(String name) {
String[] path = name.split("\\.");
return path[path.length-1];
}

private void excludeNonPrimitiveFields(Multimap<Class<?>, String> excludesMap, Class<?> type) {
for (Field field : new Mirror().on(type).reflectAll().fields()) {
if (!isPrimitive(field.getType())) {
excludesMap.put(field.getDeclaringClass(), field.getName());
}
}
}

}
Expand Up @@ -16,28 +16,20 @@
package br.com.caelum.vraptor.serialization.xstream;

import static br.com.caelum.vraptor.serialization.xstream.VRaptorClassMapper.isPrimitive;
import static com.google.common.base.Objects.firstNonNull;
import static com.google.common.base.Preconditions.checkNotNull;

import java.io.Writer;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import net.vidageek.mirror.dsl.Mirror;
import br.com.caelum.vraptor.interceptor.TypeNameExtractor;
import br.com.caelum.vraptor.serialization.ProxyInitializer;
import br.com.caelum.vraptor.serialization.Serializer;
import br.com.caelum.vraptor.serialization.SerializerBuilder;

import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import com.thoughtworks.xstream.XStream;

/**
Expand Down Expand Up @@ -133,55 +125,17 @@ private Set<Class<?>> findElementTypes(Collection<Object> list) {
return set;
}

private void excludeNonPrimitiveFields(Multimap<Class<?>, String> excludesMap, Class<?> type) {
for (Field field : new Mirror().on(type).reflectAll().fields()) {
if (!isPrimitive(field.getType())) {
excludesMap.put(field.getDeclaringClass(), field.getName());
}
}
}

public Serializer include(String... fields) {
serializee.includeAll(fields);
return this;
}

private void parseInclude(Multimap<Class<?>, String> excludesMap, Entry<String, Class<?>> include) {
Class<?> parentType = include.getValue();
String fieldName = getNameFor(include.getKey());
Type genericType = new Mirror().on(parentType).reflect().field(fieldName).getGenericType();
Class<?> fieldType = Serializee.getActualType(genericType);

if (!excludesMap.containsKey(fieldType)) {
excludeNonPrimitiveFields(excludesMap, fieldType);
}
excludesMap.remove(parentType, fieldName);
}

public void serialize() {
if (xstream instanceof VRaptorXStream) {
VRaptorClassMapper mapper = ((VRaptorXStream) xstream).getVRaptorMapper();
mapper.setSerializee(serializee);
} else {
Multimap<Class<?>, String> excludesMap = LinkedListMultimap.create();
if (!serializee.isRecursive()) {
Class<?> type = serializee.getRootClass();
excludeNonPrimitiveFields(excludesMap, type);

for (Class<?> eType : firstNonNull(serializee.getElementTypes(), Collections.<Class<?>>emptySet())) {
excludeNonPrimitiveFields(excludesMap, eType);
}
}
for (Entry<String, Class<?>> exclude : serializee.getExcludes().entries()) {
parseExclude(exclude);
}
for (Entry<String, Class<?>> include : serializee.getIncludes().entries()) {
parseInclude(excludesMap, include);
}

for (Entry<Class<?>, String> exclude : excludesMap.entries()) {
xstream.omitField(exclude.getKey(), exclude.getValue());
}
new OldAndProbablyBuggyConfigurer(xstream).configure(serializee);
}

registerProxyInitializer();
Expand All @@ -197,13 +151,4 @@ private void registerProxyInitializer() {
xstream.registerConverter(new ProxyConverter(initializer, xstream));
}

private void parseExclude(Entry<String, Class<?>> exclude) {
xstream.omitField(exclude.getValue(), getNameFor(exclude.getKey()));
}

private String getNameFor(String name) {
String[] path = name.split("\\.");
return path[path.length-1];
}

}
@@ -0,0 +1,67 @@
package br.com.caelum.vraptor.serialization.xstream;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.Collections;

import javax.servlet.http.HttpServletResponse;

import org.junit.Before;

import br.com.caelum.vraptor.interceptor.DefaultTypeNameExtractor;
import br.com.caelum.vraptor.serialization.NullProxyInitializer;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.SingleValueConverter;
import com.thoughtworks.xstream.mapper.MapperWrapper;

/**
* testing the same cases as {@link XStreamXMLSerializationTest}
* but using an arbitrary {@link XStream} implementation, not the {@link VRaptorXStream}.
* @author lucascs
*
*/
public class XStreamSerializerTest extends XStreamXMLSerializationTest {

@Override
@Before
public void setup() throws Exception {
this.stream = new ByteArrayOutputStream();

HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getWriter()).thenReturn(new PrintWriter(stream));


final DefaultTypeNameExtractor extractor = new DefaultTypeNameExtractor();
this.serialization = new XStreamXMLSerialization(response, extractor, new NullProxyInitializer(), new XStreamBuilderImpl(
new XStreamConverters(Collections.<Converter>emptyList(), Collections.<SingleValueConverter>emptyList()),
extractor) {
@Override
public XStream xmlInstance() {
return configure(new XStream() {
{setMode(NO_REFERENCES);}
@Override
protected MapperWrapper wrapMapper(MapperWrapper next) {

return new MapperWrapper(next) {
@Override
public String serializedClass(Class type) {
String superName = super.serializedClass(type);
if (type.getName().equals(superName)) {
return extractor.nameFor(type);
}
return superName;
}
};
}
});
}
});
}

}

Expand Up @@ -32,8 +32,8 @@

public class XStreamXMLSerializationTest {

private Serialization serialization;
private ByteArrayOutputStream stream;
protected Serialization serialization;
protected ByteArrayOutputStream stream;

@Before
public void setup() throws Exception {
Expand Down

0 comments on commit 9ee57ce

Please sign in to comment.