1
1
package diffutils ;
2
2
3
3
import java .io .BufferedReader ;
4
- import java .io .File ;
5
4
import java .io .FileReader ;
6
5
import java .io .IOException ;
7
6
import java .util .LinkedList ;
@@ -18,48 +17,74 @@ public class EmptyContextUnifiedDiffTest extends TestCase {
18
17
public List <String > fileToLines (String filename ) {
19
18
List <String > lines = new LinkedList <String >();
20
19
String line = "" ;
20
+ BufferedReader in = null ;
21
21
try {
22
- BufferedReader in = new BufferedReader (new FileReader (filename ));
22
+ in = new BufferedReader (new FileReader (filename ));
23
23
while ((line = in .readLine ()) != null ) {
24
24
lines .add (line );
25
25
}
26
26
} catch (IOException e ) {
27
27
e .printStackTrace ();
28
28
fail (e .getMessage ());
29
+ } finally {
30
+ if (in != null ) {
31
+ try {
32
+ in .close ();
33
+ } catch (IOException e ) {
34
+ // ignore ... any errors should already have been
35
+ // reported via an IOException from the final flush.
36
+ }
37
+ }
29
38
}
30
39
return lines ;
31
40
}
32
41
33
- public void testUnifiedContextInsert () {
34
- testUnifiedContextXXX (TestConstants .MOCK_FOLDER + "uc_insert_patch.txt" , TestConstants .MOCK_FOLDER
35
- + "uc_insert_revised.txt" );
42
+ public void testEmptyUnifiedContextPatch () {
43
+ List <String > origLines = fileToLines (TestConstants .MOCK_FOLDER + "unified_empty_context_original.txt" );
44
+ List <String > revLines = fileToLines (TestConstants .MOCK_FOLDER + "unified_empty_context_revised.txt" );
45
+ List <String > unifiedDiff = fileToLines (TestConstants .MOCK_FOLDER + "unified_empty_context_patch.txt" );
46
+
47
+ List <String > patchedLines = null ;
48
+ Patch patch = DiffUtils .parseUnifiedDiff (unifiedDiff );
49
+
50
+ try {
51
+ patchedLines = (List <String >) patch .applyTo (origLines );
52
+ } catch (PatchFailedException e ) {
53
+ fail (e .getMessage ());
54
+ }
55
+
56
+ verifyLinesEqual (patchedLines , revLines );
57
+ }
58
+
59
+ public void testEmptyUnifiedContextDiff () {
60
+ List <String > origLines = fileToLines (TestConstants .MOCK_FOLDER + "unified_empty_context_original.txt" );
61
+ List <String > revLines = fileToLines (TestConstants .MOCK_FOLDER + "unified_empty_context_revised.txt" );
62
+
63
+ List <String > patchedLines = null ;
64
+
65
+ // Generate a 0-context diff then reapply
66
+ Patch generatedPatch = DiffUtils .diff (origLines , revLines );
67
+ List <String > generatedDiff = DiffUtils .generateUnifiedDiff ("original" , "revised" , origLines , generatedPatch , 0 );
68
+ Patch newPatch = DiffUtils .parseUnifiedDiff (generatedDiff );
69
+
70
+ try {
71
+ patchedLines = (List <String >) newPatch .applyTo (origLines );
72
+ } catch (PatchFailedException e ) {
73
+ fail (e .getMessage ());
74
+ }
75
+
76
+ verifyLinesEqual (patchedLines , revLines );
77
+ }
78
+
79
+ public void verifyLinesEqual (List <String > patchedLines , List <String > revLines ) {
80
+ assertTrue (revLines .size () == patchedLines .size ());
81
+ for (int i = 0 ; i < revLines .size (); i ++) {
82
+ String l1 = revLines .get (i );
83
+ String l2 = patchedLines .get (i );
84
+ if (!l1 .equals (l2 )) {
85
+ fail ("Line " + (i + 1 ) + " of the patched file did not match the revised original" );
86
+ }
87
+ }
36
88
}
37
89
38
- public void testUnifiedContextXXX (String patch_file , String revised_file ) {
39
- List <String > origLines = fileToLines (TestConstants .MOCK_FOLDER + "uc_original.txt" );
40
- List <String > revLines = fileToLines (revised_file );
41
- List <String > unifiedDiff = fileToLines (patch_file );
42
- List <String > patchedLines = null ;
43
- Patch patch = DiffUtils .parseUnifiedDiff (unifiedDiff );
44
-
45
- try {
46
- patchedLines = (List <String >) patch .applyTo (origLines );
47
- } catch (PatchFailedException e ) {
48
- fail (e .getMessage ());
49
- }
50
-
51
- verifyLinesEqual (patchedLines , revLines );
52
- }
53
-
54
- public void verifyLinesEqual (List <String > patchedLines , List <String > revLines ) {
55
- assertTrue (revLines .size () == patchedLines .size ());
56
- for (int i = 0 ; i < revLines .size (); i ++) {
57
- String l1 = revLines .get (i );
58
- String l2 = patchedLines .get (i );
59
- if (!l1 .equals (l2 )) {
60
- fail ("Line " + (i + 1 ) + " of the patched file did not match the revised original" );
61
- }
62
- }
63
- }
64
-
65
90
}
0 commit comments