Skip to content

Commit 5e6b1ea

Browse files
authored
Make ReactiveDefaultBookmarkManager return a copy of bookmarks (#2769)
This update makes sure that the `ReactiveDefaultBookmarkManager` returns an unmodifiable copy of its current bookmarks instead of returning a view of the bookmarks.
1 parent b3d30af commit 5e6b1ea

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Default bookmark manager.
3030
*
3131
* @author Michael J. Simons
32+
* @author Dmitriy Tverdiakov
3233
* @soundtrack Helge Schneider - The Last Jazz
3334
* @since 7.0
3435
*/
@@ -48,7 +49,7 @@ final class ReactiveDefaultBookmarkManager extends AbstractBookmarkManager {
4849
@Override
4950
public Collection<Bookmark> getBookmarks() {
5051
this.bookmarks.addAll(bookmarksSupplier.get());
51-
return Collections.synchronizedSet(Collections.unmodifiableSet(this.bookmarks));
52+
return Collections.synchronizedSet(Collections.unmodifiableSet(new HashSet<>(this.bookmarks)));
5253
}
5354

5455
@Override
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
20+
21+
import java.util.Arrays;
22+
import java.util.Collections;
23+
import java.util.HashSet;
24+
import java.util.Set;
25+
26+
import org.junit.jupiter.api.Test;
27+
import org.neo4j.driver.Bookmark;
28+
29+
/**
30+
* @author Dmitriy Tverdiakov
31+
*/
32+
class ReactiveDefaultBookmarkManagerTest {
33+
@Test
34+
void shouldReturnBookmarksCopy() {
35+
var manager = new ReactiveDefaultBookmarkManager(null);
36+
var initialBookmarks = new HashSet<>(Arrays.asList(Bookmark.from("bookmark 1"), null));
37+
manager.updateBookmarks(Collections.emptyList(), initialBookmarks);
38+
39+
var bookmarks = manager.getBookmarks();
40+
manager.updateBookmarks(initialBookmarks, Set.of(Bookmark.from("bookmark2")));
41+
42+
assertEquals(initialBookmarks, bookmarks);
43+
}
44+
45+
@Test
46+
void shouldReturnUnmodifiableBookmarks() {
47+
var manager = new ReactiveDefaultBookmarkManager(null);
48+
var initialBookmarks = Set.of(Bookmark.from("bookmark1"));
49+
manager.updateBookmarks(Collections.emptyList(), initialBookmarks);
50+
var bookmarks = manager.getBookmarks();
51+
52+
assertThrows(UnsupportedOperationException.class, () -> bookmarks.add(Bookmark.from("bookmark 2")));
53+
assertThrows(UnsupportedOperationException.class, () -> bookmarks.remove(Bookmark.from("bookmark 1")));
54+
assertThrows(UnsupportedOperationException.class, bookmarks::clear);
55+
}
56+
}

0 commit comments

Comments
 (0)