Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////// FILE HEADER (INCLUDE IN EVERY FILE) //////////////////////////
- //
- // Title: P07 File Finder
- // Files: ShallowFileIterator.java, DeepFileIterator.java, P07Tester.java
- // Course: CS 300 - Spring - 2020
- //
- // Author: Andrew Vujnovich
- // Email: avujnovich@wisc.edu
- // Lecturer's Name: Hobbes LeGault
- //
- //////////// PAIR PROGRAMMING (MAY SKIP WHEN WORKING INDIVIDUALLY) ////////////
- //
- // Partner Name: null
- // Partner Email: null
- // Partner Lecturer's Name: null
- //
- // VERIFY THE FOLLOWING BY PLACING AN X NEXT TO EACH TRUE STATEMENT:
- // X Write-up states that pair programming is allowed for this assignment.
- // X We have both read and understood the course Pair Programming Policy.
- // X We have registered our team prior to the team registration deadline.
- //
- ///////////////////////// ALWAYS CREDIT OUTSIDE HELP //////////////////////////
- //
- // Students who get help from sources other than their partner and the course
- // staff must fully acknowledge and credit those sources here. If you did not
- // receive any help of any kind from outside sources, explicitly indicate NONE
- // next to each of the labels below.
- //
- // Persons: NONE
- // Online Sources: NONE
- //
- ///////////////////////////////////////////////////////////////////////////////
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.NoSuchElementException;
- public class P07Tester {
- public static void main(String[] args) {
- File filesystem = new File("filesystem");
- System.out.println("testShallowFileIterator(): " + testShallowFileIterator(filesystem));
- System.out.println("testFileNotFoundError(): " + testFileNotFoundError());
- System.out.println("testFileFound(): " + testFileFound());
- System.out.println("testDeepFileIterator(): " + testDeepFileIterator(filesystem));
- }
- /**
- * returns true if ShallowFileIterator functions properly with
- * the given file, or returns false otherwise.
- *
- * @param file
- */
- public static boolean testShallowFileIterator(File file) {
- try {
- String expectedResults =
- "assignments, exam preparation, lecture notes, " + "reading notes, todo.txt, ";
- String testString = "";
- boolean testHasNext = true;
- File testNext = null;
- File[] contents = file.listFiles();
- if (!file.exists()) {
- ShallowFileIterator s = new ShallowFileIterator(file);
- System.out.println(
- "Problem. ShallowFileIterator did not throw a FileNotFoundException when given a " +
- "file" + " that doesn't exist in the directory.");
- return false;
- } else {
- ShallowFileIterator s = new ShallowFileIterator(file);
- for (int i = 0; i < contents.length; i++) {
- testString += s.next().getName() + ", ";
- testHasNext = s.hasNext();
- }
- }
- if (testString.compareTo(expectedResults) != 0) {
- System.out.println("The returned string of files does not match the expected string.");
- System.out.println("This was returned: " + testString);
- return false;
- }
- if (testHasNext == true) {
- System.out.println("The hasNext method is not working properly.");
- return false;
- }
- } catch (FileNotFoundException e1) {
- System.out.println(
- "The proper FileNotFoundException was thrown for a nonexistent file. Therefore, this " +
- "test was true.");
- return true;
- } catch (NoSuchElementException e2) {
- System.out.println(
- "The NoSuchElementException should not have been thrown, as the test method only " +
- "iterates through the exact number of times to cover " + "each file in the folder.");
- return false;
- }
- return true;
- }
- public static boolean testFileNotFoundError() {
- try {
- new ShallowFileIterator(new File("penis.txt"));
- } catch (FileNotFoundException e) {
- return true;
- }
- return false;
- }
- public static boolean testFileFound() {
- try {
- new ShallowFileIterator(new File("filesystem"));
- } catch (FileNotFoundException e) {
- return false;
- }
- return true;
- }
- public static boolean testDeepFileIterator(File file) {
- try {
- File theFolder = new File(file.getPath() + File.separator + "assignments");
- String expectedResults = "P01, PiggyBank.java, P02, CalendarPrinter.java, P03, "
- + "ElasticBank.java, P04, ExceptionalPiggyBank.java, P05, ExtendedVersion, "
- + "WinterCarnival.java, WinterCarnival.java, P06, AlphabetTrain.java, ";
- String testString = "";
- boolean testHasNext = true;
- File testNext = null;
- File[] contents = theFolder.listFiles();
- System.out.println(contents.length);
- if (!file.exists()) {
- DeepFileIterator s = new DeepFileIterator(file);
- System.out.println(
- "Problem. ShallowFileIterator did not throw a FileNotFoundException "
- + "when given a file that doesn't exist in the directory.");
- return false;
- }
- else {
- DeepFileIterator testFolder = new DeepFileIterator(theFolder);
- while (testFolder.hasNext()) {
- testString += testFolder.next().getName() + ", ";
- testHasNext = testFolder.hasNext();
- }
- }
- if (testString.compareTo(expectedResults) != 0) {
- System.out.println(
- "The returned string of files does not match the expected string.");
- System.out.println("This was returned: " + testString);
- return false;
- }
- if (testHasNext == true) {
- System.out.println(
- "The hasNext method is not working properly.");
- return false;
- }
- } catch (FileNotFoundException e1) {
- System.out.println(
- "The proper FileNotFoundException was thrown for a nonexistent file. "
- + "Therefore, this test was true.");
- return true;
- } catch (NoSuchElementException e2) {
- System.out.println(
- "The NoSuchElementException should not have been thrown, as the test "
- + "method only iterates through the exact number of times to cover "
- + "each file in the folder.");
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement