001: package org.uispec4j.utils;
002:
003: public class UtilsTest extends UnitTestCase {
004: public void test() throws Exception {
005: checkNormalize("text", 4, "text");
006: checkNormalize("text ", 6, "text");
007: checkNormalize("text ", 8, "text");
008: checkNormalize("te", 3, "text");
009: checkNormalize("", 0, "text");
010: checkNormalize("", -1, "text");
011: }
012:
013: public void testAssertSetEquals() throws Exception {
014: final Item bag = new Item("bag");
015: final Item bike = new Item("bike");
016: final Item motorcycle = new Item("motorcycle");
017: final Object[] collection = new Object[] { bike, motorcycle,
018: bag };
019:
020: checkAssertionFailedError(new Functor() {
021: public void run() throws Exception {
022: Utils.assertSetEquals(new Object[] { bag, motorcycle },
023: collection, new ItemStringifier());
024: }
025: }, "2 elements instead of 3\n"
026: + "Expected: [bag,motorcycle],\n"
027: + "but was: [bike,motorcycle,bag]");
028: checkAssertionFailedError(new Functor() {
029: public void run() throws Exception {
030: Utils.assertSetEquals(new Object[] { bag, motorcycle,
031: bag }, collection, new ItemStringifier());
032: }
033: }, "Unexpected element 'bike'\n"
034: + "Expected: [bag,motorcycle,bag],\n"
035: + "but was: [bike,motorcycle,bag]");
036: Utils.assertSetEquals(new Object[] { bag, bike, motorcycle },
037: collection, new ItemStringifier());
038: Utils.assertSetEquals(new Object[] { bike, motorcycle, bag },
039: collection, new ItemStringifier());
040: }
041:
042: public void testAssertEquals() throws Exception {
043: final Item bag = new Item("bag");
044: final Item bike = new Item("bike");
045: final Item motorcycle = new Item("motorcycle");
046: final Object[] collection = new Object[] { bike, motorcycle,
047: bag };
048:
049: checkAssertionFailedError(new Functor() {
050: public void run() throws Exception {
051: Utils.assertEquals(new Object[] { bag, motorcycle },
052: collection, new ItemStringifier());
053: }
054: }, "2 elements instead of 3\n"
055: + "Expected: [bag,motorcycle],\n"
056: + "but was: [bike,motorcycle,bag]");
057: checkAssertionFailedError(new Functor() {
058: public void run() throws Exception {
059: Utils.assertEquals(
060: new Object[] { bag, motorcycle, bag },
061: collection, new ItemStringifier());
062: }
063: }, "Unexpected element 'bike'\n"
064: + "Expected: [bag,motorcycle,bag],\n"
065: + "but was: [bike,motorcycle,bag]");
066: checkAssertionFailedError(new Functor() {
067: public void run() throws Exception {
068: Utils.assertEquals(
069: new Object[] { bag, bike, motorcycle },
070: collection, new ItemStringifier());
071: }
072: }, "Unexpected order in the collection\n"
073: + "Expected: [bag,bike,motorcycle],\n"
074: + "but was: [bike,motorcycle,bag]");
075: Utils.assertEquals(new Object[] { bike, motorcycle, bag },
076: collection, new ItemStringifier());
077: }
078:
079: private void checkNormalize(String result, int size, String input) {
080: assertEquals(result, Utils.normalize(input, size));
081: }
082:
083: private static class Item {
084: private String description;
085:
086: public Item(String description) {
087: this .description = description;
088: }
089:
090: public String getDescription() {
091: return description;
092: }
093: }
094:
095: private static class ItemStringifier implements Stringifier {
096: public String toString(Object obj) {
097: return ((Item) obj).getDescription();
098: }
099: }
100: }
|