Wednesday, May 19, 2010

Debugging Tests For Blackberry Development in IntelliJ Idea

Thanks to Andrew's persistent and genius effort, the team now has a way to debug the tests in IntelliJ Idea for BlackBerry development. This still does not debug UI code in simulator, but is still a huge jump forward to help the team with super-effective unit testing. The build file has been updated to reflect the changes. Besides using this build file and the structure mentioned in the previous post, you need to follow these steps:

1. Lets assume your main application project is called SampleApp. This should appear in your Idea project as a single module. Create another module for your project and name it "testing" (say)

2. This will create a "testing" folder on your hard disk. Create an empty folder under the module and name it "src". In the project module settings set this to be source folder for the module

3. Using the ant build file, first build and test your SampleApp. This will create a "tests.jar" in the "test_build" folder and a "SampleApp.jar" in the application folder

4. Now, for the testing module open the module settings and go to the "Dependencies" pane. Add the SampleApp as Module Dependency. After this add the SampleApp.jar and test.jar. Finally, add all the dependency jars that we added for SampleApp and all the jars in the lib folder of the installed Ant. Use J2ME SDK as the module JDK

5. Now, open Run->Edit Configurations. Add a new configuration to "Application" and name it "ant run". Set the following parameters:

Main class: org.apache.tools.ant.launch.Launcher

Program parameters: -buildfile bb_build.xml -Dbasedir=C:\development\code\SampleApp test
(the base directory of your project with the ant build file in it)

Working Directory : C:\development\code\SampleApp
(has your build file)

Environment Variables: -Dant.home=C:\development\ant_installation

Use classpath and JDK of module: testing


Voila! You are ready to debug your tests! Put a break-point in your tests in SampleApp module and debug the testing module using this configuration.

Monday, May 17, 2010

Ant Script for Blackberry Development in IntelliJ Idea

To say that developing Blackberry J2ME applications is a nightmare is an understatement, especially is you wish to setup a comprehensive build and test environment for the team. It is possible to do things in bits and pieces, but having a single integrated development environment handle it all gracefully looks like a distant dream, forcing us to move to Ant and use its build scripts to get a build environment that is not only independent of the IDE but can also be used in continuous builds. Many thanks to Andrew for helping us to get this far. I have compiled the steps needed to set up the Ant environment in IntelliJ Idea.

1. Install Sun's Java ME SDK

2. Install Ant such as its path does NOT have a blank space in its installation path name

3. Install the relevant version of RIM JDE and emulator

4. Create an ordinary Java project. Change SDK to ME in project settings (Project Settings->General, you can bring up the dialog from File->Settings)

5. Check the folder in which the project has been created. Create the following sub-folders inside it: application, bin, lib, src, test, test_build, test_results. Within "test_build" create a "classes" folder

6. Even though you could link jar files from anywhere in the system, it may be a good idea to put following jars for following projects at one place in the lib directory:

Blackberry Ant Tools
JMUnit and Hammock
JDOM
J2MEUnit
Microemulator

Additionally, to make your life easier while coding, copy the net_rim_api.jar from the lib folder of the installed RIM JDE

7. In project setting (Project Settings->Modules), set "src" as the source folder and "test" as the test folder. In dependencies, add jars for RIM, JMunit, Hammock and J2MEUnit.

8. Expand the "Ant Build" panel and open properties dialog. Under execution, use custom ant and point to the intallation that does not have any blank spaces in its path. Under Additional Classpath, add all the jars that we had put in step 6. Be sure to have the ant tasks for JMUnit and Blackberry Ant Tools.

9. You are all ready to start coding under your "src" folder and write tests under your "test" folder. Now we need a build file that can enable you to actually build and test your code. For that, make a "build.xml" file, which will be your ant script, and place under the root folder. Use this script provided for you. Don't forget to adjust the paths as per your installations. With some luck, this should bring up 8 tasks on your pane: build, build_and_deploy, build_deploy_and_launch_simulator, launch_simulator, test, test_compile. Note that the build task will not launch test task in the current script, but you could change it if you please.

These tasks will accrue you the following benefits:

1. You can write your blackberry development code in Idea
2. You have an ant script that can be used to make contunuous builds and tests. The test also generates reports that are stored under the test_results folder.

My team has still not found a satisfactory answer to debugging, but current set-up helps us to develop using a more effective tool and get our automated build systems to run effectively for the wretched J2ME. Moreover, this Ant script can be run from any IDE (with similar configuration for the ant used). It is still not 100% perfect, but we are getting there: constantly thinking about testability, logging and sustainability of build environment. I will keep updating the build file so as it gets more comprehensive.

PS: We were able to build and test using Ant and Netbeans, but the build/deploy part was tightly coupled with Netbeans, which was not encouraging for automated build tasks.