Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.27 KB | None | 0 0
  1. //////////////// FILE HEADER (INCLUDE IN EVERY FILE) //////////////////////////
  2. //
  3. // Title:   P07 File Finder
  4. // Files:   ShallowFileIterator.java, DeepFileIterator.java, P07Tester.java
  5. // Course:  CS 300 - Spring - 2020
  6. //
  7. // Author:  Andrew Vujnovich
  8. // Email:   avujnovich@wisc.edu
  9. // Lecturer's Name: Hobbes LeGault
  10. //
  11. //////////// PAIR PROGRAMMING (MAY SKIP WHEN WORKING INDIVIDUALLY) ////////////
  12. //
  13. // Partner Name:    null
  14. // Partner Email:   null
  15. // Partner Lecturer's Name: null
  16. //
  17. // VERIFY THE FOLLOWING BY PLACING AN X NEXT TO EACH TRUE STATEMENT:
  18. //   X Write-up states that pair programming is allowed for this assignment.
  19. //   X We have both read and understood the course Pair Programming Policy.
  20. //   X We have registered our team prior to the team registration deadline.
  21. //
  22. ///////////////////////// ALWAYS CREDIT OUTSIDE HELP //////////////////////////
  23. //
  24. // Students who get help from sources other than their partner and the course
  25. // staff must fully acknowledge and credit those sources here.  If you did not
  26. // receive any help of any kind from outside sources, explicitly indicate NONE
  27. // next to each of the labels below.
  28. //
  29. // Persons:         NONE
  30. // Online Sources:  NONE
  31. //
  32. ///////////////////////////////////////////////////////////////////////////////
  33.  
  34.  
  35. import java.io.File;
  36. import java.io.FileNotFoundException;
  37. import java.util.NoSuchElementException;
  38.  
  39. public class P07Tester {
  40.   public static void main(String[] args) {
  41.     File filesystem = new File("filesystem");
  42.     System.out.println("testShallowFileIterator(): " + testShallowFileIterator(filesystem));
  43.     System.out.println("testFileNotFoundError(): " + testFileNotFoundError());
  44.     System.out.println("testFileFound(): " + testFileFound());
  45.     System.out.println("testDeepFileIterator(): " + testDeepFileIterator(filesystem));
  46.   }
  47.  
  48.   /**
  49.    * returns true if ShallowFileIterator functions properly with
  50.    * the given file, or returns false otherwise.
  51.    *
  52.    * @param file
  53.    */
  54.   public static boolean testShallowFileIterator(File file) {
  55.     try {
  56.       String expectedResults =
  57.           "assignments, exam preparation, lecture notes, " + "reading notes, todo.txt, ";
  58.       String testString = "";
  59.       boolean testHasNext = true;
  60.       File testNext = null;
  61.       File[] contents = file.listFiles();
  62.       if (!file.exists()) {
  63.         ShallowFileIterator s = new ShallowFileIterator(file);
  64.         System.out.println(
  65.             "Problem. ShallowFileIterator did not throw a FileNotFoundException when given a " +
  66.                 "file" + " that doesn't exist in the directory.");
  67.         return false;
  68.       } else {
  69.         ShallowFileIterator s = new ShallowFileIterator(file);
  70.         for (int i = 0; i < contents.length; i++) {
  71.           testString += s.next().getName() + ", ";
  72.           testHasNext = s.hasNext();
  73.         }
  74.       }
  75.       if (testString.compareTo(expectedResults) != 0) {
  76.         System.out.println("The returned string of files does not match the expected string.");
  77.         System.out.println("This was returned: " + testString);
  78.         return false;
  79.       }
  80.       if (testHasNext == true) {
  81.         System.out.println("The hasNext method is not working properly.");
  82.         return false;
  83.       }
  84.     } catch (FileNotFoundException e1) {
  85.       System.out.println(
  86.           "The proper FileNotFoundException was thrown for a nonexistent file. Therefore, this " +
  87.               "test was true.");
  88.       return true;
  89.     } catch (NoSuchElementException e2) {
  90.       System.out.println(
  91.           "The NoSuchElementException should not have been thrown, as the test method only " +
  92.               "iterates through the exact number of times to cover " + "each file in the folder.");
  93.       return false;
  94.     }
  95.     return true;
  96.   }
  97.  
  98.   public static boolean testFileNotFoundError() {
  99.     try {
  100.       new ShallowFileIterator(new File("penis.txt"));
  101.     } catch (FileNotFoundException e) {
  102.       return true;
  103.     }
  104.     return false;
  105.   }
  106.  
  107.   public static boolean testFileFound() {
  108.     try {
  109.       new ShallowFileIterator(new File("filesystem"));
  110.     } catch (FileNotFoundException e) {
  111.       return false;
  112.     }
  113.     return true;
  114.   }
  115.  
  116.   public static boolean testDeepFileIterator(File file) {
  117.     try {
  118.       File theFolder = new File(file.getPath() + File.separator + "assignments");
  119.       String expectedResults = "P01, PiggyBank.java, P02, CalendarPrinter.java, P03, "
  120.           + "ElasticBank.java, P04, ExceptionalPiggyBank.java, P05, ExtendedVersion, "
  121.           + "WinterCarnival.java, WinterCarnival.java, P06, AlphabetTrain.java, ";
  122.       String testString = "";
  123.       boolean testHasNext = true;
  124.       File testNext = null;
  125.       File[] contents = theFolder.listFiles();
  126.       System.out.println(contents.length);
  127.       if (!file.exists()) {
  128.         DeepFileIterator s = new DeepFileIterator(file);
  129.         System.out.println(
  130.             "Problem. ShallowFileIterator did not throw a FileNotFoundException "
  131.                 + "when given a file that doesn't exist in the directory.");
  132.         return false;
  133.       }
  134.       else {
  135.         DeepFileIterator testFolder = new DeepFileIterator(theFolder);
  136.         while (testFolder.hasNext()) {
  137.           testString += testFolder.next().getName() + ", ";
  138.           testHasNext = testFolder.hasNext();
  139.         }
  140.       }
  141.       if (testString.compareTo(expectedResults) != 0) {
  142.         System.out.println(
  143.             "The returned string of files does not match the expected string.");
  144.         System.out.println("This was returned: " + testString);
  145.         return false;
  146.       }
  147.       if (testHasNext == true) {
  148.         System.out.println(
  149.             "The hasNext method is not working properly.");
  150.         return false;
  151.       }
  152.     } catch (FileNotFoundException e1) {
  153.       System.out.println(
  154.           "The proper FileNotFoundException was thrown for a nonexistent file. "
  155.               + "Therefore, this test was true.");
  156.       return true;
  157.     } catch (NoSuchElementException e2) {
  158.       System.out.println(
  159.           "The NoSuchElementException should not have been thrown, as the test "
  160.               + "method only iterates through the exact number of times to cover "
  161.               + "each file in the folder.");
  162.       return false;
  163.     }
  164.     return true;
  165.   }
  166.  
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement