Search in sources :

Example 1 with Nullable

use of org.spockframework.util.Nullable in project spock by spockframework.

the class ConfigurationScriptLoader method loadScriptFromClassPathLocation.

@Nullable
private DelegatingScript loadScriptFromClassPathLocation(String location) {
    URL url = this.getClass().getClassLoader().getResource(location);
    if (url == null)
        return null;
    GroovyShell shell = createShell();
    try {
        return (DelegatingScript) shell.parse(new GroovyCodeSource(url));
    } catch (IOException e) {
        throw new ConfigurationException("Error reading configuration script '%s'", location);
    } catch (CompilationFailedException e) {
        throw new ConfigurationException("Error compiling configuration script '%s'", location);
    }
}
Also used : ConfigurationException(spock.config.ConfigurationException) DelegatingScript(org.spockframework.builder.DelegatingScript) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException) URL(java.net.URL) Nullable(org.spockframework.util.Nullable)

Example 2 with Nullable

use of org.spockframework.util.Nullable in project spock by spockframework.

the class ConfigurationScriptLoader method loadScriptFromSystemPropertyInducedLocation.

@Nullable
private DelegatingScript loadScriptFromSystemPropertyInducedLocation(String propertyKey) {
    String location = System.getProperty(propertyKey);
    if (location == null || location.length() == 0)
        return null;
    DelegatingScript script = loadScriptFromClassPathLocation(location);
    if (script != null)
        return script;
    script = loadScriptFromFileSystemLocation(location);
    if (script != null)
        return script;
    throw new ConfigurationException("Cannot find configuration script '%s'", location);
}
Also used : ConfigurationException(spock.config.ConfigurationException) DelegatingScript(org.spockframework.builder.DelegatingScript) Nullable(org.spockframework.util.Nullable)

Example 3 with Nullable

use of org.spockframework.util.Nullable in project spock by spockframework.

the class ExpressionInfoValueRenderer method renderValue.

/**
   * Returns a string representation of a value, or <tt>null</tt> if
   * the value should not be shown (because it does not add any valuable
   * information). Note that the method may also change rendered values
   * of child expression.
   *
   * @param expr the expression whose value is to be rendered
   * @return a string representation of the value
   */
@Nullable
private String renderValue(ExpressionInfo expr) {
    Object value = expr.getValue();
    if (value == null)
        return "null";
    // value.equals() might throw exception, so we use "".equals() instead
    if ("".equals(value))
        return "\"\"";
    String str;
    try {
        str = doRenderValue(expr);
    } catch (Exception e) {
        return String.format("%s (renderer threw %s)", javaLangObjectToString(value), e.getClass().getSimpleName());
    }
    if (str == null || str.equals("")) {
        return javaLangObjectToString(value);
    }
    // only print enum values that add valuable information
    if (value instanceof Enum) {
        String text = expr.getText().trim();
        int index = text.lastIndexOf('.');
        String potentialEnumConstantNameInText = text.substring(index + 1);
        if (str.equals(potentialEnumConstantNameInText))
            return null;
    }
    return str;
}
Also used : GString(groovy.lang.GString) Nullable(org.spockframework.util.Nullable)

Example 4 with Nullable

use of org.spockframework.util.Nullable in project spock by spockframework.

the class ConfigurationScriptLoader method loadAutoDetectedScript.

@Nullable
public DelegatingScript loadAutoDetectedScript() {
    DelegatingScript script = loadScriptFromSystemPropertyInducedLocation(configPropertyKey);
    if (script != null)
        return script;
    script = loadScriptFromClassPathLocation(classPathLocation);
    if (script != null)
        return script;
    script = loadScriptFromFileSystemLocation(fileSystemLocation);
    if (script != null)
        return script;
    return null;
}
Also used : DelegatingScript(org.spockframework.builder.DelegatingScript) Nullable(org.spockframework.util.Nullable)

Example 5 with Nullable

use of org.spockframework.util.Nullable in project spock by spockframework.

the class ConfigurationScriptLoader method loadScriptFromFileSystemLocation.

@Nullable
private DelegatingScript loadScriptFromFileSystemLocation(String location) {
    File file = new File(location);
    try {
        if (!file.exists())
            return null;
    } catch (AccessControlException e) {
        // so let's just assume it's not there and continue
        return null;
    }
    GroovyShell shell = createShell();
    try {
        return (DelegatingScript) shell.parse(file);
    } catch (IOException e) {
        throw new ConfigurationException("Error reading configuration script '%s'", location);
    } catch (CompilationFailedException e) {
        throw new ConfigurationException("Error compiling configuration script '%s'", location);
    }
}
Also used : ConfigurationException(spock.config.ConfigurationException) DelegatingScript(org.spockframework.builder.DelegatingScript) AccessControlException(java.security.AccessControlException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException) File(java.io.File) Nullable(org.spockframework.util.Nullable)

Aggregations

Nullable (org.spockframework.util.Nullable)5 DelegatingScript (org.spockframework.builder.DelegatingScript)4 ConfigurationException (spock.config.ConfigurationException)3 IOException (java.io.IOException)2 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)2 GString (groovy.lang.GString)1 File (java.io.File)1 URL (java.net.URL)1 AccessControlException (java.security.AccessControlException)1