Core IDE - Part IV - ANT, JUnit and the Scrapbook in Application Developer

INTRODUCTION/OBJECTIVES

This workshop takes you through a demonstration of the WebSphere Studio Application Developer version 5.0 (hereafter called Application Developer) Core IDE. It focuses on the features in which a Java developer would be interested. The sample application is a simple on-line banking application represented by the Account project. We can withdraw funds from this account. This project has with two packages called com.ibm.demo.account and com.ibm.demo.account.testcases.

Note: This workshop requires that the following products are installed:
  • WebSphere Studio Application Developer Early Availability Version 5.0
  • JavaTM 2 Software Development Kit, Enterprise Edition (J2EE SDK) 1.4

Section 1 - ANT

___ 1. ANT Support

a. Apache ANT (Another Neat Tool) is a Java-based build tool using XML-based configuration files. ANT is used for traditional product builds as well as deployment and almost anything else in need of automation. Application Developer provides built-in ANT support.

b. If not already in a Java Perspective, open one by clicking on the Open Perspective button and selecting Java.

c. If not already in the Package Explorer view, switch to it by clicking on the Package Explorer tab.

JASB-0000.jpg

d. Select build.xml, click mouse-button 2 and select Run Ant....

e. In the Run Ant dialog, developers can choose which Available target they want to execute. Select just the compile target.

JASB-0001.jpg


Click the Finish button to run ANT.

f. In the Console the output from the ANT task is displayed.

JASB-0002.jpg

g. Rerun the same ANT task. Notice that nothing is run. This is because all the class files are up to date with respect to their source code. If you want to recompile, rerun the ANT task and in the Run Ant dialog, deselect the compile target, select the clean target and then select the compile target. This is to make the clean target execute first.

JASB-0003.jpg

Section 2 - JUnit

___ 1. Creating JUnit test cases

a. JUnit is a simple framework to write repeatable tests. Application Developer provides built-in JUnit support.

b. If not already in a Java Perspective, open one by clicking on the Open Perspective button and selecting Java.

c. If MyTestClient.java is not already open in the Java editor, double-click on MyTestClient.java in the Package Explorer view of the Java perspective.

The purpose of MyTestClient.java is to functionally test the CheckingAccount class. Many people would prefer to use a framework to write their test cases which is what JUnit provides.

d. In the Packages view of the Java perspective, highlight CheckingAccount.java.  Click File->New->Other....

e. In the New dialog, in the left-hand pane expand Java and select JUnit. In the right-hand pane select TestCase.

JASB-0004.jpg

Click the Next> button.

f. In the JUnit TestCase dialog, click the Browse... button next to the Package: entry field. Browse to and select com.ibm.demo.account.testcases. Type CheckingAccountJUnitTest for the Test case name. Select the checkboxes to generate the main method as well as setUp and tearDown methods.

JASB-0012.jpg

Click the Next> button.

g. In the Test Method dialog box, specify the methods for which test method stubs should be created. Select withdraw and dumpTxnHistory.

JASB-0005.jpg

Click the Finish button to generate the test method stubs.

h. The CheckingAccountJUnitTest is generated and opened in the Java editor. In this class, developers would modify the setUp method to perform any set up tasks, call constructors, database connections, etc. They would modify the tearDown method to perform any tear down tasks, dereference objects, release database connections, etc.

In the generated test method stubs testWithdraw and testDumpTxnHistory the developer would add logic to test the withdraw and dumpTxnHistory methods.

i. Double-click on CheckingAccountTest.java in the Package Explorer view of the Java perspective. This class is a generated JUnit test case with the test case logic completed.

Note that the setUp method creates a new CheckingAccount object and the tearDown method dereferences it. The testWithdraw and testDumpTxnHistory methods contain logic to test those methods.

___ 2. Running JUnit test cases

a. Run CheckingAccountTest.java as a JUnit test case by clicking on the arrow next  to the Run button and selecting Run as->JUnit Test.

JASB-0006.jpg


b. CheckingAccountTest.java runs as a JUnit test case inside Application Developer and the JUnit view is displayed. Note that there was one failure.

JASB-0006.jpg

The failure states that the expected result was 90 but the actual result was 89.

c. The Failure Trace portion of the JUnit view indicates that the problem was in the testWithdraw method. Double-clicking on the failure displays the line of code that caused the failure. It appears there is something wrong with the withdraw method of the CheckingAccount class.


JASB-0007.jpg

d. Display CheckingAccount.java in the Java editor. Go to the withdraw method. Notice the embezzlement code is still present and is the cause of the failure.

JASB-0008.jpg

Highlight the two lines of code and either remove them by press the Delete key or comment them out by pressing Ctrl+/ .

Press Ctrl+S to save and compile CheckingAccount.java.

e. Switch the Java editor back to CheckingAccountTest.java.  Run CheckingAccountTest.java as a JUnit test case by clicking on the arrow next  to the Run button and selecting Run.... This will display the Launch Configurations dialog.  The first time CheckingAccountTest.java was executed as a JUnit test case, a new launch configuration was created.

Click the Run button.

CheckingAccountTest.java should complete this time with no failures.

Section 3 - The Scrapbook

___ 1. Scrapbook

a. The Scrapbook is a freeform area where the developer can experiment with and evaluate code before adding it to a class.

Click on File->New->Scrapbook Page.

b. In the Create Java Scrapbook Page dialog, select Account as the folder and type Scrapbookpage as the file name.

JASB-0009.jpg

Click Finish.

c. A scrapbook page is created. Copy and paste the following lines of code into this free-form area. You could also type them by hand, as Code Assist is available:
java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_MONTH);
try {
java.sql.Connection _con = null;
String _sourceURL = "jdbc:db2:ais";
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
// Create a connection through the DriverManager
_con = java.sql.DriverManager.getConnection(_sourceURL);
} catch (java.sql.SQLException sqle) {}
} catch (ClassNotFoundException cnfe){}
d. Highlight the line:
java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_MONTH);
click mouse-button 2 and select Display. (Note that you could also press Ctrl+D).

JASB-0010.jpg

Application Developer compiles the selected code, runs it and displays the results, which in this case displays the day of the month.

JASB-0011.jpg

e. Highlight the line:
java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_MONTH);
click mouse-button 2 and select Inspect to bring up an inspector view for that code. (You may get a message box informing you the JRE has changed. That is fine!) The inspecting shows the result of evaluating an expression.

f. All manner of code can be evaluated in the scrapbook page regardless of complexity. It can also be used as a convenient place to place code snippets for demonstrations.

In the Package Explorer view, double-click on Scrapbook.jpage to see some code snippets used during this lab.

g. Close all open files.  If asked, save the changes.
Congratulations, you've completed Part IV, "ANT, JUnit and the Scrapbook in Application Developer" of the Core IDE lab and finished with the Core IDE lab!