001: /*
002: * Copyright (c) 2003-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.validation.tests;
032:
033: import java.awt.Color;
034: import java.awt.Component;
035: import java.awt.Container;
036:
037: import javax.swing.JComponent;
038: import javax.swing.JPanel;
039: import javax.swing.JTextField;
040: import javax.swing.border.Border;
041: import javax.swing.text.JTextComponent;
042:
043: import junit.framework.TestCase;
044:
045: import com.jgoodies.validation.Severity;
046: import com.jgoodies.validation.ValidationResult;
047: import com.jgoodies.validation.view.ValidationComponentUtils;
048:
049: /**
050: * A test case for class {@link ValidationComponentUtils}.
051: *
052: * @author Karsten Lentzsch
053: * @version $Revision: 1.7 $
054: */
055: public final class ValidationComponentUtilsTest extends TestCase {
056:
057: private JTextComponent blankField;
058: private JTextComponent filledField;
059: private JTextComponent blankMandatoryField;
060: private JTextComponent filledMandatoryField;
061: private JTextComponent disabledField;
062: private JTextComponent nonEditableField;
063: private JTextComponent customField;
064: private Container container;
065:
066: // Initialization *********************************************************
067:
068: @Override
069: protected void setUp() throws Exception {
070: super .setUp();
071: setupComponents();
072: setupComponentAnnotations();
073:
074: container = new JPanel();
075: container.add(blankField);
076: container.add(filledField);
077: container.add(blankMandatoryField);
078: container.add(filledMandatoryField);
079: container.add(disabledField);
080: container.add(nonEditableField);
081: container.add(customField);
082: }
083:
084: private void setupComponents() {
085: blankField = new JTextField(" ");
086: filledField = new JTextField("Filled");
087: blankMandatoryField = new JTextField(" ");
088: filledMandatoryField = new JTextField("Filled");
089: disabledField = new JTextField("disabled");
090: disabledField.setEnabled(false);
091: nonEditableField = new JTextField("non-editable");
092: nonEditableField.setEditable(false);
093: customField = new JTextField("custom");
094: customField.setBackground(new Color(5, 12, 67));
095: }
096:
097: private void setupComponentAnnotations() {
098: ValidationComponentUtils
099: .setMandatory(blankMandatoryField, true);
100: ValidationComponentUtils.setMandatory(filledMandatoryField,
101: true);
102: }
103:
104: @Override
105: protected void tearDown() throws Exception {
106: super .setUp();
107: blankField = null;
108: filledField = null;
109: blankMandatoryField = null;
110: filledMandatoryField = null;
111: disabledField = null;
112: nonEditableField = null;
113: container = null;
114: }
115:
116: // Tests *****************************************************************
117:
118: public void testUpdateComponentTreeMandatoryBackground() {
119: Color defaultBackground = getDefaultBackground();
120: Color mandatoryBackground = ValidationComponentUtils
121: .getMandatoryBackground();
122: Color disabledBackground = disabledField.getBackground();
123: Color nonEditableBackground = nonEditableField.getBackground();
124:
125: ValidationComponentUtils
126: .updateComponentTreeMandatoryBackground(container);
127:
128: assertBackground("Blank field has the default background",
129: blankField, defaultBackground);
130:
131: assertBackground("Filled field has the default background",
132: filledField, defaultBackground);
133:
134: assertBackground(
135: "Mandatory blank field has the mandatory background.",
136: blankMandatoryField, mandatoryBackground);
137:
138: assertBackground(
139: "Mandatory filled field has the mandatory background.",
140: filledMandatoryField, mandatoryBackground);
141:
142: assertBackground(
143: "Mandatory filled field has the mandatory background.",
144: filledMandatoryField, mandatoryBackground);
145:
146: assertBackground("Disabled field has the disabled background.",
147: disabledField, disabledBackground);
148:
149: assertBackground(
150: "Non-editable field has the non-editable background.",
151: nonEditableField, nonEditableBackground);
152: }
153:
154: public void testUpdateComponentTreeMandatoryAndBlankBackground() {
155: Color defaultBackground = getDefaultBackground();
156: Color mandatoryBackground = ValidationComponentUtils
157: .getMandatoryBackground();
158: Color disabledBackground = disabledField.getBackground();
159: Color nonEditableBackground = nonEditableField.getBackground();
160:
161: ValidationComponentUtils
162: .updateComponentTreeMandatoryAndBlankBackground(container);
163:
164: assertBackground("Blank field has the default background",
165: blankField, defaultBackground);
166:
167: assertBackground("Filled field has the default background",
168: filledField, defaultBackground);
169:
170: assertBackground(
171: "Mandatory blank field has the mandatory background.",
172: blankMandatoryField, mandatoryBackground);
173:
174: assertBackground(
175: "Mandatory filled field has the default background.",
176: filledMandatoryField, defaultBackground);
177:
178: assertBackground("Disabled field has the disabled background.",
179: disabledField, disabledBackground);
180:
181: assertBackground(
182: "Non-editable field has the non-editable background.",
183: nonEditableField, nonEditableBackground);
184: }
185:
186: public void testUpdateComponentTreeMandatoryBorder() {
187: Border defaultBorder = getDefaultBorder();
188: Border mandatoryBorder = ValidationComponentUtils
189: .getMandatoryBorder();
190: Border disabledBorder = disabledField.getBorder();
191: Border nonEditableBorder = nonEditableField.getBorder();
192:
193: ValidationComponentUtils
194: .updateComponentTreeMandatoryBorder(container);
195:
196: assertBorder("Blank field has the default border", blankField,
197: defaultBorder);
198:
199: assertBorder("Filled field has the default border",
200: filledField, defaultBorder);
201:
202: assertBorder("Mandatory blank field has the mandatory border.",
203: blankMandatoryField, mandatoryBorder);
204:
205: assertBorder(
206: "Mandatory filled field has the mandatory border.",
207: filledMandatoryField, mandatoryBorder);
208:
209: assertBorder("Disabled field has the disabled border.",
210: disabledField, disabledBorder);
211:
212: assertBorder("Non-editable field has the non-editable border.",
213: nonEditableField, nonEditableBorder);
214: }
215:
216: public void testUpdateComponentTreeSeverityBackground() {
217: Color defaultBackground = getDefaultBackground();
218: Color warningBackground = ValidationComponentUtils
219: .getWarningBackground();
220: Color errorBackground = ValidationComponentUtils
221: .getErrorBackground();
222: Color disabledBackground = disabledField.getBackground();
223: Color nonEditableBackground = nonEditableField.getBackground();
224: Color customBackground = customField.getBackground();
225:
226: testUpdateComponentTreeSeverityBackground(
227: "A field without message key has the default background for an empty result.",
228: defaultBackground, disabledBackground,
229: nonEditableBackground, customBackground, null,
230: ValidationResult.EMPTY);
231: testUpdateComponentTreeSeverityBackground(
232: "A field without message key has the default background for a warning result.",
233: defaultBackground, disabledBackground,
234: nonEditableBackground, customBackground, null,
235: ValidationResults.W1);
236: testUpdateComponentTreeSeverityBackground(
237: "A field without message key has the default background for an error result.",
238: defaultBackground, disabledBackground,
239: nonEditableBackground, customBackground, null,
240: ValidationResults.E1);
241:
242: // If no message is associated, show the default background.
243: testUpdateComponentTreeSeverityBackground(
244: "A field with message key has the default background for an empty result.",
245: defaultBackground, disabledBackground,
246: nonEditableBackground, customBackground,
247: ValidationResults.KEY1, ValidationResult.EMPTY);
248: testUpdateComponentTreeSeverityBackground(
249: "A field with message key has the warning background if the warning result contains this message.",
250: warningBackground, warningBackground,
251: warningBackground, warningBackground,
252: ValidationResults.KEY1, ValidationResults.W1);
253: testUpdateComponentTreeSeverityBackground(
254: "A field with message key has the default background if the warning result doesn't contain this message.",
255: defaultBackground, disabledBackground,
256: nonEditableBackground, customBackground,
257: ValidationResults.KEY1, ValidationResults.W2);
258: testUpdateComponentTreeSeverityBackground(
259: "A field with message key has the error background if the error result contains this message.",
260: errorBackground, errorBackground, errorBackground,
261: errorBackground, ValidationResults.KEY1,
262: ValidationResults.E1);
263: testUpdateComponentTreeSeverityBackground(
264: "A field with message key has the default background if the error result doesn't contain this message.",
265: defaultBackground, disabledBackground,
266: nonEditableBackground, customBackground,
267: ValidationResults.KEY1, ValidationResults.E2);
268: testUpdateComponentTreeSeverityBackground(
269: "A field with message key has the default background for an empty result.",
270: defaultBackground, disabledBackground,
271: nonEditableBackground, customBackground,
272: ValidationResults.KEY1, ValidationResult.EMPTY);
273: }
274:
275: private void testUpdateComponentTreeSeverityBackground(
276: String assertionText, Color expectedBackground,
277: Color disabledBackground, Color nonEditableBackground,
278: Color customBackground, Object messageKey,
279: ValidationResult validationResult) {
280: // Set the message keys to all test components.
281: ValidationComponentUtils.setMessageKey(blankField, messageKey);
282: ValidationComponentUtils.setMessageKey(filledField, messageKey);
283: ValidationComponentUtils.setMessageKey(blankMandatoryField,
284: messageKey);
285: ValidationComponentUtils.setMessageKey(filledMandatoryField,
286: messageKey);
287: ValidationComponentUtils.setMessageKey(disabledField,
288: messageKey);
289: ValidationComponentUtils.setMessageKey(nonEditableField,
290: messageKey);
291: ValidationComponentUtils.setMessageKey(customField, messageKey);
292:
293: ValidationComponentUtils.updateComponentTreeSeverityBackground(
294: container, validationResult);
295:
296: assertBackground("Blank field: " + assertionText, blankField,
297: expectedBackground);
298: assertBackground("Filled field: " + assertionText, filledField,
299: expectedBackground);
300: assertBackground("Blank mandatory field: " + assertionText,
301: blankMandatoryField, expectedBackground);
302: assertBackground("Filled mandatory field: " + assertionText,
303: filledMandatoryField, expectedBackground);
304: assertBackground("Disabled field: " + assertionText,
305: disabledField, disabledBackground);
306: assertBackground("Non-editable field: " + assertionText,
307: nonEditableField, nonEditableBackground);
308: assertBackground("Field with custom background: "
309: + assertionText, customField, customBackground);
310: }
311:
312: public void testUpdateComponentTreeSeverity() {
313: testUpdateComponentTreeSeverity(
314: "A field without message key has no severity for an empty result.",
315: null, ValidationResult.EMPTY, null);
316: testUpdateComponentTreeSeverity(
317: "A field without message key has no severity for a warning result.",
318: null, ValidationResults.W1, null);
319: testUpdateComponentTreeSeverity(
320: "A field without message key has no severity for an error result.",
321: null, ValidationResults.E1, null);
322:
323: testUpdateComponentTreeSeverity(
324: "A field with message key has OK severity for an empty result.",
325: ValidationResults.KEY1, ValidationResult.EMPTY,
326: Severity.OK);
327: testUpdateComponentTreeSeverity(
328: "A field with message key has the warning severity if the warning result contains this message.",
329: ValidationResults.KEY1, ValidationResults.W1,
330: Severity.WARNING);
331: testUpdateComponentTreeSeverity(
332: "A field with message key has the OK severity if the warning result doesn't contain this message.",
333: ValidationResults.KEY1, ValidationResults.W2,
334: Severity.OK);
335: testUpdateComponentTreeSeverity(
336: "A field with message has the error severity if the error result contains this message.",
337: ValidationResults.KEY1, ValidationResults.E1,
338: Severity.ERROR);
339: testUpdateComponentTreeSeverity(
340: "A field with message key has the OK severity if the error result doesn't contain this message.",
341: ValidationResults.KEY1, ValidationResults.E2,
342: Severity.OK);
343: testUpdateComponentTreeSeverity(
344: "A field with message key has the OK severity for an empty result.",
345: ValidationResults.KEY1, ValidationResult.EMPTY,
346: Severity.OK);
347: testUpdateComponentTreeSeverity(
348: "Second check: A field without message key has no severity for an empty result.",
349: null, ValidationResult.EMPTY, null);
350: }
351:
352: private void testUpdateComponentTreeSeverity(String assertionText,
353: Object messageKey, ValidationResult validationResult,
354: Severity expectedSeverity) {
355: ValidationComponentUtils.setMessageKey(blankField, messageKey);
356: ValidationComponentUtils.updateComponentTreeSeverity(container,
357: validationResult);
358: assertEquals(assertionText, expectedSeverity,
359: ValidationComponentUtils.getSeverity(blankField));
360: }
361:
362: // Helper Code ************************************************************
363:
364: private void assertBackground(String text, Component component,
365: Color background) {
366: assertEquals(text, background, component.getBackground());
367: }
368:
369: private void assertBorder(String text, JComponent component,
370: Border border) {
371: assertEquals(text, border, component.getBorder());
372: }
373:
374: private Color getDefaultBackground() {
375: return new JTextField().getBackground();
376: }
377:
378: private Border getDefaultBorder() {
379: return new JTextField().getBorder();
380: }
381:
382: }
|