|
| 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 | +} |
0 commit comments