Skip to content

Commit 782ffd8

Browse files
GH-5382 Create console command fix for config ns change, plus a test case.
1 parent 175cebd commit 782ffd8

File tree

2 files changed

+138
-27
lines changed

2 files changed

+138
-27
lines changed

tools/console/src/main/java/org/eclipse/rdf4j/console/command/Create.java

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.HashMap;
2929
import java.util.List;
3030
import java.util.Map;
31+
import java.util.Optional;
3132
import java.util.stream.Collectors;
3233
import java.util.stream.Stream;
3334

@@ -40,6 +41,7 @@
4041
import org.eclipse.rdf4j.model.impl.LinkedHashModel;
4142
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
4243
import org.eclipse.rdf4j.model.util.Models;
44+
import org.eclipse.rdf4j.model.vocabulary.CONFIG;
4345
import org.eclipse.rdf4j.model.vocabulary.RDF;
4446
import org.eclipse.rdf4j.repository.RepositoryReadOnlyException;
4547
import org.eclipse.rdf4j.repository.config.ConfigTemplate;
@@ -143,12 +145,12 @@ private String getUserTemplates() {
143145
*/
144146
private String getBuiltinTemplates() {
145147
// assume the templates are all located in the same jar "directory" as the RepositoryConfig class
146-
Class cl = RepositoryConfig.class;
148+
Class<RepositoryConfig> cl = RepositoryConfig.class;
147149

148150
try {
149151
URI dir = cl.getResource(cl.getSimpleName() + ".class").toURI();
150152
if (dir.getScheme().equals("jar")) {
151-
try (FileSystem fs = FileSystems.newFileSystem(dir, Collections.EMPTY_MAP, null)) {
153+
try (FileSystem fs = FileSystems.newFileSystem(dir, Collections.emptyMap(), null)) {
152154
// turn package structure into directory structure
153155
String pkg = cl.getPackage().getName().replaceAll("\\.", "/");
154156
return getOrderedTemplates(fs.getPath(pkg));
@@ -187,15 +189,9 @@ private void createRepository(final String templateName) {
187189
boolean eof = inputParameters(valueMap, variableMap, configTemplate.getMultilineMap());
188190
if (!eof) {
189191
final String configString = configTemplate.render(valueMap);
190-
final Model graph = new LinkedHashModel();
192+
final Model graph = parseConfigIntoModel(configString);
191193

192-
final RDFParser rdfParser = Rio.createParser(RDFFormat.TURTLE, SimpleValueFactory.getInstance());
193-
rdfParser.setRDFHandler(new StatementCollector(graph));
194-
rdfParser.parse(new StringReader(configString), RepositoryConfigSchema.NAMESPACE);
195-
196-
final Resource repositoryNode = Models
197-
.subject(graph.getStatements(null, RDF.TYPE, RepositoryConfigSchema.REPOSITORY))
198-
.orElseThrow(() -> new RepositoryConfigException("missing repository node"));
194+
final Resource repositoryNode = findRepositoryNode(graph);
199195

200196
final RepositoryConfig repConfig = RepositoryConfig.create(graph, repositoryNode);
201197
repConfig.validate();
@@ -232,6 +228,25 @@ private void createRepository(final String templateName) {
232228
}
233229
}
234230

231+
private Resource findRepositoryNode(final Model graph) {
232+
Optional<Resource> modern = Models
233+
.subject(graph.getStatements(null, RDF.TYPE, CONFIG.Rep.Repository));
234+
Resource repositoryNode = modern
235+
.orElseGet(() -> Models
236+
.subject(graph.getStatements(null, RDF.TYPE, RepositoryConfigSchema.REPOSITORY))
237+
.orElseThrow(() -> new RepositoryConfigException("missing repository node")));
238+
return repositoryNode;
239+
}
240+
241+
private Model parseConfigIntoModel(final String configString) throws IOException {
242+
final Model graph = new LinkedHashModel();
243+
244+
final RDFParser rdfParser = Rio.createParser(RDFFormat.TURTLE, SimpleValueFactory.getInstance());
245+
rdfParser.setRDFHandler(new StatementCollector(graph));
246+
rdfParser.parse(new StringReader(configString), CONFIG.NAMESPACE);
247+
return graph;
248+
}
249+
235250
/**
236251
* Ask user to specify values for the template variables
237252
*
@@ -251,23 +266,7 @@ private boolean inputParameters(final Map<String, String> valueMap, final Map<St
251266
final String var = entry.getKey();
252267
final List<String> values = entry.getValue();
253268

254-
StringBuilder sb = new StringBuilder();
255-
sb.append(var);
256-
257-
if (values.size() > 1) {
258-
sb.append(" (");
259-
for (int i = 0; i < values.size(); i++) {
260-
if (i > 0) {
261-
sb.append("|");
262-
}
263-
sb.append(values.get(i));
264-
}
265-
sb.append(")");
266-
}
267-
if (!values.isEmpty()) {
268-
sb.append(" [" + values.get(0) + "]");
269-
}
270-
String prompt = sb.append(": ").toString();
269+
String prompt = buildPromptString(values, var);
271270
String value = multilineInput.containsKey(var) ? consoleIO.readMultiLineInput(prompt)
272271
: consoleIO.readln(prompt);
273272
eof = (value == null);
@@ -284,6 +283,25 @@ private boolean inputParameters(final Map<String, String> valueMap, final Map<St
284283
return eof;
285284
}
286285

286+
private String buildPromptString(final List<String> values, String var) {
287+
StringBuilder sb = new StringBuilder();
288+
sb.append(var);
289+
if (values.size() > 1) {
290+
sb.append(" (");
291+
for (int i = 0; i < values.size(); i++) {
292+
if (i > 0) {
293+
sb.append("|");
294+
}
295+
sb.append(values.get(i));
296+
}
297+
sb.append(")");
298+
}
299+
if (!values.isEmpty()) {
300+
sb.append(" [" + values.get(0) + "]");
301+
}
302+
return sb.append(": ").toString();
303+
}
304+
287305
/**
288306
* Create input stream from a template file in the specified file directory. If the file cannot be found, try to
289307
* read it from the embedded java resources instead.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 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.console.command;
12+
13+
import static org.junit.jupiter.api.Assertions.assertFalse;
14+
import static org.mockito.Mockito.mock;
15+
import static org.mockito.Mockito.when;
16+
17+
import java.io.IOException;
18+
import java.io.InputStream;
19+
import java.io.OutputStream;
20+
21+
import org.eclipse.rdf4j.common.exception.RDF4JException;
22+
import org.eclipse.rdf4j.console.ConsoleIO;
23+
import org.eclipse.rdf4j.repository.manager.LocalRepositoryManager;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
26+
27+
/**
28+
* Test verify command
29+
*
30+
* @author Bart Hanssens
31+
*/
32+
public class CreateTest extends AbstractCommandTest {
33+
private Create cmd;
34+
private ConsoleIO io;
35+
36+
@BeforeEach
37+
public void setUp() throws IOException, RDF4JException {
38+
InputStream input = mock(InputStream.class);
39+
OutputStream out = mock(OutputStream.class);
40+
when(mockConsoleState.getDataDirectory()).thenReturn(locationFile);
41+
when(mockConsoleState.getManager()).thenReturn(new LocalRepositoryManager(locationFile));
42+
43+
io = new ConsoleIO(input, out, mockConsoleState) {
44+
45+
@Override
46+
public String readMultiLineInput(String promt) {
47+
switch (promt) {
48+
default:
49+
return null;
50+
}
51+
}
52+
53+
@Override
54+
public String readMultiLineInput() {
55+
return null;
56+
}
57+
58+
@Override
59+
public String readln(String... message) {
60+
switch (message[0]) {
61+
case "Repository ID [memory]: ":
62+
return "Y";
63+
case "Repository title [Memory store]: ":
64+
return "Create-Test-Memory-Store";
65+
case "Query Iteration Cache sync threshold [10000]: ":
66+
return "10";
67+
case "Persist (true|false) [true]: ":
68+
return "false";
69+
case "Sync delay [0]: ":
70+
return "0";
71+
case "Query Evaluation Mode (STRICT|STANDARD) [STRICT]: ":
72+
return "STANDARD";
73+
}
74+
return null;
75+
}
76+
77+
@Override
78+
public boolean askProceed(String msg, boolean defaultValue) {
79+
return true;
80+
}
81+
82+
};
83+
84+
cmd = new Create(io, mockConsoleState);
85+
}
86+
87+
@Test
88+
public final void startCreate() {
89+
cmd.execute("create", "memory");
90+
assertFalse(io.wasErrorWritten());
91+
}
92+
93+
}

0 commit comments

Comments
 (0)