Skip to content

Commit

Permalink
Add test for issue e-gineering#105
Browse files Browse the repository at this point in the history
  • Loading branch information
robth committed Jan 15, 2019
1 parent 649537b commit 9e6a257
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.e_gineering.maven.gitflowhelper;

import org.apache.maven.it.Verifier;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

@RunWith(BlockJUnit4ClassRunner.class)
public class DeployFeatureBranchIT extends AbstractIntegrationTest {

@Test
public void deployFeatureBranchWithBranchNameInMavenVersion() throws Exception {
String version = "1.0.0-feature-my-feature-SNAPSHOT"; // POM has 1.0.0-SNAPSHOT, branch name should be mixed in

Verifier verifier = createVerifier("/multi-module-project-stub", "feature/my-feature", version);

try {
verifier.executeGoal("deploy");
verifier.verifyErrorFreeLog();

// Check if the parent POM file exists in the local repo
File parentArtifactDir = getArtifactDir("gitflow-helper-maven-plugin-multi-module-parent-test-stub", version);
Assert.assertTrue(artifactHasFiles(parentArtifactDir));
File parentPomFile = getPomFromArtifactDir(parentArtifactDir);
Assert.assertNotNull(parentPomFile);

// Check if the parent POM version number contains the branch name
Model parentPom = parsePomFile(parentPomFile);
Assert.assertEquals(version, parentPom.getVersion());

// Check if the module POM file exists in the local repo
File moduleArtifactDir = getArtifactDir("module", version);
Assert.assertTrue(artifactHasFiles(moduleArtifactDir));
File modulePomFile = getPomFromArtifactDir(moduleArtifactDir);
Assert.assertNotNull(modulePomFile);

// Check if the module POM version number contains the branch name
Model modulePom = parsePomFile(modulePomFile);
Assert.assertEquals(version, modulePom.getParent().getVersion());
} finally {
verifier.resetStreams();
}
}

private File getArtifactDir(final String artifactId, final String version) {
return new File(System.getProperty("basedir"),
"target/it-repositories/snapshots/com/e-gineering/" + artifactId + "/" + version
);
}

private boolean artifactHasFiles(final File artifactDir) {
return artifactDir.exists() && artifactDir.isDirectory() && artifactDir.list().length > 0;
}

private File getPomFromArtifactDir(final File artifactDir) {
File pom = null;

File[] poms = artifactDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".pom");
}
});

if (poms != null && poms.length > 0) {
Arrays.sort(poms, new Comparator<File>() {
@Override
public int compare(File file1, File file2) {
return Long.compare(file2.lastModified(), file1.lastModified());
}
});

pom = poms[0]; // The latest POM
}

return pom;
}

private Model parsePomFile(final File pomFile) {
Model pom = null;
try (FileReader pomReader = new FileReader(pomFile)) {
pom = new MavenXpp3Reader().read(pomReader);
} catch (IOException | XmlPullParserException e) {
Assert.fail("Cannot parse POM");
}

return pom;
}

}
13 changes: 13 additions & 0 deletions src/test/resources/multi-module-project-stub/module/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.e-gineering</groupId>
<artifactId>gitflow-helper-maven-plugin-multi-module-parent-test-stub</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>module</artifactId>

</project>
63 changes: 63 additions & 0 deletions src/test/resources/multi-module-project-stub/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.e-gineering</groupId>
<artifactId>gitflow-helper-maven-plugin-multi-module-parent-test-stub</artifactId>
<version>1.0.0-SNAPSHOT</version>

<packaging>pom</packaging>

<repositories>
<repository>
<id>releases</id>
<url>file:${it.repository.basedir}/releases</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
<repository>
<id>test-releases</id>
<url>file:${it.repository.basedir}/test-releases</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
<repository>
<id>snapshots</id>
<url>file:${it.repository.basedir}/snapshots</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
</repositories>

<build>
<plugins>
<plugin>
<groupId>com.e-gineering</groupId>
<artifactId>gitflow-helper-maven-plugin</artifactId>
<version>${version.gitflow.plugin}</version>
<extensions>true</extensions>
<configuration>
<releaseDeploymentRepository>releases</releaseDeploymentRepository>
<stageDeploymentRepository>test-releases</stageDeploymentRepository>
<snapshotDeploymentRepository>snapshots</snapshotDeploymentRepository>
<otherDeployBranchPattern>(origin/)?feature/.*</otherDeployBranchPattern>
<otherBranchVersionDelimiter>-</otherBranchVersionDelimiter>
</configuration>
<executions>
<execution>
<goals>
<goal>set-properties</goal>
<goal>enforce-versions</goal>
<goal>retarget-deploy</goal>
<goal>promote-master</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<modules>
<module>module</module>
</modules>
</project>

0 comments on commit 9e6a257

Please sign in to comment.