11package io .github .yangziwen .diff .calculate ;
22
3+ import java .io .File ;
4+ import java .io .IOException ;
35import java .util .Arrays ;
46
57import org .eclipse .jgit .diff .DiffEntry ;
68import org .eclipse .jgit .diff .DiffEntry .ChangeType ;
79import org .eclipse .jgit .diff .Edit ;
8- import org .eclipse .jgit .diff .Edit .Type ;
910import org .junit .Assert ;
1011import org .junit .Test ;
11- import org .junit .runner .RunWith ;
1212import org .mockito .Mockito ;
13- import org .powermock .api .mockito .PowerMockito ;
14- import org .powermock .core .classloader .annotations .PrepareForTest ;
15- import org .powermock .modules .junit4 .PowerMockRunner ;
1613
17- @ RunWith (PowerMockRunner .class )
18- @ PrepareForTest ({ Edit .class })
1914public class DiffEntryWrapperTest {
2015
2116 @ Test
@@ -30,14 +25,64 @@ public void testIsDeleted() {
3025
3126 @ Test
3227 public void testIsAllDeletedEdits () {
33- Edit edit1 = PowerMockito .mock (Edit .class );
34- Edit edit2 = PowerMockito .mock (Edit .class );
35- PowerMockito .when (edit1 .getType ()).thenReturn (Type .DELETE );
36- PowerMockito .when (edit2 .getType ()).thenReturn (Type .DELETE );
3728 DiffEntryWrapper wrapper = DiffEntryWrapper .builder ()
38- .editList (Arrays .asList (edit1 , edit2 ))
29+ .editList (Arrays .asList (buildDelete (), buildDelete () ))
3930 .build ();
4031 Assert .assertTrue (wrapper .isAllDeletedEdits ());
4132 }
4233
34+ @ Test
35+ public void getAbsoluteNewPath () throws IOException {
36+ String file = "file" ;
37+ String directory = "/some/directory" ;
38+ DiffEntryWrapper wrapper = DiffEntryWrapper .builder ()
39+ .gitDir (new File (directory ))
40+ .diffEntry (new DummyDiffEntry (file ))
41+ .build ();
42+ String result = wrapper .getAbsoluteNewPath ();
43+ Assert .assertEquals (new File (directory , file ).getCanonicalPath (), result );
44+ }
45+
46+ @ Test
47+ public void getAbsoluteNewPathWithRelativeGitDir () throws IOException {
48+ String file = "file" ;
49+ DiffEntryWrapper wrapper = DiffEntryWrapper .builder ()
50+ .gitDir (new File ("." ))
51+ .diffEntry (new DummyDiffEntry (file ))
52+ .build ();
53+ String result = wrapper .getAbsoluteNewPath ();
54+ Assert .assertEquals (new File ("." , file ).getCanonicalPath (), result );
55+ }
56+
57+ @ Test
58+ public void getAbsoluteNewPathException () throws IOException {
59+ File mock = Mockito .mock (File .class );
60+ Mockito .when (mock .getCanonicalPath ()).thenThrow (new IOException ("" ));
61+ DiffEntryWrapper wrapper = DiffEntryWrapper .builder ()
62+ .gitDir (mock )
63+ .diffEntry (new DummyDiffEntry ("" ))
64+ .build ();
65+ String result = wrapper .getAbsoluteNewPath ();
66+ Assert .assertEquals ("/" , result );
67+ }
68+
69+ // A "delete" edit is one where: beginA < endA && beginB == endB
70+ private static Edit buildDelete () {
71+ return new Edit (5 , 10 , 8 , 8 );
72+ }
73+
74+ private class DummyDiffEntry extends DiffEntry {
75+
76+ private final String filePath ;
77+
78+ DummyDiffEntry (String filePath ) {
79+ super ();
80+ this .filePath = filePath ;
81+ }
82+
83+ @ Override
84+ public String getNewPath () {
85+ return filePath ;
86+ }
87+ }
4388}
0 commit comments