Skip to content

Commit

Permalink
Fix concurrent admins note editing and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TatuJLund committed Jul 14, 2024
1 parent 6cf314c commit c543d64
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.vaadin.tatu.vaadincreate.backend.data.Message;
import org.vaadin.tatu.vaadincreate.backend.data.User.Role;
import org.vaadin.tatu.vaadincreate.eventbus.EventBus;
import org.vaadin.tatu.vaadincreate.eventbus.EventBus.EventBusListener;
import org.vaadin.tatu.vaadincreate.i18n.HasI18N;
import org.vaadin.tatu.vaadincreate.util.Utils;

Expand All @@ -29,7 +30,8 @@

@AllPermitted
@SuppressWarnings("serial")
public class AboutView extends VerticalLayout implements View, HasI18N {
public class AboutView extends VerticalLayout
implements View, EventBusListener, HasI18N {

public static final String VIEW_NAME = "about";

Expand All @@ -41,12 +43,14 @@ public class AboutView extends VerticalLayout implements View, HasI18N {

private Button editButton;
private Label adminsNote;
private TextArea textArea;

private EventBus eventBus = EventBus.get();

public AboutView() {
var aboutContent = createAboutContent();

var textArea = createTextArea();
textArea = createTextArea();
textArea.setVisible(false);

var adminsContent = new HorizontalLayout();
Expand Down Expand Up @@ -81,6 +85,7 @@ public AboutView() {
addComponents(aboutContent, adminsContent);
setComponentAlignment(aboutContent, Alignment.MIDDLE_CENTER);
setComponentAlignment(adminsContent, Alignment.MIDDLE_CENTER);
eventBus.registerEventBusListener(this);
}

private void handleValueChange(ValueChangeEvent<String> e) {
Expand Down Expand Up @@ -149,5 +154,28 @@ public void enter(ViewChangeEvent event) {
adminsNote.setValue(message.getMessage());
}

@Override
public void eventFired(Object event) {
if (event instanceof Message) {
getUI().access(() -> {
if (textArea.isVisible()) {
textArea.setVisible(false);
adminsNote.setVisible(true);
editButton.setVisible(true);
}
Message mes = (Message) event;
adminsNote.setCaption(
Utils.formatDate(mes.getDateStamp(), getLocale()));
adminsNote.setValue(mes.getMessage());
});
}
}

@Override
public void detach() {
super.detach();
eventBus.unregisterEventBusListener(this);
}

private Logger logger = LoggerFactory.getLogger(this.getClass());
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void leaveMessageSanitized() {
var notification = $(Notification.class).last();
assertEquals("<b><img>A new message</b>",
notification.getDescription());
assertEquals(note.getCaption(), notification.getCaption());

// Return the old value
test($(Button.class).id("admin-edit")).click();
Expand All @@ -74,10 +75,21 @@ public void leaveMessageSanitized() {

@Test
public void messageFromEventShown() {
test($(Button.class).id("admin-edit")).click();
var area = $(TextArea.class).id("admins-text-area");
assertTrue(area.isVisible());

EventBus.get().post(new Message("Hello", LocalDateTime.now()));

waitWhile(view, n -> $(Notification.class).last() == null, 2);
var notification = $(Notification.class).last();

assertFalse(area.isVisible());

assertEquals("Hello", notification.getDescription());
var note = $(Label.class).id("admins-note");
assertEquals("Hello", note.getValue());
assertEquals(note.getCaption(), notification.getCaption());
}

}

0 comments on commit c543d64

Please sign in to comment.