-
Notifications
You must be signed in to change notification settings - Fork 62
META-379: fix PersistentBag and PersistentSortedMap conversion #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yegres-teluer
wants to merge
1
commit into
openmrs:master
Choose a base branch
from
yegres-teluer:META-379
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
@@ -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) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
This file contains hidden or 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
155 changes: 155 additions & 0 deletions
155
...s/module/metadatasharing/serializer/converter/CollectionConverterCompatibility19Test.java
This file contains hidden or 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,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)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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