gephi / gephi-plugins

Repository for Gephi Plugins maintained by the team. Each plugin has it's branch.
269 stars 620 forks source link

How to write tests using ImportPlugin? #232

Closed yossisp closed 2 years ago

yossisp commented 2 years ago

I want to add tests which import csv files and then perform assertions on resulting graph. I wrote tests almost identical to the examples I found in ImportPlugin tests. However, I receive an exception when calling importer.setFile(file): java.lang.NullPointerException at org.gephi.io.importer.api.ImportUtils.getTextReader(ImportUtils.java:92) at org.gephi.io.importer.plugin.file.spreadsheet.ImporterSpreadsheetCSV.autoDetectFieldDelimiter(ImporterSpreadsheetCSV.java:117) at org.gephi.io.importer.plugin.file.spreadsheet.ImporterSpreadsheetCSV.refreshAutoDetections(ImporterSpreadsheetCSV.java:100) at org.gephi.io.importer.plugin.file.spreadsheet.AbstractImporterSpreadsheet.setFile(AbstractImporterSpreadsheet.java:403) at org.openuniversityofisrael.katzcentrality.KatzCentralityTest.testAdjacencyList(KatzCentralityTest.java:55) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at org.testng.TestRunner.privateRun(TestRunner.java:767) at org.testng.TestRunner.run(TestRunner.java:617) at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) at org.testng.SuiteRunner.run(SuiteRunner.java:240) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198) at org.testng.TestNG.runSuitesLocally(TestNG.java:1123) at org.testng.TestNG.run(TestNG.java:1031) at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73) at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

This happens because of this:

setFile-> autoDetectFieldDelimiter-> FileUtil.toFileObject(this.file)->URLMapper.findFileObject(url)->mapper.getFileObjects(url)->FileObject f = find(url) and finally find returns null because of this line !"memory".equals(url.getProtocol()) which returns true and sets the file as null.

So even though I can print my file using Scanner netbeans filesystems API thinks the file protocol in memory instead of being file I guess (e.g. file:/home/pathToMyFile.csv). I can't figure out the problem perhaps @eduramiba can help because he write the ImportPlugin tests.

Expected Behavior

The test passes.

Current Behavior

The test doesn't pass.

Possible Solution

Steps to Reproduce

  1. Create any package (module).
  2. Create test directory: src/test/java/your/package.
  3. Create test file with the content as below.
  4. Create resources directory src/test/resources/your/package.
  5. Create test.csv in src/test/resources/your/package.
  6. Run mvn test.

This the test file content:

package org.mypackage;

import org.gephi.graph.api.GraphController;
import org.gephi.graph.api.GraphModel;
import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.api.ImportController;
import org.gephi.io.importer.plugin.file.spreadsheet.ImporterSpreadsheetCSV;
import org.gephi.io.processor.plugin.DefaultProcessor;
import org.gephi.project.api.ProjectController;
import org.gephi.project.api.Workspace;
import org.openide.filesystems.FileUtil;
import org.openide.util.Lookup;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.Scanner;

public class Test {
    private final ProjectController projectController = Lookup.getDefault().lookup(ProjectController.class);
    private final ImportController importController = Lookup.getDefault().lookup(ImportController.class);
    private final GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
    private Workspace workspace;
    private String testName = null;

    @BeforeMethod
    public void setup(Method method) {
        testName = method.getName();
        System.out.println("Starting test: " + testName);

        projectController.newProject();
        workspace = projectController.getCurrentWorkspace();
    }

    @AfterMethod
    public void teardown() {
        projectController.closeCurrentProject();
        workspace = null;
    }

    @Test
    public void testAdjacencyList() throws FileNotFoundException, IOException {
        File file = FileUtil.archiveOrDirForURL(KatzCentralityTest.class.getResource("/org/mypackge/nodes.csv"));
        ImporterSpreadsheetCSV importer = new ImporterSpreadsheetCSV();
        importer.setFile(file); // <---- the error occurs here

        Container container = importController.importFile(
                file, importer
        );
        Assert.assertNotNull(container);

        importController.process(container, new DefaultProcessor(), workspace);
        GraphModel gm = graphController.getGraphModel(workspace);
        boolean hasEdges = gm.getGraph().getEdges().iterator().hasNext();
        Assert.assertEquals(hasEdges, false);
    }
}

Context

Your Environment