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