Automated unit testing for java as soon as code changes - java

Are there any tools/libraries like Guard (for Ruby on Rails) for Java which listen to file system changes i.e. changes to the code files in the project and automatically run unit tests associated with that class or the entire project.

Looks like you need something like Jenkins, running locally + FSTrigger plugin.
But if you need to monitor file system changes from your app (Guard is general-purpose tool), there is discussions on Stackoverflow about it:
Is there a sophisticated file system monitor for Java which is freeware or open source?
File changed listener in Java
How to detect filesystem has changed in java

The interesting thing is that yes, Guard.rb does support java thanks to the guard-java gem. Best thing of all is that this does support maven, ant, or what ever your build system is. This allows for setting up jenkins and cucumber then having guard run your tests as you edit the features/source code.
Add the following to your project's Gemfile:
gem 'guard-java'
Then issue bundler install && guard init java
The rest can be read at https://github.com/infospace/guard-java

I don't know if you are still up for an answer but I wrote a small tool, inspired from Guard, that could do what you want:
https://github.com/athieriot/JTaches
It uses the Watcher API available in Java7
Hope it helps

Related

What is the best way to add a custom compile-time processing step to Java?

What is the simplest way to add a compile-time step to analyse and modify the source code before it is compiled to byte code?
Can I write this in Java?
Would it be best written as an IDE plugin?
Can I write this in Java?
Yes, definitely. There are numerous Java based libraries for manipulating bytecode:
Commons BCEL
ASM
Javassist
Would it be best written as an IDE plugin?
In my opinion, no. You didn't mention which IDE you're using, but from my own experience, writing an IDE plugin has a steeper learning curve than adding a custom step to a build tool like Ant/Maven/Gradle. Even if you aren't currently using one of these build tools, in my personal opinion, it would be easier to adopt one of these tools rather than write an IDE plugin.
Also, tying a build step to a particular IDE makes your build less portable. Two things to consider before going this route:
1) How you would run your build on a continuous integration server like Jenkins or Bamboo. It's not impossible to invoke a headless Eclipse/Netbeans build that uses custom plugins on a build server, but it's not nearly as straightforward as running a build that uses "standard" tools like Ant/Maven/Gradle.
2) How would it impact other members of your team? You'd need to find a way to distribute to the plugin to each developer, deal with versioning and updates of the plugin, etc. Is everyone on your team using the same IDE?
I don't know anything about your project, your team (if you're working on a team), or the type of software you're developing so these considerations may not apply to you. I've only mentioned them as food for thought based on my own experiences.
What is the simplest way to add a compile-time step to analyse and
modify the source code before it is compiled to byte code?
What are you using for your builds? Ant? Maven? Gradle? The exact steps you'd follow are highly dependent on your build tool.
Depending on what you're trying to accomplish, you may not need to write anything at all.
For example, analysing parts of the code and splitting the work into
multiple threads where necessary –
Check out AspectJ. You can probably write an aspect that intercepts calls to certain methods and submits them to an ExecutorService. There are off the shelf plugins to invoke the AspectJ compiler from most common build systems.
If you do want to write something on your own, I think your best bet would be to write a custom Ant task. I suggest an Ant task because it's the lowest common denominator. It can of course be run using Ant, but both Maven and Gradle can invoke Ant tasks as well.
Write a new class that extends Task and do your thing in there.
public class MyTask extends Task {
public void execute() {
// do your bytecode manipulation here...
}
}
You'd invoke it like this from your Ant script:
<taskdef name="mytask" classname="MyTask" classpath="classes"/>
<mytask/>
Check out the Apache Axis2 code generation task for an example of doing build time code generation and how to deal with classpath issues/accessing your code.

Dropwizard hot deployment

I'm looking for a simple to use system in Java which creates a REST service for me. So I found dropwizard but as far as I can use google it turns out it lacks hot deployment although jetty is able to do so. When using the maven-shade-plugin it takes at least 10 seconds to build the thing. Also my IDE reports that it cannot use compile on save feature (aka hot deployment) when the shade-plugin is involved.
Can I use hotdeployment somehow? Or what can I use instead?
Update: If nothing will fix this I'll probably use a combination of jersey&guice etc which is explained in this post
You don't have to use the shade plugin to run your service. You could just compile as a regular jar file and I think that would let you use your IDEs hot deployment features.
Have you ever tried JRebel ? They have JAX-RS support as well...
Not an answer, but I wrote up an article detailing how to use git to push a Dropwizard project to your server and for it to initiate a hot replacement. It relies on git hooks and running Maven via a script on the server.
You can find the details about it here: http://gary-rowe.com/agilestack/2013/02/14/how-to-deploy-dynamic-sites-with-git/

Import a Eclipse project using Java Code

I have gone through the previous thread on the same topic
After reading that i tried to use the same code. I am very new to Eclipse plugin development. I tried to see the given example links but couldn't find the correct thread.
I have a similar requirement. I tried to develop a plugin following this link
Should i compulsory develop a plugin or is there any way where i can run it from java main method.
Thanks,
Vamsi
Basically, you have to write an Eclipse plug-in, as the suggested code re-uses existing features only available in an Eclipse plug-in environment.
For the referenced code to work, you have to maintain a plug-in with the dependencies org.eclipse.core.resources plug-in, and you also have to provide some functionality to execute this code (e.g. a platform command, as in another the mentioned vogella.de tutorial.
In theory, it might be possible to do it in plain Java code, but in that case it would make more sense to do it by directly editing the metadata files - and that is a way I do not recommend.

Methods to see result fo a code change faster

This question came to me when developing using Eclipse.
I use JBoss Application Server and use hot code replacement. But this option requires that the 'build automatically' option to be enabled. This makes Eclipse build the workspace automatically (periodically or when a file is saved?) and for a large code base this takes too much time and processing which makes the machine freeze for a while. Also sometimes an error message is shown saying that hot code replacement failed.
The question that I have is: is there a better way to see the result of a code change?
Currently I have the following two suggestions:
Have unit tests - this will allow to run a single test and see the result of a code change. ( But for a JavaEE application that uses EJBs is it easy to setup unit tests?)
Use OSGi - which allows to add jars to the running system without bringing down the JVM.
Any ideas on above suggestions or any other suggestion or a framework that allows to do this is welcome.
Did you take a look at http://zeroturnaround.com/jrebel/?
I tell you how I work. I hope it is useful. First of all, I disable "Build Automatically". Maybe it is better if you do a simple ANT script to compile and see errors/exceptions. Also, I make jar file with the same script. Afterwards, I use OSGi to check the application. Yo do not need to stop server, only you need to change versions in deployed bundles. Intead of JBoss Server I use Equinox which includes Jetty Web Server.
May you have a nice day!
With JRebel, you wouldn't have to build your project (skip the build totally). Plus, only the changed resources will be reloaded so the update happens instantly. Plus, session is preserved so you do not have re-login to the application after the update was performed.
Even though the question I asked was quite specific to Java, I thought that mentioning using an interpreted programming language that avoids the compilation step is anther way of seeing result of a code change faster.

Play! Java web framework. How does their development server compile automatically?

After looking at the Play! framework I find it really productive that the development server that it comes with automatically is able to compile .java files and show the changes, immediately. There's no hot deployer scanner that runs every tot seconds or so. The compilation happens when you hit refresh and it's extremely faster than my incremental mvn package. How do they do this?
I would like to know, well because I'm interested in knowing, but also because I don't want to use the entire Play! framework for my small project. I'm only interested in their development compilation process because I would like to adopt it :).
Any ideas?
I was reading about this just this morning. It actually takes your changed source files and uses the Eclipse Java Compiler (ECJ) internally before spitting out the compiled files to the built-in dev server.
The thing is, you probably don't want to go to the effort of wiring the ECJ into your "small project".
You can definitely approximate it though - the trick is to not do a mvn package, instead you want to be dropping the changed .class files into your webapp's exploded warfile directory on the filesystem.
If you're not tied to a particular app server/container, have a look at the Eclipse Jetty Plugin - looks like it's what you need, and Jetty is quick
JDT -- Play! uses Eclipse JDT to compile and load classes dynamically. Much the same way you code in Eclipse and you see an error or warning messages as soon as you type in something not desirable. See ApplicationCompiler class.
You may also want to look into JDT.
Play uses the Eclipse compiler to compile code at run-time.
Take a look at the following class, that is used by Play to perform the necessary compilation at run time.
https://github.com/playframework/play/blob/master/framework/src/play/classloading/ApplicationCompiler.java
The way they do it is by using a custom classloader that will detect changes to source files, use the Eclipse Java Compiler to compile the files and then hot swap the appropriate classes in the JVM. If you are looking for something similar, checkout ZeroTurnaround's JRebel
it is not free, but well worth the time savings when you need to redeploy a large project.
I'm not a Play developer however Struts2 is also capable of this but though the struts2-spring-plugin. Since the class reloading is provided by Spring it might be possible to use this spring feature by any project.
http://struts.apache.org/2.2.1/docs/spring-plugin.html
Search the page for "Class Reloading".

Categories