|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Eclipse RDF4J contributors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Distribution License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: BSD-3-Clause |
| 10 | + *******************************************************************************/ |
| 11 | +// Some portions generated by Codex |
| 12 | + |
| 13 | +package org.eclipse.rdf4j.sail.shacl; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 16 | +import static org.mockito.ArgumentMatchers.anyBoolean; |
| 17 | +import static org.mockito.ArgumentMatchers.isNull; |
| 18 | +import static org.mockito.Mockito.doReturn; |
| 19 | +import static org.mockito.Mockito.mock; |
| 20 | +import static org.mockito.Mockito.never; |
| 21 | +import static org.mockito.Mockito.verify; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +import java.util.NoSuchElementException; |
| 25 | + |
| 26 | +import org.eclipse.rdf4j.common.iteration.CloseableIteration; |
| 27 | +import org.eclipse.rdf4j.model.Statement; |
| 28 | +import org.eclipse.rdf4j.sail.InterruptedSailException; |
| 29 | +import org.eclipse.rdf4j.sail.SailConnection; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +class ConnectionHelperTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + void transferStatementsConvertsInterruptedIterationToInterruptedSailException() { |
| 36 | + SailConnection connection = mock(SailConnection.class); |
| 37 | + @SuppressWarnings("unchecked") |
| 38 | + CloseableIteration<? extends Statement> statements = mock(CloseableIteration.class); |
| 39 | + doReturn(statements).when(connection).getStatements(isNull(), isNull(), isNull(), anyBoolean()); |
| 40 | + when(statements.hasNext()).thenReturn(true); |
| 41 | + when(statements.next()).thenAnswer(invocation -> { |
| 42 | + Thread.currentThread().interrupt(); |
| 43 | + throw new NoSuchElementException("The iteration has been interrupted."); |
| 44 | + }); |
| 45 | + |
| 46 | + try { |
| 47 | + assertThatExceptionOfType(InterruptedSailException.class) |
| 48 | + .isThrownBy(() -> ConnectionHelper.transferStatements(connection, (subject, predicate, object, |
| 49 | + context) -> { |
| 50 | + throw new AssertionError("No statement should be transferred after interrupt"); |
| 51 | + })) |
| 52 | + .withCauseInstanceOf(NoSuchElementException.class); |
| 53 | + verify(statements).close(); |
| 54 | + } finally { |
| 55 | + Thread.interrupted(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void transferStatementsKeepsNonInterruptNoSuchElementException() { |
| 61 | + SailConnection connection = mock(SailConnection.class); |
| 62 | + @SuppressWarnings("unchecked") |
| 63 | + CloseableIteration<? extends Statement> statements = mock(CloseableIteration.class); |
| 64 | + NoSuchElementException exception = new NoSuchElementException("empty"); |
| 65 | + doReturn(statements).when(connection).getStatements(isNull(), isNull(), isNull(), anyBoolean()); |
| 66 | + when(statements.hasNext()).thenReturn(true); |
| 67 | + when(statements.next()).thenThrow(exception); |
| 68 | + |
| 69 | + assertThatExceptionOfType(NoSuchElementException.class) |
| 70 | + .isThrownBy(() -> ConnectionHelper.transferStatements(connection, (subject, predicate, object, |
| 71 | + context) -> { |
| 72 | + throw new AssertionError("No statement should be transferred"); |
| 73 | + })) |
| 74 | + .isSameAs(exception); |
| 75 | + verify(statements).close(); |
| 76 | + verify(statements, never()).remove(); |
| 77 | + } |
| 78 | +} |
0 commit comments