Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extracting nasty and buggy code to a new class, and testing it
- Loading branch information
Showing
4 changed files
with
149 additions
and
58 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
.../main/java/br/com/caelum/vraptor/serialization/xstream/OldAndProbablyBuggyConfigurer.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,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()); | ||
} | ||
} | ||
} | ||
|
||
} |
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
67 changes: 67 additions & 0 deletions
67
...core/src/test/java/br/com/caelum/vraptor/serialization/xstream/XStreamSerializerTest.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,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; | ||
} | ||
}; | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
} | ||
|
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