eclipse-maven-jetty-remote-java-web-application-debugging

I have a maven based java web application running locally on the jetty server, I would like to debug the application.

Here are the steps I followed to make it happen.

  • Enable server socket address (9999) at the server side.
  • Enable Remote Java Application to bind to that address (9999).
    
    
maven_eclipse_jetty_debug

maven_eclipse_jetty_debug

1.Declare the maven-jetty-plugin on POM.xml as shown below.

<

pre style=”padding-left:60px;”>

 <!-- Jetty server plugin -->
 <plugin>
 <groupId>org.eclipse.jetty</groupId>
 <artifactId>jetty-maven-plugin</artifactId>
 <version>9.2.7.v20150116</version>
 <configuration>
 <scanIntervalSeconds>10</scanIntervalSeconds>
 <webApp>
 <contextPath>/certifier</contextPath>
 </webApp>
 </configuration>
 </plugin>
2.  click on Run As --> External Tool Configurations as shown below.
eclipse-maven-run-as-configuration

eclipse-maven-jetty-debug-run-as-external-tool-configurations

This will bring up another window as shown below. click new to create a new configuration !
external-tool-configuration

eclipse-maven-jetty-debug- external-tool-configuration

* Location: The executable file of maven , (usr/bin/mvn is for ubuntu OS.)

* Working directory: The current project working directory.

* Arguments: jetty:run 

   3. Click on Environment Tab and add a new Environment Variable as shown below.
Environment-Maven-Opts-Server-Socket-Agent

Environment-Maven-Opts-Server-Socket-Agent

MAVEN_OPTS

-Xdebug -agentlib:jdwp=transport=dt_socket,address=9999,server=y,suspend=n

Now click Apply and Run to start the jetty server, also server will be listening on port 9999 for remote debugging.

  4. Now click on Debug As --> Debug Configurations as shown below.
remote-debug-configuration

remote-debug-configuration

 5. Click on Remote Java Application and click new to create a new configurations. 
   This should bring up a new window as shown below.
remote-debug-java-application

remote-debug-java-application

Make sure to give the correct port,name, host etc. 
6. Click on the common Tab to add this configuration to the favourite menu of Debug As.
remote-debug-java-application-favourite

remote-debug-java-application-favourite

Now you should be able to debug the application by placing few breakpoints in the java source code.

 

Advertisements

Barcode Generator in Java

I was asked to work on generating barcode labels for one of projects. I searched online and found many free java projects which would help me in achieving my task.

Here is the few i got via google search:

  1. http://barcode4j.sourceforge.net/
  2. http://sourceforge.net/projects/barbecue/
  3. http://www.onbarcode.com/products/java_barcode/

Finally i have decided to go with barcode4j as it is available under Apache Licence V2.

I have built a jar file using the source of barcode4j and kept it in a lib folder of my project.

As i am using Maven for building my project on heroku for a demo purpose,  I need to keep this jar locally , so that heroku build structure will be able to understand the local jar (barcode4j.jar).

I have followed the below steps to include the local jar in my  project in such a way that the maven build will make use of this library for solving dependencies.

https://devcenter.heroku.com/articles/local-maven-dependencies

I followed the above link for making available local maven dependencies.

 

The source code is available in the heroku git repository here

The live demo is available here

 

Barcode-Generator-Picture

Barcode-Generator-Picture

Smartclient RestDatasource (ListGrid )using JAX-RS as JSON Data service

This is for beginners who likes to understand the smartclient RIA using JAVA as JSON service.

Smartclient LPGL version does not ship with smartclient server , so we need to manually create server side datasources for all of our Databound UI components.

Say I have downloaded the smartclient LPGL version from isomorphic website. Now I would like to add ListGrid  library to my web application. Here is sample grid we are going to build here. The tutorial application is hosted in herokuapp.com. Click here to access it

smartclient-listgrid-jax-rs-json-datasource-tutorial

smartclient-listgrid-jax-rs-json-datasource-tutorial

 

Here are the few things i did to make this work.

  • Create the RestDataSource with the JSON date format (id, structure fields, date format, data urls).
  • Create UI-controls (Data grid, add, remove and delete controls).
  • Create a JSON Webservice for the above CRUD Operations
  • Run it (download smart client sdk LPGL version and run example in an atachment).

 

The complete source code for this grid (UI) and Server side implementation(JAX-RS) for CRUD operations are available in the github here.   git@heroku.com:vel-smartclient-tutorial.git

 

 

 

Failed to execute goal on project core: Could not resolve dependencies for project com.mycompany:core:jar:1.0: Could not find artifact jdk.tools:jdk.tools:jar:1.6 at specified path C:\Program Files\Java\jre7/../lib/tools.jar ->

Hi ,

When i tried to install the broadleaf commerce on my local machine i was facing this error.

Failed to execute goal on project core: Could not resolve dependencies for project com.mycompany:core:jar:1.0: Could not find artifact jdk.tools:jdk.tools:jar:1.6 at specified path C:\Program Files\Java\jre7/../lib/tools.jar ->

You will need a Java JDK not just a JRE.

jdk_home_1

jdk_home_2

Also I have added tools.jar as dependencies in the POM.xml of ecommerce-core (parent) module as below. Alternative way is to install this jar locally using mvn install command.

<dependency>
	<groupId>jdk.tools</groupId>
	<artifactId>jdk.tools</artifactId>
	<version>jdk1.7.0_45</version>
	<scope>system</scope>
	<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>                 

Test execution order – JUnit 4.X

I want to execute test methods which are annotated by @Test in specific order.

For example:

public class MyTest{
    @Test public void create(){System.out.println("create");}
    @Test public void update(){System.out.println("update");}
    @Test public void delete(){System.out.println("delete");}
}

I want to ensure to run create() before update() and updatebefore delete each time I run MyTest.

The test execution order i would like to get is,

    create
    update
    delete

Test execution order

By design, JUnit does not specify the execution order of test method invocations. Until now, the methods were simply invoked in the order returned by the reflection API. However, using the JVM order is unwise since the Java platform does not specify any particular order, and in fact JDK 7 returns a more or less random order. Of course, well-written test code would not assume any order, but some does, and a predictable failure is better than a random failure on certain platforms.

From version 4.11, JUnit will by default use a deterministic, but not predictable, order (MethodSorters.DEFAULT). To change the test execution order simply annotate your test class using @FixMethodOrder and specify one of the available MethodSorters:

@FixMethodOrder(MethodSorters.JVM): Leaves the test methods in the order returned by the JVM. This order may vary from run to run.

@FixMethodOrder(MethodSorters.NAME_ASCENDING): Sorts the test methods by method name, in lexicographic order.

package com.vel.qa.tests.detailedtests;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MyTest {
	@Test
	public void aCreate() {
		System.out.println("create");
	}

	@Test
	public void bUpdate() {
		System.out.println("update");
	}

	@Test
	public void cDelete() {
		System.out.println("delete");
	}
}

This would produce the output as i wanted.

Result Aggregation using JUnit- Rule (Testwatcher)

Hi All,

I was asked to create a custom JUnit test result file which only has test case name and pass/failure message.

result.txt should contain like mentioned below.

testmethod1 : success
testmethod2 : failed
testmethod3 : success
testmethod4 : success
etc..

1. Result Aggregation:

The challenge is , we can not log the test results within the test method itself because at this time we will not be able to know whether the test will pass or fail. You may think ,we can write a log statement at the end of the test method but that will log only the success test methods, what about theFailed/Ignored/Errors test methods? As soon as assert failed , AssertionError will be thrown by the Junit and we will not be having any control to log this anywhere.. Still you may think, you can catch this exception and write a log statement and then fail the test methods. Yeah , this would work but it has some drawbacks.

  1. we will not be able to capture the ignored tests.
  2. we will not be able to clearly classify Errors vs Failures (these are completely different)
  3. we need to write a try/catch block in all of our tests. More customization required.

The above functionlity can be achieved easily using TestWatchmen Rule provided by JUnit-Framework.

JUnit-4.9 introduced new features called "Rule" which can be used to keep a log of each passing, ignored, failing tests. Click here for more information

With this approach we need to do no modification to the existing code still we can do our result aggregation.

Step 1: Create a class which implements TestRule Interface.

 
package com.vel.qa.tests.detailedtests;

import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import com.vel.qa.helpers.LoggerUtility;

public class ResultAggregator extends TestWatcher {

	private static String resultLog = "";
	private LoggerUtility loggerUtil = null;

	ResultAggregator() {
		// TODO need to refactor this constructor
		loggerUtil = LoggerUtility.getInstance();
	}

	@Override
	public Statement apply(Statement base, Description description) {
		return super.apply(base, description);
	}

	@Override
	protected void succeeded(Description description) {
		resultLog = description.getMethodName() + ": " + "SUCCESS \n";
		loggerUtil.getLogger("result").info(resultLog);
	}

	@Override
	protected void failed(Throwable e, Description description) {
		resultLog = description.getMethodName() + ":  " + "FAILED " + "\n";
		loggerUtil.getLogger("result").info(resultLog);
	}

	@Override
	protected void starting(Description description) {
		super.starting(description);
	}

	@Override
	protected void finished(Description description) {
		super.finished(description);

	}

}

This class will keep a log of each passing and failing test:

Step 2: Create test class and add @Rule annotations.

  
package com.numerify.qa.tests.detailedtests;

import static org.junit.Assert.fail;

import org.junit.Rule;
import org.junit.Test;

public class ResultAggregatorTest {

	@Rule
	public ResultAggregator log = new ResultAggregator();

	@Test
	public void testmethod1() {

	}

	@Test
	public void testmethod2() {
		fail();
	}

}

If you execute the above test class you will get an output as shown below.

testmethod1 : SUCCESS

testmethod1 : FAILED

Note: LoggerUtil is the class which is an custom defined logger for logging different files using log4j.

2. Repeat test with different input files.

We could achieve this by extending Junit-Core Runners [Junit-4X runners , Junit3X runners will be compatible with annotations] and iterate it with our files. I understood that this runner [Parameterized.java] are provided by JUnit itself in the recent releases. Click here to know more about Parameterized-tests