Skip to content

Commit 0b935ea

Browse files
Polishing.
1 parent 5e6b1ea commit 0b935ea

File tree

4 files changed

+83
-65
lines changed

4 files changed

+83
-65
lines changed

src/main/java/org/springframework/data/neo4j/core/transaction/DefaultBookmarkManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Collection;
1919
import java.util.Collections;
2020
import java.util.HashSet;
21+
import java.util.Objects;
2122
import java.util.Set;
2223
import java.util.concurrent.locks.Lock;
2324
import java.util.concurrent.locks.ReentrantReadWriteLock;
@@ -70,7 +71,7 @@ public void updateBookmarks(Collection<Bookmark> usedBookmarks, Collection<Bookm
7071
try {
7172
write.lock();
7273
bookmarks.removeAll(usedBookmarks);
73-
bookmarks.addAll(newBookmarks);
74+
newBookmarks.stream().filter(Objects::nonNull).forEach(bookmarks::add);
7475
if (applicationEventPublisher != null) {
7576
applicationEventPublisher.publishEvent(new Neo4jBookmarksUpdatedEvent(new HashSet<>(bookmarks)));
7677
}

src/main/java/org/springframework/data/neo4j/core/transaction/ReactiveDefaultBookmarkManager.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,24 @@
1515
*/
1616
package org.springframework.data.neo4j.core.transaction;
1717

18-
import org.neo4j.driver.Bookmark;
19-
import org.springframework.context.ApplicationEventPublisher;
20-
import org.springframework.lang.Nullable;
21-
2218
import java.util.Collection;
2319
import java.util.Collections;
2420
import java.util.HashSet;
21+
import java.util.Objects;
2522
import java.util.Set;
2623
import java.util.function.Supplier;
2724

25+
import org.neo4j.driver.Bookmark;
26+
import org.springframework.context.ApplicationEventPublisher;
27+
import org.springframework.lang.Nullable;
28+
2829
/**
2930
* Default bookmark manager.
3031
*
3132
* @author Michael J. Simons
3233
* @author Dmitriy Tverdiakov
33-
* @soundtrack Helge Schneider - The Last Jazz
34-
* @since 7.0
34+
* @author Gerrit Meier
35+
* @since 7.1.2
3536
*/
3637
final class ReactiveDefaultBookmarkManager extends AbstractBookmarkManager {
3738

@@ -49,13 +50,13 @@ final class ReactiveDefaultBookmarkManager extends AbstractBookmarkManager {
4950
@Override
5051
public Collection<Bookmark> getBookmarks() {
5152
this.bookmarks.addAll(bookmarksSupplier.get());
52-
return Collections.synchronizedSet(Collections.unmodifiableSet(new HashSet<>(this.bookmarks)));
53+
return Set.copyOf(this.bookmarks);
5354
}
5455

5556
@Override
5657
public void updateBookmarks(Collection<Bookmark> usedBookmarks, Collection<Bookmark> newBookmarks) {
5758
bookmarks.removeAll(usedBookmarks);
58-
bookmarks.addAll(newBookmarks);
59+
newBookmarks.stream().filter(Objects::nonNull).forEach(bookmarks::add);
5960
if (applicationEventPublisher != null) {
6061
applicationEventPublisher.publishEvent(new Neo4jBookmarksUpdatedEvent(new HashSet<>(bookmarks)));
6162
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2011-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.core.transaction;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20+
21+
import java.util.Arrays;
22+
import java.util.Collections;
23+
import java.util.HashSet;
24+
import java.util.Set;
25+
import java.util.function.Supplier;
26+
27+
import org.junit.jupiter.params.ParameterizedTest;
28+
import org.junit.jupiter.params.provider.ValueSource;
29+
import org.neo4j.driver.Bookmark;
30+
31+
/**
32+
* @author Dmitriy Tverdiakov
33+
* @author Michael J. Simons
34+
*/
35+
class BookmarkManagerTest {
36+
37+
@ParameterizedTest
38+
@ValueSource(classes = {DefaultBookmarkManager.class, ReactiveDefaultBookmarkManager.class})
39+
void shouldReturnBookmarksCopy(Class<? extends Neo4jBookmarkManager> bookmarkManagerType) throws Exception {
40+
41+
var manager = newBookmarkManager(bookmarkManagerType);
42+
var bm1 = Bookmark.from("bookmark 1");
43+
var initialBookmarks = new HashSet<>(Arrays.asList(bm1, null));
44+
manager.updateBookmarks(Collections.emptyList(), initialBookmarks);
45+
46+
var bookmarks = manager.getBookmarks();
47+
manager.updateBookmarks(initialBookmarks, Set.of(Bookmark.from("bookmark2")));
48+
49+
assertThat(bookmarks).containsExactly(bm1);
50+
}
51+
52+
@ParameterizedTest
53+
@ValueSource(classes = {DefaultBookmarkManager.class, ReactiveDefaultBookmarkManager.class})
54+
void shouldReturnUnmodifiableBookmarks(Class<? extends Neo4jBookmarkManager> bookmarkManagerType) throws Exception {
55+
56+
var manager = newBookmarkManager(bookmarkManagerType);
57+
var initialBookmarks = Set.of(Bookmark.from("bookmark1"));
58+
manager.updateBookmarks(Collections.emptyList(), initialBookmarks);
59+
var bookmarks = manager.getBookmarks();
60+
61+
assertThatExceptionOfType(UnsupportedOperationException.class)
62+
.isThrownBy(() -> bookmarks.add(Bookmark.from("bookmark 2")));
63+
assertThatExceptionOfType(UnsupportedOperationException.class)
64+
.isThrownBy(() -> bookmarks.remove(Bookmark.from("bookmark 1")));
65+
assertThatExceptionOfType(UnsupportedOperationException.class)
66+
.isThrownBy(bookmarks::clear);
67+
}
68+
69+
static Neo4jBookmarkManager newBookmarkManager(Class<? extends Neo4jBookmarkManager> type) throws Exception {
70+
return type.getDeclaredConstructor(Supplier.class).newInstance((Supplier<?>) null);
71+
}
72+
}

src/test/java/org/springframework/data/neo4j/core/transaction/ReactiveDefaultBookmarkManagerTest.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)