Skip to content

Commit

Permalink
fixed #115: Map entries aren't sorted when maps are converted to Strings
Browse files Browse the repository at this point in the history
  • Loading branch information
S1artie committed Jul 8, 2016
1 parent 645be13 commit 3bacebb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;

import de.gebit.integrity.comparator.MapComparisonResult;
import de.gebit.integrity.parameter.conversion.Conversion;
Expand Down Expand Up @@ -51,7 +53,7 @@ public abstract class AbstractMapToString<T> extends Conversion<Map, T> {
* the conversion context
* @return the resulting string
*/
protected FormattedString convertToFormattedString(Map aSource, ConversionContext aConversionContext)
protected FormattedString convertToFormattedString(Map<?, ?> aSource, ConversionContext aConversionContext)
throws ConversionFailedException {
String tempParentMapPath = (String) aConversionContext.getProperty(MAP_PATH_PROPERTY);

Expand All @@ -66,16 +68,28 @@ protected FormattedString convertToFormattedString(Map aSource, ConversionContex
}
nestedObjectDepthMap.put(Thread.currentThread(), tempDepth);

// In order to provide a consistent ordering of map entries in the string, we want to sort the map by natural
// key ordering
SortedMap<?, ?> tempSortedSource = null;
if (aSource instanceof SortedMap) {
// Either our source map is already sorted...
tempSortedSource = (SortedMap) aSource;
} else {
// ...or we need to sort it by creating a TreeMap and filling it
tempSortedSource = new TreeMap<>(aSource);
}

try {
boolean tempFirst = true;
for (Entry<?, ?> tempEntry : ((Map<?, ?>) aSource).entrySet()) {
for (Entry<?, ?> tempEntry : ((Map<?, ?>) tempSortedSource).entrySet()) {
String tempCurrentMapPath = (tempParentMapPath != null ? tempParentMapPath + "." : "")
+ tempEntry.getKey();
aConversionContext.withProperty(MAP_PATH_PROPERTY, tempCurrentMapPath);

boolean tempCurrentPathFailed = (aConversionContext.getComparisonResult() instanceof MapComparisonResult)
&& ((MapComparisonResult) aConversionContext.getComparisonResult()).getFailedPaths().contains(
tempCurrentMapPath);
boolean tempCurrentPathFailed = (aConversionContext
.getComparisonResult() instanceof MapComparisonResult)
&& ((MapComparisonResult) aConversionContext.getComparisonResult()).getFailedPaths()
.contains(tempCurrentMapPath);

FormattedString[] tempConvertedValues = convertValueToFormattedStringArrayRecursive(
tempEntry.getValue(), aConversionContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<integrity name="Integrity JUnit Testing" timestamp="14.01.16 10:58" isotimestamp="2016-01-14T10:58:46" duration="10.996">
<variables />
<suite id="0" name="integrity.basic.arbitraryParameters.arbitraryParametersWithStringIdentifiers" timestamp="14.01.16 10:58:46.0030">
<setup />
<variables />
<statements>
<test id="1" line="42" name="arbitraryParamEchoTest" description="echoes the values: 123, 400, foo, bar" fixture="de.gebit.integrity.tests.fixtures.basic.ArbitraryParamEchoFixture#echo" timestamp="14.01.16 10:58:46.0030">
<results duration="0.404" successCount="1" failureCount="0" exceptionCount="0">
<result duration="0.404" description="echoes the values: 123, 400, foo, bar" type="success">
<parameters>
<parameter name="fixparam" value="123" />
<parameter name="testparam1" value="400" />
<parameter name="testparam2" value="foo" />
<parameter name="testparam 3" value="bar" />
</parameters>
<comparisons>
<comparison expectedValue="[FORMATTED]{[NL][T]fixedresult = 123[NL|, ][T]arbitraryResults = {[NL][T][T]testresult1 = 400[NL|, ][T][T]testresult2 = foo[NL|, ][T][T]testresult 3 = bar[NL][T]}[NL]}" value="[FORMATTED]{[NL][T]arbitraryResults = {[NL][T][T]testresult1 = 400[NL|, ][T][T]testresult2 = foo[NL|, ][T][T]testresult 3 = bar[NL][T]}[NL|, ][T]fixedresult = 123[NL]}" type="success" />
</comparisons>
</result>
</results>
</test>
</statements>
<returns />
<teardown />
<result duration="10.217" successCount="1" failureCount="0" exceptionCount="0" testExceptionCount="0" callExceptionCount="0" />
</suite>
</integrity>

<?xml version="1.0" encoding="UTF-8"?>
<integrity name="Integrity JUnit Testing" timestamp="08.07.16 14:44" isotimestamp="2016-07-08T14:44:42" duration="25.085">
<variables />
<suite id="0" name="integrity.basic.arbitraryParameters.arbitraryParametersWithStringIdentifiers" timestamp="08.07.16 14:44:42.0756">
<setup />
<variables />
<statements>
<test id="1" line="42" name="arbitraryParamEchoTest" description="echoes the values: 123, 400, foo, bar" fixture="de.gebit.integrity.tests.fixtures.basic.ArbitraryParamEchoFixture#echo" timestamp="08.07.16 14:44:42.0762">
<results duration="2.095" successCount="1" failureCount="0" exceptionCount="0">
<result duration="2.095" description="echoes the values: 123, 400, foo, bar" type="success">
<parameters>
<parameter name="fixparam" value="123" />
<parameter name="testparam1" value="400" />
<parameter name="testparam2" value="foo" />
<parameter name="testparam 3" value="bar" />
</parameters>
<comparisons>
<comparison expectedValue="[FORMATTED]{[NL][T]fixedresult = 123[NL|, ][T]arbitraryResults = {[NL][T][T]testresult1 = 400[NL|, ][T][T]testresult2 = foo[NL|, ][T][T]testresult 3 = bar[NL][T]}[NL]}" value="[FORMATTED]{[NL][T]arbitraryResults = {[NL][T][T]testresult 3 = bar[NL|, ][T][T]testresult1 = 400[NL|, ][T][T]testresult2 = foo[NL][T]}[NL|, ][T]fixedresult = 123[NL]}" type="success" />
</comparisons>
</result>
</results>
</test>
</statements>
<returns />
<teardown />
<result duration="20.554" successCount="1" failureCount="0" exceptionCount="0" testExceptionCount="0" callExceptionCount="0" />
</suite>
</integrity>

Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<integrity name="Integrity JUnit Testing" timestamp="14.01.16 10:58" isotimestamp="2016-01-14T10:58:48" duration="23.910">
<variables />
<suite id="0" name="integrity.basic.beans.beanToMapTestMapInVariable" timestamp="14.01.16 10:58:48.0402">
<setup />
<variables>
<variable name="simpleBeanAsMap" />
</variables>
<statements>
<call id="1" line="25" name="createMapForSimpleBeanUntypedCall" description="creates a map with some key-value pairs in it and returns it" fixture="de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createMapForSimpleBeanUntyped" timestamp="14.01.16 10:58:48.0417">
<parameters />
<result duration="0.127" type="success">
<variableUpdate name="simpleBeanAsMap" value="[FORMATTED]{[NL][T]thirdParameter = {[NL][T][T]innerParameter = 1.99[NL][T]}[NL|, ][T]secondParameter = 100[NL|, ][T]firstParameter = string[NL]}" />
</result>
</call>
<comment id="2" line="27">This should succeed as well, because it's the same as the example before, but just using a map stored in a variable</comment>
<test id="3" line="28" name="createSimpleBeanUntyped" description="creates a bean with predefined values and returns it" fixture="de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createSimpleBeanUntyped" timestamp="14.01.16 10:58:48.0417">
<results duration="0.121" successCount="1" failureCount="0" exceptionCount="0">
<result duration="0.121" description="creates a bean with predefined values and returns it" type="success">
<parameters />
<comparisons>
<comparison expectedValue="[FORMATTED]{[NL][T]thirdParameter = {[NL][T][T]innerParameter = 1.99[NL][T]}[NL|, ][T]secondParameter = 100[NL|, ][T]firstParameter = string[NL]}" value="[FORMATTED]{[NL][T]firstParameter = string[NL|, ][T]secondParameter = 100[NL|, ][T]thirdParameter = {[NL][T][T]innerParameter = 1.99[NL][T]}[NL]}" type="success" />
</comparisons>
</result>
</results>
</test>
</statements>
<returns />
<teardown />
<result duration="23.124" successCount="1" failureCount="0" exceptionCount="0" testExceptionCount="0" callExceptionCount="0" />
</suite>
</integrity>

<?xml version="1.0" encoding="UTF-8"?>
<integrity name="Integrity JUnit Testing" timestamp="08.07.16 15:24" isotimestamp="2016-07-08T15:24:46" duration="31.035">
<variables />
<suite id="0" name="integrity.basic.beans.beanToMapTestMapInVariable" timestamp="08.07.16 15:24:46.0062">
<setup />
<variables>
<variable name="simpleBeanAsMap" />
</variables>
<statements>
<call id="1" line="25" name="createMapForSimpleBeanUntypedCall" description="creates a map with some key-value pairs in it and returns it" fixture="de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createMapForSimpleBeanUntyped" timestamp="08.07.16 15:24:46.0066">
<parameters />
<result duration="0.924" type="success">
<variableUpdate name="simpleBeanAsMap" value="[FORMATTED]{[NL][T]firstParameter = string[NL|, ][T]secondParameter = 100[NL|, ][T]thirdParameter = {[NL][T][T]innerParameter = 1.99[NL][T]}[NL]}" />
</result>
</call>
<comment id="2" line="27">This should succeed as well, because it's the same as the example before, but just using a map stored in a variable</comment>
<test id="3" line="28" name="createSimpleBeanUntyped" description="creates a bean with predefined values and returns it" fixture="de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createSimpleBeanUntyped" timestamp="08.07.16 15:24:46.0076">
<results duration="0.789" successCount="1" failureCount="0" exceptionCount="0">
<result duration="0.789" description="creates a bean with predefined values and returns it" type="success">
<parameters />
<comparisons>
<comparison expectedValue="[FORMATTED]{[NL][T]firstParameter = string[NL|, ][T]secondParameter = 100[NL|, ][T]thirdParameter = {[NL][T][T]innerParameter = 1.99[NL][T]}[NL]}" value="[FORMATTED]{[NL][T]firstParameter = string[NL|, ][T]secondParameter = 100[NL|, ][T]thirdParameter = {[NL][T][T]innerParameter = 1.99[NL][T]}[NL]}" type="success" />
</comparisons>
</result>
</results>
</test>
</statements>
<returns />
<teardown />
<result duration="26.628" successCount="1" failureCount="0" exceptionCount="0" testExceptionCount="0" callExceptionCount="0" />
</suite>
</integrity>

0 comments on commit 3bacebb

Please sign in to comment.