Skip to content

Commit

Permalink
Use JUnit 5 for integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Mar 25, 2022
1 parent 20a0459 commit 289e13c
Show file tree
Hide file tree
Showing 15 changed files with 261 additions and 249 deletions.
22 changes: 16 additions & 6 deletions plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
<eclipse-collections.version>9.2.0</eclipse-collections.version>
<echarts-api.version>5.3.0-2</echarts-api.version>
<font-awesome-api.version>6.0.0-1</font-awesome-api.version>
<plugin-util-api.version>2.14.0</plugin-util-api.version>
<plugin-util-api.version>2.15.0-rc470.b4bb738647b7</plugin-util-api.version>
<data-tables-api.version>1.11.4-3</data-tables-api.version>
<forensics-api-plugin.version>1.12.0</forensics-api-plugin.version>
<bootstrap5-api.version>5.1.3-6</bootstrap5-api.version>
<streamex.version>0.8.1</streamex.version>
<testcontainers.version>1.16.3</testcontainers.version>
</properties>

<licenses>
Expand Down Expand Up @@ -159,18 +160,27 @@
<type>test-jar</type>
</dependency>

<!-- GitSampleRepoRule -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<classifier>tests</classifier>
<artifactId>ssh-credentials</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<classifier>tests</classifier>
<artifactId>ssh-slaves</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
</exclusion>
</exclusions>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@

import io.jenkins.plugins.util.PluginArchitectureRules;

import static com.tngtech.archunit.base.DescribedPredicate.*;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.*;
import static com.tngtech.archunit.lang.conditions.ArchPredicates.*;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.*;

/**
* Defines several architecture rules for the static analysis model and parsers.
*
Expand All @@ -21,6 +16,15 @@
@SuppressWarnings("hideutilityclassconstructor")
@AnalyzeClasses(packages = "io.jenkins.plugins.forensics.git")
class PluginArchitectureTest {
@ArchTest
static final ArchRule NO_EXCEPTIONS_WITH_NO_ARG_CONSTRUCTOR = ArchitectureRules.NO_EXCEPTIONS_WITH_NO_ARG_CONSTRUCTOR;

@ArchTest
static final ArchRule NO_PUBLIC_TEST_CLASSES = ArchitectureRules.NO_PUBLIC_TEST_CLASSES;

@ArchTest
static final ArchRule NO_PUBLIC_TEST_METHODS = ArchitectureRules.ONLY_PACKAGE_PRIVATE_TEST_METHODS;

@ArchTest
static final ArchRule NO_TEST_API_CALLED = ArchitectureRules.NO_TEST_API_CALLED;

Expand All @@ -31,10 +35,10 @@ class PluginArchitectureTest {
static final ArchRule NO_FORBIDDEN_CLASSES_CALLED = ArchitectureRules.NO_FORBIDDEN_CLASSES_CALLED;

@ArchTest
static final ArchRule NO_JENKINS_INSTANCE_CALL = PluginArchitectureRules.NO_JENKINS_INSTANCE_CALL;
static final ArchRule NO_PUBLIC_ARCHITECTURE_TESTS = ArchitectureRules.NO_PUBLIC_ARCHITECTURE_TESTS;

@ArchTest
static final ArchRule NO_PUBLIC_TEST_CLASSES = PluginArchitectureRules.NO_PUBLIC_TEST_CLASSES;
static final ArchRule NO_JENKINS_INSTANCE_CALL = PluginArchitectureRules.NO_JENKINS_INSTANCE_CALL;

@ArchTest
static final ArchRule NO_FORBIDDEN_PACKAGE_ACCESSED = PluginArchitectureRules.NO_FORBIDDEN_PACKAGE_ACCESSED;
Expand All @@ -52,15 +56,6 @@ class PluginArchitectureTest {
static final ArchRule USE_POST_FOR_VALIDATION_END_POINTS = PluginArchitectureRules.USE_POST_FOR_VALIDATION_END_POINTS;

@ArchTest
static final ArchRule USE_POST_FOR_LIST_AND_COMBOBOX_FILL = PluginArchitectureRules.USE_POST_FOR_LIST_AND_COMBOBOX_FILL;
static final ArchRule USE_POST_FOR_LIST_MODELS_RULE = PluginArchitectureRules.USE_POST_FOR_LIST_AND_COMBOBOX_FILL;

/** Test classes should not use Junit 4. */
// TODO: see https://github.com/TNG/ArchUnit/issues/136
@ArchTest
static final ArchRule NO_JUNIT_4 =
noClasses().that(doNot(
have(simpleNameEndingWith("ITest"))
.or(have(simpleNameStartingWith("Integration")))
.or(have(simpleName("ToolsLister")))))
.should().dependOnClassesThat().resideInAnyPackage("org.junit");
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void shouldCreateNullBlamerOnError() throws IOException, InterruptedException {
GitBlamerFactory gitChecker = new GitBlamerFactory();

Run<?, ?> run = mock(Run.class);
when(run.getEnvironment(NULL_LISTENER)).thenThrow(new IOException());
when(run.getEnvironment(NULL_LISTENER)).thenThrow(new IOException("Error"));

FilteredLog logger = createLogger();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.jenkins.plugins.forensics.git.blame;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;

import edu.hm.hafner.util.FilteredLog;
Expand All @@ -18,12 +18,12 @@
*
* @author Ullrich Hafner
*/
public class GitBlamerITest extends GitITest {
class GitBlamerITest extends GitITest {
/**
* Verifies that the blames are empty if there are no requests defined.
*/
@Test
public void shouldCreateEmptyBlamesIfRequestIsEmpty() {
void shouldCreateEmptyBlamesIfRequestIsEmpty() {
GitBlamer gitBlamer = createBlamer();

FilteredLog log = createLog();
Expand All @@ -38,7 +38,7 @@ public void shouldCreateEmptyBlamesIfRequestIsEmpty() {
* Verifies that the blames are empty if there are no requests defined.
*/
@Test
public void shouldCreateBlamesIfRequestIsExistingFile() {
void shouldCreateBlamesIfRequestIsExistingFile() {
create2RevisionsWithDifferentAuthors();

FileLocations locations = new FileLocations();
Expand Down Expand Up @@ -73,7 +73,7 @@ public void shouldCreateBlamesIfRequestIsExistingFile() {
*/
@Test
@Issue("JENKINS-59252")
public void shouldAssignLastCommitterIfNoLineNumberIsGiven() {
void shouldAssignLastCommitterIfNoLineNumberIsGiven() {
create2RevisionsWithDifferentAuthors();

FileLocations locations = new FileLocations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.jenkins.plugins.forensics.git.blame.GitBlamer.LastCommitRunner;

import static io.jenkins.plugins.forensics.assertions.Assertions.*;
import static org.mockito.Mockito.*;

/**
* Tests the class {@link GitBlamer}.
Expand Down Expand Up @@ -67,7 +68,7 @@ void shouldAbortIfHeadCommitIsMissing() {
@Test
void shouldAbortIfRefParseThrowsException() throws InterruptedException {
GitClient gitClient = createGitClient();
Mockito.when(gitClient.revParse(HEAD)).thenThrow(new GitException());
when(gitClient.revParse(HEAD)).thenThrow(new GitException("Error"));

GitBlamer blamer = new GitBlamer(gitClient, HEAD);

Expand All @@ -80,7 +81,7 @@ void shouldAbortIfRefParseThrowsException() throws InterruptedException {

@Test
void shouldAbortIfWithRepositoryThrowsException() throws InterruptedException, IOException {
GitClient gitClient = createStubbedClientWithException(new IOException());
GitClient gitClient = createStubbedClientWithException(new IOException("Error"));

GitBlamer blamer = new GitBlamer(gitClient, HEAD);
FilteredLog log = new FilteredLog(StringUtils.EMPTY);
Expand All @@ -92,7 +93,7 @@ void shouldAbortIfWithRepositoryThrowsException() throws InterruptedException, I

@Test
void shouldFinishWithIntermediateResultIfInterrupted() throws InterruptedException, IOException {
GitClient gitClient = createStubbedClientWithException(new InterruptedException());
GitClient gitClient = createStubbedClientWithException(new InterruptedException("Error"));

GitBlamer blamer = new GitBlamer(gitClient, HEAD);
FilteredLog log = new FilteredLog(StringUtils.EMPTY);
Expand All @@ -107,17 +108,17 @@ private GitClient createStubbedClientWithException(final Exception exception)
GitClient gitClient = Mockito.mock(GitClient.class);

ObjectId id = Mockito.mock(ObjectId.class);
Mockito.when(gitClient.revParse(HEAD)).thenReturn(id);
Mockito.when(gitClient.withRepository(ArgumentMatchers.any())).thenThrow(exception);
when(gitClient.revParse(HEAD)).thenReturn(id);
when(gitClient.withRepository(ArgumentMatchers.any())).thenThrow(exception);
FilePath workTree = createWorkTreeStub();
Mockito.when(gitClient.getWorkTree()).thenReturn(workTree);
when(gitClient.getWorkTree()).thenReturn(workTree);

return gitClient;
}

private FilePath createWorkTreeStub() {
File mock = Mockito.mock(File.class);
Mockito.when(mock.getPath()).thenReturn("/");
when(mock.getPath()).thenReturn("/");
return new FilePath(mock);
}

Expand All @@ -135,7 +136,7 @@ private void verifyExceptionHandling(final Class<? extends Exception> exception)
BlameCallback callback = createCallback(blames, blamerInput);

BlameRunner runner = Mockito.mock(BlameRunner.class);
Mockito.when(runner.run(RELATIVE_PATH)).thenThrow(exception);
when(runner.run(RELATIVE_PATH)).thenThrow(exception);
callback.run(BUILDER, RELATIVE_PATH, runner, createLastCommitRunner(), log);

assertThat(log.getErrorMessages()).hasSize(3);
Expand Down Expand Up @@ -191,7 +192,7 @@ void shouldMapResultToRequestWithTwoLinesOfAbsolutePaths() throws GitAPIExceptio

private GitClient createGitClient() {
GitClient gitClient = Mockito.mock(GitClient.class);
Mockito.when(gitClient.getWorkTree()).thenReturn(new FilePath((VirtualChannel) null, ""));
when(gitClient.getWorkTree()).thenReturn(new FilePath((VirtualChannel) null, ""));
return gitClient;
}

Expand Down Expand Up @@ -239,7 +240,7 @@ void shouldIgnoreMissingCommit() throws GitAPIException {
BlameCallback callback = createCallback(blames, locations);

BlameResult result = createResult(1);
Mockito.when(result.getSourceAuthor(0)).thenReturn(new PersonIdent(NAME, EMAIL));
when(result.getSourceAuthor(0)).thenReturn(new PersonIdent(NAME, EMAIL));

callback.run(BUILDER, RELATIVE_PATH, createBlameRunner(result), createLastCommitRunner(), log);

Expand All @@ -261,7 +262,7 @@ void shouldIgnoreMissingAuthorAndCommitter() throws GitAPIException {

BlameResult result = createResult(1);
RevCommit commit = createCommit();
Mockito.when(result.getSourceCommit(0)).thenReturn(commit);
when(result.getSourceCommit(0)).thenReturn(commit);

callback.run(BUILDER, RELATIVE_PATH, createBlameRunner(result), createLastCommitRunner(), log);

Expand All @@ -283,9 +284,9 @@ void shouldUseCommitterIfAuthorIsMissing() throws GitAPIException {

BlameResult result = createResult(1);
RevCommit commit = createCommit(TIME + 1);
Mockito.when(result.getSourceCommit(0)).thenReturn(commit);
Mockito.when(result.getSourceAuthor(0)).thenReturn(null);
Mockito.when(result.getSourceCommitter(0)).thenReturn(new PersonIdent(NAME + 1, EMAIL + 1));
when(result.getSourceCommit(0)).thenReturn(commit);
when(result.getSourceAuthor(0)).thenReturn(null);
when(result.getSourceCommitter(0)).thenReturn(new PersonIdent(NAME + 1, EMAIL + 1));

callback.run(BUILDER, RELATIVE_PATH, createBlameRunner(result), createLastCommitRunner(), log);

Expand Down Expand Up @@ -325,27 +326,27 @@ private byte[] getRawCommit(final int commitTime) {
private BlameResult createResult(final int size) {
RawText resultSize = createResultSize(size);
BlameResult result = Mockito.mock(BlameResult.class);
Mockito.when(result.getResultContents()).thenReturn(resultSize);
when(result.getResultContents()).thenReturn(resultSize);
return result;
}

private BlameRunner createBlameRunner(final BlameResult result) throws GitAPIException {
BlameRunner blameRunner = Mockito.mock(BlameRunner.class);
Mockito.when(blameRunner.run(RELATIVE_PATH)).thenReturn(result);
when(blameRunner.run(RELATIVE_PATH)).thenReturn(result);
return blameRunner;
}

private RawText createResultSize(final int size) {
RawText text = Mockito.mock(RawText.class);
Mockito.when(text.size()).thenReturn(size);
when(text.size()).thenReturn(size);
return text;
}

private void stubResultForIndex(final BlameResult result, final int index) {
int line = index + 1;
Mockito.when(result.getSourceAuthor(index)).thenReturn(new PersonIdent(NAME + line, EMAIL + line));
when(result.getSourceAuthor(index)).thenReturn(new PersonIdent(NAME + line, EMAIL + line));
RevCommit commit = createCommit(TIME + line);
Mockito.when(result.getSourceCommit(index)).thenReturn(commit);
when(result.getSourceCommit(index)).thenReturn(commit);
}

private void verifyResult(final FileBlame request, final int line) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void shouldCreateNullDeltaCalculatorOnError() throws IOException, InterruptedExc
GitDeltaCalculatorFactory factory = new GitDeltaCalculatorFactory();

Run<?, ?> run = mock(Run.class);
when(run.getEnvironment(NULL_LISTENER)).thenThrow(new IOException());
when(run.getEnvironment(NULL_LISTENER)).thenThrow(new IOException("Error"));

FilteredLog logger = createLogger();

Expand Down
Loading

0 comments on commit 289e13c

Please sign in to comment.