Skip to content

Commit

Permalink
move common assertions and test parameter providers into the GitInteg…
Browse files Browse the repository at this point in the history
…rationTest
  • Loading branch information
TheSnoozer committed Mar 2, 2024
1 parent 660a9a4 commit ff14fdc
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 135 deletions.
17 changes: 0 additions & 17 deletions src/test/java/pl/project13/maven/git/BigDiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,6 @@ public void bigDiff() throws Exception {
assertGitPropertiesPresentInProject(targetProject.getProperties());
}

private void assertGitPropertiesPresentInProject(final Properties properties) {
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.host"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.branch"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.full"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.abbrev"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.describe"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.email"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.email"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.full"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.short"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.remote.origin.url"));
}

private GitDescribeConfig createGitDescribeConfig(
final boolean forceLongFormat, final int abbrev) {
final GitDescribeConfig gitDescribeConfig = new GitDescribeConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@

package pl.project13.maven.git;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.Mockito.when;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
Expand All @@ -49,17 +47,6 @@

@RunWith(JUnitParamsRunner.class)
public class GitCommitIdMojoIntegrationTest extends GitIntegrationTest {
private static final boolean UseJGit = false;
private static final boolean UseNativeGit = true;

public static Collection<?> useNativeGit() {
return asList(UseJGit, UseNativeGit);
}

public static Collection<?> useDirty() {
return asList(true, false);
}

@Test
@Parameters(method = "useNativeGit")
public void shouldIncludeExpectedProperties(boolean useNativeGit) throws Exception {
Expand Down Expand Up @@ -1731,28 +1718,4 @@ private GitDescribeConfig createGitDescribeConfig(boolean forceLongFormat, int a
gitDescribeConfig.setDirty("");
return gitDescribeConfig;
}

private void assertPropertyPresentAndEqual(Properties properties, String key, String expected) {
assertThat(properties.stringPropertyNames()).contains(key);
assertThat(properties.getProperty(key)).isEqualTo(expected);
}

private void assertGitPropertiesPresentInProject(Properties properties) {
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.host"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.branch"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.full"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.abbrev"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.describe"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.email"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.email"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.full"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.short"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.remote.origin.url"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.closest.tag.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.closest.tag.commit.count"));
}
}
41 changes: 37 additions & 4 deletions src/test/java/pl/project13/maven/git/GitIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@

package pl.project13.maven.git;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import javax.annotation.Nonnull;
import org.apache.commons.io.FileUtils;
Expand All @@ -47,6 +46,13 @@ public abstract class GitIntegrationTest {
private static final String SANDBOX_DIR = "target" + File.separator + "sandbox" + File.separator;
protected static final String evaluateOnCommit = "HEAD";

private static final boolean UseJGit = false;
private static final boolean UseNativeGit = true;

public static Collection<?> useNativeGit() {
return asList(UseJGit, UseNativeGit);
}

/** Sandbox directory with unique name for current test. */
private String currSandbox;

Expand Down Expand Up @@ -156,4 +162,31 @@ private static List<MavenProject> getReactorProjects(@Nonnull MavenProject proje
}
return reactorProjects;
}

public static void assertPropertyPresentAndEqual(
Properties properties, String key, String expected) {
assertThat(properties.stringPropertyNames()).contains(key);
assertThat(properties.getProperty(key)).isEqualTo(expected);
}

public static void assertGitPropertiesPresentInProject(Properties properties) {
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.host"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.branch"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.full"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.abbrev"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.describe"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.email"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.email"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.full"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.short"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.committer.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.author.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.remote.origin.url"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.closest.tag.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.closest.tag.commit.count"));
}
}
21 changes: 0 additions & 21 deletions src/test/java/pl/project13/maven/git/GitPropertiesFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,4 @@ public void shouldConformPropertiesFileWhenSpecialCharactersInValueString(boolea
}
}
}

private void assertGitPropertiesPresentInProject(Properties properties) {
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.host"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.branch"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.full"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.abbrev"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.describe"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.email"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.email"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.full"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.short"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.committer.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.author.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.remote.origin.url"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.closest.tag.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.closest.tag.commit.count"));
}
}
43 changes: 12 additions & 31 deletions src/test/java/pl/project13/maven/git/GitSubmodulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@

package pl.project13.maven.git;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.nio.file.Files;
import java.util.Properties;
import javax.annotation.Nonnull;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.apache.maven.project.MavenProject;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* Testcases to verify that the git-commit-id-plugin works properly.
*/
@RunWith(JUnitParamsRunner.class)
public class GitSubmodulesTest extends GitIntegrationTest {

@Test
public void shouldResolvePropertiesOnDefaultSettingsForNonPomProject() throws Exception {
@Parameters(method = "useNativeGit")
public void shouldResolvePropertiesOnDefaultSettingsForNonPomProject(boolean useNativeGit) throws Exception {
mavenSandbox
.withParentProject("my-jar-project", "jar")
.withGitRepoInParent(AvailableGitTestRepo.WITH_SUBMODULES)
Expand All @@ -42,6 +43,7 @@ public void shouldResolvePropertiesOnDefaultSettingsForNonPomProject() throws Ex

MavenProject targetProject = mavenSandbox.getChildProject();
setProjectToExecuteMojoIn(targetProject);
mojo.useNativeGit = useNativeGit;

// when
mojo.execute();
Expand All @@ -51,7 +53,8 @@ public void shouldResolvePropertiesOnDefaultSettingsForNonPomProject() throws Ex
}

@Test
public void shouldGeneratePropertiesWithSubmodules() throws Exception {
@Parameters(method = "useNativeGit")
public void shouldGeneratePropertiesWithSubmodules(boolean useNativeGit) throws Exception {
// given
mavenSandbox
.withParentProject("my-pom-project", "pom")
Expand All @@ -68,34 +71,12 @@ public void shouldGeneratePropertiesWithSubmodules() throws Exception {
"gitdir: ../.git/modules/remote-module".getBytes()
);

mojo.useNativeGit = true; // FIXME: make me a parameter
mojo.useNativeGit = useNativeGit;

// when
mojo.execute();

// then
assertGitPropertiesPresentInProject(targetProject.getProperties());
assertThat(targetProject.getProperties().getProperty("git.commit.id.abbrev")).isEqualTo("945bfe6");
}

public void setProjectToExecuteMojoIn(@Nonnull MavenProject project) {
mojo.project = project;
mojo.dotGitDirectory = new File(project.getBasedir(), ".git");
}

private void assertGitPropertiesPresentInProject(Properties properties) {
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.host"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.branch"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.full"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.abbrev"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.email"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.email"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.full"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.short"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.remote.origin.url"));
assertPropertyPresentAndEqual(targetProject.getProperties(), "git.commit.id.abbrev", "945bfe6");
}
}
19 changes: 0 additions & 19 deletions src/test/java/pl/project13/maven/git/NaivePerformanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,4 @@ private GitDescribeConfig createGitDescribeConfig(boolean forceLongFormat, int a
gitDescribeConfig.setAbbrev(abbrev);
return gitDescribeConfig;
}

private void assertGitPropertiesPresentInProject(Properties properties) {
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.host"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.branch"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.full"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.abbrev"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.describe"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.email"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.email"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.full"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.short"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.author.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.committer.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.remote.origin.url"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,4 @@ private Properties createCopy(Properties orig) {

return p;
}

private void assertGitPropertiesPresentInProject(Properties properties) {
for (String key : GIT_KEYS) {
assertThat(properties).satisfies(new ContainsKeyCondition(key));
}
}
}

0 comments on commit ff14fdc

Please sign in to comment.