|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 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 | +package org.eclipse.rdf4j.sail.nativerdf; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.lang.reflect.Field; |
| 17 | +import java.nio.file.Files; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import org.eclipse.rdf4j.model.IRI; |
| 21 | +import org.eclipse.rdf4j.model.Resource; |
| 22 | +import org.eclipse.rdf4j.model.ValueFactory; |
| 23 | +import org.eclipse.rdf4j.sail.SailException; |
| 24 | +import org.eclipse.rdf4j.sail.base.SailSink; |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +/** |
| 30 | + * Verifies that NativeSailStore wraps unexpected RuntimeExceptions in SailException so upstream callers can reliably |
| 31 | + * handle failures (e.g., branch flush clearing pending changes). |
| 32 | + */ |
| 33 | +public class NativeSailStoreRuntimeWrappingIT { |
| 34 | + |
| 35 | + private File dataDir; |
| 36 | + private NativeSailStore store; |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + public void setUp() throws Exception { |
| 40 | + dataDir = Files.createTempDirectory("nativestore-wrap-test").toFile(); |
| 41 | + store = new NativeSailStore(dataDir, "spoc"); |
| 42 | + } |
| 43 | + |
| 44 | + @AfterEach |
| 45 | + public void tearDown() throws Exception { |
| 46 | + if (store != null) { |
| 47 | + store.close(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testFlushWrapsRuntimeIntoSailException() throws Exception { |
| 53 | + // Replace TripleStore with a stub that throws at commit() |
| 54 | + replaceTripleStore(new ThrowOnCommitTripleStore(dataDir, "spoc")); |
| 55 | + |
| 56 | + // Add a statement to ensure a triplestore transaction is started |
| 57 | + SailSink sink = store.getExplicitSailSource().sink(null); |
| 58 | + ValueFactory vf = store.getValueFactory(); |
| 59 | + IRI s = vf.createIRI("urn:s"); |
| 60 | + IRI p = vf.createIRI("urn:p"); |
| 61 | + IRI o = vf.createIRI("urn:o"); |
| 62 | + sink.approve(s, p, o, (Resource) null); |
| 63 | + |
| 64 | + // Now flush, expecting a SailException (wrapping the runtime) |
| 65 | + assertThatThrownBy(() -> sink.flush()) |
| 66 | + .isInstanceOf(SailException.class); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testRemoveStatementsWrapsRuntimeIntoSailException() throws Exception { |
| 71 | + // Replace TripleStore with a stub that throws at removeTriplesByContext() |
| 72 | + replaceTripleStore(new ThrowOnRemoveTripleStore(dataDir, "spoc")); |
| 73 | + |
| 74 | + SailSink sink = store.getExplicitSailSource().sink(null); |
| 75 | + // Expect SailException when attempting to remove (deprecateByQuery delegates) |
| 76 | + assertThatThrownBy(() -> sink.deprecateByQuery(null, null, null, new Resource[] { null })) |
| 77 | + .isInstanceOf(SailException.class); |
| 78 | + } |
| 79 | + |
| 80 | + private void replaceTripleStore(TripleStore newTripleStore) throws Exception { |
| 81 | + Field f = NativeSailStore.class.getDeclaredField("tripleStore"); |
| 82 | + f.setAccessible(true); |
| 83 | + f.set(store, newTripleStore); |
| 84 | + } |
| 85 | + |
| 86 | + private static class ThrowOnCommitTripleStore extends TripleStore { |
| 87 | + public ThrowOnCommitTripleStore(File dir, String indexSpecStr) throws Exception { |
| 88 | + super(dir, indexSpecStr, false); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void commit() { |
| 93 | + throw new RuntimeException("simulated failure during commit"); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static class ThrowOnRemoveTripleStore extends TripleStore { |
| 98 | + public ThrowOnRemoveTripleStore(File dir, String indexSpecStr) throws Exception { |
| 99 | + super(dir, indexSpecStr, false); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public Map<Integer, Long> removeTriplesByContext(int subjID, int predID, int objID, int contextId, |
| 104 | + boolean explicit) { |
| 105 | + throw new RuntimeException("simulated failure during removeTriplesByContext"); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void startTransaction() { |
| 110 | + // no-op; we're only interested in remove path throwing |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments