Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 1 addition & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,6 @@
<artifactId>test-harness</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
Expand Down Expand Up @@ -238,19 +233,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>3.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.21.3</version>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
55 changes: 22 additions & 33 deletions src/test/java/hudson/plugins/git/BranchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,45 @@
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectIdRef;
import org.eclipse.jgit.lib.Ref;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class BranchTest {
class BranchTest {

private final String branchSHA1;
private final String branchName;
private final ObjectId branchHead;
private final Branch branch;
private final String refPrefix;
private final Ref branchRef;
private final Branch branchFromRef;

private static final String REMOTE_BRANCH_NAME = "origin/master";

public BranchTest() {
this.branchSHA1 = "fa71f704f9b90fa1f857d1623f3fe33fa2277ca9";
this.branchName = REMOTE_BRANCH_NAME;
this.branchHead = ObjectId.fromString(branchSHA1);
this.refPrefix = "refs/remotes/";
this.branchRef = new ObjectIdRef.PeeledNonTag(Ref.Storage.NEW, refPrefix + branchName, branchHead);
this.branch = new Branch(branchName, branchHead);
this.branchFromRef = new Branch(branchRef);
}
private static final String BRANCH_SHA_1 = "fa71f704f9b90fa1f857d1623f3fe33fa2277ca9";
private static final String BRANCH_NAME = "origin/master";
private static final ObjectId BRANCH_HEAD = ObjectId.fromString(BRANCH_SHA_1);
private static final Branch BRANCH = new Branch(BRANCH_NAME, BRANCH_HEAD);
private static final String REF_PREFIX = "refs/remotes/";
private static final Ref BRANCH_REF =
new ObjectIdRef.PeeledNonTag(Ref.Storage.NEW, REF_PREFIX + BRANCH_NAME, BRANCH_HEAD);
private static final Branch BRANCH_FROM_REF = new Branch(BRANCH_REF);

@Test
public void testToString() {
assertThat(branch.toString(), is(branchFromRef.toString()));
void testToString() {
assertThat(BRANCH.toString(), is(BRANCH_FROM_REF.toString()));
}

@Test
public void testToString_Contents() {
String expected = "Branch " + branchName + "(" + branchSHA1 + ")";
assertThat(branch.toString(), is(expected));
void testToString_Contents() {
String expected = "Branch " + BRANCH_NAME + "(" + BRANCH_SHA_1 + ")";
assertThat(BRANCH.toString(), is(expected));
}

@Test
public void hashCodeContract() {
assertThat(branch, is(branchFromRef));
assertThat(branch.hashCode(), is(branchFromRef.hashCode()));
void hashCodeContract() {
assertThat(BRANCH, is(BRANCH_FROM_REF));
assertThat(BRANCH.hashCode(), is(BRANCH_FROM_REF.hashCode()));
}

@Test
public void constructorRefArgStripped() {
Ref ref = new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE, refPrefix + branchName, branchHead);
void constructorRefArgStripped() {
Ref ref = new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE, REF_PREFIX + BRANCH_NAME, BRANCH_HEAD);
Branch strippedBranch = new Branch(ref);
assertThat(strippedBranch.getName(), is(branchName));
assertThat(strippedBranch.getName(), is(BRANCH_NAME));
}

@Test
public void equalsContract() {
void equalsContract() {
EqualsVerifier.forClass(Branch.class)
.usingGetClass()
.withRedefinedSuperclass()
Expand Down
46 changes: 25 additions & 21 deletions src/test/java/hudson/plugins/git/GitAPIBadInitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import hudson.EnvVars;
import hudson.model.TaskListener;
Expand All @@ -13,46 +13,50 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class GitAPIBadInitTest {
class GitAPIBadInitTest {

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@TempDir
private File tempFolder;

private final EnvVars env;

public GitAPIBadInitTest() {
env = new EnvVars();
}
private final EnvVars env = new EnvVars();

private File tempDir;
private TaskListener listener;

@Before
public void setUp() throws IOException, InterruptedException {
tempDir = tempFolder.newFolder();
@BeforeEach
void setUp() throws Exception {
tempDir = newFolder(tempFolder, "junit");
listener = StreamTaskListener.fromStderr();
}

@Test
public void testInitExistingDirectory() throws Exception {
void testInitExistingDirectory() throws Exception {
GitClient git = new GitAPI("git", tempDir, listener, env);
git.init();
File gitDir = new File(tempDir, ".git");
assertTrue(gitDir + " not created", gitDir.exists());
assertTrue(gitDir + " not a directory", gitDir.isDirectory());
assertTrue(gitDir.exists(), gitDir + " not created");
assertTrue(gitDir.isDirectory(), gitDir + " not a directory");
}

@Test
public void testInitExistingFile() throws Exception {
void testInitExistingFile() throws Exception {
File existingFile = new File(tempDir, "file-exists");
Files.writeString(existingFile.toPath(), "git init should fail due to this file", StandardCharsets.UTF_8);
GitClient git = new GitAPI("git", existingFile, listener, env);
GitException e = assertThrows(GitException.class, git::init);
assertThat(e.getMessage(), is("Could not init " + existingFile.getAbsolutePath()));
}

private static File newFolder(File root, String... subDirs) throws Exception {
String subFolder = String.join("/", subDirs);
File result = new File(root, subFolder);
if (!result.mkdirs()) {
throw new IOException("Couldn't create folders " + result);
}
return result;
}
}
50 changes: 28 additions & 22 deletions src/test/java/hudson/plugins/git/GitExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,35 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isA;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import hudson.EnvVars;
import hudson.model.TaskListener;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class GitExceptionTest {
class GitExceptionTest {

@Rule
public TemporaryFolder folder = new TemporaryFolder();
@TempDir
private File folder;

@Test
public void throwsGitException() {
void throwsGitException() {
GitException e = assertThrows(GitException.class, () -> {
throw new GitException();
});
assertThat(e.getMessage(), is(nullValue()));
}

@Test
public void throwsGitExceptionExpectedMessage() {
void throwsGitExceptionExpectedMessage() {
String message = "My custom git exception message";
GitException e = assertThrows(GitException.class, () -> {
throw new GitException(message);
Expand All @@ -44,7 +42,7 @@ public void throwsGitExceptionExpectedMessage() {
}

@Test
public void throwsGitExceptionExpectedMessageWithCause() {
void throwsGitExceptionExpectedMessageWithCause() {
String message = "My custom git exception message";
GitException e = assertThrows(GitException.class, () -> {
throw new GitException(message, new IOException("Custom IOException message"));
Expand All @@ -54,7 +52,7 @@ public void throwsGitExceptionExpectedMessageWithCause() {
}

@Test
public void initCliImplThrowsGitException() throws IOException, InterruptedException {
void initCliImplThrowsGitException() throws Exception {
if (new File("/").canWrite()) { // running as root?
return;
}
Expand All @@ -72,7 +70,7 @@ public void initCliImplThrowsGitException() throws IOException, InterruptedExcep
}

@Test
public void initJGitImplThrowsGitException() throws IOException, InterruptedException {
void initJGitImplThrowsGitException() throws Exception {
if (new File("/").canWrite()) { // running as root?
return;
}
Expand All @@ -91,10 +89,10 @@ public void initJGitImplThrowsGitException() throws IOException, InterruptedExce
}

@Test
public void initCliImplCollisionThrowsGitException() throws IOException, InterruptedException {
File dir = folder.getRoot();
File dotGit = folder.newFile(".git");
Files.write(dotGit.toPath(), "file named .git".getBytes(StandardCharsets.UTF_8), APPEND);
void initCliImplCollisionThrowsGitException() throws Exception {
File dir = folder;
File dotGit = newFile(folder, ".git");
Files.writeString(dotGit.toPath(), "file named .git", APPEND);
GitClient defaultClient =
Git.with(TaskListener.NULL, new EnvVars()).in(dir).using("git").getClient();
assertThrows(
Expand All @@ -103,10 +101,10 @@ public void initCliImplCollisionThrowsGitException() throws IOException, Interru
}

@Test
public void initJGitImplCollisionThrowsGitException() throws IOException, InterruptedException {
File dir = folder.getRoot();
File dotGit = folder.newFile(".git");
Files.write(dotGit.toPath(), "file named .git".getBytes(StandardCharsets.UTF_8), APPEND);
void initJGitImplCollisionThrowsGitException() throws Exception {
File dir = folder;
File dotGit = newFile(folder, ".git");
Files.writeString(dotGit.toPath(), "file named .git", APPEND);
GitClient defaultClient =
Git.with(TaskListener.NULL, new EnvVars()).in(dir).using("jgit").getClient();
JGitInternalException e = assertThrows(
Expand All @@ -122,4 +120,12 @@ public void initJGitImplCollisionThrowsGitException() throws IOException, Interr
private static boolean isWindows() {
return File.pathSeparatorChar == ';';
}

private static File newFile(File parent, String child) throws Exception {
File result = new File(parent, child);
if (!result.createNewFile()) {
throw new IOException("Couldn't create file " + result);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.Test;
import org.junit.jupiter.api.Test;

public class GitLockFailedExceptionTest {
class GitLockFailedExceptionTest {

@Test
public void throwsGitLockFailedException() {
void throwsGitLockFailedException() {
GitLockFailedException lockFailed = assertThrows(GitLockFailedException.class, () -> {
throw new GitLockFailedException();
});
assertThat(lockFailed.getMessage(), is(nullValue()));
}

@Test
public void throwsGitLockFailedExceptionWithMessage() {
void throwsGitLockFailedExceptionWithMessage() {
String message = "My local exception message";
GitLockFailedException lockFailed = assertThrows(GitLockFailedException.class, () -> {
throw new GitLockFailedException(message);
Expand All @@ -27,7 +27,7 @@ public void throwsGitLockFailedExceptionWithMessage() {
}

@Test
public void throwsGitLockFailedExceptionWithCause() {
void throwsGitLockFailedExceptionWithCause() {
String message = "My git exception message";
GitException e = new GitException(message);
GitLockFailedException lockFailed = assertThrows(GitLockFailedException.class, () -> {
Expand All @@ -37,7 +37,7 @@ public void throwsGitLockFailedExceptionWithCause() {
}

@Test
public void throwsGitLockFailedExceptionWithCauseAndMessage() {
void throwsGitLockFailedExceptionWithCauseAndMessage() {
String message = "My git exception message";
GitException e = new GitException(message);
String lockMessage = "My lock message that is not part of the GitException";
Expand Down
Loading
Loading