How to load Spring's application context with a Fitnesse test fixture? - java

I have this test fixture I want to run with Fitnesse and it involves using Spring. I haven't been able to load the spring application context with the ClasspathXmlApplicationContext, and I'm sure is a classpath configuration thing that I just haven't figured it out.
So here's my setup.
As you can see, the fitnesse.jar is inside the project, so I can run the fitnesse server and the tests anywhere I have a working copy of the project (all the wiki files are inside the FitNesseRoot folder). The bin is the output folder of the project (where all the .class are).
Now, the code:
InventarioQueryTest (wiki page from Fitnesse, notice the !path declarations)
!define TEST_SYSTEM {slim}
!path ../bin
!path ../web/WEB-INF/lib/**.jar
|import |
|com.softclear.inventario.test.fitnesse|
|Query:listar status |
|id|nombre|entidad|descripcion|
ListarStatus.java (the text fixture invoked by Fitnesse)
public class ListarStatus {
private ServicioStatus serv;
private ClassPathXmlApplicationContext ctx;
//fitnesse calls the constructor
public ListarStatus(){
ctx = new ClassPathXmlApplicationContext(new String[] {
"applicationContext-ListarStatus.xml",
"applicationContext-dao.xml",
"applicationContext-hibernate.xml"});
//performs dependency injection of DAO and HibernateSession
serv = (ServicioStatus) ctx.getBean("servicioStatus");
}
//and the query method is the test
public List<Object> query() {
List<Status> lista = serv.listarStatus();
QueryResultBuilder builder = new QueryResultBuilder(Status.class);
QueryResult result = builder.build(lista.iterator());
return result.render();
}
}
I run fitnesse with java -jar from the working copy of my project. And when I run the test, this is the command that fitnesse executes:
java -cp fitnesse.jar;../bin;C:\Dev\WS\softclear\SistemaInventario\fitnesse\..\web\WEB-INF\lib\ajax\AjaxFileUpload-0.03.jar;%the.rest.of.the.web-inf/lib.jars...% fitnesse.slim.SlimService 8086
And all that produces this error in the test:
java.io.FileNotFoundException: class path resource [applicationContext-ListarStatus.xml] cannot be opened because it does not exist
As fas as I understand, the !path ../bin in the wiki should indicate all the subfolders and stuff to be included in the classpath (the java -cp call), but apparently it doesn't...
Any ideas? Thanks a lot for your time!

Ok so I resolved it, by doing this in the ListarStatus class:
public ListarStatus() {
ctx = new ClassPathXmlApplicationContext(new String[] {
"/com/softclear/inventario/test/fitnesse/applicationContext-ListarStatus.xml",
"applicationContext-dao.xml",
"applicationContext-hibernate.xml"});
serv = (ServicioStatus) ctx.getBean("servicioStatus");
}
Notice that the applicationContext-ListarStatus.xml file has a relative path and not just the name of the file.
Turns out that ClassPathXmlApplicationContext DOES NOT search anywhere in the classpath like I understood (read this, the part of the ClassPathXML...: http://freejavaclass.com/articles/j2ee/spring/spring_loading_types_ways.jsp). At least not in Spring 2.
It had nothing to do with Fitnesse, it was all Spring...

Related

A class from external jar throws exception while loading Resource

Hi I am working on a Maven project having dependency on a external jar which has a class ConfigLoader having following loader() method.
public class ConfigLoader {
public void initialize() {
loader();
}
private static void loader() {
URL configURL = ConfigLoader.getClass().getResource("runtimeConfiguration.xml");
//some other method calls to which configURL is an argument.
}
//other methods of ConfigLoader class
}
and the directory structure is like this -
src
|...main
|.......java
|.......resources
|................dev
|................prod
both dev and prod have a file named runtimeConfiguration.xml
and the code which uses this class is
public class Application {
private Application application;
public static void main(String []args){
application = new Application();
application.invokeConfigLoader();
//additional code
}
private void invokeConfigLoader() {
configLoader.initialize();
}
}
The error I get is
could not find: runtimeConfiguration.xml
and the exception is thrown at the getResource() line in the class from jar.
I have tried adding the dev folder to classpath but still the same error. I want to run this code from linux terminal, and the command I am giving from trunk directory (where all my exernal jars and resource folder sits after maven build) is -
java -cp /resources/dev/*:configuration-loader.jar
I am using intelliJ 2017.2 and also tried to add the resources/dev folder as module dependency, but I keep on getting the same error. The resources folder is added as a library via project structure settings. I tried to search a lot but have not found any question with this issue. Kindly help me out as I am new to this environment based development.
Thanks!
ConfigLoader.getClass().getResource("runtimeConfiguration.xml"); will try to get runtimeConfiguration.xml from the same package a the ConfigLoader is defined and not from the root of classpath. Try appending / to runtimeConfiguration.xml.
This should work ConfigLoader.getClass().getResource("/runtimeConfiguration.xml"); or ConfigLoader.getClass().getResource("/dev/runtimeConfiguration.xml"); depending how you are adding resources to your classpath.
See javadoc for more.

Using SystemLoader / SpringFactoryLoader to load external Jar in Spring-Project

first: I'm really new to spring-boot and maven. So I still don't get how everything plugs together.
What I'm trying to achieve is some kind of plugin-feature for my application. From my research it seems the best way to do this is using ServiceLoader or the spring-boot implmentation of the SpringFactoriesLoader.
According to several instructions from the web I put two projects together
James (the main application) GitHub
TemperatureSensor (the plugin) GitHub
The JamesApplication provides an interfaces which is supposed to be implemented (de.maxrakete.james.device.domain.DeviceInterface).
The TemperatureSensor implements said class and exposes this in several ways.
For the ServiceLoader in in the file META-INF\services\de.maxrakete.james.device.domain.DeviceInterface with this content
de.maxrakete.james.plugin.TemperatureSensor.TemperatureSensor
For the SpringFactoriesLoader in the file META-INF\spring.factories with this content
de.maxrakete.james.device.domain.DeviceInterface=de.maxrakete.james.plugin.TemperatureSensor.TemperatureSensor
According to this page I tried two different implementations (see in the onApplicationEvent-function) in the MainApplication:
#SpringBootApplication
public class JamesApplication implements ApplicationListener<ApplicationReadyEvent> {
public static void main(String[] args) {
SpringApplication.run(JamesApplication.class, args);
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)cl).getURLs();
for(URL url: urls){
System.out.println("Classpath file: " + url.getFile());
}
}
#Override
public void onApplicationEvent(ApplicationReadyEvent event) {
ServiceLoader<DeviceInterface> loader = ServiceLoader.load(DeviceInterface.class);
loader.iterator();
List<DeviceInterface> foos = SpringFactoriesLoader.loadFactories(DeviceInterface.class, null);
}
}
I'm trying both ways to load the jar, but nothing is happening (I'm supposed to get some log-messages from the plugin) but this is not happening.
The way I'm running the application is like this:
java -cp "./plugins/TemperatureSensor-0.0.1-SNAPSHOT.jar" -jar james.war
As you see I'm trying to add the jar in the subfolder to the classpath, but in the ouput of the main-function (where I try to print all the files in the classpath) I only get Classpath file: /home/max/folder/james.war
Conclusion
So, there are three possible error-sources
Wrong cli command to add classpath files
Wrong declaration of interfaces in the META-INF folder
Wrong implementation of the Loader
Maybe I'm compiling the sources the wrong way?
Wrong configuration of the pom.xml
I really have no idea what the problem might be. I tried to provide you with as much information as possible and all the steps of my research. I hope someone finds some helpful clues, which I might have overlooked.
Thanks veryone!

Getting resource files when executing custom maven plugin

I have my custom maven plugin, which has to run tests programmatically on a test phase for example. So I have something like that
#Mojo(name = "aggregate", requiresDependencyResolution = ResolutionScope.RUNTIME)
public class AcceptanceTestMojo extends AbstractMojo {
#Override
public void execute() throws MojoExecutionException, MojoFailureException {
TestExecutor testExecutor = new TestExecutor();
testExecutor.setTestClasses(new Class[]{TestClass.class});
testExecutor.run();
}
}
So the problem comes because the TestClass.class is from another maven module and actually the resources which I want to get are loaded in that module classpath. In that TestClass I have the following method:
public Object[][] retrieveFile() throws IOException {
String[] issuesKeys = IOUtils.toString(
Thread.currentThread().getContextClassLoader().getResourceAsStream("fileName"))
.split("\\n");
....
....
}
If I build the module where the TestClass belongs to everything is working fine, because Thread.currentThread().getContextClassLoader() is loading the correct ClassLoader, but if run it with my plugin as I run the test programatically Thread.currentThread().getContextClassLoader() is loading the classpath of my plugin, so the file I want to retrive is not there and a RuntimeException is thrown.
So my question is how to get the correct ClassLoader so that to be able to get the file or is there a way to load files in classpath manually with java?
I found one solution- I modified the currentThread ClassLoader- I got all required classes from the classpath of the maven project I am executing the plugin on. This is possible with injecting the MavenProject bean:
#Component
private MavenProject project;

gwt-test-utils does not find my entry point class

I am trying to get gwt-test-utils to work. I set up the project in the following way:
src/main/java : all the java source code
src/test/java : the test source code
src/test/resources : resource files for the tests
I am building my project with gradle and eclipse. Gradle uses these directories correctly by default and I added all three of them as source directories to Eclipse.
I have successfully built and run the project and was able to execute some plain old JUnit tests as well as a GWTTestCase, so I think I set up the project and its dependencies correctly.
Now I wanted to use gwt-test-utils for some more advanced integration tests. To do so I did the following:
Add the gwt-test-utils and gwt-test-utils-csv to my dependencies
gwtTestUtilsVersion = '0.45'
testCompile group:'com.googlecode.gwt-test-utils', name:'gwt-test-utils', version:gwtTestUtilsVersion
testCompile group:'com.googlecode.gwt-test-utils', name:'gwt-test-utils-csv', version:gwtTestUtilsVersion
Add a gwt-test-utils.properties file to the directory src/test/resources/META-INF with the following content:
path/to/my/module = gwt-module
Added a class that extends GwtCsvTest to a package in the src/test/java directory. It is modeled after the second example in HowToWriteCsvScenario from the gwt-test-utils project wiki, replacing occurrence of their example classes with mine. It looks like this
#CsvDirectory(value = "gwtTests")
public class LoginLogoutTest extends GwtCsvTest
{
#Mock
private MainServiceAsync mainService;
private AppController appController = new AppController();
#CsvMethod
public void initApp()
{
appController.onModuleLoad();
}
#Before
public void setup()
{
GwtFinder.registerNodeFinder("myApp", new NodeObjectFinder()
{
#Override
public Object find(Node node)
{
return csvRunner.getNodeValue(appController, node);
}
});
GwtFinder.registerNodeFinder("loginView", new NodeObjectFinder()
{
#Override
public Object find(Node node)
{
return csvRunner.getNodeValue(appController.getRootPresenter().getCurrentlyActiveSubPresenters().iterator().next().getView(), node);
}
});
addGwtCreateHandler(createRemoteServiceCreateHandler());
}
}
added a csv-file for configuring the test to src/test/resources/gwtTests with the following content
start
initApp
assertExist;/loginView/emailTextBox
I tried executing it via the Eclipse's Run As > JUnit Test and indirectly via gradle build (which executes all the test cases, not just this one). Both lead to the same error:
ERROR GwtTreeLogger Unable to find type 'myPackage.client.AppController'
ERROR GwtTreeLogger Hint: Check that the type name 'myPackage.client.AppController' is really what you meant
ERROR GwtTreeLogger Hint: Check that your classpath includes all required source roots
The AppController class is the entry-point configured in the module I configured in gwt-test-utils.properties, which makes me think that configuration works correctly and the rest of the setup (dependencies and all) work as well.
In an earlier version I used the same file as a subclass of GWTTestCase and created an AppController instance in the same way. That worked, so I'm pretty sure the class path is setup correctly to include it as well. I also tried changing it back to the previous version just now and it still works.
I have no clue why the class is not found. Is there anything gwt-test-utils does differently which means I need to specifically set the class path for it? Otherwise it should just work, since both gradle and eclipse know about all the relevant source folders and dependencies.

Where to put a text file in Grails, and how to get the path

I need to read in a .txt file into a groovy class in order to interrogate it line by line. But I am not sure what folder I put it into in my grails app, and how to get the path to it?
So far I have tried placing it under src and also in a new folder web-app/txt
and I have tried the the following to read it in
fileIn = new File('/lexicon.txt').text
and
fileIn = new File('txt/lexicon.txt').text
to no avail.
Any body have any pointers?
Grails is a Java Web Application, so it will be compiled into a sigle file .war, with all files/classes/etc inside. Most Web containers do unpack war, but there are no any guaranteee, so it's not a good idea to use File to access this file as a file.
Btw, you can place your file into grails-app/conf, at this case it will be placed into classpath, and you'll be able to access it by using:
InputStream lexicon = this.class.classLoader.getResourceAsStream('lexicon.txt')
You could also put this file into a subdirectory, like grails-app/conf/data and load it as ***.getResourceAsStream('data/lexicon.txt')
You can put your file under web-app/
Example:
web-app/lexicon.txt
And then in your controller or service use grailsApplication:
class MyService {
def grailsApplication
public myMethod() {
File myFile = grailsApplication.mainContext.getResource("lexicon.txt").file
}
}
Hope this helps
You can use Spring's resource loading to access the file. With this method you can access the file from a Spring bean, which means Grails can autowire the resource in to its artifacts.
See below for the following steps examples
Place the file in grails-app/conf/.
Make a resource holder class in src/groovy
Add the resource holder as a Spring bean in grails-app/spring/resources.groovy
Then autowire and use the resource wherever you need it
Step 2:
package resource
import org.springframework.core.io.Resource
class ResourceHolder {
Resource lexicon
}
Step 3:
beans = {
lexiconHolder(resource.ResourceHolder) {
lexicon = 'classpath:lexicon.txt'
}
}
Step 4:
class AnyGrailsService {
def lexiconHolder
void aMethodUsingTheLexicon() {
File lexicon = lexiconHolder.lexicon.file
/* Do stuff with the lexicon */
}
In Grails 2, you can use the Grails Resource Locator
class MyService {
def grailsResourceLocator
myMethod() {
def fileIn = grailsResourceLocator.findResourceForURI('/txt/lexicon.txt').file
}
}
Handy tip: to mock this in Spock, use GroovyPageStaticResourceLoader
#TestFor(MyService)
class MyServiceSpec extends Specification {
def setup() {
service.grailsResourceLocator = Mock(GroovyPageStaticResourceLocator)
}
}

Categories

Resources