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 junit.framework.TestCase;
034:
035: import com.jgoodies.validation.ValidationResult;
036: import com.jgoodies.validation.message.SimpleValidationMessage;
037:
038: /**
039: * A test case for class {@link ValidationResult}.
040: *
041: * @author Karsten Lentzsch
042: * @version $Revision: 1.13 $
043: */
044: public final class ValidationResultTest extends TestCase {
045:
046: // Equals *****************************************************************
047:
048: /**
049: * Verifies that two empty validation results are equal
050: * even if they are not identical.
051: */
052: public void testEqualityOnEmptyResults() {
053: assertTrue("Two empty validation results are equal.",
054: ValidationResult.EMPTY.equals(new ValidationResult()));
055: }
056:
057: /**
058: * Verifies that two different validation results are not equal.
059: */
060: public void testEqualityOnDifferentResults() {
061: assertFalse(
062: "An empty result is not equal to a non-empty result.",
063: ValidationResult.EMPTY.equals(ValidationResults.E1));
064: assertFalse(
065: "An result with an error is not equal to a result with a warning.",
066: ValidationResults.E1.equals(ValidationResults.W1));
067: assertFalse(
068: "An result with two errors is not equal to a result with three errors.",
069: ValidationResults.E1E2.equals(ValidationResults.E1E2E3));
070: }
071:
072: /**
073: * Verifies that two different validation results are not equal.
074: */
075: public void testEqualityOnEqualResults() {
076: ValidationResult result1 = new ValidationResult();
077: result1.addError("Error1");
078: result1.addError("Error2");
079: result1.addWarning("Warning1");
080:
081: ValidationResult result2 = new ValidationResult();
082: result2.addAllFrom(result1);
083:
084: ValidationResult result3 = new ValidationResult();
085: result3.addError("Error1");
086: result3.addError("Error2");
087: result3.addWarning("Warning1");
088:
089: assertEquals(
090: "A result R1 equals R2 if R1 is built using R1's messages.",
091: result1, result2);
092:
093: assertEquals(
094: "Two results are equal if they contain equal simple messages.",
095: result1, result3);
096: }
097:
098: // Result State ***********************************************************
099: /**
100: * Verifies that a validation result that contains error messages
101: * reports that it has errors.
102: */
103: public void testHasErrors() {
104: assertFalse("An empty result shall not report errors.",
105: ValidationResult.EMPTY.hasErrors());
106:
107: assertTrue("An error result shall not report errors.",
108: ValidationResults.E1.hasErrors());
109:
110: assertFalse("A warning result shall not report errors.",
111: ValidationResults.W1.hasErrors());
112:
113: assertTrue("Mixed result1 shall report errors.",
114: ValidationResults.E1W1.hasErrors());
115: assertTrue("Mixed result2 shall report errors.",
116: ValidationResults.W1E1.hasErrors());
117: }
118:
119: /**
120: * Verifies that a validation result that contains messages
121: * reports that it has messages.
122: */
123: public void testHasMessages() {
124: assertFalse("An empty result shall not report messages.",
125: ValidationResult.EMPTY.hasMessages());
126:
127: assertTrue("An error result shall report messages.",
128: ValidationResults.E1.hasMessages());
129:
130: assertTrue("A warning result shall report messages.",
131: ValidationResults.W1.hasMessages());
132:
133: assertTrue("Mixed result1 shall report messages.",
134: ValidationResults.W1E1.hasMessages());
135: assertTrue("Mixed result2 shall report messages.",
136: ValidationResults.E1W1.hasMessages());
137: }
138:
139: /**
140: * Verifies that a validation result that contains warning messages
141: * reports that it has warnings.
142: */
143: public void testHasWarnings() {
144: assertFalse("An empty result shall not report warnings.",
145: ValidationResult.EMPTY.hasWarnings());
146:
147: assertTrue("A warning result shall not report warnings.",
148: ValidationResults.W1.hasWarnings());
149:
150: assertFalse("An error result shall not report warnings.",
151: ValidationResults.E1.hasWarnings());
152:
153: assertTrue("Mixed result1 shall report warnings.",
154: ValidationResults.E1W1.hasWarnings());
155: assertTrue("Mixed result2 shall report warnings.",
156: ValidationResults.W1E1.hasWarnings());
157: }
158:
159: // Unmodifiable ***********************************************************
160:
161: public void testIsUnmodifiable() {
162: assertFalse("ValidationResult.EMPTY is unmodifiable.",
163: ValidationResult.EMPTY.isModifiable());
164:
165: assertTrue("new ValidationResult() is modifiable.",
166: new ValidationResult().isModifiable());
167:
168: ValidationResult result = new ValidationResult();
169: result.addWarning("A warning");
170: assertTrue("ValidationResults are initialized as modifiable.",
171: result.isModifiable());
172: assertFalse(
173: "ValidationResults can be turned into unmodifiable results.",
174: ValidationResult.unmodifiableResult(result)
175: .isModifiable());
176: }
177:
178: public void testUnmodifiableResultIgnoresInternalModifications() {
179: ValidationResult internal = new ValidationResult();
180: internal.addWarning("A warning");
181: assertEquals("The internal result has one message", 1, internal
182: .size());
183: assertTrue("The internal result has a warning", internal
184: .hasWarnings());
185: ValidationResult unmodifiable = ValidationResult
186: .unmodifiableResult(internal);
187: assertEquals("The unmodifiable result has one message", 1,
188: unmodifiable.size());
189: assertTrue("The unmodifiable result has a warning",
190: unmodifiable.hasWarnings());
191:
192: internal.addError("An error");
193: assertEquals("The internal result has two messages", 2,
194: internal.size());
195: assertEquals("The unmodifiable result still has one message",
196: 1, unmodifiable.size());
197: }
198:
199: public void testUnmodifiableResultRejectsModifications() {
200: testUnmodifiableResultRejectsModifications(ValidationResult.EMPTY);
201: testUnmodifiableResultRejectsModifications(ValidationResult
202: .unmodifiableResult(new ValidationResult()));
203: ValidationResult result = new ValidationResult();
204: result.addWarning("A warning");
205: testUnmodifiableResultRejectsModifications(ValidationResult
206: .unmodifiableResult(result));
207: }
208:
209: private void testUnmodifiableResultRejectsModifications(
210: ValidationResult result) {
211: try {
212: result.add(new SimpleValidationMessage("A warning"));
213: fail("An unmodifiable result shall reject the #add operation.");
214: } catch (UnsupportedOperationException e) {
215: // The expected behavior
216: }
217: try {
218: result.addAll(ValidationResults.E1E2E3.getMessages());
219: fail("An unmodifiable result shall reject the #addAll operation.");
220: } catch (UnsupportedOperationException e) {
221: // The expected behavior
222: }
223: try {
224: result.addAllFrom(ValidationResults.E1E2E3);
225: fail("An unmodifiable result shall reject the #addAllFrom operation.");
226: } catch (UnsupportedOperationException e) {
227: // The expected behavior
228: }
229: try {
230: result.addError("An error");
231: fail("An unmodifiable result shall reject the #addError operation.");
232: } catch (UnsupportedOperationException e) {
233: // The expected behavior
234: }
235: try {
236: result.addWarning("A warning");
237: fail("An unmodifiable result shall reject the #addWarning operation.");
238: } catch (UnsupportedOperationException e) {
239: // The expected behavior
240: }
241: }
242:
243: }
|