Skip to content

Commit 1584747

Browse files
committed
Implemented cucumber support
1 parent 087a07d commit 1584747

18 files changed

+455
-208
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#Target folder
22
**/target
3-
/test-shards
43
/${env.M3_REPO}
54

65
#IDE settings

pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,6 @@
7373
<scope>test</scope>
7474
</dependency>
7575

76-
<!-- Cucumber test execution -->
77-
<dependency>
78-
<groupId>io.cucumber</groupId>
79-
<artifactId>cucumber-core</artifactId>
80-
<version>RELEASE</version>
81-
</dependency>
82-
8376
<!-- Test dependencies -->
8477
<dependency>
8578
<groupId>junit</groupId>

src/main/java/com/telekom/gis/psa/test/shard/maven/plugin/AbstractShardCreatorMojo.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
public abstract class AbstractShardCreatorMojo extends AbstractShardMojo {
2424

2525
private ShardFileWriter writer;
26-
private TestFileReader reader;
2726

2827
@Parameter(property = "shard.create.shardCount", required = true)
2928
protected int shardCount;
@@ -37,15 +36,11 @@ public abstract class AbstractShardCreatorMojo extends AbstractShardMojo {
3736
@Parameter(property = "shard.create.testFolders")
3837
protected String[] testFolders;
3938

40-
@Parameter(property = "shard.create.pathToPackage", defaultValue = "java")
41-
protected String pathToPackage;
42-
4339
/**
4440
* Default constructor, initializes a reader for the test classes and a writer for the test shards.
4541
*/
4642
public AbstractShardCreatorMojo() {
4743
writer = new ShardFileWriter();
48-
reader = new TestFileReader();
4944
}
5045

5146
/**
@@ -60,6 +55,8 @@ public AbstractShardCreatorMojo() {
6055
public void createShards(String shardNamePattern, TestClassFileFilter fileFilter) throws MojoExecutionException, MojoFailureException {
6156
getLog().info("Start creation of " + shardNamePattern + " files.");
6257

58+
TestFileReader reader = getReader();
59+
6360
writer.clear();
6461
reader.setFilenameFilter(fileFilter);
6562

@@ -163,7 +160,5 @@ private void adjustSplitList(List<String[]> splitList, int i) {
163160
*
164161
* @return the test file reader
165162
*/
166-
public TestFileReader getReader() {
167-
return reader;
168-
}
163+
public abstract TestFileReader getReader();
169164
}

src/main/java/com/telekom/gis/psa/test/shard/maven/plugin/AbstractShardExecutorMojo.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/main/java/com/telekom/gis/psa/test/shard/maven/plugin/AbstractShardMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
public abstract class AbstractShardMojo extends AbstractMojo {
1717

18-
@Parameter(property = "tests.outputFolder", defaultValue = "${project.build.directory}/test-shards")
18+
@Parameter(property = "shard.outputFolder", defaultValue = "${project.build.directory}/test-shards")
1919
protected String outputFolder;
2020

2121
@Parameter(property = "project", readonly = true, defaultValue = "${project}")

src/main/java/com/telekom/gis/psa/test/shard/maven/plugin/TestShardCleanerMojo.java renamed to src/main/java/com/telekom/gis/psa/test/shard/maven/plugin/ShardCleanerMojo.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
*
1818
* @author Patrick Fischer patrick.fischer@qaware.de
1919
*/
20-
@Mojo(name = "shard-clean")
21-
public class TestShardCleanerMojo extends AbstractShardMojo {
20+
@Mojo(name = "clean-shards")
21+
public class ShardCleanerMojo extends AbstractShardMojo {
2222

2323
/**
2424
* The execution function for this goal.
@@ -33,7 +33,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
3333
return;
3434
}
3535

36-
File[] shardFiles = folder.listFiles(this::isShardFile);
36+
File[] shardFiles = folder.listFiles(((dir, name) -> ShardConstants.isShardFile(name)));
3737

3838
if(shardFiles == null){
3939
return;
@@ -46,10 +46,4 @@ public void execute() throws MojoExecutionException, MojoFailureException {
4646
}
4747
getLog().info("Deleted all test shards");
4848
}
49-
50-
private boolean isShardFile(File dir, String fileName) {
51-
return fileName.matches(ShardConstants.JUNIT_SHARD_REGEX) ||
52-
fileName.matches(ShardConstants.CUCUMBER_SHARD_REGEX);
53-
54-
}
5549
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright 2012, Deutsche Telekom AG, DTAG GHS GIS. All rights reserved.
3+
*/
4+
5+
package com.telekom.gis.psa.test.shard.maven.plugin;
6+
7+
import com.telekom.gis.psa.test.shard.maven.plugin.cucumber.CucumberFeatureFileManager;
8+
import com.telekom.gis.psa.test.shard.maven.plugin.utils.ShardConstants;
9+
import org.apache.maven.plugin.MojoExecutionException;
10+
import org.apache.maven.plugin.MojoFailureException;
11+
import org.apache.maven.plugins.annotations.Mojo;
12+
import org.apache.maven.plugins.annotations.Parameter;
13+
14+
import java.io.File;
15+
import java.util.Arrays;
16+
import java.util.List;
17+
18+
/**
19+
* The include mojo, loads one shard file an puts it into the surefire plugin
20+
*
21+
* @author Patrick Fischer patrick.fischer@qaware.de
22+
*/
23+
@Mojo(name = "execute-shard")
24+
public class ShardExecutorMojo extends AbstractShardMojo {
25+
26+
@Parameter(property = "shard.execute.cucumberWrapperClass")
27+
protected String cucumberWrapperClass;
28+
29+
@Parameter(property = "shard.execute.shardIndex", required = true)
30+
private int shardIndex;
31+
32+
/**
33+
* The execution function for this goal. It checks the given shardIndex and executes just the shard with the given
34+
* shard index
35+
*
36+
* @throws MojoFailureException if something wrong with the dependencies or sources of a the plugin
37+
* @throws MojoExecutionException if there is a problem in the properties
38+
*/
39+
@Override
40+
public void execute() throws MojoExecutionException, MojoFailureException {
41+
File outputFolder = new File(this.outputFolder);
42+
List<String> testShardPath = getTestShards(outputFolder);
43+
44+
if(shardIndex < 0 || shardIndex >= testShardPath.size()){
45+
throw new MojoExecutionException("Invalid shard number, (shard count: " + testShardPath.size() + "; index: " +
46+
shardIndex);
47+
}
48+
49+
String shardName = testShardPath.get(shardIndex);
50+
51+
if(shardName.matches(ShardConstants.CUCUMBER_SHARD_REGEX)){
52+
CucumberFeatureFileManager.disableOtherFeatures(outputFolder, shardName);
53+
54+
if(cucumberWrapperClass == null || cucumberWrapperClass.isEmpty()){
55+
throw new MojoExecutionException("Failed to include cucumber file: missing cucumber wrapper class, use" +
56+
" property <cucumberWrapperClass>");
57+
}
58+
59+
project.getProperties().setProperty("surefire.includes", cucumberWrapperClass);
60+
}else if(shardName.matches(ShardConstants.JUNIT_SHARD_REGEX)){
61+
project.getProperties().setProperty("surefire.includesFile", outputFolder + File.separator + shardName);
62+
}else{
63+
getLog().warn(shardName + " does not match any shard regex, the file will be ignored");
64+
return;
65+
}
66+
67+
getLog().info("Added test shard \"" + shardName + "\" to surefire.");
68+
}
69+
70+
private List<String> getTestShards(File outputFolder) throws MojoExecutionException {
71+
String[] testShardArray = outputFolder.list((dir, name) -> ShardConstants.isShardFile(name));
72+
if(testShardArray == null || testShardArray.length == 0){
73+
throw new MojoExecutionException("No test shards found, shards must be created first.");
74+
}
75+
76+
return Arrays.asList(testShardArray);
77+
}
78+
}

src/main/java/com/telekom/gis/psa/test/shard/maven/plugin/cucumber/CucumberExecuteMojo.java

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* Copyright 2012, Deutsche Telekom AG, DTAG GHS GIS. All rights reserved.
3+
*/
4+
5+
package com.telekom.gis.psa.test.shard.maven.plugin.cucumber;
6+
7+
import com.telekom.gis.psa.test.shard.maven.plugin.utils.ShardConstants;
8+
import org.apache.maven.plugin.MojoExecutionException;
9+
import org.apache.maven.plugin.MojoFailureException;
10+
11+
import java.io.BufferedReader;
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.nio.file.Files;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
18+
/**
19+
* The Cucumber feature file manager handles the .feature files. They can be disable by appending .ignore to the file
20+
* name. This manager renames the .feature files.
21+
*
22+
* @author Patrick Fischer patrick.fischer@qaware.de
23+
*/
24+
public final class CucumberFeatureFileManager {
25+
26+
private CucumberFeatureFileManager() {
27+
//No instances
28+
}
29+
30+
/**
31+
* Renames all cucumber shards, which where not used. The result file as the file type .ignore and will not be
32+
* recognized buy cucumber
33+
*
34+
* @param shardDirectory the directory of the test shards
35+
* @param shardName the name of the excluded cucumber shard
36+
* @throws MojoExecutionException if there is a problem in the properties
37+
*/
38+
public static void disableOtherFeatures(File shardDirectory, String shardName) throws MojoExecutionException {
39+
File[] otherShards = shardDirectory
40+
.listFiles((dir, name) -> name.matches(ShardConstants.CUCUMBER_SHARD_REGEX) && !name.equals(shardName));
41+
42+
if (otherShards == null) {
43+
throw new MojoExecutionException("Shard directory " + shardDirectory.getPath() + " does not exist.");
44+
}
45+
46+
for (File shard : otherShards) {
47+
disableAllFeaturesOf(shard);
48+
}
49+
}
50+
51+
private static void disableAllFeaturesOf(File shard) throws MojoExecutionException {
52+
List<String> featureFiles = getAllFeatureFileOf(shard);
53+
54+
for (String featureFileName : featureFiles) {
55+
File featureFile = new File(featureFileName);
56+
File disabledFeatureFile = new File(featureFile.getPath() + ".ignore");
57+
if (disabledFeatureFile.exists()) {
58+
throw new MojoExecutionException(
59+
"Failed to disable " + featureFile.getPath() + ": ignored feature already exists, " +
60+
"call the clean-cucumber-features goal to clean the feature files.");
61+
}
62+
63+
if (!featureFile.renameTo(disabledFeatureFile)) {
64+
throw new MojoExecutionException("Failed to disable " + featureFile.getPath() + ": renaming failed.");
65+
}
66+
}
67+
}
68+
69+
/**
70+
* Returns a list of all feature files in a cucumber shard file.
71+
*
72+
* @param shard the shard file
73+
* @return the list of the feature files
74+
* @throws MojoExecutionException if there is a problem in the properties
75+
*/
76+
public static List<String> getAllFeatureFileOf(File shard) throws MojoExecutionException {
77+
List<String> features = new ArrayList<>();
78+
BufferedReader reader = null;
79+
try {
80+
reader = Files.newBufferedReader(shard.toPath());
81+
82+
String line;
83+
while ((line = reader.readLine()) != null) {
84+
features.add(line);
85+
}
86+
} catch (IOException e) {
87+
throw new MojoExecutionException("Failed to read feature files.", e);
88+
} finally {
89+
if (reader != null) {
90+
try {
91+
reader.close();
92+
} catch (IOException ignore) {
93+
}
94+
}
95+
}
96+
return features;
97+
}
98+
99+
/**
100+
* Enables a disabled feature file by removing the .ignore file name ending
101+
*
102+
* @param file the feature file to enable
103+
* @throws MojoFailureException if something wrong with the dependencies or sources of a the plugin
104+
* @throws MojoExecutionException if there is a problem in the properties
105+
*/
106+
public static void enableFeatureFile(File file) throws MojoFailureException, MojoExecutionException {
107+
String fileName = file.getPath();
108+
fileName = fileName.substring(0, fileName.lastIndexOf('.'));
109+
110+
if (!fileName.endsWith(".feature")) {
111+
throw new MojoFailureException("Invalid ignored feature file " + file.getPath());
112+
}
113+
114+
File renamedFile = new File(fileName);
115+
if (renamedFile.exists()) {
116+
throw new MojoExecutionException(
117+
"Failed to enable " + file.getPath() + ", non-ignored file already exists.");
118+
}
119+
120+
if (!file.renameTo(renamedFile)) {
121+
throw new MojoExecutionException("Failed to enable " + file.getPath() + ": renaming failed.");
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)