Search code examples
javafiletestcase

Having trouble understanding what this test case code is doing


I am in CS1050, and we are doing a lab that includes grabbing information from files in order to print new information onto a different file. I have no idea what one of the methods my teacher wrote in the test case class is trying to do. Ive looked up all of the methods that this method uses, but I dont know what the end result is.

static String getBadPath(String name) {
        return new File(new File(TestSuite.class.getResource("empty.txt").getPath()).getParent(), name).getAbsolutePath();
    }

Solution

  • This basically get the absolute path of a file whose name is name and resides in the same directory with empty.txt.

    You can break it down into following code:

    //get the File object named "empty.txt".
    File emptyTxt=new File(TestSuite.class.getResource("empty.txt").getPath());
    //get the directory this emptyTxt reside in
    File parentDirectory=emptyTxt.getPath().getParent();
    //get the File whose name is same as the parameter name and reside in parentDirectory.
    File resultFile=new File(parentDirectory,name)
    //return the absolute path of the resultFile 
    return resultFile.getAbsolutePath();