Skip to content
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 @@ -14,9 +14,9 @@
package org.openmrs.module.metadatasharing.serializer.converter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
Expand All @@ -25,11 +25,6 @@
import java.util.TreeSet;

import org.hibernate.collection.PersistentCollection;
import org.hibernate.collection.PersistentList;
import org.hibernate.collection.PersistentMap;
import org.hibernate.collection.PersistentSet;
import org.hibernate.collection.PersistentSortedMap;
import org.hibernate.collection.PersistentSortedSet;
import org.openmrs.annotation.OpenmrsProfile;

import com.thoughtworks.xstream.converters.ConverterLookup;
Expand All @@ -48,16 +43,18 @@ public boolean canConvert(Class type) {
public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context, ConverterLookup converterLookup) {

if (source instanceof PersistentList) {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This missed PersistentBag and PersistentIdentifierBag

source = new ArrayList((Collection) source);
} else if (source instanceof PersistentMap) {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking this before checking for PersistentSortedMap was wrong

source = new HashMap((Map) source);
} else if (source instanceof PersistentSortedMap) {
source = new TreeMap((SortedMap) source);
} else if (source instanceof PersistentSortedSet) {
source = new TreeSet((SortedSet) source);
} else if (source instanceof PersistentSet) {
source = new HashSet((Set) source);
if (source instanceof PersistentCollection) {
if (source instanceof List) {
source = new ArrayList((List) source);
} else if (source instanceof SortedMap) {
source = new TreeMap((SortedMap) source);
} else if (source instanceof Map) {
source = new HashMap((Map) source);
} else if (source instanceof SortedSet) {
source = new TreeSet((SortedSet) source);
} else if (source instanceof Set) {
source = new HashSet((Set) source);
}
}

// delegate the collection to the approapriate converter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,16 @@
package org.openmrs.module.metadatasharing.serializer.converter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

import org.hibernate.collection.internal.PersistentList;
import org.hibernate.collection.internal.PersistentMap;
import org.hibernate.collection.internal.PersistentSet;
import org.hibernate.collection.internal.PersistentSortedMap;
import org.hibernate.collection.internal.PersistentSortedSet;
import org.hibernate.collection.spi.PersistentCollection;
import org.openmrs.annotation.OpenmrsProfile;

Expand All @@ -48,16 +43,18 @@ public boolean canConvert(Class type) {
public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context, ConverterLookup converterLookup) {

if (source instanceof PersistentList) {
source = new ArrayList((Collection) source);
} else if (source instanceof PersistentMap) {
source = new HashMap((Map) source);
} else if (source instanceof PersistentSortedMap) {
source = new TreeMap((SortedMap) source);
} else if (source instanceof PersistentSortedSet) {
source = new TreeSet((SortedSet) source);
} else if (source instanceof PersistentSet) {
source = new HashSet((Set) source);
if (source instanceof PersistentCollection) {
if (source instanceof List) {
source = new ArrayList((List) source);
} else if (source instanceof SortedMap) {
source = new TreeMap((SortedMap) source);
} else if (source instanceof Map) {
source = new HashMap((Map) source);
} else if (source instanceof SortedSet) {
source = new TreeSet((SortedSet) source);
} else if (source instanceof Set) {
source = new HashSet((Set) source);
}
}

// delegate the collection to the appropriate converter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package org.openmrs.module.metadatasharing.serializer.converter;

import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasItem;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.same;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.ConverterLookup;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import org.hibernate.collection.PersistentBag;
import org.hibernate.collection.PersistentList;
import org.hibernate.collection.PersistentMap;
import org.hibernate.collection.PersistentSet;
import org.hibernate.collection.PersistentSortedMap;
import org.hibernate.collection.PersistentSortedSet;
import org.hibernate.engine.SessionImplementor;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;

/**
* Tests if hibernate collections are converted properly
*
* @see CollectionConverterCompatibility1_9
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public class CollectionConverterCompatibility19Test {

@Mock
ConverterLookup converterLookup;
@Mock
Converter converter;
@Mock
HierarchicalStreamWriter writer;
@Mock
MarshallingContext context;

@Mock
SessionImplementor session;
Integer key = 3;
Integer value = 2;

@Before
public void setup() {
MockitoAnnotations.initMocks(this);
when(converterLookup.lookupConverterForType(any(Class.class)))
.thenReturn(converter);
}

@Test
public void marshal_PersistentList() {
PersistentList source = PowerMockito.mock(PersistentList.class);
when(source.toArray())
.thenReturn(new Object[]{value});

new CollectionConverterCompatibility1_9().marshal(source, writer, context, converterLookup);

verify(converterLookup).lookupConverterForType(ArrayList.class);
verify(converter).marshal(argThat(hasItem(value)),
same(writer), same(context));
}

@Test
public void marshal_PersistentBag() {
PersistentBag source = PowerMockito.mock(PersistentBag.class);
when(source.toArray()).thenReturn(new Object[]{value});

new CollectionConverterCompatibility1_9().marshal(source, writer, context, converterLookup);

verify(converterLookup).lookupConverterForType(ArrayList.class);
verify(converter).marshal(argThat(hasItem(value)),
same(writer), same(context));
}

@Test
public void marshal_PersistentSortedMap() {
SortedMap backingMap = new TreeMap();
backingMap.put(key, value);
PersistentSortedMap source = new PersistentSortedMap(session, backingMap);

new CollectionConverterCompatibility1_9().marshal(source, writer, context, converterLookup);

verify(converterLookup).lookupConverterForType(TreeMap.class);
verify(converter).marshal(argThat(hasEntry(key, value)),
same(writer), same(context));
}

@Test
public void marshal_PersistentMap() {
Map backingMap = new HashMap();
backingMap.put(key, value);
PersistentMap source = new PersistentMap(session, backingMap);

new CollectionConverterCompatibility1_9().marshal(source, writer, context, converterLookup);

verify(converterLookup).lookupConverterForType(HashMap.class);
verify(converter).marshal(argThat(hasEntry(key, value)),
same(writer), same(context));
}

@Test
public void marshal_PersistentSortedSet() {
SortedSet backingSet = new TreeSet();
backingSet.add(value);
PersistentSortedSet source =
new PersistentSortedSet(session, backingSet);

new CollectionConverterCompatibility1_9().marshal(source, writer, context, converterLookup);

verify(converterLookup).lookupConverterForType(TreeSet.class);
verify(converter).marshal(argThat(hasItem(value)),
same(writer), same(context));
}

@Test
public void marshal_PersistentSet() {
Set backingSet = new HashSet();
backingSet.add(value);
PersistentSet source = new PersistentSet(session, backingSet);

new CollectionConverterCompatibility1_9().marshal(source, writer, context, converterLookup);

verify(converterLookup).lookupConverterForType(HashSet.class);
verify(converter).marshal(argThat(hasItem(value)),
same(writer), same(context));
}

@Test
public void marshal_nonPersistentCollection() {
Set source = new HashSet();
source.add(value);

new CollectionConverterCompatibility1_9().marshal(source, writer, context, converterLookup);

verify(converterLookup).lookupConverterForType(HashSet.class);
verify(converter).marshal(same(source), same(writer), same(context));
}
}