package tests; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; import javax.swing.JOptionPane; public class TestsSupport { // Leave the following variable set to false. private static boolean generateOfficialResults = false; /** * You can use the correctResults method while developing your own tests. * * If you define text files with expected results, the text * files must be named starting with "studentTest". * * * @param filename * @param results * @return true if the contents of the file corresponds to those in results */ public static boolean isCorrect(String filename, String results) { officialUseIgnore(filename, results); String officialResults=""; try { BufferedReader fin = new BufferedReader(new FileReader(filename)); String line; while ((line = fin.readLine()) != null) { officialResults += line + "\n"; } fin.close(); }catch (IOException e) { System.out.println("File operation in isCorrect failed."); return false; } results = removeBlanks(results); officialResults = removeBlanks(officialResults); if (results.equals(officialResults)) { return true; } return false; } public static boolean sameContents(String firstFile, String secondFile) { if (removeBlanks(fileData(firstFile)).equals(removeBlanks(fileData(secondFile)))) return true; return false; } public static String fileData(String fileName) { StringBuffer stringBuffer = new StringBuffer(); try { FileReader fileReader = new FileReader(fileName); BufferedReader bufferedReader = new BufferedReader(fileReader); Scanner fileScanner = new Scanner(bufferedReader); while (fileScanner.hasNextLine()) stringBuffer.append(fileScanner.nextLine()); fileScanner.close(); } catch (IOException e) { System.out.println(e.getMessage()); } return stringBuffer.toString(); } public static String removeBlanks(String src) { StringBuffer resultsBuf = new StringBuffer(); char curr; for (int i=0; i