001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.tests.shared;
034:
035: import com.flexive.shared.FxFormatUtils;
036: import com.flexive.shared.exceptions.FxInvalidParameterException;
037: import org.testng.annotations.Test;
038:
039: /**
040: * Unit tests for com.flexive.shared.FxFormatUtils
041: *
042: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
043: * @see com.flexive.shared.FxFormatUtils
044: */
045: @Test(groups={"shared"})
046: public class FormatUtilTest {
047: /**
048: * Tests for com.flexive.shared.FxFormatUtils.isRGBCode.
049: */
050: @Test
051: public void isRGBCode() {
052: checkRgb("#000000", true);
053: checkRgb("#123456", true);
054: checkRgb("#FFFFFF", true);
055: checkRgb("#ABCDEFF", false);
056: checkRgb("#ABCDEG", false);
057: checkRgb("##ABCDE", false);
058: checkRgb("123456", true);
059: checkRgb("99999A", true);
060: checkRgb(null, false);
061: checkRgb("", false);
062: }
063:
064: /**
065: * Check the given RGB code
066: *
067: * @param code the RGB code to check
068: * @param expectedResult if the code is expected to be valid
069: */
070: private void checkRgb(String code, boolean expectedResult) {
071: assert expectedResult == FxFormatUtils.isRGBCode(code) : "Failed to check RGB code "
072: + code;
073: }
074:
075: /**
076: * Tests for com.flexive.shared.FxFormatUtils.colorCodeToStyle
077: */
078: @Test
079: public void colorCodeToStyle() {
080: assert "style=\"color:#F0F0F0\"".equals(FxFormatUtils
081: .colorCodeToStyle("#F0F0F0").trim()) : "Failed to convert color code";
082: assert "style=\"color:#000000\"".equals(FxFormatUtils
083: .colorCodeToStyle("000000").trim()) : "Failed to convert color code";
084: assert "class=\"test123\"".equals(FxFormatUtils
085: .colorCodeToStyle("test123").trim()) : "Failed to convert color code";
086: }
087:
088: /**
089: * Tests for com.flexive.shared.FxFormatUtils.isEmail
090: */
091: @Test
092: public void isEmail() {
093: checkEmail("abc@abc.com", true);
094: checkEmail("a@b", false);
095: checkEmail("a@b.com", true);
096: checkEmail(" a@b.com ", true);
097: checkEmail("test.point@match.point.com", true);
098: checkEmail("test@@test.com", false);
099: }
100:
101: /**
102: * Check the given email address.
103: *
104: * @param eMail the email address to be checked
105: * @param valid if the address is expected to be valid
106: */
107: private void checkEmail(String eMail, boolean valid) {
108: try {
109: assert FxFormatUtils.isEmail(eMail).equals(
110: (eMail != null) ? eMail.trim() : "INV") : "Failed to return valid email address: "
111: + eMail;
112: } catch (FxInvalidParameterException e) {
113: if (valid) {
114: assert false : "Failed to recognize valid email address: "
115: + eMail;
116: }
117: }
118: }
119:
120: /**
121: * Tests for com.flexive.shared.FxFormatUtils.processColorString
122: */
123: @Test
124: public void processColorString() {
125: checkColorString("#ABCDEF", true);
126: checkColorString("cssClassName", true);
127: checkColorString("#0X0X0X", false);
128: checkColorString("#AAAAAG", false);
129: checkColorString("#123ABC", true);
130: checkColorString("anotherCss123", true);
131: }
132:
133: /**
134: * Check the given color string
135: *
136: * @param value the string to be checked
137: * @param valid if the given value is expected to be valid
138: */
139: private void checkColorString(String value, boolean valid) {
140: try {
141: assert value.equals(FxFormatUtils.processColorString(
142: "TEST", value)) : "Failed to recognize valid color string: "
143: + value;
144: } catch (FxInvalidParameterException e) {
145: if (valid) {
146: assert false : "Failed to recognize valid color string "
147: + value;
148: }
149: }
150: }
151: }
|