Groovy resource from classpath not loaded - groovy

Good day everyone.
I am using spock framework for testing in my groovy project(IDE - Intellij Idea 12.6). My spock specification class pass filename to groovy object for processing (that file is in classpath for sure), but when i try to get that file this way
def resource = getClass().getClassloader().getResourceAsStream(filepath)
assert resource != null : "No input stream found for path ${filepath}"
def rootNode = new XmlParser().parse(resource)
Then resource == null.
I tried debugging and in Expression Evaluation windows this code getClass().getResource(fileName) returns resource.
I tried to check which classloader used in first case (in class with the code) and in second case (Expression Evaluation window).
In first case classloader was sun.misc.Launcher$AppClassLoader#18dabf1, but in Expression Evaluation window classloader was groovy.lang.GroovyClassLoader$InnerLoader#1e69757 I suppose that's the reason my resource was null.
Can someone guide me about what I am doing wrong and how can I load that resource file ?
UPDATE:
Changed the way resource file was parsed. When filepath - full path to file this works, but if filepath is just file name and that file in classpath then resource == null
UPDATE2:
Change the way resource file loaded, clean up dependencies bit and all is working, I guess yesterday just wasn't my day.

The problem is very likely unrelated to Spock. It's hard to say from a distance what's causing it, but the safest way to read a resource is getClass().getClassLoader().getResourceAsStream() or Thread.currentThread().getContextClassLoader().getResourceAsStream(), depending on the environment.
Not sure what Groovy does when you do new File(resource), as there is no File(URL) constructor (only a File(URI) constructor). In any case, getting a File from a class path should be avoided whenever possible.

This is likely due to the fact that Groovy may interpret the class of the object differently that what you think is happening. See the following other StackOverflow item:
Why does groovy .class return a different value than .getClass()
When the class is wrong, then the ClassLoader may well by the bootstrap loader and getClassLoader returns null.
So instead of using a statement like
def resource = getClass().getClassloader().getResourceAsStream(filepath)
specify the actual class using a statement like
def resource = MyClass.class.getClassLoader().getResourceAsStream(filePath)
worked for me in nearly identical circumstances.

def resource = MyClass.class.getResourceAsStream(fileName)
or if you want the content of the file as String:
def str = new String(MyClass.class.getResourceAsStream(fileName).readAllBytes(), StandardCharsets.UTF_8)
Please note:
MyClass is used, not this.getClass();
In resources you must create the same directory structure as the package of your class, and put the files there;
fileName is just simply the name of the file, without any path;
You must clean and rebuild your project.

Related

Xpages runtime interpreting java package name as String object

From all the weirdness in our current Xpages project this one currently hits the ceiling:
we have created a few java beans in our current project. Inside Domino Designer they all are stored below Code >> Java, so that it is clear that they are automatically part of the project's classpath. All our beans belong to a package structure de.edcom.* (that's what we have been using forever without any problems). The objects are mostly called from SSJS using the full package names (the aren't registered as managed beans for various reasons) as in
var o = de.edcom.myObject.someMethod();
In none of my previous Xpages projects this caused any problems, it just worked. In the current project, however the XSP runtime all of a sudden started to interpret the package name as a String object giving us this runtime error:
Unknown member 'edcom' in Java class 'java.lang.String'
the ssjs code line in question is looking like this:
return de.edcom.TOC.buildTOC();
We absolutely don't have any clue as to what could be causing this, why only in this project, and why it sometimes IS working, but mostly isn't.
There's one difference between this projects and others before, and that is locallization: users can switch between "english" and "german" locale, and of course we are using codes like
context.setLocaleString("de")
and of course we are having several javascript code fragments looking for local settings as in
if(context.getLocalString()==="de"){...
This morning we in fact have renamed / refactored all java beans to different package names (com.edcom.*), and since then the error hasn't appeared (fingers crossed!).
But then again I think this is just too stupid, there can't really be a connection, or can it?
EDIT:
I tried using importPackage(), in conjunction with an xe:objectData datasource (as recommended by Adrian and Paul in their answers), but I'm still receiving that "unknown member 'edcom' in Java class 'java.lang,String'" message, now only at a different position in the code at my line saying importPackage(de.edcom).
I'll be switching back to the "com.edcom" package and keep looking for a better solution; unfortunately searching for the string "de" inside the entire code yields close to 12.000 matches; now way to find the real reason for this in that haystack
EDIT #2:
looks like we finally found the dreaded "de" variable: it was well hidden in a computed customControl property; I don't have a clue why all the File Searches that I performed over the last few days couldn't find this one.
Anyways it is very good to know that we have to be even more careful when naming our ssjs variables; I never would have thought that a ssjs variable name could ever interfere with TLD parts in Java packages; we probably will make it an internal policy that our variables have to must be named "vDe", "vCom", "vIt" etc. instead of just short lowercase letters...
Probably you used a variable de (which is a String) in an other SSJS script that run before that one faces the problem.
I've seen similar issues that a variable that is not explicitly declared in an script block can inherit values from another script block.
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:this.beforeRenderResponse><![CDATA[#{javascript:
var ex1 = "Hello World";
var ex2 = "Bye bye"}]]>
</xp:this.beforeRenderResponse>
<xp:this.afterRenderResponse><![CDATA[#{javascript:
print("value ex1: " + ex1);
print("value ex2: " + ex2);}]]>
</xp:this.afterRenderResponse>
</xp:view>
results in:
[1CA8:000C-4354] 10.06.2016 14:33:01 HTTP JVM: value ex1: Hello World
[1CA8:000C-4354] 10.06.2016 14:33:01 HTTP JVM: value ex2: Bye bye
So you should use the importPackage() function to import the references to your java classes or much better, use managed beans or dataContexts.
SSJS requires variables which will be put in a scope. Anything with a dot in it will first go to those variables. It sounds like localization stores the translations in a variable named "de", that would explain your problem.
Maybe importPackage(de.edcom) and then using return TOC.buildTOC(); would resolve the problem. I would consider that better practice, but either way in SSJS you're risking variable name collisions.
Personally, I prefer to back every XPage with a controller Java class (I use Jesse Gallagher's frostillicus framework, and so it's always accessible with the variable pageController), so my SSJS just calls pageController.myMethod(), which then avoids all name collisions and allows Java imports to ensure I map to the right Java class. There are more basic ways of doing it, e.g. with an xe:dataObject at the top of every XPage.

Append content from file using Email Ext Jenkins plugin

I have been modifying the default groovy template that the Email Ext plugin supplies.
Firstly, I had to modify the JUnitTestResult and need to format it accordingly to my need. I found in the it.JUnitTestResult, it is a reference to the ScriptContentBuildWrapper class. And then I was able to format the JUnitTestResult according to my need.
Now I am facing a second difficulty:
Along with those contents, I need to append more content from a file that resides in the job workspace. How to access the files that reside in the workspace directory.
I would be interested to know how I can access the build context object. Whats the java class name and things like that.
Just use build which returns an AbstractBuild
Try -
build.workspace
Which returns the FilePath of the directory where the build is being built.
See AbstractBuild.getWorkspace.
Tip: in Groovy, you can avoid the "get" and use field-like access notation.
Depending on which version of email-ext you are using, you can use the tokens provided to get access to things, so if you look at the token help, you'll see lots of tokens. These can be used in the groovy templates to do the same thing. For instance, the FILE token can be used in the Groovy by doing FILE(path: 'path/to/file') and it will replace with the contents of the file (only works on files that are below the workspace).
The build object is not available directly in all groovy scripts (e.g. groovy build script, groovy system build script, groovy post-build script, groovy script as evaluated in email-ext). The most portable way of obtaining build object in groovy script for a running build is:
import hudson.model.*
def build = Thread.currentThread().executable
Then you can get workspace and access files inside like this:
workspace = build.getEnvVars()["WORKSPACE"]
afilename = workspace + "/myfile"
afile = new File(afilename);
// afile.write "write new file"
// afile << "append to file"
// def lines = afile.readLines()

Cucumber options annotation

The cucumber-jvm javadocs states that purpose of the glue element is to specify the location of the stepdefinitions and hooks. However, this doesn't seem to work for me. Lets say I have my features in directory a, and my step definitions in directory b. Then,
#Cucumber.Options(
features= "directory_a",
glue="directory_b"
)
will load my feature files from directory_a, but, it doesn't load my step definitions from directly_b. However, if I use
#Cucumber.Options(
features= {"directory_a", "directory_b"}
)
then my features from directory_a is loaded, and my step definitions from directory_b are also picked up. Which is exactly what I want, however, I don't understand why the former isn't working? I'm guessing it has something to do with it expecting the URI to be formatted differently (maybe i need to prepend a classpath:// or something like that), but I can't find any information on this in the documentation.
I have successfully used something like:
#RunWith(Cucumber.class)
#Cucumber.Options(
//this code will only look into "features/" folder for features
features={"classpath:features/"},
glue = { "com.mycompany.cucumber.stepdefinitions", "com.mycompany.cucumber.hooks" },
format = { "com.mycompany.cucumber.formatter.RuntimeInfoCatcher", "json:target/cucumber.json" },
tags = { "#working" }
)
public class CucumberStarterIT {
}
Looking at the doc at http://cukes.info/api/cucumber/jvm/javadoc/cucumber/api/junit/Cucumber.Options.html it specifies the options to be of type String[] so perhaps it's not expected to work "well" if you don't give it a single-value list. Try glue={"directory_b"} and see what happens for you.
I had this problem too... and so far it seems to be that:
"features" is looking for a filesystem path:
features = "src/foo/bar"
whereas "glue" is looking for a package name:
glue = "foo.bar"
Not sure why they are different, but this seems to be working for me.
Hi as per my knowledge it all depends on the structure of you project. For example if you add the "Directory_a" ( directory which contains feature files) in the root level and StepDefinition, Hooks at src > test > java "Directory_b" And the TestRunner class at the same level ( src > test > java ) in "Directory_c"
Dir_a
|
src
|---main
|---test
|------java
|------Dir_b
|------Dir_c
You saying "Dir_b" while you are in the "Dir_c" It will identify "Dir_b" or any directory in same level with out any additional paths so,
It will be
glue = {"Dir_b"},
But when you look at the directory that includes feature file you have to give the path from the root level
In this case it's
features = {"Dir_a"}
or Giving the actual path eg :- "E://Project_Name//Dir_a" should work too
If your feature directory is NOT in root level make sure you give the path like "src/path to feature directory"
It will work fine :)

Debugging Groovy scripts running in a ScriptEngine from IDEA

In my app, I load a script file from the classpath into a String, then pass it into the ScriptEngine. Howerver, the breakpoint set in the script file doesn't trigger. How can I make it work? I use Intellij IDEA.
ScriptEngine engine = ...;
String script = FileUtils.readFileToString(file);
Bindings bindings = engine.createBindings();
Object result = engine.eval(script, bindings);
Since the ScriptEngine.eval() method only takes the script as a String or as a generic Reader, I don't think it is possible to achieve this. The GroovyScriptEngineImpl class will generate a script name and compile it to a class at runtime, which will make it hard (impossible?) for the debugger to know which breakpoint(s) are associated with the running script.
It might not be a solution for you, but if you instead invoke the script using GroovyShell, then it pretty much works out of the box.
Example:
File file = new File(scriptDir, "ScriptToRun.groovy");
Binding binding = new Binding();
Object result = new GroovyShell(binding).evaluate(file);
Just remember to set the correct package in the script if it is not located at the root.

ClassLoader.getSystemResource(...).getPath() seems to return wrong path

I'm trying to wrap code that requires two *.db4o data files for easy use. I've added the data files to my eclipse .classpath by placing the files in ${project_dir}/res/ and adding the line:
<classpathentry kind="src" path="res"/>
to my .classpath.
I then defined a default constructor to my wrapper class that takes no arguments but goes and finds the paths to the *.db4o files (the paths are required by the compiled code I'm using to set things up). My approach for getting the paths is:
String datapath = ClassLoader.getSystemResource("resource_name").getPath();
This works great when I debug/run my code in eclipse. However when I export it as a jar, I can see that the *.db4o files are in the jar, as well as my compiled code, but the path returned to "datapath" is of the form:
datapath = ${pwd}/file:${absolute_path_to_jar}!/{resource_name}
Is there something about the resource being inside of the jar that prevents an absolute path from working? Also, why is the behavior different simply because the code and resources live in a jar file? One last note is that while my application is intended for wider use (from PIG, python, etc. code) I'm testing it from Matlab which is where I'm getting the odd value assigned to "datapath".
Thanks in advance for any responses.
getSystemResource() returns URL to resource. If your resource is zipped in a jar file then the URL will point into it (with the "!" notation). getPath() returns the "path" part of the URL, not always an actual file path. URL can be one of many things, not just a file.

Resources