Unit Testing RPG with JUnit
Upcoming SlideShare
Loading in...5
×
 

Unit Testing RPG with JUnit

on

  • 1,545 views

 

Statistics

Views

Total Views
1,545
Views on SlideShare
1,384
Embed Views
161

Actions

Likes
1
Downloads
16
Comments
0

3 Embeds 161

http://www.scoop.it 147
http://www.linkedin.com 13
http://webcache.googleusercontent.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Unit Testing RPG with JUnit Unit Testing RPG with JUnit Presentation Transcript

  • Unit Testing RPG with JUnit"Never in the field of softwaredevelopment was so much owed byso many to so few lines of code"Martin Fowler (speaking of JUnit)
  • Agenda Costs of Manual Testing Value of Automated Unit Testing The Tools We’ll Use Creating the Infrastructure Reviewing the Component to be Tested The Test Components Writing the interface code and the Tests Compiling the Java Running the Tests Automating the Process
  • Costs of Manual Testing Manual testing generally occurs late, justbefore integration. The cost to correctproblems at this stage are ten to a hundredtimes greater than the cost to correct theproblem before the project reaches thisstage. A problem found late in the developmentcycle can delay the carefully planned workof a dozen people.
  • Value of Automated Testing Writing tests before writing the applicationcode increases cohesion and reducescoupling. You won’t understand it until you’ve doneit. The tests are reusable. Reuse is free.Write tests once; verify quality a thousandtimes without lifting a finger.
  • The Tools We’ll Use The JUnit JAR file (junit.jar) iSeries QSHELL and shell scripts javac, the Java compiler JNI, Java Native Interface iSeries Navigator iSeries IFS (integrated file system) PC-based Text Editor iSeries Source Editor RPG Compiler
  • Creating the Infrastructure Add a “Share” to your IFS folder. Map the share to Windows Explorer. Place a copy of the JUnit jar file in your IFSfolder.
  • To add the file share, use iSeries Navigatorand select the Integrated File System on theiSeries on which you’ll be developing.
  • Find or create your IFS folder. Werequire one to be under Root/home.
  • Right click and select Sharing/NewShare…
  • Create the file system shareBe sure to select Read/Write access!
  • In Windows Explorer, select Tools/MapNetwork Drive…
  • Complete mapping a drive to the sharecreated earlier.Example: servershare
  • Getting the JAR File Download JUnit from http://junit.org Open the Zip file, select the junit.jar andextract it your development folder on theiSeries share drive.
  • Next Steps Review the ILE RPG procedures we willtest. Create the test components. Run the tests.
  • Review the RPG Procedures to be Tested* Trivial Example: Add and Subtract*h nomainh option(*srcstmt:*nodebugio)h datfmt(*ISO)*d addInts pr 15P 0 extproc(addInts)d 15P 0 CONSTd 15P 0 CONST*d subInts pr 15P 0 extproc(subInts)d 15P 0 CONSTd 15P 0 CONST*p addInts b EXPORTd pi 15P 0d p1 15P 0 CONSTd p2 15P 0 CONSTc return p1 + p2p e*p subInts b EXPORTd pi 15P 0d p1 15P 0 CONSTd p2 15P 0 CONSTc return p1 - p2p e
  • The Test ComponentsTo create and run the tests, 4 objectsare required: The service program to be tested. The service program that provides thenative interface to Java. The Java Class that accesses the RPGservice program. The class that implements the unit test.
  • Parameter Conversion TableThe necessary information for converting parameter values isin chapter 11 of the RPG Programmer’s Guide.
  • A second RPG module is required1. the *JAVA keyword2. the fully qualified name of the Java classthat will execute this procedure.3. the name of the Java method (markednative) which will call the ILE procedure.For Java to access the RPG, the ILEprocedures must be given a Java interfacewith the EXTPROC keyword.When interfacing with Java, the EXTPROCkeyword requires three parameters:
  • h nomainh option(*srcstmt:*nodebugio)h datfmt(*ISO)* Imported Proceduresd addInts pr 15P 0 extproc(addInts)d 15P 0 CONSTd 15P 0 CONST*d subInts pr 15P 0 extproc(subInts)d 15P 0 CONSTd 15P 0 CONST* Exported Proceduresd add pr 10I 0 extproc(*JAVA:d com.rpg.Math‘ : add)d 10I 0 valued 10I 0 value*d sub pr 10I 0 extproc(*JAVA:d com.rpg.Math‘ : sub)d 10I 0 valued 10I 0 value*p add b EXPORTd pi 10I 0d p1 10I 0 valued p2 10I 0 valuec return addInts(p1: p2)p e*p sub b EXPORTd pi 10I 0d p1 10I 0 valued p2 10I 0 valuec return subInts(p1: p2)p e
  • Compiling the RPGCreate the RPG modules:crtrpgmod calculatorcrtrpgmod calcjniCreate the service program with or withoutbinding source:crtsrvpgm jniformath module(calcjnicalculator) export(*all)
  • Write the Java code that calls RPGpackage com.rpg;public class Math {static {System.loadLibrary("JNIFORMATH");}native public int add(int add1, int add2);native public int sub(int sub1, int sub2);}The JNI magic necessary for Java to call RPGsimply requires (1.) the name of the serviceprogram to be provided in the parameter of thecall to System.loadLibrary() and (2.) thecreation of the native method signatures.Your PC’s NotePad or WordPad editor is sufficient for this task.
  • The Unit Testimport junit.framework.*;public class MathTest extends TestCase {Math math;protected void setUp() {math = new Math();}public void testAdd() {Assert.assertEquals(2, math.add(2,0));Assert.assertEquals(2, math.add(1,1));}public void testSub() {Assert.assertEquals(0, math.sub(2,2));Assert.assertEquals(0, math.sub(1,1));}}This class inherits from junit.framework.TestCase. To compileand run, junit.jar must be in the classpath.
  • Save the Java source to the iSeries’ IFS usingthe drive we mapped earlier, being careful tomatch folders to the package statements.
  • Interaction of the Software Components
  • Preparing to Compile the Java SourceThe Java source is in folders in theiSeries’s IFS. We will compile and runthe Java programs in the QSHELLenvironment on the iSeries.From the command line of the iSeriessystem where the Java and RPG areplaced, enter the command QSH tostart the alternative Unix shellenvironment on iSeries.
  • Commands Useful in QSHELL LS – list files (similar to MS-DOSDIR command) PWD – display name of currentfolder CD – change directory (just as inMS-DOS) CAT – takes a file name as itsparameter and displays file’s text
  • Java’s Compile Command JAVAC is the Java compiler. It canbe run by an IDE or from thecommand line. It is most convenientin this exercise to run JAVAC fromQSHELL.
  • Compile Dependencies The classes you compile in Javamay be dependent on JAR files justas the compile of RPG programs canbe dependent on bindingdirectories, service programs andmodules. The Test classes will depend onjunit.jar.
  • Compiling With JAVACThe Math class provides the JNI interface toRPG. It was placed in the com.rpg package sothe compile command is as follows:javac com/rpg/Math.javaThe MathTest class inherits fromjunit.framework.TestCase so, the junit.jar filemust be in the classpath when it is compiled:javac –classpath .:../lib/junit.jarMathTest.java
  • Running With JAVATo execute Java Classes, the JAVA commandis used instead of CALL. Note that theclasspath contains the junit.jar file.Also note that we are calling junit’sTestRunner class and passing it the name ofour test class.java -classpath .:../lib/junit.jarjunit.textui.TestRunner MathTestThe entire JAVA command above wraps to two lines but it is notrequired that it do so.
  • Compile and Run the Java
  • Automating the Process: Run the Compileand Test in a Shell Scriptif !(javac -classpath .:../lib/junit.jarsrc/** -d build/prod); then exit 1; fi;jar -cf Math.jar build/prod/*.classjava -classpath .:../lib/junit.jar:Math.jarjunit.textui.TestRunner MathTestANT is another neat tool from the Open Sourcecommunity. ANT could be used to automate these tasksbut, that requires quite a long discussion.A QSHELL script also provides a means to easily compilethe Java and run the tests. Create the script by placingcommands similar to those below in a file.
  • Results of Running the ScriptA compile error interrupts the script when it is firstrun. After the error in the Java source is corrected,the script runs successfully, performing both thecompile and the testing.
  • Next Steps Don’t stop with just these tests.Create more. Find out what breaksyour application’s code and attackthose problems. Implement a way for your team toshare, store and version tests. Learn Test Driven Development.
  • References1. JUnit: http://junit.org2. QSHELL for iSeries by Ted Holt and FredA. Kulack: http://skillport.books24x7.com3. JNI Articles: http://www.iseriesnetwork.com/artarchive/index.cfm=ListArticlesByAuthor&ID=8834. JNI RPG and the eBusiness World (ch 11)http://publib.boulder.ibm.com/infocenter/iserie
  • References (continued) Sun’s Tutorial – First Steps for Unix:http://java.sun.com/docs/books/tutoria