Repository: incubator-rya
Updated Branches:
  refs/heads/master 470b224b3 -> 9f611019f


RYA-428 Made the Rya Shell's load statements file command accept relative 
paths. Closes #262.


Project: http://git-wip-us.apache.org/repos/asf/incubator-rya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-rya/commit/fff50c43
Tree: http://git-wip-us.apache.org/repos/asf/incubator-rya/tree/fff50c43
Diff: http://git-wip-us.apache.org/repos/asf/incubator-rya/diff/fff50c43

Branch: refs/heads/master
Commit: fff50c439f771e9eac80b41c9b9aeb86f547074c
Parents: 470b224
Author: kchilton2 <[email protected]>
Authored: Mon Jan 8 19:30:37 2018 -0500
Committer: caleb <[email protected]>
Committed: Thu Jan 11 09:49:37 2018 -0500

----------------------------------------------------------------------
 .../java/org/apache/rya/shell/RyaCommands.java  | 17 ++++++++---
 .../org/apache/rya/shell/RyaCommandsTest.java   | 32 ++++++++++++++++++++
 2 files changed, 44 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/fff50c43/extras/shell/src/main/java/org/apache/rya/shell/RyaCommands.java
----------------------------------------------------------------------
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.File;
 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 @@ public class RyaCommands implements CommandMarker {
         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.";

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/fff50c43/extras/shell/src/test/java/org/apache/rya/shell/RyaCommandsTest.java
----------------------------------------------------------------------
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 @@ public class RyaCommandsTest {
     }
 
     @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";

Reply via email to