Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 1 addition & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<linkXRef>false</linkXRef>
<spotbugs.effort>Max</spotbugs.effort>
<spotbugs.threshold>Low</spotbugs.threshold>
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -111,16 +112,6 @@
<artifactId>mailer</artifactId>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
37 changes: 23 additions & 14 deletions src/test/java/hudson/plugins/git/AbstractGitProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,40 @@
import java.util.List;

import jenkins.MasterToSlaveFileCallable;

import static org.junit.jupiter.api.Assertions.assertTrue;
import org.eclipse.jgit.lib.Repository;

import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.JGitTool;

import static org.junit.Assert.assertTrue;

import org.junit.Rule;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.jvnet.hudson.test.CaptureEnvironmentBuilder;
import org.jvnet.hudson.test.FlagRule;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

/**
* Abstract class that provides convenience methods to configure projects.
* @author Mark Waite
*/
public class AbstractGitProject extends AbstractGitRepository {
@WithJenkins
class AbstractGitProject extends AbstractGitRepository {

protected JenkinsRule r;

@Rule
public JenkinsRule r = new JenkinsRule();
private String notifyCommitAccessControl;

@Rule
public FlagRule<String> notifyCommitAccessControl =
new FlagRule<>(() -> GitStatus.NOTIFY_COMMIT_ACCESS_CONTROL, x -> GitStatus.NOTIFY_COMMIT_ACCESS_CONTROL = x);
@BeforeEach
void beforeEach(JenkinsRule rule) {
r = rule;
notifyCommitAccessControl = GitStatus.NOTIFY_COMMIT_ACCESS_CONTROL;
}

@AfterEach
void afterEach() throws Exception {
GitStatus.NOTIFY_COMMIT_ACCESS_CONTROL = notifyCommitAccessControl;
}

protected FreeStyleProject setupProject(List<BranchSpec> branches, boolean authorOrCommitter) throws Exception {
FreeStyleProject project = r.createFreeStyleProject();
Expand Down Expand Up @@ -208,7 +217,7 @@ protected FreeStyleProject setupProject(List<UserRemoteConfig> repos, List<Branc
protected FreeStyleBuild build(final FreeStyleProject project, final Result expectedResult, final String... expectedNewlyCommittedFiles) throws Exception {
final FreeStyleBuild build = project.scheduleBuild2(0).get();
for (final String expectedNewlyCommittedFile : expectedNewlyCommittedFiles) {
assertTrue(expectedNewlyCommittedFile + " file not found in workspace", build.getWorkspace().child(expectedNewlyCommittedFile).exists());
assertTrue(build.getWorkspace().child(expectedNewlyCommittedFile).exists(), expectedNewlyCommittedFile + " file not found in workspace");
}
if (expectedResult != null) {
r.assertBuildStatus(expectedResult, build);
Expand All @@ -230,7 +239,7 @@ protected FreeStyleBuild build(final FreeStyleProject project, final String pare
protected MatrixBuild build(final MatrixProject project, final Result expectedResult, final String... expectedNewlyCommittedFiles) throws Exception {
final MatrixBuild build = project.scheduleBuild2(0).get();
for (final String expectedNewlyCommittedFile : expectedNewlyCommittedFiles) {
assertTrue(expectedNewlyCommittedFile + " file not found in workspace", build.getWorkspace().child(expectedNewlyCommittedFile).exists());
assertTrue(build.getWorkspace().child(expectedNewlyCommittedFile).exists(), expectedNewlyCommittedFile + " file not found in workspace");
}
if (expectedResult != null) {
r.assertBuildStatus(expectedResult, build);
Expand All @@ -239,7 +248,7 @@ protected MatrixBuild build(final MatrixProject project, final Result expectedRe
}

protected String getHeadRevision(AbstractBuild build, final String branch) throws IOException, InterruptedException {
return build.getWorkspace().act(new MasterToSlaveFileCallable<String>() {
return build.getWorkspace().act(new MasterToSlaveFileCallable<>() {
@Override
public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
try (@SuppressWarnings("deprecation") // Local repository reference
Expand Down
20 changes: 7 additions & 13 deletions src/test/java/hudson/plugins/git/AbstractGitRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import java.util.List;

import hudson.plugins.git.util.GitUtilsTest;
import jenkins.plugins.git.junit.jupiter.WithGitSampleRepo;
import org.eclipse.jgit.util.SystemReader;
import org.junit.Before;
import org.junit.Rule;

import hudson.EnvVars;
import org.junit.jupiter.api.BeforeEach;
import hudson.model.TaskListener;
import hudson.util.StreamTaskListener;

Expand All @@ -30,16 +28,17 @@
*
* @author Mark Waite
*/
@WithGitSampleRepo
public abstract class AbstractGitRepository {

protected File testGitDir;
protected GitClient testGitClient;

@Rule
public GitSampleRepoRule repo = new GitSampleRepoRule();
protected GitSampleRepoRule repo;

@Before
public void createGitRepository() throws Exception {
@BeforeEach
protected void beforeEach(GitSampleRepoRule repo) throws Exception {
this.repo = repo;
SystemReader.getInstance().getUserConfig().clear();
TaskListener listener = StreamTaskListener.fromStderr();
repo.init();
Expand Down Expand Up @@ -78,9 +77,4 @@ protected List<UserRemoteConfig> remoteConfigs() throws IOException {
list.add(new UserRemoteConfig(testGitDir.getAbsolutePath(), "origin", "", null));
return list;
}

/** inline ${@link hudson.Functions#isWindows()} to prevent a transient remote classloader issue */
private boolean isWindows() {
return File.pathSeparatorChar==';';
}
}
50 changes: 28 additions & 22 deletions src/test/java/hudson/plugins/git/AbstractGitTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,22 @@
import java.util.List;

import jenkins.MasterToSlaveFileCallable;
import jenkins.plugins.git.junit.jupiter.WithGitSampleRepo;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.util.SystemReader;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.gitclient.JGitTool;
import org.junit.Before;
import org.junit.Rule;
import org.junit.jupiter.api.BeforeEach;
import jenkins.plugins.git.GitSampleRepoRule;
import org.jvnet.hudson.test.CaptureEnvironmentBuilder;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.kohsuke.stapler.DataBoundConstructor;

/**
Expand All @@ -60,12 +62,13 @@
* @author Kohsuke Kawaguchi
* @author ishaaq
*/
@WithJenkins
@WithGitSampleRepo
public abstract class AbstractGitTestCase {
@Rule
public JenkinsRule r = new JenkinsRule();

@Rule
public GitSampleRepoRule sampleRepo = new GitSampleRepoRule();
protected JenkinsRule r;

protected GitSampleRepoRule sampleRepo;

protected TaskListener listener;

Expand All @@ -78,8 +81,11 @@ public abstract class AbstractGitTestCase {
protected FilePath workspace; // aliases "gitDirPath"
protected GitClient git;

@Before
public void setUp() throws Exception {
@BeforeEach
protected void beforeEach(JenkinsRule rule, GitSampleRepoRule repo) throws Exception {
r = rule;
sampleRepo = repo;

SystemReader.getInstance().getUserConfig().clear();
listener = StreamTaskListener.fromStderr();

Expand Down Expand Up @@ -246,7 +252,7 @@ protected FreeStyleProject setupSimpleProject(String branchString) throws Except
protected FreeStyleBuild build(final FreeStyleProject project, final Result expectedResult, final String...expectedNewlyCommittedFiles) throws Exception {
final FreeStyleBuild build = project.scheduleBuild2(0).get();
for(final String expectedNewlyCommittedFile : expectedNewlyCommittedFiles) {
assertTrue(expectedNewlyCommittedFile + " file not found in workspace", build.getWorkspace().child(expectedNewlyCommittedFile).exists());
assertTrue(build.getWorkspace().child(expectedNewlyCommittedFile).exists(), expectedNewlyCommittedFile + " file not found in workspace");
}
if(expectedResult != null) {
r.assertBuildStatus(expectedResult, build);
Expand All @@ -268,7 +274,7 @@ protected FreeStyleBuild build(final FreeStyleProject project, final String pare
protected MatrixBuild build(final MatrixProject project, final Result expectedResult, final String...expectedNewlyCommittedFiles) throws Exception {
final MatrixBuild build = project.scheduleBuild2(0).get();
for(final String expectedNewlyCommittedFile : expectedNewlyCommittedFiles) {
assertTrue(expectedNewlyCommittedFile + " file not found in workspace", build.getWorkspace().child(expectedNewlyCommittedFile).exists());
assertTrue(build.getWorkspace().child(expectedNewlyCommittedFile).exists(), expectedNewlyCommittedFile + " file not found in workspace");
}
if(expectedResult != null) {
r.assertBuildStatus(expectedResult, build);
Expand All @@ -294,19 +300,19 @@ protected void setVariables(Node node, EnvironmentVariablesNodeProperty.Entry...
}

protected String getHeadRevision(AbstractBuild build, final String branch) throws IOException, InterruptedException {
return build.getWorkspace().act(new MasterToSlaveFileCallable<String>() {
public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
try {
@SuppressWarnings("deprecation") // Local repository reference
org.eclipse.jgit.lib.Repository repo = Git.with(null, null).in(f).getClient().getRepository();
ObjectId oid = repo.resolve("refs/heads/" + branch);
return oid.name();
} catch (GitException e) {
throw new RuntimeException(e);
}
return build.getWorkspace().act(new MasterToSlaveFileCallable<>() {
public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
try {
@SuppressWarnings("deprecation") // Local repository reference
org.eclipse.jgit.lib.Repository repo = Git.with(null, null).in(f).getClient().getRepository();
ObjectId oid = repo.resolve("refs/heads/" + branch);
return oid.name();
} catch (GitException e) {
throw new RuntimeException(e);
}
}

});
});
}

public static class HasCredentialBuilder extends Builder {
Expand Down
50 changes: 27 additions & 23 deletions src/test/java/hudson/plugins/git/BranchSpecTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package hudson.plugins.git;

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

import java.util.HashMap;
import static org.junit.Assert.*;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.*;

import org.jvnet.hudson.test.Issue;


public class BranchSpecTest {
@Test
public void testMatch() {
class BranchSpecTest {

@Test
void testMatch() {
BranchSpec l = new BranchSpec("master");
assertTrue(l.matches("origin/master"));
assertFalse(l.matches("origin/something/master"));
Expand Down Expand Up @@ -54,9 +56,9 @@ public void testMatch() {
assertTrue(p.matches("origin/x"));
assertFalse(p.matches("origin/my-branch/b1"));
}

@Test
public void testMatchEnv() {
void testMatchEnv() {
HashMap<String, String> envMap = new HashMap<>();
envMap.put("master", "master");
envMap.put("origin", "origin");
Expand Down Expand Up @@ -116,26 +118,28 @@ public void testMatchEnv() {
}

@Test
public void testEmptyName() {
void testEmptyName() {
BranchSpec branchSpec = new BranchSpec("");
assertEquals("**",branchSpec.getName());
}

@Test(expected = IllegalArgumentException.class)
public void testNullName() {
BranchSpec branchSpec = new BranchSpec(null);
@Test
void testNullName() {
assertThrows(IllegalArgumentException.class, () -> {
BranchSpec branchSpec = new BranchSpec(null);
});
}

@Test
public void testNameTrimming() {
void testNameTrimming() {
BranchSpec branchSpec = new BranchSpec(" master ");
assertEquals("master",branchSpec.getName());
branchSpec.setName(" other ");
assertEquals("other",branchSpec.getName());
}

@Test
public void testUsesRefsHeads() {
void testUsesRefsHeads() {
BranchSpec m = new BranchSpec("refs/heads/j*n*");
assertTrue(m.matches("refs/heads/jenkins"));
assertTrue(m.matches("refs/heads/jane"));
Expand All @@ -144,9 +148,9 @@ public void testUsesRefsHeads() {
assertFalse(m.matches("origin/jenkins"));
assertFalse(m.matches("remote/origin/jane"));
}

@Test
public void testUsesJavaPatternDirectlyIfPrefixedWithColon() {
void testUsesJavaPatternDirectlyIfPrefixedWithColon() {
BranchSpec m = new BranchSpec(":^(?!(origin/prefix)).*");
assertTrue(m.matches("origin"));
assertTrue(m.matches("origin/master"));
Expand All @@ -159,7 +163,7 @@ public void testUsesJavaPatternDirectlyIfPrefixedWithColon() {

@Test
@Issue("JENKINS-26842")
public void testUsesJavaPatternWithRepetition() {
void testUsesJavaPatternWithRepetition() {
// match pattern from JENKINS-26842
BranchSpec m = new BranchSpec(":origin/release-\\d{8}");
assertTrue(m.matches("origin/release-20150101"));
Expand All @@ -169,7 +173,7 @@ public void testUsesJavaPatternWithRepetition() {
}

@Test
public void testUsesJavaPatternToExcludeMultipleBranches() {
void testUsesJavaPatternToExcludeMultipleBranches() {
BranchSpec m = new BranchSpec(":^(?!origin/master$|origin/develop$).*");
assertTrue(m.matches("origin/branch1"));
assertTrue(m.matches("origin/branch-2"));
Expand All @@ -193,7 +197,7 @@ private EnvVars createEnvMap(String key, String value) {
*/
@Test
@Issue("JENKINS-6856")
public void testUsesEnvValueWithBraces() {
void testUsesEnvValueWithBraces() {
EnvVars env = createEnvMap("GIT_BRANCH", "origin/master");

BranchSpec withBraces = new BranchSpec("${GIT_BRANCH}");
Expand All @@ -204,7 +208,7 @@ public void testUsesEnvValueWithBraces() {

@Test
@Issue("JENKINS-6856")
public void testUsesEnvValueWithoutBraces() {
void testUsesEnvValueWithoutBraces() {
EnvVars env = createEnvMap("GIT_BRANCH", "origin/master");

BranchSpec withoutBraces = new BranchSpec("$GIT_BRANCH");
Expand All @@ -215,7 +219,7 @@ public void testUsesEnvValueWithoutBraces() {

@Test
@Issue("JENKINS-6856")
public void testUsesEnvValueWithToken() {
void testUsesEnvValueWithToken() {
EnvVars env = createEnvMap("GIT_BRANCH", "origin/master");

BranchSpec withToken = new BranchSpec("${GIT_BRANCH,fullName=True}");
Expand All @@ -226,7 +230,7 @@ public void testUsesEnvValueWithToken() {

@Test
@Issue("JENKINS-6856")
public void testUsesEnvValueWithTokenFalse() {
void testUsesEnvValueWithTokenFalse() {
EnvVars env = createEnvMap("GIT_BRANCH", "origin/master");

BranchSpec withTokenFalse = new BranchSpec("${GIT_BRANCH,fullName=false}");
Expand Down
Loading
Loading