001: package org.uispec4j.utils;
002:
003: import junit.framework.Assert;
004:
005: import javax.swing.*;
006: import java.util.Arrays;
007: import java.util.List;
008:
009: public class Utils {
010: public static final String LINE_SEPARATOR = System
011: .getProperty("line.separator");
012:
013: /**
014: * Compare two objects in the case where both can be null
015: */
016: public static boolean equals(Object o1, Object o2) {
017: if ((o1 == null) && (o2 == null)) {
018: return true;
019: }
020: if (o1 != null) {
021: return o1.equals(o2);
022: } else {
023: return o2.equals(o1);
024: }
025: }
026:
027: public static String normalize(String input, int size) {
028: StringBuffer buffer = new StringBuffer();
029: if (size < 1) {
030: return "";
031: }
032: if (input.length() > size) {
033: return input.substring(0, size - 1);
034: }
035: buffer.append(input);
036: int blankCount = size - input.length();
037: for (int i = 0; i < blankCount; i++) {
038: buffer.append(" ");
039: }
040: return buffer.toString();
041: }
042:
043: public static void sleep(long time) {
044: try {
045: Thread.sleep(time);
046: } catch (InterruptedException e) {
047: throw new RuntimeException(e);
048: }
049: }
050:
051: public static void assertEquals(Object[] expected, Object[] actual) {
052: assertEquals(expected, actual, Stringifier.NULL);
053: }
054:
055: public static void assertEquals(Object[] expected, Object[] actual,
056: Stringifier stringifier) {
057: assertSetEquals(expected, actual, stringifier);
058:
059: for (int i = 0; i < actual.length; i++) {
060: Assert.assertTrue("Unexpected order in the collection"
061: + getMessage(expected, actual, stringifier),
062: expected[i].equals(actual[i]));
063: }
064: }
065:
066: public static void assertSetEquals(Object[] expected,
067: Object[] actual, Stringifier stringifier) {
068: int expectedLength = expected.length;
069: int actualLength = actual.length;
070: Assert.assertTrue(expectedLength + " elements instead of "
071: + actualLength
072: + getMessage(expected, actual, stringifier),
073: expectedLength == actualLength);
074:
075: List list = Arrays.asList(expected);
076: for (int i = 0; i < actual.length; i++) {
077: Object element = actual[i];
078: Assert.assertTrue("Unexpected element '"
079: + stringifier.toString(element) + "'"
080: + getMessage(expected, actual, stringifier), list
081: .contains(element));
082: }
083: }
084:
085: private static String getMessage(Object[] expected,
086: Object[] actual, Stringifier stringifier) {
087: return new StringBuffer("\nExpected: ").append(
088: stringify(expected, stringifier))
089: .append(",\nbut was: ").append(
090: stringify(actual, stringifier)).toString();
091: }
092:
093: public static String stringify(Object[] objects,
094: Stringifier stringifier) {
095: StringBuffer buffer = new StringBuffer("[");
096: for (int i = 0; i < objects.length; i++) {
097: buffer.append(stringifier.toString(objects[i])).append(
098: (i == objects.length - 1) ? "]" : ",");
099: }
100: return buffer.toString();
101: }
102:
103: public static void waitForPendingAwtEventsToBeProcessed()
104: throws Exception {
105: if (!SwingUtilities.isEventDispatchThread()) {
106: SwingUtilities.invokeAndWait(new Runnable() {
107: public void run() {
108: }
109: });
110: }
111: }
112: }
|