Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi-asprino committed Oct 28, 2024
1 parent 33515fa commit 8b0f6a9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> actualSet = new HashSet<>();
for (CSVRecord record : parser) {
actualSet.add(record.get(3));
}
Set<String> 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * { ?s ?p ?o OPTIONAL {?s a ?c} }

0 comments on commit 8b0f6a9

Please sign in to comment.