Skip to content
This repository has been archived by the owner on Nov 15, 2020. It is now read-only.

3.0.1 snapshot #40

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
</repository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.14.RELEASE</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>ru.sbtqa.tag</groupId>
<artifactId>datajack</artifactId>
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/cucumber/runtime/io/CucumberStaticRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cucumber.runtime.io;

import static java.util.Arrays.asList;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Мне кажется, этого статик импорта можно избежать.


import java.io.IOException;
import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cucumber.runtime.ClassFinder;
import cucumber.runtime.Runtime;
import cucumber.runtime.RuntimeOptions;

public class CucumberStaticRunner {

private static String status = "\nFinished: SUCCESS";
private static final Logger LOG = LoggerFactory.getLogger(CucumberStaticRunner.class);

public static void startTests(String[] argv) throws Throwable {
byte exitstatus = run(argv, Thread.currentThread().getContextClassLoader());
System.exit(exitstatus);
}

public static byte run(String[] argv, ClassLoader classLoader) throws IOException {
RuntimeOptions runtimeOptions = new RuntimeOptions(new ArrayList<String>(asList(argv)));

ResourceLoader resourceLoader = new CustomMultiLoader(classLoader);
ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
Runtime runtime = new Runtime(resourceLoader, classFinder, classLoader, runtimeOptions);
runtime.run();
if (runtime.getErrors().size() > 0 || runtime.getSnippets().size() > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Мне кажется, здесь мы можем использовать метод isEmpty() вместо size() > 0

status = "Finished: FAILURE";
}
LOG.warn(status);
return runtime.exitStatus();
}
}
66 changes: 66 additions & 0 deletions src/main/java/cucumber/runtime/io/CustomMultiLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cucumber.runtime.io;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

public class CustomMultiLoader implements ResourceLoader {
public static final String CLASSPATH_SCHEME = "classpath*:";
public static final String CLASSPATH_SCHEME_TO_REPLACE = "classpath:";
private final ClasspathResourceLoader classpath;
private final FileResourceLoader fs;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я думаю, стоит придумать для этой переменной более читаемое имя.


public CustomMultiLoader(ClassLoader classLoader) {
classpath = new ClasspathResourceLoader(classLoader);
fs = new FileResourceLoader();
}

@Override
public Iterable<Resource> resources(String path, String suffix) {
if (isClasspathPath(path)) {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String locationPattern = path.replace(CLASSPATH_SCHEME_TO_REPLACE, CLASSPATH_SCHEME) + "/**/*" + suffix;
org.springframework.core.io.Resource[] resources;
try {
resources = resolver.getResources(locationPattern);
} catch (IOException e) {
resources = null;
e.printStackTrace();
}
return convertToCucumberIterator(resources);
} else {
return fs.resources(path, suffix);
}
}

private Iterable<Resource> convertToCucumberIterator(org.springframework.core.io.Resource[] resources) {
List<Resource> results = new ArrayList<Resource>();
for (org.springframework.core.io.Resource resource : resources) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Мне кажется, здесь возможен npe в случае возникновения исключения в блоке:
try {
resources = resolver.getResources(locationPattern);
} catch (IOException e) {
resources = null;
e.printStackTrace();
}
и последующего вызова метода convertToCucumberIterator(resources)

results.add(new ResourceAdapter(resource));
}
return results;
}

public static String packageName(String gluePath) {
if (isClasspathPath(gluePath)) {
gluePath = stripClasspathPrefix(gluePath);
}
return gluePath.replace('/', '.').replace('\\', '.');
}

private static boolean isClasspathPath(String path) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Мне кажется, стоит отсортировать методы по разделам: public -> protected -> private

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь не совсем согласен. Считаю, что порядок методов должен быть
resources
isClasspathPath
convertToCucumberIterator
packageName
stripClasspathPrefix

Основываюсь на том, что если располагать методы придерживаясь правила "Если внутри метода используются другие методы, то они должны быть расположены в порядке их вызовов", тогда код класса читается как "рассказ" и не нприходится листать листинг туда сюда в поисках реализации используемых методов.

if (path.startsWith(CLASSPATH_SCHEME_TO_REPLACE)) {
path = path.replace(CLASSPATH_SCHEME_TO_REPLACE, CLASSPATH_SCHEME);
}
return path.startsWith(CLASSPATH_SCHEME);
}

private static String stripClasspathPrefix(String path) {
if (path.startsWith(CLASSPATH_SCHEME_TO_REPLACE)) {
path = path.replace(CLASSPATH_SCHEME_TO_REPLACE, CLASSPATH_SCHEME);
}
return path.substring(CLASSPATH_SCHEME.length());
}

}
54 changes: 54 additions & 0 deletions src/main/java/cucumber/runtime/io/ResourceAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cucumber.runtime.io;

import java.io.IOException;
import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ResourceAdapter implements Resource {
org.springframework.core.io.Resource springResource;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А что не так с этим типом? Почему везде фкдн? Мне кажется стоит просто импортнуть

private static final Logger LOG = LoggerFactory.getLogger(ResourceAdapter.class);

public ResourceAdapter(org.springframework.core.io.Resource springResource) {
this.springResource = springResource;
}

public String getPath() {
try {
return springResource.getFile().getPath();
} catch (IOException e) {
try {
return springResource.getURL().toString();
} catch (IOException e1) {
LOG.error("Ошибка работы с файлом: " + e1);
return "";
}
}
}

public String getAbsolutePath() {
try {
return springResource.getFile().getAbsolutePath();
} catch (IOException e) {
return null;
}
}

public InputStream getInputStream() throws IOException {
return this.springResource.getInputStream();
}

public String getClassName(String extension) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лишняя строка

String path = this.getPath();
if (path.startsWith("jar:")) {
path = path.substring(path.lastIndexOf("!") + 2);
return path.substring(0, path.length() - extension.length()).replace('/', '.');
} else {
path = path.substring(path.lastIndexOf("classes") + 8);
return path.substring(0, path.length() - extension.length()).replace('\\', '.');
}

}
}
Loading