📄 testfileset.java
字号:
package com.wiley.compBooks.EJwithUML.Base.TestUtilities;import java.io.*;/** * A TestFileSet represents a set of output files from a set * of unit tests within a test case. Each output files are compared * to a cooresponding file that has been verified and renamed * with the prefix "gold". * * As you develop a primitive, you can use a TestFileSet to * capture the output. Once the output is acceptable for all * of the tests in the test case, simply rename the output files * with the prefix "gold". Thereafter, if the output is unchanges, * then the test will pass. * */public class TestFileSet{ private String path; /** Construct a TestFileSet with the specified file path which * should be unique for a particular test case.*/ public TestFileSet(String path) throws IOException { this.path = path; File basePath = new File(path); basePath.mkdirs(); } /** Answers true if the output matches a previously verified "gold" file.*/ public boolean fileMatchesGold(String test, String output) throws IOException { File test_file = new File(path+test); File gold_file = new File(path+"gold"+test); BufferedWriter writer = new BufferedWriter(new FileWriter(test_file)); writer.write(output); writer.flush(); writer.close(); if (!gold_file.exists()) { System.out.println("gold file for " +test+ " does not exist"); return false; } if (!test_file.exists()) { System.out.println("test file for " +test+ " does not exist"); return false; } BufferedReader test_reader = new BufferedReader(new FileReader(test_file)); BufferedReader gold_reader = new BufferedReader(new FileReader(gold_file)); boolean done = false; int line_ctr = 0; while (!done) { String test_line = test_reader.readLine(); String gold_line = gold_reader.readLine(); if (test_line == null && gold_line == null) { done = true; break; } if (test_line == null) { System.out.println("Test file for " +test+ " is shorter than the gold file"); return false; } if (gold_line == null) { System.out.println("Test file for " +test+ " is longer than the gold file"); return false; } if (!test_line.equals(gold_line)) { System.out.println("Lines differ for = " +test+ ". line_ctr = " +line_ctr+ "\ntest: " +test_line+ "\ngold: " +gold_line); return false; } line_ctr++; } return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -