View Javadoc

1   package org.kite9.framework.common;
2   
3   import java.io.BufferedInputStream;
4   import java.io.BufferedReader;
5   import java.io.File;
6   import java.io.FileInputStream;
7   import java.io.IOException;
8   import java.io.InputStreamReader;
9   import java.util.Comparator;
10  import java.util.HashSet;
11  import java.util.Iterator;
12  import java.util.Set;
13  
14  
15  /***
16   * Compares two files and returns true if they are identical
17   * 
18   * @author robmoffat
19   *
20   */
21  public class FileDiff {
22  
23  	public static void areFilesSame(File a, File b) throws IOException, DiffException {
24  		BufferedInputStream fisA = new BufferedInputStream(new FileInputStream(a));
25  		BufferedInputStream fisB = new BufferedInputStream(new FileInputStream(b));
26  		int outA = 0;
27  		int outB = 0;
28  	
29  		do {
30  			if (outB != outA)
31  				throw new DiffException("Files are different "+a.getPath()+" and "+b.getPath());
32  			
33  			outA = fisA.read();
34  			outB = fisB.read();
35  			
36  			
37  		} while ((outA != -1) && (outB != -1));
38  	
39  	}
40  	
41  	public static void filesContainSameLines(File a, File b) throws IOException, DiffException {
42  		filesContainSameLines(a, b, new Comparator<String>() {
43  
44  			public int compare(String arg0, String arg1) {
45  				return arg0.compareTo(arg1);
46  			}
47  			
48  		});
49  		
50  	}
51  	
52  	/***
53  	 * Allows testing of file comparing where order of files is undefined
54  	 * @throws DiffException 
55  	 */
56  	public static void filesContainSameLines(File a, File b, Comparator<String> lc) throws IOException, DiffException {
57  		BufferedReader rA = new BufferedReader(new InputStreamReader(new FileInputStream(a)));
58  		BufferedReader rB = new BufferedReader(new InputStreamReader(new FileInputStream(b)));
59  		
60  		Set<String> aLines = new HashSet<String>();
61  		Set<String> bLines = new HashSet<String>();
62  		
63  		String line1 = rA.readLine();
64  		while (line1!=null) {
65  			aLines.add(line1);
66  			line1 = rA.readLine();
67  		}
68  		
69  		line1 = rB.readLine();
70  		while (line1!=null) {
71  			bLines.add(line1);
72  			line1 = rB.readLine();
73  		}
74  		
75  		// remove common lines
76  		for (String string : bLines) {
77  			boolean found = false;
78  			for (Iterator<?> iter = aLines.iterator(); iter.hasNext();) {
79  				String element = (String) iter.next();
80  				if (lc.compare(string, element)==0) {
81  					iter.remove();
82  					found = true;
83  					break;
84  				}
85  			}
86  			if (!found) {
87  				throw new DiffException("Not in "+a.getPath()+":"+string);
88  			}
89  		}
90  		
91  		for (String string : aLines) {
92  			throw new DiffException("Not in "+b.getPath()+":"+string);			
93  		}
94  	}
95  	
96  	public static void fileContainsLines(File f, String... lines) throws IOException, DiffException {
97  	    BufferedReader rA = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
98  		
99  		Set<String> aLines = new HashSet<String>();
100 		
101 		String line1 = rA.readLine();
102 		while (line1!=null) {
103 			aLines.add(line1.trim());
104 			line1 = rA.readLine();
105 		}
106 		
107 	
108 		// check lines are present
109 		for (String string : lines) {
110 			boolean found = aLines.contains(string.trim());
111 			
112 			if (!found) {
113 				throw new DiffException("Not in "+f.getPath()+":"+string);
114 			}
115 		}
116 	}
117 }