How to run script recursively for all the subdirectories of a directory in Java

Viewed 74

I am writing a Junit test. I have several components inside of a big module. I have managed to modify a exisitng script, which will scan through one component for now (hard coded the path) and checks the testcases and executes the tests.

Root Directory: Module

List of sub directories:

Component1 Component2 Component3 Component4 Component5 . . . . Component6

I have the following code:

@RunWith(Parameterized.class)
public class ContentTest {
    
    private static final Logger log = LoggerFactory.getLogger(ContentTest.class);
    @Parameter
    public String testCase;    
    
    @Parameters( name = "{index}: {0}" )
    public static String[] data() {
        FileSystem fs = FileSystems.getDefault();
        Path p = fs.getPath("Component1/");
        try {
            PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**/*TEST.tab");
            Set<String> tests = 
                    Files.walk(p,FileVisitOption.FOLLOW_LINKS)
            .filter(x -> matcher.matches(x)|| false)
            .map(x -> x.getFileName().toFile().getName())
            .map(x -> x.substring(0, x.length()-4))
            .collect(Collectors.toSet());
            log.info("Going to run the following tests " );
            tests.stream().forEach(log::info);
            String [] result = (String[]) tests.toArray(new String[tests.size()]);
            return result;
        } catch (IOException e) {
            log.error("Could not get testcases." + e);
        }
        return new String[0];
    }
    
    
    @Test
    public void test1() {
        TestCommand tc = new TestCommand();
        tc.setBaseDir(new HdfsResource("Component1/"));
        List<String> args = new ArrayList<>();
        args.add(testCase);
        args.add("--settings");
        tc.run(args.toArray(new String[args.size()]));
    }   


 }

But, in mycase I have tests inside all the Components.

Now my query is how can I, modify or change the above script in such a way that. It will execute tests from all the Components one after the other.

Any ideas and suggestions are helpful!!

1 Answers

This can be done fairly simply with a recursive method, ie:


public class MyClass {
    private List<File> directories;

    public static void recurse() {
    
        String[] directories = file.list(new FilenameFilter() {
          @Override
          public boolean accept(File current, String name) {
            return new File(current, name).isDirectory();
          }
        });
        
        for (String dir : directories) {
            File f = new File(dir) {
                if (f.exists()) {
                    this.directories.add(f);
                } else {
                    System.out.println("Directory " + f.getAbsolutePath() + " does not exist");
                }
        }
    }

    public static void main(String[] args) {
        System.out.println("Loop beginning");
        recurse();
        System.out.println("Loop ending");
    }
        

Hope this helped

Related