-
Notifications
You must be signed in to change notification settings - Fork 3
3.0.1 snapshot #40
base: master
Are you sure you want to change the base?
3.0.1 snapshot #40
Changes from all commits
59d0ff9
70b9187
8cf70ea
fb2455d
bd7792d
99dec14
d97e243
7b16e1a
c00941e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cucumber.runtime.io; | ||
|
||
import static java.util.Arrays.asList; | ||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Мне кажется, здесь мы можем использовать метод isEmpty() вместо size() > 0 |
||
status = "Finished: FAILURE"; | ||
} | ||
LOG.warn(status); | ||
return runtime.exitStatus(); | ||
} | ||
} |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Мне кажется, здесь возможен npe в случае возникновения исключения в блоке: |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Мне кажется, стоит отсортировать методы по разделам: public -> protected -> private There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Здесь не совсем согласен. Считаю, что порядок методов должен быть Основываюсь на том, что если располагать методы придерживаясь правила "Если внутри метода используются другие методы, то они должны быть расположены в порядке их вызовов", тогда код класса читается как "рассказ" и не нприходится листать листинг туда сюда в поисках реализации используемых методов. |
||
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()); | ||
} | ||
|
||
} |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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('\\', '.'); | ||
} | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Мне кажется, этого статик импорта можно избежать.