Skip to content
Snippets Groups Projects
Commit 2dd9d851 authored by tpylak's avatar tpylak
Browse files

LMS-2166 minor utility methods (assert for arrays, mappnig the list)

SVN: 20744
parent b2bf95d6
No related branches found
No related tags found
No related merge requests found
...@@ -37,9 +37,8 @@ public final class CollectionUtils ...@@ -37,9 +37,8 @@ public final class CollectionUtils
/** /**
* Abbreviates a given array of <code>Object</code>. * Abbreviates a given array of <code>Object</code>.
* <p> * <p>
* By default it shows the number of items left, * By default it shows the number of items left, {@link CollectionStyle#DEFAULT} and
* {@link CollectionStyle#DEFAULT} and {@link ToStringDefaultConverter} are * {@link ToStringDefaultConverter} are used.
* used.
* </p> * </p>
* *
* @param maxLength the maximum number of items that should be shown. If <code>-1</code> then * @param maxLength the maximum number of items that should be shown. If <code>-1</code> then
...@@ -53,9 +52,8 @@ public final class CollectionUtils ...@@ -53,9 +52,8 @@ public final class CollectionUtils
/** /**
* Abbreviates a given <code>Collection</code>. * Abbreviates a given <code>Collection</code>.
* <p> * <p>
* By default it shows the number of items left, * By default it shows the number of items left, {@link CollectionStyle#DEFAULT} and
* {@link CollectionStyle#DEFAULT} and {@link ToStringDefaultConverter} are * {@link ToStringDefaultConverter} are used.
* used.
* </p> * </p>
* *
* @param maxLength the maximum number of items that should be shown. If <code>-1</code> then * @param maxLength the maximum number of items that should be shown. If <code>-1</code> then
...@@ -108,8 +106,7 @@ public final class CollectionUtils ...@@ -108,8 +106,7 @@ public final class CollectionUtils
public final static <T> String abbreviate(final Collection<T> collection, final int maxLength, public final static <T> String abbreviate(final Collection<T> collection, final int maxLength,
final IToStringConverter<? super T> converter) final IToStringConverter<? super T> converter)
{ {
return abbreviate(collection, maxLength, converter, return abbreviate(collection, maxLength, converter, CollectionStyle.DEFAULT);
CollectionStyle.DEFAULT);
} }
/** /**
...@@ -130,8 +127,7 @@ public final class CollectionUtils ...@@ -130,8 +127,7 @@ public final class CollectionUtils
/** /**
* Abbreviates a given array of <code>Object</code>. * Abbreviates a given array of <code>Object</code>.
* <p> * <p>
* By default {@link CollectionStyle#DEFAULT} and * By default {@link CollectionStyle#DEFAULT} and {@link ToStringDefaultConverter} are used.
* {@link ToStringDefaultConverter} are used.
* </p> * </p>
* *
* @param maxLength the maximum number of items that should be shown. If <code>-1</code> then * @param maxLength the maximum number of items that should be shown. If <code>-1</code> then
...@@ -146,8 +142,7 @@ public final class CollectionUtils ...@@ -146,8 +142,7 @@ public final class CollectionUtils
/** /**
* Abbreviates a given <code>Collection</code>. * Abbreviates a given <code>Collection</code>.
* <p> * <p>
* By default {@link CollectionStyle#DEFAULT} and * By default {@link CollectionStyle#DEFAULT} and {@link ToStringDefaultConverter} are used.
* {@link ToStringDefaultConverter} are used.
* </p> * </p>
* *
* @param maxLength the maximum number of items that should be shown. If <code>-1</code> then * @param maxLength the maximum number of items that should be shown. If <code>-1</code> then
...@@ -171,8 +166,7 @@ public final class CollectionUtils ...@@ -171,8 +166,7 @@ public final class CollectionUtils
public final static <T> String abbreviate(final T[] objects, final int maxLength, public final static <T> String abbreviate(final T[] objects, final int maxLength,
final boolean showLeft, final IToStringConverter<? super T> converter) final boolean showLeft, final IToStringConverter<? super T> converter)
{ {
return abbreviate(objects, maxLength, showLeft, converter, return abbreviate(objects, maxLength, showLeft, converter, CollectionStyle.DEFAULT);
CollectionStyle.DEFAULT);
} }
/** /**
...@@ -199,8 +193,7 @@ public final class CollectionUtils ...@@ -199,8 +193,7 @@ public final class CollectionUtils
public final static <T> String abbreviate(final Collection<T> collection, final int maxLength, public final static <T> String abbreviate(final Collection<T> collection, final int maxLength,
final boolean showLeft, final IToStringConverter<? super T> converter) final boolean showLeft, final IToStringConverter<? super T> converter)
{ {
return abbreviate(collection, maxLength, showLeft, converter, return abbreviate(collection, maxLength, showLeft, converter, CollectionStyle.DEFAULT);
CollectionStyle.DEFAULT);
} }
/** /**
...@@ -298,4 +291,21 @@ public final class CollectionUtils ...@@ -298,4 +291,21 @@ public final class CollectionUtils
} }
return result; return result;
} }
public static interface CollectionMappingFunction<K, V>
{
K map(V element);
}
/** Transforms one list into another by converting each element with the specified function. */
public static final <K, V> List<K> map(Collection<V> list,
CollectionMappingFunction<K, V> mapping)
{
List<K> mapped = new ArrayList<K>();
for (V elem : list)
{
mapped.add(mapping.map(elem));
}
return mapped;
}
} }
\ No newline at end of file
...@@ -16,7 +16,8 @@ ...@@ -16,7 +16,8 @@
package ch.systemsx.cisd.common.test; package ch.systemsx.cisd.common.test;
import org.testng.AssertJUnit; import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
/** /**
* Utilities for making assertions in unit tests. * Utilities for making assertions in unit tests.
...@@ -30,7 +31,7 @@ public class AssertionUtil ...@@ -30,7 +31,7 @@ public class AssertionUtil
{ {
String errorMsg = String errorMsg =
String.format("String '%s' was expected to start with '%s'.", text, expectedPrefix); String.format("String '%s' was expected to start with '%s'.", text, expectedPrefix);
AssertJUnit.assertTrue(errorMsg, text.startsWith(expectedPrefix)); assertTrue(errorMsg, text.startsWith(expectedPrefix));
} }
/** asserts that given text ends with expectedSubstring */ /** asserts that given text ends with expectedSubstring */
...@@ -38,7 +39,7 @@ public class AssertionUtil ...@@ -38,7 +39,7 @@ public class AssertionUtil
{ {
String errorMsg = String errorMsg =
String.format("String '%s' was expected to end with '%s'.", text, expectedSuffix); String.format("String '%s' was expected to end with '%s'.", text, expectedSuffix);
AssertJUnit.assertTrue(errorMsg, text.endsWith(expectedSuffix)); assertTrue(errorMsg, text.endsWith(expectedSuffix));
} }
/** asserts that given text contains expectedSubstring */ /** asserts that given text contains expectedSubstring */
...@@ -47,7 +48,7 @@ public class AssertionUtil ...@@ -47,7 +48,7 @@ public class AssertionUtil
String errorMsg = String errorMsg =
String.format("String '%s' was expected to be a substring of '%s'.", String.format("String '%s' was expected to be a substring of '%s'.",
expectedSubstring, text); expectedSubstring, text);
AssertJUnit.assertTrue(errorMsg, text.contains(expectedSubstring)); assertTrue(errorMsg, text.contains(expectedSubstring));
} }
/** asserts that given text contains expectedSubstring. Comparision is case insensitive. */ /** asserts that given text contains expectedSubstring. Comparision is case insensitive. */
...@@ -55,4 +56,25 @@ public class AssertionUtil ...@@ -55,4 +56,25 @@ public class AssertionUtil
{ {
assertContains(expectedSubstring.toUpperCase(), text.toUpperCase()); assertContains(expectedSubstring.toUpperCase(), text.toUpperCase());
} }
/** asserts that two int arrays are equal **/
public static void assertArraysEqual(int[] a1, int[] a2)
{
assertEquals(a1.length, a2.length);
for (int i = 0; i < a1.length; i++)
{
assertEquals("Different elements at position, " + i, a1[i], a2[i]);
}
}
/** asserts that two float arrays are equal **/
public static void assertArraysEqual(float[] a1, float[] a2)
{
assertEquals(a1.length, a2.length);
for (int i = 0; i < a1.length; i++)
{
assertEquals("Different elements at position, " + i, a1[i], a2[i]);
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment