-
Notifications
You must be signed in to change notification settings - Fork 27
/
GherkinFactory.java
109 lines (95 loc) · 4.05 KB
/
GherkinFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* NoraUi is licensed under the license GNU AFFERO GENERAL PUBLIC LICENSE
*
* @author Nicolas HALLOUIN
* @author Stéphane GRILLON
*/
package com.github.noraui.gherkin;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.noraui.utils.Constants;
import com.github.noraui.utils.Context;
public class GherkinFactory {
/**
* Specific logger
*/
private static final Logger logger = LoggerFactory.getLogger(GherkinFactory.class);
private static final String DATA = "#DATA";
private static final String DATA_END = "#END";
/**
* Private constructor
*/
private GherkinFactory() {
}
/**
* @param filename
* name of input Gherkin file.
* @param lines
* is a table of data (line by line and without headers).
*/
public static void injectDataInGherkinExamples(String filename, List<String[]> lines) {
try {
if (!lines.isEmpty()) {
Path filePath = getFeaturePath(filename);
String fileContent = new String(Files.readAllBytes(filePath), Charset.forName(Constants.DEFAULT_ENDODING));
StringBuilder examples = new StringBuilder();
examples.append(" ");
for (int j = 0; j < lines.size(); j++) {
examples.append("|");
examples.append(j + 1);
for (String col : lines.get(j)) {
examples.append("|");
examples.append(col);
}
examples.append("|\n ");
}
fileContent = fileContent.replaceAll("(" + DATA + "\r?\n.*\r?\n)[\\s\\S]*(" + DATA_END + ")", "$1" + examples.toString() + "$2");
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath.toString()), Charset.forName(Constants.DEFAULT_ENDODING)));) {
bw.write(fileContent);
}
}
} catch (IOException e) {
logger.error("error GherkinFactory.injectDataInGherkinExamples()", e);
}
}
public static int getNumberOfGherkinExamples(String filename) {
return getExamples(filename).length;
}
public static String[] getExamples(String filename) {
try {
Path filePath = getFeaturePath(filename);
String fileContent = new String(Files.readAllBytes(filePath), Charset.forName(Constants.DEFAULT_ENDODING));
Pattern pattern = Pattern.compile(DATA + "([\\s\\S]*)" + DATA_END);
Matcher matcher = pattern.matcher(fileContent);
String lines = "";
if (matcher.find() && matcher.groupCount() == 1) {
lines = matcher.group(0);
}
String[] examples = lines.split("\\n");
// Return lines - #DATA - #END
return (examples.length > 2) ? Arrays.copyOfRange(examples, 1, examples.length - 1) : new String[] {};
} catch (IOException e) {
logger.error("error GherkinFactory.getExamples()", e);
}
return new String[] {};
}
private static Path getFeaturePath(String filename) {
int indexOfUnderscore = filename.lastIndexOf('_');
String path = indexOfUnderscore != -1
? Context.getResourcesPath() + Context.getScenarioProperty(filename.substring(0, indexOfUnderscore)) + filename.substring(0, indexOfUnderscore) + ".feature"
: Context.getResourcesPath() + Context.getScenarioProperty(filename) + filename + ".feature";
return Paths.get(path);
}
}