@ -12,11 +12,13 @@ import org.junit.Before;
@@ -12,11 +12,13 @@ import org.junit.Before;
import org.junit.Test ;
import org.springframework.ui.alert.Severity ;
import org.springframework.ui.alert.support.DefaultAlertContext ;
import org.springframework.ui.binding.Bound ;
import org.springframework.ui.binding.Model ;
import org.springframework.ui.binding.support.GenericFormatterRegistry ;
import org.springframework.ui.format.number.CurrencyFormat ;
import org.springframework.ui.format.number.IntegerFormatter ;
public class WebBindAndLifecycleTests {
public class WebBindAndValidate LifecycleTests {
private WebBindAndValidateLifecycle lifecycle ;
@ -67,6 +69,37 @@ public class WebBindAndLifecycleTests {
@@ -67,6 +69,37 @@ public class WebBindAndLifecycleTests {
assertEquals ( Severity . ERROR , alertContext . getAlerts ( "integer" ) . get ( 0 ) . getSeverity ( ) ) ;
assertEquals ( "Failed to bind to property 'integer'; the user value 'bogus' has an invalid format and could no be parsed" , alertContext . getAlerts ( "integer" ) . get ( 0 ) . getMessage ( ) ) ;
}
@Test
public void testExecuteLifecycleAnnotatedModel ( ) {
TestAnnotatedBean model = new TestAnnotatedBean ( ) ;
lifecycle = new WebBindAndValidateLifecycle ( model , alertContext ) ;
Map < String , Object > userMap = new HashMap < String , Object > ( ) ;
GenericFormatterRegistry registry = new GenericFormatterRegistry ( ) ;
registry . add ( new IntegerFormatter ( ) , Integer . class ) ;
lifecycle . setFormatterRegistry ( registry ) ;
userMap . put ( "editable" , "foo" ) ;
lifecycle . execute ( userMap ) ;
assertEquals ( 0 , alertContext . getAlerts ( ) . size ( ) ) ;
assertEquals ( "foo" , model . getEditable ( ) ) ;
}
@Test
public void testExecuteLifecycleAnnotatedModelNonEditableBindingAttempt ( ) {
TestAnnotatedBean model = new TestAnnotatedBean ( ) ;
lifecycle = new WebBindAndValidateLifecycle ( model , alertContext ) ;
Map < String , Object > userMap = new HashMap < String , Object > ( ) ;
GenericFormatterRegistry registry = new GenericFormatterRegistry ( ) ;
registry . add ( new IntegerFormatter ( ) , Integer . class ) ;
lifecycle . setFormatterRegistry ( registry ) ;
userMap . put ( "editable" , "foo" ) ;
userMap . put ( "nonEditable" , "whatev" ) ;
lifecycle . execute ( userMap ) ;
assertEquals ( 1 , alertContext . getAlerts ( ) . size ( ) ) ;
assertEquals ( "foo" , model . getEditable ( ) ) ;
assertEquals ( null , model . getNotEditable ( ) ) ;
assertEquals ( "noSuchBinding" , alertContext . getAlerts ( "nonEditable" ) . get ( 0 ) . getCode ( ) ) ;
}
public static enum FooEnum {
BAR , BAZ , BOOP ;
@ -188,4 +221,31 @@ public class WebBindAndLifecycleTests {
@@ -188,4 +221,31 @@ public class WebBindAndLifecycleTests {
}
}
@Model ( value = "testBean" , strictBinding = true )
public class TestAnnotatedBean {
private String editable ;
private String notEditable ;
@Bound
public String getEditable ( ) {
return editable ;
}
public void setEditable ( String editable ) {
this . editable = editable ;
}
public String getNotEditable ( ) {
return notEditable ;
}
public void setNotEditable ( String notEditable ) {
this . notEditable = notEditable ;
}
}
}