Alerts test
This commit is contained in:
parent
3b68cde32b
commit
60c2b38c03
|
|
@ -19,7 +19,7 @@ package org.springframework.ui.alert;
|
|||
* A static factory for conveniently constructing Alerts.
|
||||
* Usage example:
|
||||
* <pre>
|
||||
* static import org.springframework.ui.alert.Alerts;
|
||||
* import static org.springframework.ui.alert.Alerts;
|
||||
*
|
||||
* public void example() {
|
||||
* info("An info alert");
|
||||
|
|
@ -40,7 +40,7 @@ public final class Alerts {
|
|||
* @see Severity#INFO
|
||||
*/
|
||||
public static Alert info(String message) {
|
||||
return new GenericAlert(Severity.INFO, null, message);
|
||||
return new GenericAlert(Severity.INFO, message);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -50,7 +50,7 @@ public final class Alerts {
|
|||
* @see Severity#WARNING
|
||||
*/
|
||||
public static Alert warning(String message) {
|
||||
return new GenericAlert(Severity.WARNING, null, message);
|
||||
return new GenericAlert(Severity.WARNING, message);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -60,7 +60,7 @@ public final class Alerts {
|
|||
* @see Severity#ERROR
|
||||
*/
|
||||
public static Alert error(String message) {
|
||||
return new GenericAlert(Severity.ERROR, null, message);
|
||||
return new GenericAlert(Severity.ERROR, message);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -70,41 +70,34 @@ public final class Alerts {
|
|||
* @see Severity#ERROR
|
||||
*/
|
||||
public static Alert fatal(String message) {
|
||||
return new GenericAlert(Severity.FATAL, null, message);
|
||||
return new GenericAlert(Severity.FATAL, message);
|
||||
}
|
||||
|
||||
private static class GenericAlert implements Alert {
|
||||
|
||||
private Severity severity;
|
||||
|
||||
private String code;
|
||||
|
||||
private String message;
|
||||
|
||||
public GenericAlert(Severity severity, String code, String message) {
|
||||
public GenericAlert(Severity severity, String message) {
|
||||
this.severity = severity;
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Severity getSeverity() {
|
||||
return severity;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
if (getCode() != null) {
|
||||
return getCode() + " - " + getMessage();
|
||||
} else {
|
||||
return getMessage();
|
||||
}
|
||||
return getSeverity() + ":" + getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
package org.springframework.ui.alert;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.ui.alert.Alerts.error;
|
||||
import static org.springframework.ui.alert.Alerts.fatal;
|
||||
import static org.springframework.ui.alert.Alerts.info;
|
||||
import static org.springframework.ui.alert.Alerts.warning;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AlertsTests {
|
||||
|
||||
@Test
|
||||
public void testFactoryMethods() {
|
||||
Alert a1 = info("alert 1");
|
||||
assertEquals(Severity.INFO, a1.getSeverity());
|
||||
assertEquals("alert 1", a1.getMessage());
|
||||
|
||||
Alert a2 = warning("alert 2");
|
||||
assertEquals(Severity.WARNING, a2.getSeverity());
|
||||
assertEquals("alert 2", a2.getMessage());
|
||||
|
||||
Alert a3 = error("alert 3");
|
||||
assertEquals(Severity.ERROR, a3.getSeverity());
|
||||
assertEquals("alert 3", a3.getMessage());
|
||||
|
||||
Alert a4 = fatal("alert 4");
|
||||
assertEquals(Severity.FATAL, a4.getSeverity());
|
||||
assertEquals("alert 4", a4.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.springframework.ui.alert;
|
||||
package org.springframework.ui.alert.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
|
@ -8,7 +8,7 @@ import org.springframework.ui.alert.Alert;
|
|||
import org.springframework.ui.alert.Severity;
|
||||
import org.springframework.ui.alert.support.DefaultAlertContext;
|
||||
|
||||
public class DefaultMessageContextTests {
|
||||
public class DefaultAlertContextTests {
|
||||
|
||||
private DefaultAlertContext context;
|
||||
|
||||
|
|
@ -20,10 +20,6 @@ public class DefaultMessageContextTests {
|
|||
@Test
|
||||
public void addAlert() {
|
||||
Alert alert = new Alert() {
|
||||
public String getElement() {
|
||||
return "form.property";
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return "invalidFormat";
|
||||
}
|
||||
|
|
@ -36,7 +32,7 @@ public class DefaultMessageContextTests {
|
|||
return Severity.ERROR;
|
||||
}
|
||||
};
|
||||
context.add(alert);
|
||||
context.add("form.property", alert);
|
||||
assertEquals(1, context.getAlerts().size());
|
||||
assertEquals("invalidFormat", context.getAlerts("form.property").get(0).getCode());
|
||||
}
|
||||
|
|
@ -103,9 +103,7 @@ public class GenericBinderTests {
|
|||
|
||||
@Test
|
||||
public void bindSingleValueWithFormatterRegistedByType() throws ParseException {
|
||||
GenericFormatterRegistry registry = new GenericFormatterRegistry();
|
||||
registry.add(Date.class, new DateFormatter());
|
||||
binder.setFormatterRegistry(registry);
|
||||
binder.registerFormatter(Date.class, new DateFormatter());
|
||||
binder.bind(Collections.singletonMap("date", "2009-06-01"));
|
||||
assertEquals(new DateFormatter().parse("2009-06-01", Locale.US), bean.getDate());
|
||||
}
|
||||
|
|
@ -121,9 +119,7 @@ public class GenericBinderTests {
|
|||
|
||||
@Test
|
||||
public void bindSingleValueWithnAnnotationFormatterFactoryRegistered() throws ParseException {
|
||||
GenericFormatterRegistry registry = new GenericFormatterRegistry();
|
||||
registry.add(new CurrencyAnnotationFormatterFactory());
|
||||
binder.setFormatterRegistry(registry);
|
||||
binder.registerFormatterFactory(new CurrencyAnnotationFormatterFactory());
|
||||
binder.bind(Collections.singletonMap("currency", "$23.56"));
|
||||
assertEquals(new BigDecimal("23.56"), bean.getCurrency());
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue