PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/main/java/cucumber/runtime/FeatureBuilder.java

https://gitlab.com/chiavegatto/cucumber-jvm
Java | 163 lines | 134 code | 25 blank | 4 comment | 6 complexity | 2584bf3218b3e7f2b71d78fa279ff6e3 MD5 | raw file
  1. package cucumber.runtime;
  2. import cucumber.runtime.io.Resource;
  3. import cucumber.runtime.model.CucumberFeature;
  4. import gherkin.I18n;
  5. import gherkin.formatter.FilterFormatter;
  6. import gherkin.formatter.Formatter;
  7. import gherkin.formatter.model.Background;
  8. import gherkin.formatter.model.Examples;
  9. import gherkin.formatter.model.Feature;
  10. import gherkin.formatter.model.Scenario;
  11. import gherkin.formatter.model.ScenarioOutline;
  12. import gherkin.formatter.model.Step;
  13. import gherkin.lexer.Encoding;
  14. import gherkin.parser.Parser;
  15. import gherkin.util.FixJava;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.io.InputStreamReader;
  19. import java.math.BigInteger;
  20. import java.nio.charset.Charset;
  21. import java.security.MessageDigest;
  22. import java.security.NoSuchAlgorithmException;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. public class FeatureBuilder implements Formatter {
  27. private static final Charset UTF8 = Charset.forName("UTF-8");
  28. private final List<CucumberFeature> cucumberFeatures;
  29. private final char fileSeparatorChar;
  30. private final MessageDigest md5;
  31. private final Map<String, String> pathsByChecksum = new HashMap<String, String>();
  32. private CucumberFeature currentCucumberFeature;
  33. private String featurePath;
  34. public FeatureBuilder(List<CucumberFeature> cucumberFeatures) {
  35. this(cucumberFeatures, File.separatorChar);
  36. }
  37. FeatureBuilder(List<CucumberFeature> cucumberFeatures, char fileSeparatorChar) {
  38. this.cucumberFeatures = cucumberFeatures;
  39. this.fileSeparatorChar = fileSeparatorChar;
  40. try {
  41. this.md5 = MessageDigest.getInstance("MD5");
  42. } catch (NoSuchAlgorithmException e) {
  43. throw new CucumberException(e);
  44. }
  45. }
  46. @Override
  47. public void uri(String uri) {
  48. this.featurePath = uri;
  49. }
  50. @Override
  51. public void feature(Feature feature) {
  52. currentCucumberFeature = new CucumberFeature(feature, featurePath);
  53. cucumberFeatures.add(currentCucumberFeature);
  54. }
  55. @Override
  56. public void background(Background background) {
  57. currentCucumberFeature.background(background);
  58. }
  59. @Override
  60. public void scenario(Scenario scenario) {
  61. currentCucumberFeature.scenario(scenario);
  62. }
  63. @Override
  64. public void scenarioOutline(ScenarioOutline scenarioOutline) {
  65. currentCucumberFeature.scenarioOutline(scenarioOutline);
  66. }
  67. @Override
  68. public void examples(Examples examples) {
  69. currentCucumberFeature.examples(examples);
  70. }
  71. @Override
  72. public void step(Step step) {
  73. currentCucumberFeature.step(step);
  74. }
  75. @Override
  76. public void eof() {
  77. }
  78. @Override
  79. public void syntaxError(String state, String event, List<String> legalEvents, String uri, Integer line) {
  80. }
  81. @Override
  82. public void done() {
  83. }
  84. @Override
  85. public void close() {
  86. }
  87. @Override
  88. public void startOfScenarioLifeCycle(Scenario scenario) {
  89. // NoOp
  90. }
  91. @Override
  92. public void endOfScenarioLifeCycle(Scenario scenario) {
  93. // NoOp
  94. }
  95. public void parse(Resource resource, List<Object> filters) {
  96. String gherkin = read(resource);
  97. String checksum = checksum(gherkin);
  98. String path = pathsByChecksum.get(checksum);
  99. if (path != null) {
  100. return;
  101. }
  102. pathsByChecksum.put(checksum, resource.getPath());
  103. Formatter formatter = this;
  104. if (!filters.isEmpty()) {
  105. formatter = new FilterFormatter(this, filters);
  106. }
  107. Parser parser = new Parser(formatter);
  108. try {
  109. parser.parse(gherkin, convertFileSeparatorToForwardSlash(resource.getPath()), 0);
  110. } catch (Exception e) {
  111. throw new CucumberException(String.format("Error parsing feature file %s", convertFileSeparatorToForwardSlash(resource.getPath())), e);
  112. }
  113. I18n i18n = parser.getI18nLanguage();
  114. if (currentCucumberFeature != null) {
  115. // The current feature may be null if we used a very restrictive filter, say a tag that isn't used.
  116. // Might also happen if the feature file itself is empty.
  117. currentCucumberFeature.setI18n(i18n);
  118. }
  119. }
  120. private String convertFileSeparatorToForwardSlash(String path) {
  121. return path.replace(fileSeparatorChar, '/');
  122. }
  123. private String checksum(String gherkin) {
  124. return new BigInteger(1, md5.digest(gherkin.getBytes(UTF8))).toString(16);
  125. }
  126. public String read(Resource resource) {
  127. try {
  128. String source = FixJava.readReader(new InputStreamReader(resource.getInputStream(), "UTF-8"));
  129. String encoding = new Encoding().encoding(source);
  130. if (!"UTF-8".equals(encoding)) {
  131. source = FixJava.readReader(new InputStreamReader(resource.getInputStream(), encoding));
  132. }
  133. return source;
  134. } catch (IOException e) {
  135. throw new CucumberException("Failed to read resource:" + resource.getPath(), e);
  136. }
  137. }
  138. }