[Mulgara-svn] r517 - branches/nw-interface/src/jar/ant-task/java/org/mulgara/ant/task/rdf
pag at mulgara.org
pag at mulgara.org
Tue Nov 6 19:09:34 UTC 2007
Author: pag
Date: 2007-11-06 13:09:33 -0600 (Tue, 06 Nov 2007)
New Revision: 517
Modified:
branches/nw-interface/src/jar/ant-task/java/org/mulgara/ant/task/rdf/RDFLoad.java
branches/nw-interface/src/jar/ant-task/java/org/mulgara/ant/task/rdf/RDFLoadLog.java
branches/nw-interface/src/jar/ant-task/java/org/mulgara/ant/task/rdf/RDFLoadUnitTest.java
Log:
Reformatted code. Added generics.
Modified: branches/nw-interface/src/jar/ant-task/java/org/mulgara/ant/task/rdf/RDFLoad.java
===================================================================
--- branches/nw-interface/src/jar/ant-task/java/org/mulgara/ant/task/rdf/RDFLoad.java 2007-11-05 17:48:14 UTC (rev 516)
+++ branches/nw-interface/src/jar/ant-task/java/org/mulgara/ant/task/rdf/RDFLoad.java 2007-11-06 19:09:33 UTC (rev 517)
@@ -29,7 +29,6 @@
import java.io.*;
import java.net.*;
-import java.sql.*;
// Java
import java.util.*;
@@ -38,7 +37,6 @@
import org.apache.log4j.*;
import org.apache.log4j.xml.*;
import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
// Ant
@@ -47,7 +45,6 @@
import org.apache.tools.ant.types.Path;
import org.mulgara.itql.ItqlInterpreterBean;
import org.mulgara.query.Answer;
-import org.mulgara.query.QueryException;
import org.mulgara.query.TuplesException;
/**
@@ -124,109 +121,67 @@
*/
public class RDFLoad extends Task {
- /**
- * The logging catgory *
- */
+ /** The logging catgory */
private final static Logger logger = Logger.getLogger(RDFLoad.class);
- /**
- * Get line separator.
- */
+ /** Get line separator. */
private static final String eol = System.getProperty("line.separator");
- /**
- * The file to log to.
- */
+ /** The file to log to. */
protected File logFile = null;
- /**
- * The URI of the model to load into.
- */
+ /** The URI of the model to load into. */
protected URI modelURI = null;
- /**
- * The security domain of server where the model resides.
- */
+ /** The security domain of server where the model resides. */
protected URI domainURI = null;
- /**
- * The username for the security domain.
- */
+ /** The username for the security domain. */
protected String username = null;
- /**
- * The password for the security domain.
- */
+ /** The password for the security domain. */
protected String password = null;
- /**
- * The RDF files to load
- */
+ /** The RDF files to load */
protected Path rdfPath = null;
- /**
- * The directory to load RDF from
- */
+ /** The directory to load RDF from */
protected File rdfDir = null;
- /**
- * Drop the model before loading
- */
+ /** Drop the model before loading */
protected boolean dropModel = false;
- /**
- * The maximum number of permissable errors before aborting the load
- */
+ /** The maximum number of permissable errors before aborting the load */
protected int maxErrors = 0;
- /**
- * Ignore errors
- */
+ /** Ignore errors */
protected boolean ignoreErrors = false;
- /**
- * The number of files to load in each transaction
- */
+ /** The number of files to load in each transaction */
protected int transactionSize = 0;
- /**
- * The log4j XML config file
- */
+ /** The log4j XML config file */
protected File logConfig = null;
- /**
- * The ITQL interpreter
- */
+ /** The ITQL interpreter */
protected ItqlInterpreterBean interpreter = null;
- /**
- * The load log
- */
+ /** The load log */
protected RDFLoadLog loadLog = null;
- /**
- * Is logging enabled
- */
+ /** Is logging enabled */
private boolean loggingEnabled = false;
- /**
- * Should credentials be used
- */
+ /** Should credentials be used */
private boolean presentCredentials = false;
- /**
- * The number of files loaded
- */
+ /** The number of files loaded */
private int numLoaded = 0;
- /**
- * The number of errors
- */
+ /** The number of errors */
private int numErrors = 0;
- /**
- * The default value for <code>autcommit</code> is on
- */
+ /** The default value for <code>autcommit</code> is on */
private boolean autocommit = true;
/**
@@ -235,7 +190,6 @@
* @param log The file to log to.
*/
public void setLogfile(File log) {
-
logFile = log;
}
@@ -245,7 +199,6 @@
* @param model The URI of the model to load into.
*/
public void setModeluri(URI model) {
-
modelURI = model;
}
@@ -255,7 +208,6 @@
* @param domain The security domain of server where the model resides.
*/
public void setDomainuri(URI domain) {
-
domainURI = domain;
}
@@ -266,7 +218,6 @@
* @param user The username to use for the security domain.
*/
public void setUsername(String user) {
-
username = user;
}
@@ -277,7 +228,6 @@
* @param pass The password to use for the security domain.
*/
public void setPassword(String pass) {
-
password = pass;
}
@@ -287,7 +237,6 @@
* @param dir The dir to load RDF from.
*/
public void setDir(File dir) {
-
rdfDir = dir;
}
@@ -298,7 +247,6 @@
* loading.
*/
public void setDropmodel(boolean drop) {
-
dropModel = drop;
}
@@ -310,7 +258,6 @@
* load.
*/
public void setMaxerrors(int max) {
-
maxErrors = max;
}
@@ -320,7 +267,6 @@
* @param ignore <code>true</code> if errors should be ignored.
*/
public void setIgnoreErrors(boolean ignore) {
-
ignoreErrors = ignore;
}
@@ -330,7 +276,6 @@
* @param size The number of documents loaded per transaction.
*/
public void setTransactionsize(int size) {
-
transactionSize = size;
}
@@ -341,7 +286,6 @@
* @param config The log4j XML config file.
*/
public void setLogconfig(File config) {
-
logConfig = config;
}
@@ -351,7 +295,6 @@
* @return The number of files loaded.
*/
public int getNumLoaded() {
-
return numLoaded;
}
@@ -361,7 +304,6 @@
* @return The number of errors that occurred.
*/
public int getNumErrors() {
-
return numErrors;
}
@@ -371,7 +313,6 @@
* @throws BuildException on error.
*/
public void init() throws BuildException {
-
super.init();
}
@@ -382,9 +323,7 @@
* @return A path element specifying the RDF location.
*/
public Path createRdfpath() {
-
rdfPath = new Path(this.getProject());
-
return rdfPath;
}
@@ -394,47 +333,35 @@
* @throws BuildException on error.
*/
public void execute() throws BuildException {
-
try {
-
log("Running with this config:" + eol + this.toString(), Project.MSG_DEBUG);
checkParams();
-
setupLog();
if (logger.isDebugEnabled()) {
-
logger.debug("Running with this config:" + eol + this.toString());
}
interpreter = new ItqlInterpreterBean();
presentCredentials();
-
createModel();
if (dropModel) {
-
dropModel();
createModel();
}
loadRDF();
- }
- finally {
+ } finally {
- if (loadLog != null) {
+ if (loadLog != null) loadLog.close();
- loadLog.close();
- }
-
if (interpreter != null) {
-
try {
interpreter.close();
- }
- finally {
+ } finally {
interpreter = null;
}
}
@@ -476,42 +403,25 @@
// log4j logging
if (logConfig != null) {
-
try {
-
DOMConfigurator.configure(logConfig.toURL());
- }
- catch (MalformedURLException me) {
-
+ } catch (MalformedURLException me) {
throw new BuildException("Could not configure log4j!", me);
}
- }
- else {
-
+ } else {
BasicConfigurator.configure();
-
// Disable debug
LogManager.getLoggerRepository().setThreshold(Level.OFF);
}
if (logFile != null) {
-
loggingEnabled = true;
// Don't read from existing log as model is dropped
try {
-
- if (dropModel) {
-
- loadLog = new RDFLoadLog(logFile, false);
- }
- else {
-
- loadLog = new RDFLoadLog(logFile, true);
- }
- }
- catch (IOException ioe) {
-
+ if (dropModel) loadLog = new RDFLoadLog(logFile, false);
+ else loadLog = new RDFLoadLog(logFile, true);
+ } catch (IOException ioe) {
throw new BuildException("A problem occurred with the log file.", ioe);
}
}
@@ -527,30 +437,20 @@
*/
protected Object getQueryResult(String query) {
- if (logger.isDebugEnabled()) {
+ if (logger.isDebugEnabled()) logger.debug("Executing query: " + query);
- logger.debug("Executing query: " + query);
- }
-
log("Executing query: " + query, Project.MSG_DEBUG);
- List list = interpreter.executeQueryToList(query, true);
+ List<Object> list = interpreter.executeQueryToList(query, true);
if (list.size() > 0) {
-
Object answer = list.get(0);
-
try {
if (answer instanceof String) {
-
return (String) answer;
- }
- else if (answer instanceof Exception) {
-
+ } else if (answer instanceof Exception) {
return answer;
- }
- else {
-
+ } else {
throw new BuildException(
"Expected a string or exception but got a result of type: " +
answer.getClass().getName());
@@ -564,9 +464,7 @@
throw new BuildException("Received bad answer from query: " + query);
}
}
- }
- else {
-
+ } else {
return null;
}
}
@@ -579,29 +477,22 @@
protected void checkParams() throws BuildException {
// Only some set
- if ( (domainURI != null) && (password != null) && (username != null)) {
-
+ if ((domainURI != null) && (password != null) && (username != null)) {
presentCredentials = true;
- }
- else if ( (domainURI != null) || (password != null) || (username != null)) {
-
+ } else if ( (domainURI != null) || (password != null) || (username != null)) {
throw new BuildException("Either none or all of the attributes " +
"'domainuri' 'username' 'password' must be set.");
}
// Is model set?
if (modelURI == null) {
-
throw new BuildException("The modeluri attribute must be set.");
}
// RDF files to load
- if ( (rdfDir != null) && (rdfPath != null)) {
-
+ if ((rdfDir != null) && (rdfPath != null)) {
throw new BuildException("Only one of rdfpath or rdfdir may be set.");
- }
- else if ( (rdfDir == null) && (rdfPath == null)) {
-
+ } else if ( (rdfDir == null) && (rdfPath == null)) {
throw new BuildException("Either one of rdfpath or rdfdir must be set.");
}
}
@@ -614,7 +505,6 @@
protected void presentCredentials() throws BuildException {
if (presentCredentials) {
-
executeQuery("su <" + domainURI + "> " + username + " " + password +
" ;", "credentials", "Credential presented");
}
@@ -626,7 +516,6 @@
* @throws BuildException on error.
*/
protected void createModel() throws BuildException {
-
executeQuery("create <" + modelURI + "> ;", "create model",
"Successfully created model ");
}
@@ -637,7 +526,6 @@
* @throws BuildException on error.
*/
protected void dropModel() throws BuildException {
-
executeQuery("drop <" + modelURI + "> ;", "drop model",
"Successfully dropped model ");
}
@@ -651,8 +539,7 @@
// TODO: This badly needs to be refactored - but it works!! :-)
// If no path specified, then create one using the directory specified.
- if ( (rdfPath == null) && (rdfDir != null)) {
-
+ if ((rdfPath == null) && (rdfDir != null)) {
createRdfpath();
FileSet fileSet = new FileSet();
@@ -660,28 +547,24 @@
rdfPath.addFileset(fileSet);
}
- Set loadSet = new HashSet();
+ Set<String> loadSet = new HashSet<String>();
String[] fileArray = rdfPath.list();
// Build set of files to load
for (int i = 0; i < fileArray.length; i++) {
-
if (loggingEnabled && !loadLog.isLoaded(fileArray[i])) {
-
loadSet.add(fileArray[i]);
- }
- else if (!loggingEnabled) {
-
+ } else if (!loggingEnabled) {
loadSet.add(fileArray[i]);
}
}
// Sort the list - greater chance that problem files are grouped
// and log will be easier to read.
- ArrayList loadList = new ArrayList(loadSet);
+ ArrayList<String> loadList = new ArrayList<String>(loadSet);
Collections.sort(loadList);
- List transList = new ArrayList();
+ List<File> transList = new ArrayList<File>();
int fileIndex = 0;
numErrors = 0;
numLoaded = 0;
@@ -694,9 +577,7 @@
// Turn off auto commit off if it needs to be
if (transactionSize > 0) {
// if on then toggle it off for transaction support
- if ( autocommit ) {
- this.setAutoCommit(false);
- }
+ if (autocommit) this.setAutoCommit(false);
}
@@ -704,16 +585,11 @@
File file = new File(filename);
URI fileURI = file.toURI();
- Object result =
- getQueryResult("load <" + fileURI + "> into <" + modelURI + "> ;");
+ Object result = getQueryResult("load <" + fileURI + "> into <" + modelURI + "> ;");
if (result == null) {
-
- throw new BuildException(
- "Did not get a result back from the load file query");
- }
- else if (result instanceof String) {
-
+ throw new BuildException("Did not get a result back from the load file query");
+ } else if (result instanceof String) {
String string = (String) result;
if (!string.trim().startsWith("Successfully loaded ")) {
@@ -726,11 +602,8 @@
log("Could not load file '" + filename + "'. The message was '" +
string + "', continuing...", Project.MSG_INFO);
- if (loggingEnabled) {
+ if (loggingEnabled) loadLog.logLoadError(file, string);
- loadLog.logLoadError(file, string);
- }
-
// Remove file from load list
loadList.remove(fileIndex);
@@ -742,40 +615,30 @@
// since an error has occured we need to
// set autocommit is on;
this.setAutoCommit(true);
- }
- // Max errors reached
- else {
+ } else {
+ // Max errors reached
- log("Could not load file '" + filename + "'. The message was '" +
- string + "'");
+ log("Could not load file '" + filename + "'. The message was '" + string + "'");
- if (loggingEnabled) {
+ if (loggingEnabled) loadLog.logLoadError(file, string);
- loadLog.logLoadError(file, string);
- }
+ log("Maximum number of load errors (" + maxErrors + ") reached. Aborting load.", Project.MSG_INFO);
- log("Maximum number of load errors (" + maxErrors +
- ") reached. Aborting load.", Project.MSG_INFO);
-
// since an error has occured we need to
// set autocommit is on;
this.setAutoCommit(true);
break;
}
- }
- // Successful load
- else {
+ } else {
+ // Successful load
log("Successful load for: " + fileURI, Project.MSG_DEBUG);
- if (logger.isDebugEnabled()) {
+ if (logger.isDebugEnabled()) logger.debug("Successful load for: " + fileURI);
- logger.debug("Successful load for: " + fileURI);
- }
-
if (transactionSize > 0) {
transList.add(file);
@@ -783,39 +646,30 @@
// New transaction?
if (transList.size() == transactionSize) {
- executeQuery("commit;", "commit",
- "Successfully committed transaction");
+ executeQuery("commit;", "commit", "Successfully committed transaction");
// Log all files in transaction
- if (loggingEnabled) {
+ if (loggingEnabled) loadLog.logLoadedFiles(transList);
- loadLog.logLoadedFiles(transList);
- }
-
numLoaded += transList.size();
// New transaction
transList.clear();
}
- }
- // Not using transactions
- else {
+ } else {
+ // Not using transactions
- if (loggingEnabled) {
+ if (loggingEnabled) loadLog.logLoadedFile(file);
- loadLog.logLoadedFile(file);
- }
-
numLoaded++;
}
}
fileIndex++;
- }
- // Exception
- else {
+ } else {
+ // Exception
Exception ex = (Exception) result;
StringWriter swriter = new StringWriter();
@@ -846,19 +700,15 @@
// since an error has occured we need to
// set autocommit is on;
this.setAutoCommit(true);
- }
- // Max errors reached
- else {
+ } else {
+ // Max errors reached
log("Could not load file '" + filename + "'. The exception was " +
eol + swriter.toString(), Project.MSG_INFO);
- if (loggingEnabled) {
+ if (loggingEnabled) loadLog.logLoadError(file, swriter.toString());
- loadLog.logLoadError(file, swriter.toString());
- }
-
log("Maximum number of load errors (" + maxErrors +
") reached. Aborting load.", Project.MSG_INFO);
@@ -881,28 +731,20 @@
executeQuery("commit;", "commit", "Successfully committed transaction");
// Log all files in transaction
- if (loggingEnabled) {
+ if (loggingEnabled) loadLog.logLoadedFiles(transList);
- loadLog.logLoadedFiles(transList);
- }
-
numLoaded += transList.size();
}
this.setAutoCommit(true);
}
- log("Loaded " + numLoaded + " files with " + numErrors + " errors.",
- Project.MSG_INFO);
- logger.info("Loaded " + numLoaded + " files with " + numErrors +
- " errors.");
+ log("Loaded " + numLoaded + " files with " + numErrors + " errors.", Project.MSG_INFO);
+ logger.info("Loaded " + numLoaded + " files with " + numErrors + " errors.");
if (loggingEnabled) {
-
- log("Total files loaded in log is " + loadLog.getNumLoaded() + ".",
- Project.MSG_INFO);
- logger.info("Total files loaded in log is " + loadLog.getNumLoaded() +
- ".");
+ log("Total files loaded in log is " + loadLog.getNumLoaded() + ".", Project.MSG_INFO);
+ logger.info("Total files loaded in log is " + loadLog.getNumLoaded() + ".");
}
}
@@ -921,35 +763,26 @@
Object result = getQueryResult(query);
if (result == null) {
+ throw new BuildException("Did not get a result back from the " + queryName + " query");
+ } else if (result instanceof String) {
- throw new BuildException("Did not get a result back from the " +
- queryName + " query");
- }
- else if (result instanceof String) {
+ String string = (String)result;
- String string = (String) result;
-
if (!string.trim().startsWith(successMessage)) {
// an unexpected response.
throw new BuildException("Bad " + queryName + " query: " + string);
- }
- else {
+ } else {
log("Query result: " + string, Project.MSG_DEBUG);
- if (logger.isDebugEnabled()) {
-
- logger.debug("Query result: " + string);
- }
+ if (logger.isDebugEnabled()) logger.debug("Query result: " + string);
}
- }
- // Exception
- else {
+ } else {
+ // Exception
Exception e = (Exception) result;
- throw new BuildException("Bad " + queryName + " query: (" + query + ")",
- e);
+ throw new BuildException("Bad " + queryName + " query: (" + query + ")", e);
}
}
@@ -959,10 +792,9 @@
* @param state value of autocommit
*/
private void setAutoCommit( boolean state ) {
- if ( state ) {
+ if (state) {
executeQuery("set autocommit on;", "autocommit on", "Auto commit is on");
- }
- else {
+ } else {
executeQuery("set autocommit off;", "autocommit off", "Auto commit is off");
}
this.autocommit = state;
Modified: branches/nw-interface/src/jar/ant-task/java/org/mulgara/ant/task/rdf/RDFLoadLog.java
===================================================================
--- branches/nw-interface/src/jar/ant-task/java/org/mulgara/ant/task/rdf/RDFLoadLog.java 2007-11-05 17:48:14 UTC (rev 516)
+++ branches/nw-interface/src/jar/ant-task/java/org/mulgara/ant/task/rdf/RDFLoadLog.java 2007-11-06 19:09:33 UTC (rev 517)
@@ -57,39 +57,25 @@
*/
public class RDFLoadLog {
- /**
- * Get line separator.
- */
+ /** Get line separator. */
private static final String eol = System.getProperty("line.separator");
- /**
- * The file to log to.
- */
+ /** The file to log to. */
private File logFile = null;
- /**
- * Writer for writing to the log file
- */
+ /** Writer for writing to the log file */
private PrintWriter logWriter = null;
- /**
- * Set of files from the log that are already loaded
- */
- private Set loadedFileSet = new HashSet();
+ /** Set of files from the log that are already loaded */
+ private Set<String> loadedFileSet = new HashSet<String>();
- /**
- * A buffer to log errors to
- */
+ /** A buffer to log errors to */
private StringBuffer errorBuffer = new StringBuffer();
- /**
- * The number of files loaded in all logged runs
- */
+ /** The number of files loaded in all logged runs */
private int numLoaded = 0;
- /**
- * The number of errors that occurred
- */
+ /** The number of errors that occurred */
private int numErrors = 0;
/**
@@ -104,17 +90,13 @@
logFile = file;
- List loadedList = null;
+ List<String> loadedList = null;
- if (read) {
+ if (read) loadedList = readLog();
- loadedList = readLog();
- }
-
setupLogWriter();
- if ( (loadedList != null) && (loadedList.size() > 0)) {
-
+ if ((loadedList != null) && (loadedList.size() > 0)) {
writeExisting(loadedList);
}
}
@@ -122,22 +104,19 @@
/**
* Is the file already loaded.
*
- * @param file PARAMETER TO DO
+ * @param file The name of the file to check.
* @return true if the file is already loaded.
*/
public boolean isLoaded(String file) {
-
return loadedFileSet.contains(file);
}
/**
* Get the number of files loaded from the current and previous logged runs.
*
- * @return The number of files loaded from the current and previous logged
- * runs.
+ * @return The number of files loaded from the current and previous logged runs.
*/
public int getNumLoaded() {
-
return numLoaded;
}
@@ -147,7 +126,6 @@
* @return The the number of errors that occurred.
*/
public int getNumErrors() {
-
return numErrors;
}
@@ -157,7 +135,6 @@
* @param file The file to log as loaded.
*/
public void logLoadedFile(File file) {
-
logWriter.println(file.getAbsolutePath());
logWriter.flush();
numLoaded++;
@@ -168,13 +145,8 @@
*
* @param list The list of files (File objects) to log as loaded.
*/
- public void logLoadedFiles(List list) {
-
- Iterator iter = list.iterator();
-
- while (iter.hasNext()) {
-
- File file = (File) iter.next();
+ public void logLoadedFiles(List<File> list) {
+ for (File file: list) {
logWriter.println(file.getAbsolutePath());
numLoaded++;
}
@@ -189,7 +161,6 @@
* @param message An error message.
*/
public void logLoadError(File file, String message) {
-
errorBuffer.append("Could not load file '" + file.getAbsolutePath() +
"'." + eol + message + eol + eol);
numErrors++;
@@ -201,7 +172,6 @@
* @param message An error message.
*/
public void logLoadError(String message) {
-
errorBuffer.append(message + eol + eol);
numErrors++;
}
@@ -211,7 +181,6 @@
*
*/
public void close() {
-
// Write out the errors
logWriter.println();
logWriter.println("Total files loaded: " + numLoaded);
@@ -238,13 +207,9 @@
*/
private void setupLogWriter() throws IOException {
- // Make the log file and any required directories
- if (!logFile.exists()) {
-
- logFile.getParentFile().mkdirs();
- }
-
if (logFile != null) {
+ // Make the log file and any required directories
+ if (!logFile.exists()) logFile.getParentFile().mkdirs();
logWriter = new PrintWriter(new FileWriter(logFile));
}
@@ -253,12 +218,12 @@
/**
* Reads in the files already loaded from the log file.
*
- * @return RETURNED VALUE TO DO
+ * @return A list of all the lines from the log file.
* @throws IOException on error.
*/
- private List readLog() throws IOException {
+ private List<String> readLog() throws IOException {
- List loadedList = new ArrayList();
+ List<String> loadedList = new ArrayList<String>();
if (logFile.exists()) {
@@ -266,12 +231,11 @@
String line = inReader.readLine();
// Read in the files previously loaded.
- while ( (line != null) && (line.trim().length() != 0)) {
+ while ((line != null) && (line.trim().length() != 0)) {
loadedFileSet.add(line);
- // Use list as well to keep files in same log order when written out
- // again.
+ // Use list as well to keep files in same log order when written out again.
loadedList.add(line);
line = inReader.readLine();
}
@@ -287,13 +251,10 @@
*
* @param loadedList The list of file loaded last time.
*/
- private void writeExisting(List loadedList) {
+ private void writeExisting(List<String> loadedList) {
- Iterator iter = loadedList.iterator();
-
- while (iter.hasNext()) {
-
- logWriter.println(iter.next());
+ for (String file: loadedList) {
+ logWriter.println(file);
numLoaded++;
}
Modified: branches/nw-interface/src/jar/ant-task/java/org/mulgara/ant/task/rdf/RDFLoadUnitTest.java
===================================================================
--- branches/nw-interface/src/jar/ant-task/java/org/mulgara/ant/task/rdf/RDFLoadUnitTest.java 2007-11-05 17:48:14 UTC (rev 516)
+++ branches/nw-interface/src/jar/ant-task/java/org/mulgara/ant/task/rdf/RDFLoadUnitTest.java 2007-11-06 19:09:33 UTC (rev 517)
@@ -33,7 +33,6 @@
import java.io.*;
import java.net.*;
-import java.sql.*;
// Java
import java.util.*;
@@ -44,8 +43,6 @@
// Ant
import org.apache.tools.ant.Project;
-import org.apache.tools.ant.Target;
-import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Path;
import org.mulgara.itql.ItqlInterpreterBean;
@@ -74,83 +71,57 @@
*/
public class RDFLoadUnitTest extends TestCase {
- /**
- * Log category
- */
- private final static Logger log =
- Logger.getLogger(RDFLoadUnitTest.class);
+ /** Log category */
+ @SuppressWarnings("unused")
+ private final static Logger log = Logger.getLogger(RDFLoadUnitTest.class);
- /**
- * Description of the Field
- */
+ /** URI string for rdf:type */
String RDF_TYPE_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
- /**
- * Description of the Field
- */
+ /** URI string for the Mulgara model/graph type */
String MODEL_URI = "http://mulgara.org/mulgara#Model";
- /**
- * Description of the Field
- */
+ /** URI string for journals */
String JOURNAL_URI = "urn:medline:Journal";
- /**
- * Description of the Field
- */
+ /** The query object */
ItqlInterpreterBean interpreter = null;
- /**
- * Description of the Field
- */
+ /** The name of the server */
String hostName = null;
- /**
- * Description of the Field
- */
+ /** The name of the graph for testing */
String testModel = null;
- /**
- * Description of the Field
- */
+ /** The load object */
RDFLoad load = null;
- /**
- * Description of the Field
- */
+ /** The directory to work under */
String baseDir = System.getProperty("basedir");
- /**
- * Description of the Field
- */
+ /** A directory to work with for RDF files */
File goodRDFDir =
new File(baseDir + File.separator + "jxdata" + File.separator +
"ant-tasks" + File.separator + "rdf-good");
- /**
- * Description of the Field
- */
+ /** An erroneous directory to work with for RDF files */
File badRDFDir =
new File(baseDir + File.separator + "jxdata" + File.separator +
"ant-tasks" + File.separator + "rdf-bad");
- /**
- * Description of the Field
- */
+ /** An erroneous directory to work with for RDF files */
File badRDFDir2 =
new File(baseDir + File.separator + "jxdata" + File.separator +
"ant-tasks" + File.separator + "rdf-bad2");
- /**
- * Description of the Field
- */
+ /** File to write log info into */
File logFile = new File(TempDir.getTempDir(), "rdfload-log.txt");
/**
* Public constructor.
*
- * @param name PARAMETER TO DO
- * @throws Exception EXCEPTION TO DO
+ * @param name The name of the test
+ * @throws Exception Thrown if the test cannot be set up
*/
public RDFLoadUnitTest(String name) throws Exception {
super(name);
@@ -162,7 +133,7 @@
* Builds a test suite.
*
* @return A test suite.
- * @throws Exception EXCEPTION TO DO
+ * @throws Exception Thrown if any of the tests cannot be set up
*/
public static TestSuite suite() throws Exception {
@@ -196,14 +167,13 @@
* Runs the tests.
*
* @param args The args.
- * @throws Exception EXCEPTION TO DO
+ * @throws Exception Thrown if the tests cannot be run
*/
public static void main(String[] args) throws Exception {
String baseDir = System.getProperty("basedir");
if (baseDir == null) {
-
throw new RuntimeException("Could not get the 'basedir' system property");
}
@@ -217,7 +187,6 @@
/**
* Set up for tests.
- *
*/
public void setUp() {
interpreter = new ItqlInterpreterBean();
@@ -237,7 +206,6 @@
/**
* Test presenting credentials.
- *
*/
public void testPresentCredentials() {
@@ -250,12 +218,9 @@
load.interpreter = interpreter;
try {
-
load.checkParams();
load.presentCredentials();
- }
- catch (BuildException be) {
-
+ } catch (BuildException be) {
fail("Unexpected exception whilst presenting credentials: " + be);
}
@@ -263,12 +228,10 @@
load.setUsername("");
try {
-
load.checkParams();
load.presentCredentials();
fail("Credential presentation should have failed");
- }
- catch (BuildException be) {
+ } catch (BuildException be) {
}
@@ -276,11 +239,9 @@
load.setDomainuri(null);
try {
-
load.checkParams();
fail("Credential presentation should have failed");
- }
- catch (BuildException be) {
+ } catch (BuildException be) {
}
}
@@ -288,7 +249,7 @@
/**
* Test creating the model.
*
- * @throws Exception EXCEPTION TO DO
+ * @throws Exception General declaration for failed tests.
*/
public void testCreateDropModel() throws Exception {
@@ -297,23 +258,19 @@
load.interpreter = interpreter;
try {
-
load.createModel();
- }
- catch (BuildException be) {
-
+ } catch (BuildException be) {
fail("Unexpected exception whilst creating model: " + be);
}
// Check for the model
- List list =
+ List<Object> list =
interpreter.executeQueryToList("select 'text' from <rmi://" + hostName +
"/server1#> where " + " <" + testModel +
"> <" + RDF_TYPE_URI + "> <" +
MODEL_URI + ">;");
if (list.get(0)instanceof String) {
-
fail("Got exception instead of answer: " + list.get(0));
}
@@ -321,7 +278,6 @@
answer.beforeFirst();
if (answer.isUnconstrained() || !answer.next()) {
-
fail("Model was not created!");
}
assertEquals("Query should not return multiple answers", list.size(), 1);
@@ -337,11 +293,10 @@
"> <" + RDF_TYPE_URI + "> <" +
MODEL_URI + ">;");
- answer = (Answer) list.get(0);
+ answer = (Answer)list.get(0);
answer.beforeFirst();
if (answer.next()) {
-
fail("Model was not dropped!");
}
assertEquals("Query should not return multiple answers", list.size(), 1);
@@ -351,32 +306,26 @@
load.setModeluri(URI.create("rmi://blah"));
try {
-
load.createModel();
fail("Model creation should have failed");
+ } catch (BuildException be) {
}
- catch (BuildException be) {
- }
-
// Missing model
load.setModeluri(null);
try {
-
load.interpreter = null;
load.execute();
fail("Model creation should have failed");
+ } catch (BuildException be) {
}
- catch (BuildException be) {
-
- }
}
/**
* Test a normal load with no logging using a directory.
*
- * @throws Exception EXCEPTION TO DO
+ * @throws Exception General declaration for failed tests.
*/
public void testDirLoadNoLog() throws Exception {
@@ -386,13 +335,12 @@
load.execute();
Answer answer =
- (Answer) interpreter.executeQueryToList("select $s from <" + testModel +
- "> where $s <" + RDF_TYPE_URI +
- "> <" + JOURNAL_URI +
+ (Answer)interpreter.executeQueryToList("select $s from <" + testModel +
+ "> where $s <" + RDF_TYPE_URI +
+ "> <" + JOURNAL_URI +
">;").get(0);
if (answer.isUnconstrained()) {
-
fail("The data did not load");
}
@@ -407,7 +355,7 @@
/**
* Test a normal load with no logging using an rdf path.
*
- * @throws Exception EXCEPTION TO DO
+ * @throws Exception General declaration for failed tests.
*/
public void testPathLoadWithLog() throws Exception {
@@ -421,13 +369,12 @@
load.execute();
Answer answer =
- (Answer) interpreter.executeQueryToList("select $s from <" + testModel +
+ (Answer)interpreter.executeQueryToList("select $s from <" + testModel +
"> where $s <" + RDF_TYPE_URI +
"> <" + JOURNAL_URI +
">;").get(0);
if (answer.isUnconstrained()) {
-
fail("The data did not load");
}
@@ -451,7 +398,7 @@
* This test relies on the test {@link #testPathLoadWithLog()} running first.
* </p>
*
- * @throws Exception EXCEPTION TO DO
+ * @throws Exception General declaration for failed tests.
*/
public void testReadLog() throws Exception {
@@ -473,7 +420,7 @@
*
* This test relies on the test {@link #testReadLog()} running first. </p>
*
- * @throws Exception EXCEPTION TO DO
+ * @throws Exception General declaration for failed tests.
*/
public void testDropModelWithLog() throws Exception {
@@ -490,14 +437,12 @@
load.execute();
Answer answer =
- (Answer) interpreter.executeQueryToList("select $s from <" +
- testModel +
- "> where $s <" + RDF_TYPE_URI +
- "> <" + JOURNAL_URI + ">;").
- get(0);
+ (Answer)interpreter.executeQueryToList("select $s from <" +
+ testModel +
+ "> where $s <" + RDF_TYPE_URI +
+ "> <" + JOURNAL_URI + ">;").get(0);
if (answer.isUnconstrained()) {
-
fail("The data did not load");
}
@@ -509,22 +454,16 @@
// Check loader
assertEquals("Five documents should have been loaded!", 5,
load.getNumLoaded());
+ } finally {
+ // Get rid of the log file
+ if (logFile.exists()) logFile.delete();
}
-
- // Get rid of the log file
- finally {
-
- if (logFile.exists()) {
-
- logFile.delete();
- }
- }
}
/**
* The load should stop after the first error.
*
- * @throws Exception EXCEPTION TO DO
+ * @throws Exception General declaration for failed tests.
*/
public void test1ErrorNoLog() throws Exception {
@@ -533,13 +472,12 @@
load.execute();
Answer answer =
- (Answer) interpreter.executeQueryToList("select $s from <" + testModel +
- "> where $s <" + RDF_TYPE_URI +
- "> <" + JOURNAL_URI +
+ (Answer)interpreter.executeQueryToList("select $s from <" + testModel +
+ "> where $s <" + RDF_TYPE_URI +
+ "> <" + JOURNAL_URI +
">;").get(0);
if (answer.isUnconstrained()) {
-
fail("The data did not load");
}
@@ -572,14 +510,12 @@
load.execute();
Answer answer =
- (Answer) interpreter.executeQueryToList("select $s from <" +
- testModel +
- "> where $s <" + RDF_TYPE_URI +
- "> <" + JOURNAL_URI + ">;").
- get(0);
+ (Answer)interpreter.executeQueryToList("select $s from <" +
+ testModel +
+ "> where $s <" + RDF_TYPE_URI +
+ "> <" + JOURNAL_URI + ">;").get(0);
if (answer.isUnconstrained()) {
-
fail("The data did not load");
}
@@ -595,22 +531,16 @@
RDFLoadLog loadLog = new RDFLoadLog(logFile, true);
assertEquals("Wrong number of documents in log!", 1,
loadLog.getNumLoaded());
+ } finally {
+ // Get rid of the log file
+ if (logFile.exists()) logFile.delete();
}
-
- // Get rid of the log file
- finally {
-
- if (logFile.exists()) {
-
- logFile.delete();
- }
- }
}
/**
* The load should stop after the first 2 errors.
*
- * @throws Exception EXCEPTION TO DO
+ * @throws Exception General declaration for failed tests.
*/
public void test2ErrorsNoLog() throws Exception {
@@ -620,19 +550,17 @@
load.execute();
Answer answer =
- (Answer) interpreter.executeQueryToList("select $s from <" + testModel +
- "> where $s <" + RDF_TYPE_URI +
- "> <" + JOURNAL_URI +
+ (Answer)interpreter.executeQueryToList("select $s from <" + testModel +
+ "> where $s <" + RDF_TYPE_URI +
+ "> <" + JOURNAL_URI +
">;").get(0);
if (answer.isUnconstrained()) {
-
fail("The data did not load");
}
// Mulgara - should be 1 loaded
- assertEquals("Wrong number of documents loaded!", 1,
- answer.getRowCount());
+ assertEquals("Wrong number of documents loaded!", 1, answer.getRowCount());
answer.close();
// Should be 2 errors
@@ -642,11 +570,11 @@
/**
* The load should stop after the first 2 errors.
*
- * @throws Exception EXCEPTION TO DO
+ * @throws Exception General declaration for failed tests.
*/
public void test2ErrorsWithLog() throws Exception {
- try {
+ // try {
load.setModeluri(URI.create(testModel));
load.setLogfile(logFile);
@@ -660,20 +588,17 @@
load.execute();
Answer answer =
- (Answer) interpreter.executeQueryToList("select $s from <" +
- testModel +
- "> where $s <" + RDF_TYPE_URI +
- "> <" + JOURNAL_URI + ">;").
- get(0);
+ (Answer)interpreter.executeQueryToList("select $s from <" +
+ testModel +
+ "> where $s <" + RDF_TYPE_URI +
+ "> <" + JOURNAL_URI + ">;").get(0);
if (answer.isUnconstrained()) {
-
fail("The data did not load");
}
// Mulgara - should be 1 loaded
- assertEquals("Wrong number of documents loaded!", 1,
- answer.getRowCount());
+ assertEquals("Wrong number of documents loaded!", 1, answer.getRowCount());
answer.close();
// Should be 2 errors
@@ -682,14 +607,9 @@
// // Check log
// RDFLoadLog loadLog = new RDFLoadLog(logFile, true);
// assertEquals("Wrong number of documents in log!", 1, loadLog.getNumLoaded());
- }
- finally {
-
- //
- // if (logFile.exists()) {
- // logFile.delete();
- // }
- }
+ // } finally {
+ // if (logFile.exists()) logFile.delete();
+ // }
}
/**
@@ -698,7 +618,7 @@
*
* This test depends on {@link #test2ErrorsWithLog()}. </p>
*
- * @throws Exception EXCEPTION TO DO
+ * @throws Exception General declaration for failed tests.
*/
public void test3ErrorsWithLog() throws Exception {
@@ -715,20 +635,17 @@
load.execute();
Answer answer =
- (Answer) interpreter.executeQueryToList("select $s from <" +
- testModel +
- "> where $s <" + RDF_TYPE_URI +
- "> <" + JOURNAL_URI + ">;").
- get(0);
+ (Answer)interpreter.executeQueryToList("select $s from <" +
+ testModel +
+ "> where $s <" + RDF_TYPE_URI +
+ "> <" + JOURNAL_URI + ">;").get(0);
if (answer.isUnconstrained()) {
-
fail("The data did not load");
}
// Mulgara - should be 1 loaded as tear down would have dropped the model...
- assertEquals("Wrong number of documents loaded!", 1,
- answer.getRowCount());
+ assertEquals("Wrong number of documents loaded!", 1, answer.getRowCount());
answer.close();
// Should be 3 errors
@@ -741,22 +658,16 @@
RDFLoadLog loadLog = new RDFLoadLog(logFile, true);
assertEquals("Wrong number of documents in log!", 2,
loadLog.getNumLoaded());
+ } finally {
+ // Get rid of the log file
+ if (logFile.exists()) logFile.delete();
}
-
- // Get rid of the log file
- finally {
-
- if (logFile.exists()) {
-
- logFile.delete();
- }
- }
}
/**
* Test a normal load with 1 files per transaction.
*
- * @throws Exception EXCEPTION TO DO
+ * @throws Exception General declaration for failed tests.
*/
public void test1PerTrans() throws Exception {
@@ -774,14 +685,12 @@
load.execute();
Answer answer =
- (Answer) interpreter.executeQueryToList("select $s from <" +
- testModel +
- "> where $s <" + RDF_TYPE_URI +
- "> <" + JOURNAL_URI + ">;").
- get(0);
+ (Answer)interpreter.executeQueryToList("select $s from <" +
+ testModel +
+ "> where $s <" + RDF_TYPE_URI +
+ "> <" + JOURNAL_URI + ">;").get(0);
if (answer.isUnconstrained()) {
-
fail("The data did not load");
}
@@ -796,20 +705,16 @@
// Should be 0 errors
assertEquals("Wrong number of errors!", 0, load.getNumErrors());
- }
- finally {
+ } finally {
- if (logFile.exists()) {
-
- logFile.delete();
- }
+ if (logFile.exists()) logFile.delete();
}
}
/**
* Test a normal load with 5 file per transaction.
*
- * @throws Exception EXCEPTION TO DO
+ * @throws Exception General declaration for failed tests.
*/
public void test5PerTrans() throws Exception {
@@ -826,14 +731,12 @@
load.execute();
Answer answer =
- (Answer) interpreter.executeQueryToList("select $s from <" +
- testModel +
- "> where $s <" + RDF_TYPE_URI +
- "> <" + JOURNAL_URI + ">;").
- get(0);
+ (Answer)interpreter.executeQueryToList("select $s from <" +
+ testModel +
+ "> where $s <" + RDF_TYPE_URI +
+ "> <" + JOURNAL_URI + ">;").get(0);
if (answer.isUnconstrained()) {
-
fail("The data did not load");
}
@@ -848,14 +751,9 @@
// Should be 0 errors
assertEquals("Wrong number of errors!", 0, load.getNumErrors());
+ } finally {
+ if (logFile.exists()) logFile.delete();
}
- finally {
-
- if (logFile.exists()) {
-
- logFile.delete();
- }
- }
}
/**
@@ -876,7 +774,7 @@
*/
public void test2PerTransWith1Error() throws Exception {
- try {
+ // try {
load.setModeluri(URI.create(testModel));
load.setLogfile(logFile);
@@ -890,14 +788,12 @@
load.execute();
Answer answer =
- (Answer) interpreter.executeQueryToList("select $s from <" +
- testModel +
- "> where $s <" + RDF_TYPE_URI +
- "> <" + JOURNAL_URI + ">;").
- get(0);
+ (Answer)interpreter.executeQueryToList("select $s from <" +
+ testModel +
+ "> where $s <" + RDF_TYPE_URI +
+ "> <" + JOURNAL_URI + ">;").get(0);
if (answer.isUnconstrained()) {
-
fail("Null result set");
}
@@ -914,13 +810,11 @@
// assertEquals("No documents should be logged!", 0, loadLog.getNumLoaded());
// Should be 2 errors
assertEquals("Wrong number of errors!", 2, load.getNumErrors());
- }
- finally {
-
+ // } finally {
// if (logFile.exists()) {
// logFile.delete();
// }
- }
+ // }
}
/**
@@ -962,14 +856,12 @@
load.execute();
Answer answer =
- (Answer) interpreter.executeQueryToList("select $s from <" +
- testModel +
- "> where $s <" + RDF_TYPE_URI +
- "> <" + JOURNAL_URI + ">;").
- get(0);
+ (Answer)interpreter.executeQueryToList("select $s from <" +
+ testModel +
+ "> where $s <" + RDF_TYPE_URI +
+ "> <" + JOURNAL_URI + ">;").get(0);
if (answer.isUnconstrained()) {
-
fail("The data did not load");
}
@@ -987,14 +879,9 @@
// Should be 3 errors
assertEquals("Wrong number of errors!", 3, load.getNumErrors());
+ } finally {
+ if (logFile.exists()) logFile.delete();
}
- finally {
-
- if (logFile.exists()) {
-
- logFile.delete();
- }
- }
}
/**
@@ -1024,14 +911,12 @@
load.execute();
Answer answer =
- (Answer) interpreter.executeQueryToList("select $s from <" +
- testModel +
- "> where $s <" + RDF_TYPE_URI +
- "> <" + JOURNAL_URI + ">;").
- get(0);
+ (Answer)interpreter.executeQueryToList("select $s from <" +
+ testModel +
+ "> where $s <" + RDF_TYPE_URI +
+ "> <" + JOURNAL_URI + ">;").get(0);
if (answer.isUnconstrained()) {
-
fail("The data did not load");
}
@@ -1048,14 +933,9 @@
// Should be 3 errors
assertEquals("Wrong number of errors!", 6, load.getNumErrors());
+ } finally {
+ if (logFile.exists()) logFile.delete();
}
- finally {
-
- if (logFile.exists()) {
-
- logFile.delete();
- }
- }
}
/**
More information about the Mulgara-svn
mailing list