Code Analyzing a Project
This tutorial provides a brief introduction to the AppPerfect Java Code Test product using a set of hands-on practice exercises. This tutorial assumes you have successfully downloaded and installed AppPerfect Java Code Test on your machine with the default options. Apart from this pre-requisite, this tutorial is self contained. Instructions given below are Windows-specific; if you are using a non-Windows machine, please use equivalent commands/instructions.
This document is divided into following sections
Within each section, multiple exercises are listed. Each exercise forms a logical unit which is expected to take no more than a few minutes to perform. All exercises use the PetStore demo application running inside Apache Tomcat. The modified PetStore application and Tomcat are both bundled with this product.
All exercises assume you have installed the product in C:\AppPerfect\CodeTest folder and will be referred as CodeTest_HOME henceforth in tutorial. If you have installed the product in some other folder, modify the instructions below appropriately.
This tutorial is not intended as a comprehensive training guide. Please refer to the product documentation for detailed information. However, this tutorial will give you a very good overview of the products and substantially improve your productivity with the product.
Creating Project
Exercise 1: Launch AppPerfect Java Code Test
- Click on Start -> Programs ->AppPerfect Code Test x.x.x -> AppPerfect Java Code Test
- On launching AppPerfect Java Code Test a Welcome page will be displayed. Go through the brief description
given for product.
NB: Welcome page is displayed only when Code Test x.x.x is launched and last time no project was opened.
Exercise 2: Creating a Project
- Launch the Project Wizard by clicking File ->New... menu option. The New Project wizard will be launched.
- Go through the instruction provided on top of the General tab.
- Keep the default project name and location for the purpose of this exercise. We don't have to provide "Notification" settings or "Remote Application/AppServer" settings for this exercise. Click on the Next button.
- For this exercise we don't need to import any IDE project. Click on the Next button.
- In the Source tab click on the "Add File" button on the r.h.s., select "Files of type:" as *.jsp in the Open dialog and add jsp files from the location "CodeTest_HOME\tomcat\webapps\petstore"
- Now click on the "Add Folder" button on the r.h.s. and select the folder "CodeTest_HOME\tomcat\webapps\petstore\src". Click the Next button.
- Use the default JDK which is bundled with AppPerfect Java Code Test and click on the Next button.
- In the "Environment" tab we will provide the classpath for the sources added in the project.
- Click on the "Add File" button on the r.h.s. and navigate to the location - "CodeTest_HOME\tomcat\webapps\petstore\WEB-INF\lib" - select all the jars present in the "lib" folder.
- Click on the "Add File" button on the r.h.s. and navigate to the location - "CodeTest_HOME\tomcat\lib" - select "ant.jar", "el-api.jar", "jsp-api.jar" and "servlet-api.jar" jars and add them to classpath.
- Click on "Verify Classpath" button to validate the classpath.
- Classpath validation dialog will be launched and the classpath will be verified. A message saying that the classpath specified is correct should be displayed. Click on the "OK" button. Click on the Next button.
- In the "Target" tab select project type as "SERVER".
- Keep the default URL in the "Starting URL" field i.e., "http://localhost:8380/petstore". Select the "AppServers Settings" tab.
- Select the "Specify AppServer Settings" checkbox and select "Tomcat_5.x/6.x" from the drop down menu.
NB: This is required only if you want to launch AppServer automatically while profiling, recording and replaying in AppPerfect Unit Tester or if you want to profile AppServer in AppPerfect Java Profiler. - Specify the Server Home path - "CodeTest_HOME\tomcat" and the "Startup file" for the server - "CodeTest_HOME\tomcat\bin\catalina.bat".
- Select the "Launch server automatically (When required)" checkbox. Click on the Finish button.
- If applications asks for Starting URL validation, click on 'No' button. Normally Confirmation for Starting URL validation is not asked when "Launch server automatically (When required)" is checked. A confirmation message saying that the project is saved will be displayed. Click on the OK button.
Now using this Project we will create tests to demonstrate the functionalities of AppPerfect Java Code Test product.
AppPerfect Java Code Test
NB: Please follow the steps provided in the "Creating Project" section to first create a Project, then proceed further.
Exercise 1: Define a Java Code Test project
- Once the Project is successfully created another dialog - Define Project Properties - will be displayed.
- Read the instructions at top of each tab.
- In the General tab select analyze JSP files and specify following path - "CodeTest_HOME\tomcat\webapps\petstore" as Document Root folder.
- In the Rules tab, we need to choose the rules that need to be applied to the selected source files. Scroll through and review all the rules. For this exercise, apart from the selected default rules we will select one more rule viz.,'Declare_variable_final'. To do this, right click on any of the rule and select 'Find...' option from the popup menu. Type the rule name 'Declare_variable_final' in the 'Find Node:' text field. The rule will be listed below. Select the rule from the list. The rule will be highlighted in the Rules tree. Click the Close button to close the Find dialog. Mark check the rule for it to be applied for the test. Select Auto-Fix tab.
- In this tab, we need to choose the rules which should auto fix their violations. Right click on any of the rule and select 'Find...' option from the popup menu. Type the rule name 'Declare_variable_final' in the 'Find Node:' text field. The rule will be listed below. Select the rule from the list. The rule will be highlighted in the Rules tree. Click the Close button to close the Find dialog. Mark check the rule for it to be applied for the test. Keep the default options selected related to prompting before auto-fixing the rule and backup of your original code. The backup of your code is stored as a .java.BAK file in the same folder as the original file. Click OK.
- Now all the JSP & Java files which were added to the Source in the Project Wizard will be displayed in the Source tab.
- Click through all the menu items to familiarize yourself with the available features and how to access them. Viewing through all menu items will provide a reasonable overview of the application.
- The central concept of code analysis is Java coding rules. AppPerfect Java Code Test defines over 750 coding rules that are applied to your source code to identify issues. These rules can be viewed by clicking on Project -> Rules Manager... menu item. Detailed description, sample code, etc. are provided.
- Click on Tools ->Options... menu item. Click on the JDKs tab and ensure that the JDK path has been set correctly. This is the path provided for JDK during installation of AppPerfect Java Code Test. You may modify the path or add new JDK through this dialog box. It is critical that a correct version of JDK is available for AppPerfect Java Code Test to perform correctly.
- Click Help -> Table of Contents menu item to see AppPerfect Java Code Test product documentation.
Exercise 2: Perform Code Analysis, View and Analyze results
- To analyze the project, click on Project -> Run. As source files are being analyzed, output is displayed in the "Java Code Test Console" screen.
- Auto-Fix confirmation dialog will be displayed showing the Rule being violated, a short description for the rule, Original violated code and a suggested fix for the violation. Click on "Stop auto-fixing" button of the dialog. We will not Auto-Fix for this project.
- As rules violations are detected, they are added to the list in the main Window. Once the analysis is completed, you will see 'Finished Analyzing' in the status bar at the bottom.
- To view rules violations based on source files, click on Source tab at top left and select any of the files in the tree on the left. Files with rule violations are marked with a red flag mark. Files with no rule violations are marked with a green flag mark.
- To view rules violations based on rules, click on Rules tab at top left and select any of the rules in the tree on the left. Rules which were violated are marked with a red flag mark. Rules which were not violated are marked with a green flag mark. Click on any of the rules to see details about which files violated that rule.
- To view other available results, click on the other tabs shown on the right hand side view.
- We recommend that users fix rules violations based on the rules violation severity. Hence, fix "Critical" category rules violations first, then move on to "High", etc.
Exercise 3: Perform Auto-Fix
- Auto-Fix can be done only for Java files, it is not supported for JSP files.
- Select CheckForm.java in the Source tab. The Violated rules are displayed in the Violations tab of the main panel on the r.h.s. frame.
- Right click on the rule "Declare_variable_final" and select Fix Rule Violations -> This Line.
- A Auto-fix confirmation dialog will be displayed showing the Rule being violated, a short description for the rule, Original violated code and a suggested fix for the violation. Click Ok.
- Now select the Auto fixes tab. The auto fix details will be displayed here.
- Right click on the Auto fixed code in the Auto fix tab. Select Restore File option to restore the original file. This will revert the changes done by Auto fix. Now, you can see the violated rule for the line in the violations tab.