Testing Ext Objects Using JsUnit

We are using JsUnit to test our JavaScript code.  Most of the time we end up needing to stub out methods, save the method pointers and then put the original method back once done with the test.  This is very easy to do in JavaScript so why not!

An example test method usually looks like this:

1
2
3
4
5
6
7
8
9
10
11
function test_orderTabs() {
  var originalFunction = PTC.infoPage.TabSet.fooMethod;
  PTC.infoPage.TabSet.fooMethod = function() {
    //Do somthing else instead
  };
 
  //Do testing
 
  //Put back original function
  PTC.infoPage.TabSet.fooMethod = originalFunction;
}

Recently while doing some test driven development, I thought of extending our Ext component to make a Mock object.  This is really the same idea that we use in Java for JUnit testing.  The nice thing that happens when we do this in JavaScript is that we don’t need to worry about cleaning up function pointers!

1
2
3
4
5
6
7
8
9
PTC.infoPage.TabSetMock = Ext.extend(PTC.infoPage.TabSet, {
  fooMethod: function(){
  //Do Stuff
}
});
 
tabSetMock = new PTC.infoPage.TabSetMock({
  fooConfig: BarItems
});

Then later in the test method all I needed to do was something like the following:

1
2
3
function test_orderTabs() {
  var result = tabSetMock.fooMethod();
}

The benefit is that you don’t have to save function pointers. The mock was created in the context of the test method so once the test is over the memory of the mock is eased.

Grails findBy for hasMany Relationship

Interesting that this is not built into grails. As many people know who use grails you automatically get dynamic “findBy” methods on persisted objects. It is incredibly useful and provides easy access to attributes of a persisted class. The only problem is when you try to do the same thing on a “hasMany” relationship of that class. For example:

1
2
3
4
class Airport {
    String name
    static hasMany = [flights:Flight]
}

You can do:

1
Airport.findByName("MSP")

You cannot do:

1
Airport.findByFlights(flight)

What do you do? In my case, I decided to make it.

Here is my implementation:

1
2
3
4
5
6
7
8
9
10
class Airport {
   static Airport findByFlight(Flight flight)
   def c = Airport.createCriteria()
   def result = c.get {
      flights {
         idEq(flight.id)
      }
   }
   return result;
}

Connect To HSQLDB Grails Database

This is how you can connect to use the DBVisualize to connect to grails using the HSQL database. I wanted to learn more about GORM and to understand how it was generating the tables.

Datasource.groovy

1
2
3
4
5
6
 development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop','update'
url = "jdbc:hsqldb:file:devDB;shutdown=true"
}
}
  • Choose HSQLDB embedded as your Driver using the hsqldb.jar located under your Grails home directory ($GRAILS_HOME/lib)
  • Update the Database URL to include your grails app base directory (jdbc:hsqldb:file:<grails app base>/devDB). Window users don’t have to specify the drive (ie. “C:”).

Groovy Links

Here are some useful links for getting your hands wet with Groovy/Grails programming:

Programming Resources

Groovy resource: http://pleac.sourceforge.net/pleac_groovy/index.html

Plug-ins: Grails Plugins

Forum: http://grails.1312388.n4.nabble.com/

Blogs: http://groovyblogs.org/entries/recent

Book references: Recipes!

Design Ideas

Get the ideas flowing: 99designs.com

Quickly generate rounded corners: http://www.roundedcornr.com/

Help with Profiling and Memory Leaks in WPF C#

There are lots of great free tools out there to help you profile and find memory leaks in your C# WPF application.  My favorite for finding memory leaks has recently been CLR Profiler.  I also found the SlimTune to be helpful as well.  Enjoy the links!