Demonstrates the implementation of a scanner plugin.

This tutorial is written for version 1.4.0 of jQAssistant.

1. Overview

A scanner plugin allows extending the functionality of jQAssistant to read arbitrary structures (e.g. from files) and create the corresponding information in the database. The following example demonstrates this for CSV files using a Maven project.

2. Setup Maven Project

The plugin is an artifact which contains the scanner implementation, model classes and a plugin descriptor. A Maven project needs to declare the following dependencies:

    <!-- Test dependencies -->

The following artifacts dependencies are declared:


Provides the scanner plugin API


Contains common functionality shared by plugins,


A library 'opencsv' for reading CSV files. This library is already included in the jQAssistant distribution, therefore the scope is provided.

There are also several dependencies defined for implementing tests, see Add A Test.

3. Define The Graph Model

The model to be stored in the database is defined using the approach provided by eXtended Objects. It is based on annotated interfaces declaring methods representing properties and relations.

First a label "CSV" is defined which is going to be used for all nodes created by the scanner:

 * Defines the label which is shared by all nodes representing CSV structures.
public interface CSVDescriptor extends Descriptor {

A CSV file is represented by CSVFileDescriptor which inherits from CSVDescriptor and FileDescriptor (provided by the common plugin). Thus a node of this type will carry the labels "CSV" and "File". Furthermore a list of "HAS_ROW" relations is defined by the property "rows".

 * Represents a CSV file. The labels are inherited from {@link CSVDescriptor}
 * and {@link FileDescriptor}.
public interface CSVFileDescriptor extends CSVDescriptor, FileDescriptor {

    List<CSVRowDescriptor> getRows();


A single row is a node defined by the type CSVRowDescriptor which inherits the label "CSV" from CSVDescriptor and provides its own label "Row", a property "lineNumber" and a list of "HAS_COLUMN" relations.

 * Represents a row of a CSV file.
public interface CSVRowDescriptor extends CSVDescriptor {

    int getLineNumber();

    void setLineNumber(int lineNumber);

    List<CSVColumnDescriptor> getColumns();


CSVColumnDescriptor finally defines a column of a row following the principles explained above:

 * Represents a column within a row of a CSV file.
public interface CSVColumnDescriptor extends CSVDescriptor {

    String getValue();

    void setValue(String value);

    int getIndex();

    void setIndex(int index);


4. Implement The Scanner Plugin

The implementation of the plugin itself inherits from AbstractScannerPlugin which requires generic type parameters for the item type it handles and the descriptor type it creates. In the example FileResource is used which represents a file contained in a directory or archive. This allows plugins to be independent of the source where files or directories are picked up by the scanner.

The method accepts is called by the scanner to determine if the plugin can handle the given item. The example matches the value of the parameter path against the file extension ".csv". The scope parameter may be used to further restrict executions of the plugin, e.g. by checking equality against JavaScope.CLASSPATH.

The scan method actually reads the CSV file and stores the gathered data into the database using the interface Store provided by the scanner context.

 * A CSV file scanner plugin.
@Requires(FileDescriptor.class) // The file descriptor is created by the file scanner plugin and enriched by
                                // this one
public class CSVFileScannerPlugin extends AbstractScannerPlugin<FileResource, CSVFileDescriptor> {

    public boolean accepts(FileResource item, String path, Scope scope) {
        return path.toLowerCase().endsWith(".csv");

    public CSVFileDescriptor scan(FileResource item, String path, Scope scope, Scanner scanner) throws IOException {
        ScannerContext context = scanner.getContext();
        final Store store = context.getStore();
        // Open the input stream for reading the file.
        try (InputStream stream = item.createStream()) {
            // Retrieve the scanned file node from the scanner context.
            final FileDescriptor fileDescriptor = context.getCurrentDescriptor();
            // Add the CSV label.
            final CSVFileDescriptor csvFileDescriptor = store.addDescriptorType(fileDescriptor, CSVFileDescriptor.class);
            // Parse the stream using OpenCSV.
            CSVReader csvReader = new CSVReader(new InputStreamReader(stream));
            String[] columns;
            int row = 0;
            while ((columns = csvReader.readNext()) != null) {
                // Create the node for a row
                CSVRowDescriptor rowDescriptor = store.create(CSVRowDescriptor.class);
                for (int i = 0; i < columns.length; i++) {
                    // Create the node for a column
                    CSVColumnDescriptor columnDescriptor = store.create(CSVColumnDescriptor.class);
            return csvFileDescriptor;

Finally the model and the plugin implementation must be declared in the jQAssistant plugin descriptor:

        xmlns:jqa-plugin="" name="CSV Example">
    <description>Provides a scanner for CSV file.</description>

The plugin is automatically loaded by the scanner if it can be found on the classpath, e.g. by adding it as dependency to the Maven plugin.

5. Add A Test

jQAssistant comes with a test framework which provides convenience functionality for scanning and verifying data.

The CSV scanner plugin shall be tested using the following file:

Figure 1. Graph representation of the CSV file

The test class CSVScannerPluginTest extends from AbstractPluginIT and

  • scans the CSV file which is located in the root of the test class path

  • executes a Cypher query to lookup the created file node with the labels CSV and Test

  • verifies the created graph representing the CSV structure

public class CSVScannerPluginTest extends AbstractPluginIT {

    public void scanCSVFile() {
        // Scan the test CSV file located as resource in the classpath
        File testFile = new File(getClassesDirectory(CSVScannerPluginTest.class), "/test.csv");

        // Scan the CSV file and assert that the returned descriptor is a CSVFileDescriptor
        assertThat(getScanner().scan(testFile, "/test.csv", DefaultScope.NONE), CoreMatchers.<Descriptor>instanceOf(CSVFileDescriptor.class));

        // Determine the CSVFileDescriptor by executing a Cypher query
        TestResult testResult = query("MATCH (csvFile:CSV:File) RETURN csvFile");
        List<CSVFileDescriptor> csvFiles = testResult.getColumn("csvFile");
        assertThat(csvFiles.size(), equalTo(1));

        CSVFileDescriptor csvFile = csvFiles.get(0);
        assertThat(csvFile.getFileName(), equalTo("/test.csv"));

        // Get rows and verify expected count
        List<CSVRowDescriptor> rows = csvFile.getRows();
        assertThat(rows.size(), equalTo(1));

        // Verify first (and only) row
        CSVRowDescriptor row0 = rows.get(0);
        assertThat(row0.getLineNumber(), equalTo(0));
        // Verify the columns of the first row
        List<CSVColumnDescriptor> row0Columns = row0.getColumns();
        assertThat(row0Columns.size(), equalTo(2));

        CSVColumnDescriptor headerColumn0 = row0Columns.get(0);
        assertThat(headerColumn0.getIndex(), equalTo(0));
        assertThat(headerColumn0.getValue(), equalTo("Indiana"));

        CSVColumnDescriptor headerColumn1 = row0Columns.get(1);
        assertThat(headerColumn1.getIndex(), equalTo(1));
        assertThat(headerColumn1.getValue(), equalTo("Jones"));


6. Resources