11package diffutils ;
22
3- import difflib .DiffUtils ;
4- import difflib .Patch ;
5- import difflib .PatchFailedException ;
6- import junit .framework .TestCase ;
7-
83import java .io .BufferedReader ;
9- import java .io .File ;
104import java .io .FileReader ;
115import java .io .IOException ;
126import java .util .ArrayList ;
137import java .util .Arrays ;
148import java .util .LinkedList ;
159import java .util .List ;
1610
11+ import junit .framework .TestCase ;
12+ import difflib .DiffUtils ;
13+ import difflib .Patch ;
14+ import difflib .PatchFailedException ;
15+
1716public class GenerateUnifiedDiffTest extends TestCase {
1817
1918
2019 public List <String > fileToLines (String filename ) {
2120 List <String > lines = new LinkedList <String >();
2221 String line = "" ;
22+ BufferedReader in = null ;
2323 try {
24- BufferedReader in = new BufferedReader (new FileReader (filename ));
24+ in = new BufferedReader (new FileReader (filename ));
2525 while ((line = in .readLine ()) != null ) {
2626 lines .add (line );
2727 }
2828 } catch (IOException e ) {
2929 e .printStackTrace ();
3030 fail (e .getMessage ());
31- }
31+ } finally {
32+ if (in != null ) {
33+ try {
34+ in .close ();
35+ } catch (IOException e ) {
36+ // ignore ... any errors should already have been
37+ // reported via an IOException from the final flush.
38+ }
39+ }
40+ }
3241 return lines ;
3342 }
3443
@@ -48,14 +57,14 @@ public void testGenerateUnifiedWithOneDelta() {
4857
4958 public void testGenerateUnifiedDiffWithoutAnyDeltas () {
5059 List <String > test = Arrays .asList ("abc" );
51- Patch patch = DiffUtils .diff (test , test );
60+ Patch < String > patch = DiffUtils .diff (test , test );
5261 DiffUtils .generateUnifiedDiff ("abc" , "abc" , test , patch , 0 );
5362 }
5463
5564 public void testDiff_Issue10 () {
5665 final List <String > baseLines = fileToLines (TestConstants .MOCK_FOLDER + "issue10_base.txt" );
5766 final List <String > patchLines = fileToLines (TestConstants .MOCK_FOLDER + "issue10_patch.txt" );
58- final Patch p = DiffUtils .parseUnifiedDiff (patchLines );
67+ final Patch < String > p = DiffUtils .parseUnifiedDiff (patchLines );
5968 try {
6069 DiffUtils .patch (baseLines , p );
6170 } catch (PatchFailedException e ) {
@@ -96,20 +105,19 @@ public void testDiffWithHeaderLineInText() {
96105 revised .add ("test line 4" );
97106 revised .add ("test line 5" );
98107
99- Patch patch = DiffUtils .diff (original , revised );
108+ Patch < String > patch = DiffUtils .diff (original , revised );
100109 List <String > udiff = DiffUtils .generateUnifiedDiff ("original" , "revised" ,
101110 original , patch , 10 );
102111 DiffUtils .parseUnifiedDiff (udiff );
103112 }
104113
105- @ SuppressWarnings ("unchecked" )
106114 private void verify (List <String > origLines , List <String > revLines ,
107115 String originalFile , String revisedFile ) {
108- Patch patch = DiffUtils .diff (origLines , revLines );
116+ Patch < String > patch = DiffUtils .diff (origLines , revLines );
109117 List <String > unifiedDiff = DiffUtils .generateUnifiedDiff (originalFile , revisedFile ,
110118 origLines , patch , 10 );
111119
112- Patch fromUnifiedPatch = DiffUtils .parseUnifiedDiff (unifiedDiff );
120+ Patch < String > fromUnifiedPatch = DiffUtils .parseUnifiedDiff (unifiedDiff );
113121 List <String > patchedLines ;
114122 try {
115123 patchedLines = (List <String >) fromUnifiedPatch .applyTo (origLines );
0 commit comments