"README.md" did not exist on "9226879b5baf22e21358da58f737445fd6a9a6ac"
Newer
Older
/*
* Copyright 2008 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.test;
import static org.testng.AssertJUnit.assertEquals;
felmer
committed
import static org.testng.AssertJUnit.assertFalse;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import static org.testng.AssertJUnit.assertTrue;
import static org.testng.AssertJUnit.fail;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.jmock.api.ExpectationError;
/**
* Utilities for making assertions in unit tests.
*
* @author Tomasz Pylak
*/
public class AssertionUtil
{
/** asserts that given text starts with expectedSubstring */
public static void assertStarts(String expectedPrefix, String text)
{
String errorMsg =
String.format("String \n'%s'\nwas expected to start with \n'%s'.", text,
expectedPrefix);
assertTrue(errorMsg, text.startsWith(expectedPrefix));
}
/** asserts that given text ends with expectedSubstring */
public static void assertEnds(String expectedSuffix, String text)
{
String errorMsg =
String.format("String \n'%s'\nwas expected to end with \n'%s'.", text,
expectedSuffix);
assertTrue(errorMsg, text.endsWith(expectedSuffix));
}
/** asserts that given text contains expectedSubstring */
public static void assertContains(String expectedSubstring, String text)
{
String errorMsg =
String.format("String \n'%s'\nwas expected to be a substring of \n'%s'.",
expectedSubstring, text);
assertTrue(errorMsg, text.contains(expectedSubstring));
}
felmer
committed
/** asserts that given text does not contain unExpectedSubstring */
public static void assertContainsNot(String unExpectedSubstring, String text)
{
String errorMsg =
String.format("String \n'%s'\nwas expected to be NOT a substring of \n'%s'.",
unExpectedSubstring, text);
assertFalse(errorMsg, text.contains(unExpectedSubstring));
}
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/** asserts that given text contains expectedSubstring */
public static void assertSize(Collection<?> collection, int size)
{
String errorMsg =
String.format("Collection \n'%s'\nwas expected to have %d elements, but has %d.",
collection.toString(), size, collection.size());
assertTrue(errorMsg, collection.size() == size);
}
public static void assertMatches(String expectedRegexp, String actual)
{
String errorMsg =
String.format("String:\n'%s'\nwas expected to match the following regexp:\n'%s'", actual, expectedRegexp);
assertTrue(errorMsg, actual != null && actual.matches(expectedRegexp));
}
/** asserts that given text contains expectedSubstring. Comparision is case insensitive. */
public static void assertContainsInsensitive(String expectedSubstring, String text)
{
assertContains(expectedSubstring.toUpperCase(), text.toUpperCase());
}
public static void assertContainsLines(String expected, String actual)
{
Collection<String> expectedLines = getLines(expected);
Collection<String> actualLines = getLines(actual);
actualLines.retainAll(expectedLines);
assertTrue("Expected to contain lines:\n" + expected + "\nactual lines:\n" + actual, expectedLines.equals(actualLines));
}
public static void assertContainsLinesInAnyOrder(String expected, String actual)
{
List<String> expectedLines = getLines(expected);
List<String> actualLines = getLines(actual);
actualLines.retainAll(expectedLines);
Collections.sort(expectedLines);
Collections.sort(actualLines);
assertTrue("Expected to contain lines:\n" + expected + "\nactual lines:\n" + actual, expectedLines.equals(actualLines));
}
/** 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]);
}
}
/**
* asserts that two enums have the same values. Usage example:
*
* <pre>
* List<MyEnum1> values1 = Arrays.asList(MyEnum1.values());
* List<MyEnum2> values2 = Arrays.asList(MyEnum2.values());
* AssertionUtil.assertEnumsEqual(values1, values2);
* </pre>
*/
public static void assertEnumsEqual(List<? extends Enum<?>> values1,
List<? extends Enum<?>> values2)
{
Set<String> valuesSet1 = asSet(values1);
Set<String> valuesSet2 = asSet(values2);
assertEquals(valuesSet1, valuesSet2);
}
private static Set<String> asSet(List<? extends Enum<?>> enumValues)
{
Set<String> stringValues = new HashSet<String>();
for (Enum<?> enumInst : enumValues)
{
stringValues.add(enumInst.name());
}
return stringValues;
}
private static List<String> getLines(String text)
{
if (text == null || text.isEmpty())
{
return new ArrayList<String>();
} else
{
return new ArrayList<String>(Arrays.asList(text.split("\n")));
}
}
/**
* returns true if error was caused by unexpected invocation.
*/
public static Throwable tryAsErrorCausedByUnexpectedInvocation(Throwable t)
{
if (t == null)
{
return null;
}
if (t instanceof ExpectationError)
{
return t;
}
return tryAsErrorCausedByUnexpectedInvocation(t.getCause());
}
/**
* Assert that toString() of at least one of objects in given array contains given substring.
*/
public static void assertCollectionContainsString(Collection<?> objects, String subString)
{
for (Object object : objects)
{
if (object.toString().contains(subString))
{
return;
}
}
fail("expected that representation of collection: <" + objects + "> contains: <" + subString + ">");
}
/**
* Assert given collection contains given item
*/
public static <T> void assertCollectionContains(Collection<T> objects, T item)
{
if (false == objects.contains(item))
{
fail("expected that collection: <" + objects + "> contains: <" + item + ">");
}
}
/**
* Assert given collection contains given item
*/
public static <T> void assertCollectionDoesntContain(Collection<T> objects, T item)
{
if (objects.contains(item))
{
fail("expected that collection: <" + objects + "> does not contain: <" + item + ">");
}
}
/**
* Assert given collection contains given items
*/
public static <T> void assertCollectionContainsAtLeast(Collection<T> objects, T... items)
{
Set<T> objectsSet = new HashSet<T>(objects);
for (T item : items)
{
assertCollectionContains(objectsSet, item);
}
}
/**
* Assert given collection contains only given items
*/
public static <T> void assertCollectionContainsOnly(Collection<T> objects, T... items)
{
Set<T> objectsSet = new HashSet<T>(objects);
Set<T> itemsSet = new HashSet<T>(Arrays.asList(items));
if (false == objectsSet.equals(itemsSet))
{
fail("expected that collection: <" + objects + "> contains only: <" + itemsSet + ">");
}
}
public static void assertCollectionSize(Collection<?> objects, int size)
{
if (objects.size() != size)
{
fail("Collection size expected: <" + size + "> but was: <" + objects.size() + "> " + objects.toString());
}
}
public static void assertCollectionIsEmpty(Collection<?> objects)
{
if (objects.size() != 0)
{
fail("Collection should be empty but was: " + objects.toString() + "");
}
}
}