diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/ClubhelperErrorDialog.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/ClubhelperErrorDialog.java index 40dc19e..2971cc1 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/ClubhelperErrorDialog.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/ClubhelperErrorDialog.java @@ -18,15 +18,17 @@ public class ClubhelperErrorDialog { VerticalLayout layout = new VerticalLayout(); layout.addComponent(new Label(errormessage)); layout.addComponent(new Label(exception.getMessage())); - StringWriter out = new StringWriter(); - PrintWriter writer = new PrintWriter(out); - exception.printStackTrace(writer); - layout.addComponent(toComponent(out)); + layout.addComponent(toStacktraceComponent(exception)); layout.addComponent(new Button("Schließen", event -> errorDlg.close())); errorDlg.setContent(layout); } - public Label toComponent(StringWriter out) { + Label toStacktraceComponent(Exception exception) { + + StringWriter out = new StringWriter(); + PrintWriter writer = new PrintWriter(out); + exception.printStackTrace(writer); + writer.close(); Label label = new Label(out.toString()); label.setContentMode(ContentMode.PREFORMATTED); return label; diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/ClubhelperErrorDialogTest.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/ClubhelperErrorDialogTest.java new file mode 100644 index 0000000..6c27b3b --- /dev/null +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/ui/ClubhelperErrorDialogTest.java @@ -0,0 +1,69 @@ +package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.verify; + +import java.io.PrintWriter; +import java.io.StringWriter; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.vaadin.ui.Component; +import com.vaadin.ui.HasComponents; +import com.vaadin.ui.Label; +import com.vaadin.ui.UI; +import com.vaadin.ui.Window; + +class ClubhelperErrorDialogTest { + + @Mock + private UI ui; + + @BeforeEach + void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + void testErrorDialogContent() { + Exception e = new Exception("Exception message"); + ClubhelperErrorDialog dlg = new ClubhelperErrorDialog("errormessage", e); + dlg.show(ui); + ArgumentCaptor windowCaptor = ArgumentCaptor.forClass(Window.class); + verify(ui).addWindow(windowCaptor.capture()); + Window window = windowCaptor.getValue(); + Component content = window.getContent(); + assertTrue(content instanceof HasComponents); + boolean errorMessageFound = false; + boolean exceptionMessageFound = false; + boolean stacktraceFound = false; + StringWriter out = new StringWriter(); + PrintWriter writer = new PrintWriter(out); + e.printStackTrace(writer); + writer.close(); + String stacktraceLine = out.toString().split("\n")[0].trim(); + for (Component c : (HasComponents) content) { + if (c instanceof Label) { + Label label = (Label) c; + String text = label.getValue(); + if ("errormessage".equals(text)) { + errorMessageFound = true; + } + else if ("Exception message".equals(text)) { + exceptionMessageFound = true; + } + else if (text.contains(stacktraceLine)) { + stacktraceFound = true; + } + } + } + assertTrue(errorMessageFound, "Errormessage (caption) not found!"); + assertTrue(exceptionMessageFound, "Exception message not found!"); + assertTrue(stacktraceFound, "Stacktrace not found!"); + } + +}