StorageConnector extends AbstractConnector { @Override public <T> T unwrap(Class<T> cls) throws OntoDriverException { if (cls.isAssignableFrom(getClass())) { return cls.cast(this); } if (cls.isAssignableFrom(repository.getClass())) { return cls.cast(repository); } throw new SesameDriverException("No instance of class " + cls + " found."); } StorageConnector(DriverConfiguration configuration); @Override void close(); @Override TupleQueryResult executeSelectQuery(String query); @Override boolean executeBooleanQuery(String query); @Override void executeUpdate(String query); @Override List<Resource> getContexts(); @Override ValueFactory getValueFactory(); @Override void begin(); @Override void commit(); @Override void rollback(); @Override void addStatements(Collection<Statement> statements); @Override void removeStatements(Collection<Statement> statements); @Override Collection<Statement> findStatements(Resource subject, IRI property, Value value, boolean includeInferred); @Override Collection<Statement> findStatements(Resource subject, org.eclipse.rdf4j.model.IRI property,
                                                Value value, boolean includeInferred, IRI context); @Override boolean containsStatement(Resource subject, IRI property, Value value, boolean includeInferred); @Override boolean containsStatement(Resource subject, IRI property, Value value, boolean includeInferred, IRI context); @Override T unwrap(Class<T> cls); void setRepository(Repository newRepository);  }
@Test void initializationLoadsRepositoryConfigurationFromFileOnClasspathAndCreatesRepo() throws Exception { final DriverConfiguration conf = TestUtils.createDriverConfig("urn:test"); conf.setProperty(SesameConfigParam.REPOSITORY_CONFIG, "classpath:repo-configs/memory-rdfs.ttl"); this.connector = new StorageConnector(conf); final Repository repo = connector.unwrap(Repository.class); assertTrue(repo instanceof SailRepository); assertTrue(((SailRepository) repo).getSail() instanceof SchemaCachingRDFSInferencer); }
@Test void initializationLoadsRepositoryConfigurationFromFileWithAbsolutePathAndCreatesRepo() throws Exception { final Path file = Files.createTempFile("memory-rdfs", ".ttl"); try (final BufferedReader in = new BufferedReader( new InputStreamReader( getClass().getClassLoader().getResourceAsStream("repo-configs/memory-rdfs.ttl")))) { final String content = in.lines().collect(Collectors.joining("\n")); Files.write(file, content.getBytes()); } file.toFile().deleteOnExit(); final DriverConfiguration conf = TestUtils.createDriverConfig("urn:test"); conf.setProperty(SesameConfigParam.REPOSITORY_CONFIG, file.toString()); this.connector = new StorageConnector(conf); final Repository repo = connector.unwrap(Repository.class); assertTrue(repo instanceof SailRepository); assertTrue(((SailRepository) repo).getSail() instanceof SchemaCachingRDFSInferencer); }
@Test void initializationLoadsRepositoryConfigurationFromFileAndCreatesNativeRepo() throws Exception { final Path serverDir = Files.createTempDirectory("sesame-config-test"); final String physicalUri = serverDir.toString() + "/repositories/native-lucene"; try { final DriverConfiguration conf = TestUtils.createDriverConfig(physicalUri); conf.setProperty(SesameConfigParam.REPOSITORY_CONFIG, "classpath:repo-configs/native-lucene.ttl"); this.connector = new StorageConnector(conf); final Repository repo = connector.unwrap(Repository.class); assertTrue(repo instanceof SailRepository); assertTrue(((SailRepository) repo).getSail() instanceof LuceneSail); final File repoDir = new File(physicalUri); assertTrue(repoDir.exists()); } finally { Files.walk(serverDir) .sorted(Comparator.reverseOrder()) .map(Path::toFile) .forEach(File::delete); } }
@Test void unwrapReturnsItselfWhenClassMatches() throws Exception { createInMemoryConnector(); assertSame(connector, connector.unwrap(StorageConnector.class)); }
@Test void unwrapReturnsSesameRepository() throws Exception { createInMemoryConnector(); final Repository repo = connector.unwrap(Repository.class); assertNotNull(repo); assertTrue(repo.isInitialized()); }
@Test void unwrapOfUnsupportedClassThrowsException() throws Exception { createInMemoryConnector(); assertThrows(SesameDriverException.class, () -> connector.unwrap(Boolean.class)); }