From 8b0f6a9b4beb793f171a346e1b00e3fa839be8a9 Mon Sep 17 00:00:00 2001 From: Luigi Asprino Date: Mon, 28 Oct 2024 12:36:42 +0100 Subject: [PATCH] #511 --- .../io/github/sparqlanything/cli/CLI.java | 34 ++------- .../io/github/sparqlanything/cli/CLITest.java | 73 +++++++++++++++++++ .../test/resources/CLITestOnFileQuery.sparql | 1 + 3 files changed, 81 insertions(+), 27 deletions(-) create mode 100644 sparql-anything-cli/src/test/java/io/github/sparqlanything/cli/CLITest.java create mode 100644 sparql-anything-cli/src/test/resources/CLITestOnFileQuery.sparql diff --git a/sparql-anything-cli/src/main/java/io/github/sparqlanything/cli/CLI.java b/sparql-anything-cli/src/main/java/io/github/sparqlanything/cli/CLI.java index 982826d7..a6081227 100644 --- a/sparql-anything-cli/src/main/java/io/github/sparqlanything/cli/CLI.java +++ b/sparql-anything-cli/src/main/java/io/github/sparqlanything/cli/CLI.java @@ -23,19 +23,18 @@ import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; +import org.apache.commons.io.IOUtils; import org.apache.jena.query.Query; import org.apache.jena.riot.Lang; import org.apache.jena.util.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; import java.io.IOException; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; +import java.nio.charset.Charset; import java.util.Locale; public class CLI { @@ -85,33 +84,14 @@ public void parse(String[] args) throws ParseException { this.commandLine = cmdLineParser.parse(options, args); } - private static String getQuery(String queryArgument) throws IOException { - String query = queryArgument; - // XXX Check if queryArgument is a URI first - File queryFile; + public String getQuery() throws IOException { + String queryArgument = commandLine.getOptionValue(CLI.QUERY); try{ - queryFile = new File(new URL(queryArgument).toURI()); - }catch(MalformedURLException | URISyntaxException e){ - queryFile = new File(queryArgument); - } - if (queryFile.exists()) { - logger.trace("Loading query from file"); - // LOAD query from file - BufferedReader br = new BufferedReader(new FileReader(queryFile)); - StringBuilder sb = new StringBuilder(); - String line; - while ((line = br.readLine()) != null) { - sb.append(line); - sb.append('\n'); - } - query = sb.toString(); - br.close(); + return IOUtils.toString(new URL(queryArgument).toURI(), Charset.defaultCharset()); + } catch (MalformedURLException | URISyntaxException e) { + return queryArgument; } - return query; - } - public String getQuery() throws IOException { - return getQuery(commandLine.getOptionValue(CLI.QUERY)); } void init(){ this.options = new Options(); diff --git a/sparql-anything-cli/src/test/java/io/github/sparqlanything/cli/CLITest.java b/sparql-anything-cli/src/test/java/io/github/sparqlanything/cli/CLITest.java new file mode 100644 index 00000000..8369424e --- /dev/null +++ b/sparql-anything-cli/src/test/java/io/github/sparqlanything/cli/CLITest.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2024 SPARQL Anything Contributors @ http://github.com/sparql-anything + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.github.sparqlanything.cli; + +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; +import org.apache.commons.csv.CSVRecord; +import org.junit.Assert; +import org.junit.Test; + +import java.io.StringReader; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +public class CLITest { + + @Test + public void inlineQuery() throws Exception { + String f = Objects.requireNonNull(getClass().getClassLoader().getResource("books.xml")).toURI().toString(); + String q = "SELECT * { ?s ?p ?o OPTIONAL {?s a ?c} }"; + query(f, q); + } + + private static void query(String f, String q) throws Exception { + String out = SPARQLAnything.callMain(new String[]{"-q", q, "-c", "location=" + f, "-f", "CSV"}); + CSVParser parser = new CSVParser(new StringReader(out), CSVFormat.DEFAULT); + Set actualSet = new HashSet<>(); + for (CSVRecord record : parser) { + actualSet.add(record.get(3)); + } + Set expectedSet = new HashSet<>(); + expectedSet.add("c"); + expectedSet.add("http://sparql.xyz/facade-x/ns/root"); + expectedSet.add("http://sparql.xyz/facade-x/data/catalog"); + expectedSet.add("http://sparql.xyz/facade-x/data/book"); + expectedSet.add("http://sparql.xyz/facade-x/data/author"); + expectedSet.add("http://sparql.xyz/facade-x/data/price"); + expectedSet.add("http://sparql.xyz/facade-x/data/title"); + expectedSet.add("http://sparql.xyz/facade-x/data/genre"); + expectedSet.add("http://sparql.xyz/facade-x/data/publish_date"); + Assert.assertEquals(expectedSet, actualSet); + } + + + @Test + public void infileQuery() throws Exception { + String f = Objects.requireNonNull(getClass().getClassLoader().getResource("books.xml")).toURI().toString(); + String queryFile = Objects.requireNonNull(getClass().getClassLoader().getResource("CLITestOnFileQuery.sparql")).toURI().toString(); + query(f, queryFile); + } + + @Test + public void remoteQuery() throws Exception { + String f = Objects.requireNonNull(getClass().getClassLoader().getResource("books.xml")).toURI().toString(); + String queryFile = "https://sparql-anything.cc/examples/CLITestOnFileQuery.sparql"; + query(f, queryFile); + } +} diff --git a/sparql-anything-cli/src/test/resources/CLITestOnFileQuery.sparql b/sparql-anything-cli/src/test/resources/CLITestOnFileQuery.sparql new file mode 100644 index 00000000..d76e1539 --- /dev/null +++ b/sparql-anything-cli/src/test/resources/CLITestOnFileQuery.sparql @@ -0,0 +1 @@ +SELECT * { ?s ?p ?o OPTIONAL {?s a ?c} } \ No newline at end of file