Skip to content

Commit

Permalink
Bug fix and add unit test for pessimistic locking
Browse files Browse the repository at this point in the history
  • Loading branch information
TatuLund committed May 27, 2024
1 parent 689fc28 commit 0856424
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ public void setEdited(Product product) {
@Override
public void eventFired(Object event) {
if (event instanceof BookEvent && isAttached()) {
logger.info("Book locking update");
var bookEvent = (BookEvent) event;
getUI().access(() -> {
ListDataProvider<Product> dataProvider = (ListDataProvider<Product>) getDataProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public void editProduct(Product product) {
if (product == null) {
view.setFragmentParameter("");
unlockBook();
} else if (product.getId() == -1) {
view.setFragmentParameter("new");
} else {
view.setFragmentParameter(product.getId() + "");
lockBook(product.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ private HorizontalLayout createTopBar() {
ResetButtonForTextField.extend(filter);
// Apply the filter to grid's data provider. TextField value is never
// null
filter.addValueChangeListener(
event -> dataProvider
.setFilter(book -> passesFilter(book, event.getValue())));
filter.addValueChangeListener(event -> dataProvider
.setFilter(book -> passesFilter(book, event.getValue())));

newProduct = new Button(getTranslation(NEW_PRODUCT));
newProduct.setId("new-product");
Expand Down Expand Up @@ -299,9 +298,11 @@ public void beforeLeave(ViewBeforeLeaveEvent event) {
// IMHO: Navigator clears url too early and this workaround
// shouldn't be necessary. This is a possible bug.
var book = getSelectedRow();
getUI().access(() -> {
setFragmentParameter("" + book.getId());
});
if (book != null) {
getUI().access(() -> {
setFragmentParameter("" + book.getId());
});
}
} else {
event.navigate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ public Collection<Integer> lockedBooks() {

@Override
public void lock(Integer id) {
if (id != null && id < 0) {
throw new IllegalArgumentException(
"Id can't be null and must be positive");
}
synchronized (books) {
var match = books.keySet().stream().filter(i -> i.equals(id))
.findFirst();
if (match.isPresent()) {
throw new IllegalStateException(
"Can't open book already opened: " + id);
"Can't locked book already locked: " + id);
}
books.put(id, null);
eventBus.post(new BookEvent(id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ public void editProduct() {
assertEquals("Edited book", edited.getProductName());
}

@Test
public void editLockedProduct() {
var book = test(grid).item(0);
LockedBooks.get().lock(book.getId());

test(grid).click(1, 0);
assertFalse(form.isShown());
LockedBooks.get().unlock(book.getId());
}

@Test
public void editProductDiscardChanges() {
test(grid).click(1, 0);
Expand Down

0 comments on commit 0856424

Please sign in to comment.