Archive for the ‘java’ Category

Generating Code from html tables using javascript.

July 1st, 2011 No comments

I frequently write sdks for different applications / sites. I frequently find when I do an integration of some kind for a customer the company does not provide a good sdk for the platform I am using. Because I prefer to make things easy. I usually like to write a tool to auto-generate an sdk for that tool.

An example of this is my 3dcart sdk:

While these sdks aren’t always perfect they allow me to create the integration MUCH faster. Which means I make more money.

Recently I needed to do this for Infusionsoft to create a simple integration that could access a few objects in java via their DataService api. I didn’t need much, just 3 objects so that a fulfillment company could read order information from Infusionsoft. When I worked at Infusionsoft I created a tool that took an xml file and automatically generated their api documentation. Unfortunately, since I am no longer an employee I don’t have access to that xml document. But! I do have access to the public api documentation that my tool auto-generated. And it’s in a table!

So, I quickly used the following code to auto generate the code for my class, my class factory (that populates the class from a map) and the code to populate a list of available fields.

It worked beautifully! This same technique could be adapted to any well organized api documentation. The page I used is located here:

Remember if the page doesn’t have jQuery on it, just install it as follows:

var s = document.createElement('script');
s.setAttribute('src', '');
jQuery('#staticFields, #publicFields, #factoryCode').remove();
jQuery('body').append('<pre id="staticFields"></pre> <br/>');
jQuery('body').append('<pre id="publicFields"></pre> <br/>');
jQuery('body').append('<pre id="factoryCode"></pre> <br/>');

jQuery('table tr').each(
        var $this = jQuery(this);
        $tds = $this.find('td');
        if($tds.length == 3){
            jQuery('#staticFields').append('fields.add("' + jQuery($tds.get(0)).html() + '");\n');                   

            jQuery('#publicFields').append('public ' + jQuery($tds.get(1)).html() + ' ' + jQuery($tds.get(0)).html().substr(0, 1).toLowerCase() + jQuery($tds.get(0)).html().substr(1) + ';\n');                   

            jQuery('#factoryCode').append('contact.' + jQuery($tds.get(0)).html().substr(0, 1).toLowerCase() + jQuery($tds.get(0)).html().substr(1) + ' = (' + jQuery($tds.get(1)).html() + ') map.get("' + jQuery($tds.get(0)).html()+ '");\n');                   

Categories: code generation, code snippets, java Tags:

Why you NEED to become framiliar with Maven.

November 18th, 2010 No comments

If you program in Java, you NEED to become fluent in Maven.

Why?  Because it simplifies working on projects a lot. 


  • Lots of utilities that can be run on any and all maven projects at any time with only one command, and without downloading anything manual.
  • Generate Javadocs instantly.(mvn javadoc:javadoc)
  • Run all tests with one command and receive pretty html reports of your test results.(mvn test)
  • View Test Code coverage with one command and no downloading. (mvn cobertura:cobertra)
  • Almost instantly use the latest Java libraries, like Apache Commons. (IDE detects unknown class and prompts you to add a dependency to your project).
  • Do selenium testing with one command. (mvn selenium:start-server)
  • Generate web projects that include Hibernate, Spring, and more in minutes. (mvn archetype generate)
  • Run your tomcat projects with a single command. (mvn tomcat:run)
  • And more!!!  If you develop in Java,  learning Maven will increase productivity a lot.


Start off with this video:

Categories: java, maven, spring, web Tags:

Spring MVC Redirect using “redirect:url” syntax.

October 27th, 2010 2 comments

I searched online for how to do redirects from a controller, and only came up with the RedirectView…

But I KNEW there was an easier way I had seen before…

The key is make your controller function return a string…  And then return “redirect:URL” instead of a view name.  Violaa!

Absolute and relative urls work great.

See: Spring 3.0.x docs for UrlBasedViewResolver

Categories: http, java, spring Tags:

Oracle has wasted no time in Rebranding Sun products.

October 26th, 2010 1 comment

Unless you’ve been out of touch for a long time, you’ve heard that Sun bought MySql, and was subsequently bought by Oracle.  If you use java, you’ve noticed that Oracle hasn’t drug their feet in rebranding sun products as Oracle…  java url’s are now instead of

MySQL appears to have escaped the re-branding at the moment.  It will be interesting to see what Oracle does with it in the future…  If they killed it, they would tick off half the development community in the world, making them less-likely to choose Oracle products in the future (let’s face it, there are LOTS of un-objective decision makers in the world).  But they could put some spin on it, and use it to build their market share…  Let’s wait and see…

Categories: community, java, oracle, sun Tags:

MultiThreaded TestNG, Surefire Craziness.

June 30th, 2009 No comments

The Setup

Maven 2.0.9
Surefire 2.4.3
TestNG version 5.8

The Craziness

If you annotate a method to have an invocationCount of 2, and a threadPoolSize of 2, and your test fails in an @BeforeMethod or an @AfterMethod when run by surefire, the test run count reported by surefire will be inaccurate. Significantly innacurate at times.

For example:

package com.infusion.crm.application;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;

* Created by IntelliJ IDEA.
* User: joey
* Date: Jun 29, 2009
* Time: 3:19:13 PM
* To change this template use File | Settings | File Templates.
public class TestThreadPoolSize {
public void before(){

public void test1(){

Returns the following from maven test:

Running TestSuite
Tests run: 8, Failures: 1, Errors: 0, Skipped: 7, Time elapsed: 10.298 sec <<< FAILURE! Results : Failed tests: before(com.infusion.crm.application.TestThreadPoolSize) Tests run: 8, Failures: 1, Errors: 0, Skipped: 7

This problem is also manifested if you create an AnnotationTransformer that forces the invocationCount and threadPoolSize of a test to both be greater then 1.

This bug is only manifested when multiple threads are used. If you set the invocation count to 20 and the thread pool size to 1, the bug does not appear.

Sometimes it is unpredictable and worse then just 2x the right number of tests. I ran a test, with 9 methods, and 4 threads, and a very exhaustive @BeforeMethod that takes several seconds to run. It failed repeatedly and said: 142 test run, 3 failed, 139 skipped.

Categories: java, maven, testng Tags:

Converting jUnit test to TestNG tests.

June 30th, 2009 No comments


After writing the below script, and submitting my change for a code review, the reviewer pointed me to a TestNG provided tool that will convert them. So here it is:

java org.testng.JUnitConverter -overwrite -annotation -srcdir src

Be sure to include the testng jar, along with the $JAVA_HOME/lib/tools.jar in your classpath when you run that.

Original SED way of converting

Because of the exceptional design of TestNG, this is a pretty simple text replace exercise. Here is a shell script using sed that will do most of the work for you. After it runs, just remove jUnit from your dependency list / classpath and re-compile, hand edit any errors.

There are two steps to running this, first, create a list of all the java files that contain junit references in your src.

find . -name "*.java" | xargs grep -L junit > toconvert

Then, use xargs to run the script on all the files.

cat toconvert | xargs testngify

Here is the contents of the testngify shell script:


function replace {
#echo executing pattern: $1
sed "$1" < $IN_FILE > $OUT_FILE

replace "s/import org\.junit\.After;/import org\.testng\.annotations\.AfterMethod;/g"
replace "s/import org\.junit\.Assert;/import org\.testng\.AssertJUnit;/g"
replace "s/import org\.junit\.Before;/import org\.testng\.annotations\.BeforeMethod;/g"
replace "s/import org\.junit\.Test;/import org\.testng\.annotations\.Test;/g"
replace "s/import junit\.framework\.Assert;/import org\.testng\.AssertJUnit;/g"
replace "s/import\s*static\s*org\.junit\.Assert\.\*;/import static org\.testng\.AssertJUnit\.\*;/g"
replace "s/import\s*static\s*org\.junit\.Assert\.assertEquals;/import static org\.testng\.AssertJUnit\.assertEquals;/g"
replace "s/\(\s\)Assert\./\1AssertJUnit\./g"
replace "s/import org\.junit\.Test;/import org\.testng\.annotations\.Test;/g"
replace "s/\(@Test\s*\)(expected\s*=/\1(expectedExceptions =/g"
replace "s/\(@Test\.*\)ComparisonFailure\.class/\1AssertionError\.class/g"
replace "s/@After\s*$/@AfterMethod/g"
replace "s/@Before\s*$/@BeforeMethod/g"
replace "s/@Before()\s*$/@BeforeMethod/g"
replace "s/org\.junit\.Assert\./org.testng.AssertJUnit\./g"
replace "s/.*@Ignore(\"\(.*\)\")/\t@Test(enabled=false) \/\/\1/g"
replace "s/\(.*\)@Ignore\(.*\)/\1\2/g"
replace "s/^.*org\.junit.*$//g"

Categories: java Tags: , , , ,