Skip to content

Commit

Permalink
GRAILS-12010 - throw ConstraintException instead of IllegalStateExcep…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
Jeff Scott Brown committed Mar 10, 2015
1 parent 06b6b90 commit 49bcba2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

import grails.core.GrailsDomainClass;
import grails.util.Holders;
import grails.validation.exceptions.ConstraintException;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import groovy.lang.IntRange;
import groovy.lang.ObjectRange;
import junit.framework.TestCase;
import org.grails.core.DefaultGrailsDomainClass;
import org.grails.plugins.MockGrailsPluginManager;
import org.grails.test.support.MockHibernatePluginHelper;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;

import java.math.BigDecimal;
import java.math.BigInteger;
Expand All @@ -14,14 +21,6 @@
import java.util.Iterator;
import java.util.Map;

import junit.framework.TestCase;

import org.grails.test.support.MockHibernatePluginHelper;
import org.grails.core.DefaultGrailsDomainClass;
import org.grails.plugins.MockGrailsPluginManager;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;

public class ConstrainedPropertyTests extends TestCase {

private int testValidatorValue = 0;
Expand Down Expand Up @@ -55,82 +54,6 @@ public void testGetSetURL() {
assertTrue("should be an url", cp.isUrl());
}

public void testIsEmailOnNonStringProperty() {
ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Integer.class);
assertFalse(cp.isEmail());
}

public void testSetEmailOnForStringProperty() {
ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Integer.class);
try {
cp.setEmail(true);
fail("The call to setEmail(true) on a ConstrainedProperty associated with an Integer property should throw an IllegalStateException.");
} catch (IllegalStateException expected) {
assertEquals("Email constraint can only be applied to String properties", expected.getMessage());
}
}

public void testIsCreditCardOnNonStringProperty() {
ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Integer.class);
assertFalse(cp.isCreditCard());
}

public void testSetCreditCardOnForStringProperty() {
ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Integer.class);
try {
cp.setCreditCard(true);
fail("The call to setCreditCard(true) on a ConstrainedProperty associated with an Integer property should throw an IllegalStateException.");
} catch (IllegalStateException expected) {
assertEquals("CreditCard constraint can only be applied to String properties", expected.getMessage());
}
}

public void testIsMatchesOnNonStringProperty() {
ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Integer.class);
assertNull(cp.getMatches());
}

public void testSetMatchesOnForStringProperty() {
ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Integer.class);
try {
cp.setMatches("some regex");
fail("The call to setMatches(...) on a ConstrainedProperty associated with an Integer property should throw an IllegalStateException.");
} catch (IllegalStateException expected) {
assertEquals("Matches constraint can only be applied to String properties", expected.getMessage());
}
}

public void testIsUrlOnNonStringProperty() {
ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Integer.class);
assertFalse(cp.isUrl());
}

public void testSetUrlOnForStringProperty() {
ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Integer.class);
try {
cp.setUrl(true);
fail("The call to setUrl(true) on a ConstrainedProperty associated with an Integer property should throw an IllegalStateException.");
} catch (IllegalStateException expected) {
assertEquals("URL constraint can only be applied to String properties", expected.getMessage());
}
}


public void testIsBlankOnNonStringProperty() {
ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Integer.class);
assertFalse(cp.isBlank());
}

public void testSetBlankOnForStringProperty() {
ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Integer.class);
try {
cp.setBlank(true);
fail("The call to setBlank(true) on a ConstrainedProperty associated with an Integer property should throw an IllegalStateException.");
} catch (IllegalStateException expected) {
assertEquals("Blank constraint can only be applied to String properties", expected.getMessage());
}
}

public void testGetSetEmail() {
ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", String.class);
cp.setEmail(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ public boolean isBlank() {
*/
public void setBlank(boolean blank) {
if (!isValidStringType()) {
throw new IllegalStateException("Blank constraint can only be applied to String properties");
throw new ConstraintException("Blank constraint can only be applied to String properties");
}

if (!blank) {
Expand Down Expand Up @@ -605,7 +605,7 @@ public boolean isEmail() {
*/
public void setEmail(boolean email) {
if (!isValidStringType()) {
throw new IllegalStateException("Email constraint can only be applied to String properties");
throw new ConstraintException("Email constraint can only be applied to String properties");
}

Constraint c = appliedConstraints.get(EMAIL_CONSTRAINT);
Expand Down Expand Up @@ -642,7 +642,7 @@ public boolean isCreditCard() {
*/
public void setCreditCard(boolean creditCard) {
if (!isValidStringType()) {
throw new IllegalStateException("CreditCard constraint can only be applied to String properties");
throw new ConstraintException("CreditCard constraint can only be applied to String properties");
}

Constraint c = appliedConstraints.get(CREDIT_CARD_CONSTRAINT);
Expand Down Expand Up @@ -676,7 +676,7 @@ public String getMatches() {
*/
public void setMatches(String regex) {
if (!isValidStringType()) {
throw new IllegalStateException("Matches constraint can only be applied to String properties");
throw new ConstraintException("Matches constraint can only be applied to String properties");
}

Constraint c = appliedConstraints.get(MATCHES_CONSTRAINT);
Expand Down Expand Up @@ -841,7 +841,7 @@ public boolean isUrl() {
*/
public void setUrl(boolean url) {
if (!isValidStringType()) {
throw new IllegalStateException("URL constraint can only be applied to String properties");
throw new ConstraintException("Url constraint can only be applied to String properties");
}

Constraint c = appliedConstraints.get(URL_CONSTRAINT);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package grails.validation

import grails.validation.exceptions.ConstraintException
import spock.lang.Specification
import spock.lang.Unroll

class ConstraintTypeMismatchSpec extends Specification {

@Unroll
void 'test calling #methodName on a ConstrainedProperty for a non-String property'() {
given:
def cp = new ConstrainedProperty(ConstraintTypeMismatchSpec, 'testProperty', Integer);

expect:
cp."$methodName"() == expectedResult

where:
methodName | expectedResult
'isEmail' | false
'isCreditCard' | false
'isUrl' | false
'getMatches' | null
'isUrl' | false
}

@Unroll
void 'test calling set#constraintName(#argValue) on a ConstrainedProperty for a non-String property'() {
given:
def cp = new ConstrainedProperty(ConstraintTypeMismatchSpec, 'testProperty', Integer);

when:
cp."set${constraintName}"(argValue)

then:
ConstraintException ex = thrown()
"$constraintName constraint can only be applied to String properties" == ex.message

where:
constraintName | argValue
'Email' | true
'CreditCard' | true
'Url' | true
'Matches' | '.*'
'Blank' | true
}
}

0 comments on commit 49bcba2

Please sign in to comment.