0

I am using Gherkin parser to parse feature files and returning the list of Gherkin documents see the function below:

import io.cucumber.gherkin.Gherkin;
import io.cucumber.messages.IdGenerator;
import io.cucumber.messages.Messages;
import io.cucumber.messages.Messages.Envelope;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class GherkinUtils {

    private static final Logger LOG = LogManager.getLogger(GherkinUtils.class);

    public static ArrayList<Messages.GherkinDocument> getGherkinDocumentsFromFiles() {

        IdGenerator idGenerator = new IdGenerator.Incrementing();

        ArrayList<Messages.GherkinDocument> listOfGherkinDocuments = new ArrayList<>();
        String pathFolderFrameworkFeatures = SettingsUtils.getPathFolderFrameworkFeatures();
        List<String> listOfPathsForFeatureFiles = FileUtils.getAllFilePathsFromFolder(pathFolderFrameworkFeatures);

        try (Stream<Envelope> dataStream = Gherkin.fromPaths(listOfPathsForFeatureFiles, false, true, false, idGenerator)){

            List<Envelope> envelopes = dataStream.collect(Collectors.toList());
            for (Envelope env : envelopes) {
                Messages.GherkinDocument gherkinDocument = env.getGherkinDocument();
                listOfGherkinDocuments.add(gherkinDocument);
            }

        } catch (Exception e) {
            LOG.error("Error occurred while trying to read the feature files", new Exception(e));
        }

        FileUtils.renameAllFeatureFiles("b");

        return listOfGherkinDocuments;
    }
}

Just before the return statement, you can see the function that will update the name for all feature files just to check if they are not locked. The problem is that only the first file is always renamed and the rest of them are always locked.

If I will place the rename function at the top, then all the files are successfully renamed...

My understanding is that the try statement will automatically close the stream. Also, I tried to close it manually inside the try block but the results are the same.

What am I missing? How can I make it to release the file locks?

Update 1:

This exact line is making the files (except the first one to be locked):

List<Envelope> envelopes = dataStream.collect(Collectors.toList());

Here is the file name update function definition in case you want to test it:

public static void renameAllFeatureFiles(String fileName) {
        String pathFeaturesFolder = SettingsUtils.getPathFolderFrameworkFeatures();
        List<String> pathList = FileUtils.getAllFilePathsFromFolder(pathFeaturesFolder);

        int counter = 0;
        for (String path : pathList) {
            counter ++;
            File file = new File(path);
            File newFile = new File(pathFeaturesFolder + "\\" + fileName +counter+".feature");
            System.out.println("File: " + path + " locked: " + !file.renameTo(newFile));
        }
    }

And here is a sample feature file content:

Feature: Test

    Scenario: test 1
        Given User will do something
        And User will do something

Update 2: Tried with separate thread using javafx Task, still the same issue :( Except for one file (this is really strange) all files are locked...

public static void runInNewThread() {

        // define the execution task that will run in a new thread
        Task<Void> newTask = new Task<>() {

            @Override
            protected Void call() {

                ArrayList<Messages.GherkinDocument> listOfGherkinDocuments = GherkinUtils.getGherkinDocumentsFromFiles();
    
                return null;
            }
        };

        // run the task in a new thread
        Thread th = new Thread(newTask);
        th.setDaemon(true);
        th.start();
    }
psova
  • 192
  • 2
  • 11

1 Answers1

0

For now, I have used workaround with creating copies of the specific files and using parser on the copies to prevent locking of the original versions...

psova
  • 192
  • 2
  • 11