Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Nov 22, 2019
1 parent 9ab1a94 commit e850333
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import net.thucydides.core.guice.Injectors;
import net.thucydides.core.issues.IssueTracking;
import net.thucydides.core.util.EnvironmentVariables;
import net.thucydides.core.util.Inflector;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -136,5 +138,4 @@ public int compare(String o1, String o2) {
});
return sortedIssues;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ private java.util.Optional<Requirement> mostSpecificTagRequirementFor(TestOutcom

private java.util.Optional<Requirement> requirementWithMatchingPath(TestOutcome testOutcome) {

Path testOutcomeRequirementsPath = RootDirectory.definedIn(environmentVariables).getRelativePathOf(testOutcome.getPath());//.asRelativePathOn(requirementsDirectoryPaths);
Path testOutcomeRequirementsPath = RootDirectory.definedIn(environmentVariables).getRelativePathOf(testOutcome.getPath());

Optional<Requirement> requirementWithMatchingPath = AllRequirements.asStreamFrom(getRequirements())
.filter(requirement -> requirementHasPathMatching(requirement, testOutcomeRequirementsPath)).findFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* This is normally src/test/resources/features or src/test/resources/stories. For multi-module projects, it
* can be a directory with this name in one of the modules. There should only be one requirements directory in a
* multi-module project. The easiest approach is to have a dedicated module for the acceptance tests.
*
* <p>
* You can hard-code this directory using serenity.requirements.dir. Milage may vary for multi-module projects.
* If you need to override the root directory (e.g. to use src/test/resources/myFeatures), a better way is to
* set the serenity.features.directory (for Cucumber) or serenity.stories.directory (for JBehave) property to
Expand Down Expand Up @@ -86,8 +86,9 @@ public Set<String> requirementsDirectoryNames() {
}

public static RootDirectory definedIn(EnvironmentVariables environmentVariables) {
return new RootDirectory(environmentVariables,".");
return new RootDirectory(environmentVariables, ".");
}

/**
* Find the root directory in the classpath or on the file system from which the requirements will be read.
*/
Expand Down Expand Up @@ -245,7 +246,7 @@ public Optional<Path> featuresOrStoriesRootDirectory() {
}
List<File> resourceDirectories = getResourceDirectories(Paths.get(relativeRoot), environmentVariables);
for (File resourceDir : resourceDirectories) {
for(String candidateDirectoryName : requirementsDirectoryNames) {
for (String candidateDirectoryName : requirementsDirectoryNames) {
if (new File(resourceDir, candidateDirectoryName).exists()) {
return Optional.of(resourceDir.toPath().resolve(candidateDirectoryName));
}
Expand All @@ -271,7 +272,7 @@ private static List<File> getResourceDirectories(Path root, EnvironmentVariables
.map(Path::toFile)
.collect(Collectors.toList());
}
RESOURCE_DIRECTORY_CACHE.put(root,results);
RESOURCE_DIRECTORY_CACHE.put(root, results);

return results;
}
Expand All @@ -297,24 +298,32 @@ private static List<Path> listDirectories(Path path) {
* Don't bother looking for src/test/resources folders in directories with names like these
*/
private final static List<Predicate<Path>> IGNORED_DIRECTORIES = new ArrayList<>();

static {
IGNORED_DIRECTORIES.add( path -> path.getFileName().toString().startsWith("."));
IGNORED_DIRECTORIES.add( path -> path.getFileName().toString().equals("target"));
IGNORED_DIRECTORIES.add( path -> path.getFileName().toString().equals("build"));
IGNORED_DIRECTORIES.add( path -> path.getFileName().toString().equals("out"));
IGNORED_DIRECTORIES.add( path -> path.getFileName().toString().equals("java"));
IGNORED_DIRECTORIES.add( path -> path.getFileName().toString().equals("scala"));
IGNORED_DIRECTORIES.add( path -> path.getFileName().toString().equals("groovy"));
IGNORED_DIRECTORIES.add( path -> path.getFileName().toString().equals("kotlin"));
IGNORED_DIRECTORIES.add( path -> path.getFileName().toString().equals("features"));
IGNORED_DIRECTORIES.add( path -> path.getFileName().toString().equals("stories"));
IGNORED_DIRECTORIES.add(path -> path.getFileName().toString().startsWith("."));
IGNORED_DIRECTORIES.add(path -> path.getFileName().toString().equals("target"));
IGNORED_DIRECTORIES.add(path -> path.getFileName().toString().equals("build"));
IGNORED_DIRECTORIES.add(path -> path.getFileName().toString().equals("out"));
IGNORED_DIRECTORIES.add(path -> path.getFileName().toString().equals("java"));
IGNORED_DIRECTORIES.add(path -> path.getFileName().toString().equals("scala"));
IGNORED_DIRECTORIES.add(path -> path.getFileName().toString().equals("groovy"));
IGNORED_DIRECTORIES.add(path -> path.getFileName().toString().equals("kotlin"));
IGNORED_DIRECTORIES.add(path -> path.getFileName().toString().equals("features"));
IGNORED_DIRECTORIES.add(path -> path.getFileName().toString().equals("stories"));
}

private static boolean isResourceDirectoryCandidate(Path entry) {

try {
if (entry.toString().isEmpty()) { return true; }
if (!isDirectory(entry)) { return false; }
if (isHidden(entry)) { return false; }
if (entry.toString().isEmpty()) {
return true;
}
if (!isDirectory(entry)) {
return false;
}
if (isHidden(entry)) {
return false;
}

return IGNORED_DIRECTORIES.stream().noneMatch(
shouldIgnore -> shouldIgnore.test(entry)
Expand All @@ -326,24 +335,28 @@ private static boolean isResourceDirectoryCandidate(Path entry) {
}


public Path getRelativePathOf(String path) {
if (path == null) {
return Paths.get("");
}
for (String requirementsDirectory : requirementsDirectoryNames) {
if (path.startsWith("classpath:" + requirementsDirectory + "/")) {
return Paths.get(path.substring(requirementsDirectory.length() + 11));
}
Path getRelativePathOf(String path) {
if (path == null) {
return Paths.get("");
}
for (String requirementsDirectory : requirementsDirectoryNames) {
if (path.startsWith("classpath:" + requirementsDirectory + "/")) {
return Paths.get(path.substring(requirementsDirectory.length() + 11));
} else if (relativePathFromAbsolutePath(path, requirementsDirectory).isPresent()) {
return relativePathFromAbsolutePath(path, requirementsDirectory).get();
}
if (path.startsWith("classpath:")) {
return Paths.get(path.substring(11));
} else if (path.startsWith("file:///")) {
return Paths.get(path.substring(8));
} else if (path.startsWith("file://")) {
return Paths.get(path.substring(7));
} else if (path.startsWith("file:/")) {
return Paths.get(path.substring(6));
}
return Paths.get(path);
}

private Optional<Path> relativePathFromAbsolutePath(String absolutePath, String requirementsDirectory) {
if (absolutePath.startsWith("file:/")) {
String requirementsDirectoryInPath = "/" + requirementsDirectory + "/";
if (absolutePath.contains(requirementsDirectoryInPath)) {
int startOfRelativePath = absolutePath.lastIndexOf(requirementsDirectoryInPath) + requirementsDirectoryInPath.length();
return Optional.of(Paths.get(absolutePath.substring(startOfRelativePath)));
}
return Paths.get(path);
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ public String capitalize( String words) {
public String humanize( String lowerCaseAndUnderscoredWords,
String... removableTokens ) {

if (isCamelCase(lowerCaseAndUnderscoredWords)) {
lowerCaseAndUnderscoredWords = underscore(lowerCaseAndUnderscoredWords);
}
String result = humanReadableFormOf(lowerCaseAndUnderscoredWords, removableTokens);

Set<Acronym> acronyms = Acronym.acronymsIn(result);
Expand All @@ -203,6 +206,13 @@ public String humanize( String lowerCaseAndUnderscoredWords,
return StringUtils.capitalize(result);
}

private final static Pattern LOWER_CAMEL_CASE = Pattern.compile("[a-z]+((\\d)|([A-Z0-9][a-z0-9]+))*([A-Z])?");
private final static Pattern UPPER_CAMEL_CASE = Pattern.compile("([A-Z][a-z0-9]+)((\\d)|([A-Z0-9][a-z0-9]+))*([A-Z])?");

private boolean isCamelCase(String text) {
return LOWER_CAMEL_CASE.matcher(text).matches() || UPPER_CAMEL_CASE.matcher(text).matches();
}

private String humanReadableFormOf(String lowerCaseAndUnderscoredWords,
String... removableTokens) {
if (lowerCaseAndUnderscoredWords == null) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@
<#assign outcome_icon = formatter.resultIcon().forResult(scenario.result) />
<tr>
<td style="width:95%;" class="toc-title">
<a href="#${scenario.id}" title="View scenario details" >${scenario.simplifiedName}</a>
<a href="#${scenario.id}" title="View scenario details" >${formatter.humanReadableFormOf(scenario.simplifiedName)}</a>
<#if scenario.hasExamples() >
(${scenario.numberOfExamples})</#if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,23 @@ public String humanReadableFormOf(String text) {
return Inflector.inflection().humanize(text);
}

public String humanReadableTitle(String text) {
if (isAClassOrMethodName(text)) {
return Inflector.inflection().of(text).inHumanReadableForm().startingWithACapital().toString();
} else {
return text;
}
}

private boolean isAClassOrMethodName(String text) {
if (StringUtils.containsWhitespace(text)) { return false; }
if (StringUtils.isEmpty(text)) { return false; }
if (StringUtils.isAllUpperCase(text)) {
return false;
}
return true;
}

public String formatWithFields(String textToFormat) {
String textWithEscapedFields = textToFormat.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;");
Expand Down

0 comments on commit e850333

Please sign in to comment.