001: /*******************************************************************************
002: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
003: * Thomschke.
004: *
005: * All Rights Reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * Sebastian Thomschke - initial implementation.
012: *******************************************************************************/package net.sf.oval.test.guard;
013:
014: import junit.framework.TestCase;
015: import net.sf.oval.ConstraintViolation;
016: import net.sf.oval.constraint.NotNull;
017: import net.sf.oval.constraint.Range;
018: import net.sf.oval.exception.ConstraintsViolatedException;
019: import net.sf.oval.guard.Guard;
020: import net.sf.oval.guard.Guarded;
021:
022: /**
023: * @author Sebastian Thomschke
024: */
025: public class CustomConstraintMessageTest extends TestCase {
026: @Guarded(applyFieldConstraintsToSetters=true)
027: private class TestEntity {
028: @Range(min=2,max=4,message="An amount of {invalidValue} in not in the allowed range ({min}-{max})")
029: private int amount = 2;
030:
031: @NotNull(message=CUSTOM_ERROR_MESSAGE)
032: private String name = "";
033:
034: /**
035: * @return the amount
036: */
037: public int getAmount() {
038: return amount;
039: }
040:
041: /**
042: * @return the name
043: */
044: public String getName() {
045: return name;
046: }
047:
048: /**
049: * @param amount the amount to set
050: */
051: public void setAmount(final int amount) {
052: this .amount = amount;
053: }
054:
055: /**
056: * @param name the name to set
057: */
058: public void setName(final String name) {
059: this .name = name;
060: }
061:
062: }
063:
064: private final static String CUSTOM_ERROR_MESSAGE = "The property [name] cannot be null!";
065: private final static String EXPECTED_RANGE_MESSAGE = "An amount of 5 in not in the allowed range (2-4)";
066:
067: /**
068: * check that custom messages are used correctly
069: */
070: public void testCustomConstraintMessage() {
071: final Guard guard = new Guard();
072: TestGuardAspect.aspectOf().setGuard(guard);
073:
074: final TestEntity e = new TestEntity();
075:
076: try {
077: e.setName(null);
078: fail();
079: } catch (final ConstraintsViolatedException ex) {
080: final ConstraintViolation[] violations = ex
081: .getConstraintViolations();
082: assertTrue(violations != null && violations.length == 1);
083:
084: if (!CUSTOM_ERROR_MESSAGE
085: .equals(violations[0].getMessage()))
086: fail("The returned error message <"
087: + violations[0].getMessage()
088: + "> does not equal the specified custom error message <"
089: + CUSTOM_ERROR_MESSAGE + ">");
090: }
091:
092: try {
093: e.setAmount(5);
094: fail();
095: } catch (final ConstraintsViolatedException ex) {
096: final ConstraintViolation[] violations = ex
097: .getConstraintViolations();
098: assertTrue(violations != null && violations.length == 1);
099:
100: if (!EXPECTED_RANGE_MESSAGE.equals(violations[0]
101: .getMessage()))
102: fail("The returned error message <"
103: + violations[0].getMessage()
104: + "> does not equal the specified custom error message <"
105: + EXPECTED_RANGE_MESSAGE + ">");
106: }
107: }
108: }
|