001: package org.uispec4j.utils;
002:
003: import junit.framework.AssertionFailedError;
004:
005: import java.awt.*;
006:
007: /**
008: * Test class for {@link ColorUtils}
009: */
010: public class ColorUtilsTest extends UnitTestCase {
011:
012: public void testAssertEqualsWithPrefix() throws Exception {
013: ColorUtils.assertEquals("Message", "FF0000", Color.RED);
014: ColorUtils.assertEquals("Message", "red", Color.RED);
015: ColorUtils.assertEquals("Message", "red", new Color(0xDD1111));
016:
017: checkAssertEqualsError("Message -", "112233", new Color(
018: 0x332211),
019: "Message - expected:<112233> but was:<332211>");
020: checkAssertEqualsError("Message -", ColorUtils
021: .getColor("112233"), new Color(0x332211),
022: "Message - expected:<112233> but was:<332211>");
023: }
024:
025: public void testAssertEquals() throws Exception {
026: ColorUtils.assertEquals("FF0000", Color.RED);
027: ColorUtils.assertEquals("red", Color.RED);
028: ColorUtils.assertEquals("red", new Color(0xDD1111));
029:
030: checkAssertEqualsError("112233", new Color(0x332211),
031: "expected:<112233> but was:<332211>");
032: }
033:
034: public void testInvalidArgumentsToAssertEquals() throws Exception {
035: try {
036: ColorUtils.assertEquals(new Integer(2), Color.red);
037: fail();
038: } catch (IllegalArgumentException error) {
039: assertEquals(ColorUtils.UNEXPECTED_COLOR_CLASS, error
040: .getMessage());
041: }
042:
043: try {
044: ColorUtils.assertEquals("Msg", new Integer(2), Color.red);
045: fail();
046: } catch (IllegalArgumentException error) {
047: assertEquals(ColorUtils.UNEXPECTED_COLOR_CLASS, error
048: .getMessage());
049: }
050: }
051:
052: public void testEqualsByHexaAndRGB() throws Exception {
053: assertTrue(ColorUtils.equals("FF0000", new Color(255, 0, 0)));
054: assertTrue(ColorUtils.equals("FF0000", new Color(0xFF0000)));
055: }
056:
057: public void testEqualsByName() throws Exception {
058: assertTrue(ColorUtils.equals("red", Color.RED));
059: assertTrue(ColorUtils.equals("RED", Color.RED));
060:
061: assertFalse(ColorUtils.equals("blue", Color.RED));
062: assertTrue(ColorUtils.equals("darkGray", Color.darkGray));
063: }
064:
065: public void testEqualsByNameAndSimilarity() throws Exception {
066: assertFalse(ColorUtils.equals("blue", Color.red));
067: assertTrue(ColorUtils.equals("red", Color.red));
068: assertTrue(ColorUtils.equals("red", new Color(170, 5, 5)));
069: assertTrue(ColorUtils.equals("red", ColorUtils
070: .getColor("ffc8c8")));
071: assertTrue(ColorUtils.equals("yellow", ColorUtils
072: .getColor("ffffd0")));
073:
074: assertTrue(ColorUtils.equals("lightGray", Color.gray));
075: assertFalse(ColorUtils.equals("white", Color.gray));
076: assertFalse(ColorUtils.equals("black", Color.gray));
077:
078: assertFalse(ColorUtils.equals("red", ColorUtils
079: .getColor("ffaaff")));
080: }
081:
082: public void testBadColorDescription() throws Exception {
083: try {
084: ColorUtils.equals("not a color", Color.red);
085: fail("Should have failed because 'not a color' is not a color");
086: } catch (IllegalArgumentException error) {
087: assertEquals("'not a color' does not seem to be a color",
088: error.getMessage());
089: }
090:
091: try {
092: ColorUtils.equals(new Integer(2), Color.red);
093: fail();
094: } catch (IllegalArgumentException error) {
095: assertEquals(ColorUtils.UNEXPECTED_COLOR_CLASS, error
096: .getMessage());
097: }
098: }
099:
100: public void testGetColor() throws Exception {
101: assertEquals(Color.red, ColorUtils.getColor("ff0000"));
102: assertEquals(Color.green, ColorUtils.getColor("00ff00"));
103: assertEquals(Color.blue, ColorUtils.getColor("0000ff"));
104: }
105:
106: public void testGetColorDescriptionByColor() throws Exception {
107: assertEquals("112233", ColorUtils
108: .getColorDescription(new Color(0x112233)));
109:
110: assertEquals("FF0000", ColorUtils
111: .getColorDescription(Color.RED));
112: assertEquals("404040", ColorUtils
113: .getColorDescription(Color.DARK_GRAY));
114: }
115:
116: public void testGetColorDescriptionByString() throws Exception {
117: assertEquals("112233", ColorUtils.getColorDescription("112233"));
118:
119: assertEquals("RED", ColorUtils.getColorDescription("red"));
120: assertEquals("FF0000", ColorUtils.getColorDescription("FF0000"));
121: assertEquals("0000FF", ColorUtils.getColorDescription("0000ff"));
122: }
123:
124: private void checkAssertEqualsError(String messagePrefix,
125: Object expectedColor, Color actualColor, String errorMessage) {
126: try {
127: ColorUtils.assertEquals(messagePrefix, expectedColor,
128: actualColor);
129: throw new AssertionFailureNotDetectedError();
130: } catch (AssertionFailedError e) {
131: assertEquals(errorMessage, e.getMessage());
132: }
133: }
134:
135: private void checkAssertEqualsError(String expected, Color actual,
136: String errorMessage) {
137: try {
138: ColorUtils.assertEquals(expected, actual);
139: throw new AssertionFailureNotDetectedError();
140: } catch (AssertionFailedError e) {
141: assertEquals(errorMessage, e.getMessage());
142: }
143: }
144: }
|