RYA-428 Made the Rya Shell's load statements file command accept relative paths. Closes #262.
diff --git a/extras/shell/src/main/java/org/apache/rya/shell/RyaCommands.java b/extras/shell/src/main/java/org/apache/rya/shell/RyaCommands.java
index 09ee410..cfc7436 100644
--- a/extras/shell/src/main/java/org/apache/rya/shell/RyaCommands.java
+++ b/extras/shell/src/main/java/org/apache/rya/shell/RyaCommands.java
@@ -24,6 +24,8 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.text.DecimalFormat;
import java.util.Objects;
@@ -100,25 +102,30 @@
final Optional<String> ryaInstanceName = shellState.getRyaInstanceName();
try {
final long start = System.currentTimeMillis();
- final File rdfInputFile = new File(file);
+
+ // If the provided path is relative, then make it rooted in the user's home.
+ Path rootedFile = Paths.get( file.replaceFirst("^~", System.getProperty("user.home")) );
RDFFormat rdfFormat = null;
+ // If a format was provided, then go with that.
if (format != null) {
rdfFormat = RDFFormat.valueOf(format);
if (rdfFormat == null) {
throw new RuntimeException("Unsupported RDF Statement data input format: " + format);
}
}
- if (rdfFormat == null) {
- rdfFormat = RDFFormat.forFileName(rdfInputFile.getName());
+
+ // Otherwise try to figure it out using the filename.
+ else if (rdfFormat == null) {
+ rdfFormat = RDFFormat.forFileName(rootedFile.getFileName().toString());
if (rdfFormat == null) {
- throw new RuntimeException("Unable to detect RDF Statement data input format for file: " + rdfInputFile);
+ throw new RuntimeException("Unable to detect RDF Statement data input format for file: " + rootedFile);
} else {
consolePrinter.println("Detected RDF Format: " + rdfFormat);
consolePrinter.flush();
}
}
- commands.getLoadStatementsFile().loadStatements(ryaInstanceName.get(), rdfInputFile.toPath(), rdfFormat);
+ commands.getLoadStatementsFile().loadStatements(ryaInstanceName.get(), rootedFile, rdfFormat);
final String seconds = new DecimalFormat("0.0##").format((System.currentTimeMillis() - start) / 1000.0);
return "Loaded the file: '" + file + "' successfully in " + seconds + " seconds.";
diff --git a/extras/shell/src/test/java/org/apache/rya/shell/RyaCommandsTest.java b/extras/shell/src/test/java/org/apache/rya/shell/RyaCommandsTest.java
index a0a3979..21384ea 100644
--- a/extras/shell/src/test/java/org/apache/rya/shell/RyaCommandsTest.java
+++ b/extras/shell/src/test/java/org/apache/rya/shell/RyaCommandsTest.java
@@ -79,6 +79,38 @@
}
@Test
+ public void loadData_relativePath() throws Exception {
+ // Mock the object that performs the create operation.
+ final String instanceName = "unitTest";
+ final String statementsFile = "~/statements.nt";
+ final String format = null;
+
+ final LoadStatementsFile mockLoadStatementsFile = mock(LoadStatementsFile.class);
+ final RyaClient mockCommands = mock(RyaClient.class);
+ when(mockCommands.getLoadStatementsFile()).thenReturn(mockLoadStatementsFile);
+
+ final SharedShellState state = new SharedShellState();
+ state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands);
+ state.connectedToInstance(instanceName);
+
+ final SparqlPrompt mockSparqlPrompt = mock(SparqlPrompt.class);
+ final ConsolePrinter mockConsolePrinter = mock(ConsolePrinter.class);
+
+ // Execute the command.
+ final RyaCommands commands = new RyaCommands(state, mockSparqlPrompt, mockConsolePrinter);
+ final String message = commands.loadData(statementsFile, format);
+
+ // Verify the values that were provided to the command were passed through to LoadStatementsFile
+ // using a user rooted filename.
+ String rootedFile = System.getProperty("user.home") + "/statements.nt";
+ verify(mockLoadStatementsFile).loadStatements(instanceName, Paths.get(rootedFile), RDFFormat.NTRIPLES);
+
+ // Verify a message is returned that explains what was created.
+ assertTrue(message.startsWith("Loaded the file: '" + statementsFile +"' successfully in "));
+ assertTrue(message.endsWith(" seconds."));
+ }
+
+ @Test
public void testLoadData_specifyFormat() throws InstanceDoesNotExistException, RyaClientException, IOException {
// Mock the object that performs the create operation.
final String instanceName = "unitTest";