1 package com.diasparsoftware.util.junit; 2 3 import java.io.*; 4 5 import junit.framework.Assert; 6 7 /*** 8 * Use a gold master file when verifying content that is 9 * not worth parsing. 10 * 11 */ 12 public class GoldMasterFile extends Assert { 13 private File file; 14 15 public GoldMasterFile(String directory, String file) { 16 this(new File(directory, file)); 17 } 18 19 public GoldMasterFile(File file) { 20 this.file = file; 21 } 22 23 /*** 24 * Create gold master content, writing it to file. 25 * 26 * @param content The gold master content 27 * @throws IOException 28 */ 29 public void write(String content) throws IOException { 30 file.getParentFile().mkdirs(); 31 FileWriter goldMasterWriter = new FileWriter(file); 32 goldMasterWriter.write(content); 33 goldMasterWriter.close(); 34 } 35 36 /*** 37 * Check content against the gold master. 38 * 39 * @param expectedContent 40 * @throws IOException 41 */ 42 public void check(String actualContent) 43 throws IOException { 44 45 assertTrue( 46 "Gold master [" + file.getAbsolutePath() + "] not found.", 47 file.exists()); 48 49 StringWriter stringWriter = new StringWriter(); 50 PrintWriter printWriter = new PrintWriter(stringWriter); 51 52 BufferedReader goldMasterReader = 53 new BufferedReader(new FileReader(file)); 54 while (true) { 55 String line = goldMasterReader.readLine(); 56 if (line == null) 57 break; 58 printWriter.println(line); 59 } 60 61 assertEquals(stringWriter.toString(), actualContent); 62 } 63 }