Polishing.
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.data.neo4j.core.transaction;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
@@ -70,7 +71,7 @@ final class DefaultBookmarkManager extends AbstractBookmarkManager {
|
||||
try {
|
||||
write.lock();
|
||||
bookmarks.removeAll(usedBookmarks);
|
||||
bookmarks.addAll(newBookmarks);
|
||||
newBookmarks.stream().filter(Objects::nonNull).forEach(bookmarks::add);
|
||||
if (applicationEventPublisher != null) {
|
||||
applicationEventPublisher.publishEvent(new Neo4jBookmarksUpdatedEvent(new HashSet<>(bookmarks)));
|
||||
}
|
||||
|
||||
@@ -15,23 +15,24 @@
|
||||
*/
|
||||
package org.springframework.data.neo4j.core.transaction;
|
||||
|
||||
import org.neo4j.driver.Bookmark;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.neo4j.driver.Bookmark;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Default bookmark manager.
|
||||
*
|
||||
* @author Michael J. Simons
|
||||
* @author Dmitriy Tverdiakov
|
||||
* @soundtrack Helge Schneider - The Last Jazz
|
||||
* @since 7.0
|
||||
* @author Gerrit Meier
|
||||
* @since 7.1.2
|
||||
*/
|
||||
final class ReactiveDefaultBookmarkManager extends AbstractBookmarkManager {
|
||||
|
||||
@@ -49,13 +50,13 @@ final class ReactiveDefaultBookmarkManager extends AbstractBookmarkManager {
|
||||
@Override
|
||||
public Collection<Bookmark> getBookmarks() {
|
||||
this.bookmarks.addAll(bookmarksSupplier.get());
|
||||
return Collections.synchronizedSet(Collections.unmodifiableSet(new HashSet<>(this.bookmarks)));
|
||||
return Set.copyOf(this.bookmarks);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateBookmarks(Collection<Bookmark> usedBookmarks, Collection<Bookmark> newBookmarks) {
|
||||
bookmarks.removeAll(usedBookmarks);
|
||||
bookmarks.addAll(newBookmarks);
|
||||
newBookmarks.stream().filter(Objects::nonNull).forEach(bookmarks::add);
|
||||
if (applicationEventPublisher != null) {
|
||||
applicationEventPublisher.publishEvent(new Neo4jBookmarksUpdatedEvent(new HashSet<>(bookmarks)));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2011-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.neo4j.core.transaction;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.neo4j.driver.Bookmark;
|
||||
|
||||
/**
|
||||
* @author Dmitriy Tverdiakov
|
||||
* @author Michael J. Simons
|
||||
*/
|
||||
class BookmarkManagerTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(classes = {DefaultBookmarkManager.class, ReactiveDefaultBookmarkManager.class})
|
||||
void shouldReturnBookmarksCopy(Class<? extends Neo4jBookmarkManager> bookmarkManagerType) throws Exception {
|
||||
|
||||
var manager = newBookmarkManager(bookmarkManagerType);
|
||||
var bm1 = Bookmark.from("bookmark 1");
|
||||
var initialBookmarks = new HashSet<>(Arrays.asList(bm1, null));
|
||||
manager.updateBookmarks(Collections.emptyList(), initialBookmarks);
|
||||
|
||||
var bookmarks = manager.getBookmarks();
|
||||
manager.updateBookmarks(initialBookmarks, Set.of(Bookmark.from("bookmark2")));
|
||||
|
||||
assertThat(bookmarks).containsExactly(bm1);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(classes = {DefaultBookmarkManager.class, ReactiveDefaultBookmarkManager.class})
|
||||
void shouldReturnUnmodifiableBookmarks(Class<? extends Neo4jBookmarkManager> bookmarkManagerType) throws Exception {
|
||||
|
||||
var manager = newBookmarkManager(bookmarkManagerType);
|
||||
var initialBookmarks = Set.of(Bookmark.from("bookmark1"));
|
||||
manager.updateBookmarks(Collections.emptyList(), initialBookmarks);
|
||||
var bookmarks = manager.getBookmarks();
|
||||
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class)
|
||||
.isThrownBy(() -> bookmarks.add(Bookmark.from("bookmark 2")));
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class)
|
||||
.isThrownBy(() -> bookmarks.remove(Bookmark.from("bookmark 1")));
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class)
|
||||
.isThrownBy(bookmarks::clear);
|
||||
}
|
||||
|
||||
static Neo4jBookmarkManager newBookmarkManager(Class<? extends Neo4jBookmarkManager> type) throws Exception {
|
||||
return type.getDeclaredConstructor(Supplier.class).newInstance((Supplier<?>) null);
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.neo4j.core.transaction;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.neo4j.driver.Bookmark;
|
||||
|
||||
/**
|
||||
* @author Dmitriy Tverdiakov
|
||||
*/
|
||||
class ReactiveDefaultBookmarkManagerTest {
|
||||
@Test
|
||||
void shouldReturnBookmarksCopy() {
|
||||
var manager = new ReactiveDefaultBookmarkManager(null);
|
||||
var initialBookmarks = new HashSet<>(Arrays.asList(Bookmark.from("bookmark 1"), null));
|
||||
manager.updateBookmarks(Collections.emptyList(), initialBookmarks);
|
||||
|
||||
var bookmarks = manager.getBookmarks();
|
||||
manager.updateBookmarks(initialBookmarks, Set.of(Bookmark.from("bookmark2")));
|
||||
|
||||
assertEquals(initialBookmarks, bookmarks);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnUnmodifiableBookmarks() {
|
||||
var manager = new ReactiveDefaultBookmarkManager(null);
|
||||
var initialBookmarks = Set.of(Bookmark.from("bookmark1"));
|
||||
manager.updateBookmarks(Collections.emptyList(), initialBookmarks);
|
||||
var bookmarks = manager.getBookmarks();
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> bookmarks.add(Bookmark.from("bookmark 2")));
|
||||
assertThrows(UnsupportedOperationException.class, () -> bookmarks.remove(Bookmark.from("bookmark 1")));
|
||||
assertThrows(UnsupportedOperationException.class, bookmarks::clear);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user