Skip to content

Commit b5456e3

Browse files
committed
Removed code duplications.
1 parent 48af0d6 commit b5456e3

File tree

1 file changed

+22
-45
lines changed

1 file changed

+22
-45
lines changed

src/test/java/io/jenkins/plugins/git/forensics/blame/GitBlamerTest.java

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,7 @@ void shouldAbortIfRefParseThrowsException() throws InterruptedException {
6868

6969
@Test
7070
void shouldAbortIfWithRepositoryThrowsException() throws InterruptedException, IOException {
71-
GitClient gitClient = mock(GitClient.class);
72-
73-
ObjectId id = mock(ObjectId.class);
74-
when(gitClient.revParse(HEAD)).thenReturn(id);
75-
when(gitClient.withRepository(any())).thenThrow(new IOException());
76-
FilePath workTree = createWorkTreeStub();
77-
when(gitClient.getWorkTree()).thenReturn(workTree);
71+
GitClient gitClient = createStubbedClientWithException(new IOException());
7872

7973
GitBlamer blamer = new GitBlamer(gitClient, HEAD);
8074
Blames blames = blamer.blame(createLocations());
@@ -83,27 +77,33 @@ void shouldAbortIfWithRepositoryThrowsException() throws InterruptedException, I
8377
assertThat(blames.getErrorMessages()).contains(GitBlamer.BLAME_ERROR);
8478
}
8579

86-
private FilePath createWorkTreeStub() {
87-
File mock = mock(File.class);
88-
when(mock.getPath()).thenReturn("/");
89-
return new FilePath(mock);
90-
}
91-
9280
@Test
9381
void shouldFinishWithIntermediateResultIfInterrupted() throws InterruptedException, IOException {
82+
GitClient gitClient = createStubbedClientWithException(new InterruptedException());
83+
84+
GitBlamer blamer = new GitBlamer(gitClient, HEAD);
85+
Blames blames = blamer.blame(createLocations());
86+
87+
assertThat(blames.isEmpty()).isTrue();
88+
assertThat(blames.getErrorMessages()).isEmpty();
89+
}
90+
91+
private GitClient createStubbedClientWithException(final Exception exception) throws InterruptedException, IOException {
9492
GitClient gitClient = mock(GitClient.class);
9593

9694
ObjectId id = mock(ObjectId.class);
9795
when(gitClient.revParse(HEAD)).thenReturn(id);
98-
when(gitClient.withRepository(any())).thenThrow(new InterruptedException());
96+
when(gitClient.withRepository(any())).thenThrow(exception);
9997
FilePath workTree = createWorkTreeStub();
10098
when(gitClient.getWorkTree()).thenReturn(workTree);
10199

102-
GitBlamer blamer = new GitBlamer(gitClient, HEAD);
103-
Blames blames = blamer.blame(createLocations());
100+
return gitClient;
101+
}
104102

105-
assertThat(blames.isEmpty()).isTrue();
106-
assertThat(blames.getErrorMessages()).isEmpty();
103+
private FilePath createWorkTreeStub() {
104+
File mock = mock(File.class);
105+
when(mock.getPath()).thenReturn("/");
106+
return new FilePath(mock);
107107
}
108108

109109
@Test
@@ -147,7 +147,7 @@ void shouldMapResultToRequestWithOneLine() throws GitAPIException {
147147
}
148148

149149
@Test
150-
void shouldMapResultToRequestWithTwoLines() throws GitAPIException {
150+
void shouldMapResultToRequestWithTwoLinesOfAbsolutePaths() throws GitAPIException {
151151
FileLocations locations = createLocations();
152152
locations.addLine(ABSOLUTE_PATH, 1);
153153
locations.addLine(ABSOLUTE_PATH, 2);
@@ -163,6 +163,8 @@ void shouldMapResultToRequestWithTwoLines() throws GitAPIException {
163163
BlameRunner blameRunner = createBlameRunner(result);
164164
callback.run(FILE, blameRunner);
165165

166+
assertThat(blames.contains(ABSOLUTE_PATH)).isTrue();
167+
assertThat(blames.contains(FILE)).isFalse();
166168
FileBlame blame = blames.getBlame(ABSOLUTE_PATH);
167169
verifyResult(blame, 1);
168170
verifyResult(blame, 2);
@@ -181,32 +183,7 @@ private BlameCallback createCallback(final Blames blames, final FileLocations bl
181183
private FileLocations createLocations() {
182184
FileSystem fileSystem = mock(FileSystem.class);
183185
when(fileSystem.resolveAbsolutePath(anyString(), any())).thenReturn(WORKSPACE);
184-
FileLocations locations = new FileLocations(WORKSPACE, fileSystem);
185-
return locations;
186-
}
187-
188-
@Test
189-
void shouldStoreAbsolutePaths() throws GitAPIException {
190-
FileLocations locations = createLocations();
191-
locations.addLine(ABSOLUTE_PATH, 1);
192-
locations.addLine(ABSOLUTE_PATH, 2);
193-
194-
Blames blames = new Blames();
195-
BlameCallback callback = createCallback(blames, locations);
196-
197-
BlameResult result = createResult(2);
198-
199-
stubResultForIndex(result, 0);
200-
stubResultForIndex(result, 1);
201-
202-
BlameRunner blameRunner = createBlameRunner(result);
203-
callback.run(FILE, blameRunner);
204-
205-
assertThat(blames.contains(ABSOLUTE_PATH)).isTrue();
206-
assertThat(blames.contains(FILE)).isFalse();
207-
FileBlame blame = blames.getBlame(ABSOLUTE_PATH);
208-
verifyResult(blame, 1);
209-
verifyResult(blame, 2);
186+
return new FileLocations(WORKSPACE, fileSystem);
210187
}
211188

212189
@Test

0 commit comments

Comments
 (0)