Counting number of lines in test classes

670 views Asked by At

I'm developing a tool that recognizes test classes and counts the number of lines in those classes. The tool will also count the lines of the business code and compare the both results, here is my code:

    for (File f : list) {
        if (f.isDirectory()) {
            walk(f.getAbsolutePath());
        }

        if (f.getName().endsWith(".java")) {

            System.out.println("File:" + f.getName());
            countFiles++;

            Scanner testScanner = new Scanner(f);
            while (testScanner.hasNextLine()) {

                String test = testScanner.nextLine();
                if (test.contains("org.junit") || test.contains("org.mockito")) {
                    System.out.println("this is a test class");
                    testCounter++;

                    break;
                }
            }
            Scanner sc2 = new Scanner(f);

            while (sc2.hasNextLine()) {

                count++;

counting the business code using (count++) is working perfectly, but counting the number of code in test classes is not working using (testCounter++) is returning the number of test classes rather than the number of lines in those classes! what can I do ?

Thanks

1

There are 1 answers

3
Scary Wombat On BEST ANSWER

Assuming that you are wanting to count the number of lines that contain either org.junit or org.mockito

then you want to do

       while (testScanner.hasNextLine()) {

            String test = testScanner.nextLine();
            if (test.contains("org.junit") || test.contains("org.mockito")) 
            {
                hasTestLines = true;
            }
            count++;
        }

        if (hasTestLines) {
             System.out.println(String.format ("there were %d lines in file %s which also had org.junit||org.mockito", 
                                    count, f.getName());
        }
        else {
             System.out.println(String.format ("there were %d lines in file %s which did NOT have org.junit||org.mockito", 
                                    count, f.getName());
       }