1. JAVADIFF
JAVA CODE DIFF TOOL
GUERRERA ALESSANDRO - MICCO ENRICO
ASSOCIATE PROFESSOR MASSIMILIANO DI PENTA
SOFTWARE ENGINEERING COURSE
UNIVERSITY OF SANNIO
2. BACKGROUND
Diff utility: developed in the early 1970s on the Unix operating
system
• File comparison utility that outputs the differences between two
files
• Finds changes made per line for text files
• Longest common subsequence problem (LCS)
Only textual differences
1/15/2014Software Engineering Course 2
3. OBJECTIVES
• Create a Java semantic diff utility
• Find semantic differences between 2 versions of the same java
file
• Textual layout independent
• Calculate a diff factor
Identify structural changes made in a new version of a Java file
1/15/2014Software Engineering Course 3
4. GOALS
• Comparison
• Java source file converted in XML format
• Modeling objects from XML files
• Diff algorithms on crated objects
• Comparison criteria
• Classes: Specifiers, Package, Import, Name, Superclass, Interfaces,
Attributes, Methods
• Methods: Name, Return Type, Specifiers, Exceptions, Parameters,
Statements
• Attributes: Name, Type, Specifiers
• Output: XML diff report
1/15/2014Software Engineering Course 4
5. ANALYSIS TYPES
Types of analysis:
1. Class analysis: specified a first file class name, are calculated differences
with all classes of the second file
2. Attributes analysis: specified the attribute name and class membership,
are calculated differences with attributes of the second file into the same
class
3. Methods analysis: specified the method name and class membership, are
calculated differences with methods of the second file into the same class
4. Quick analysis: are compared all classes of two files and are evaluated
differences as numbers of added or removed elements
5. Detailed analysis: are compared all classes of two files and are evaluated
differences as numbers of added or removed elements, considering also
modified elements
6. Full analysis: analysis on all elements, like attributes and methods.
1/15/2014Software Engineering Course 5
6. COMMAND LINE USAGE
User can interacts with the tool using command line instructions
javadiff [OPTIONS]... file1.java file2.java
Options:
• -c class, --classes class: perform a full comparison between classes in FILES
• -a class attribute, --attributes class attribute: perform a full comparison between attributes in FILES
• -m class method, --methods class method: perform a full comparison between methods in FILES
• -q, --quick: perform a quick comparison between FILES
• -d, --detailed: perform a detailed comparison between FILES
• -f, --full: perform a full comparison between FILES
• -h, --help: display this help and exit
• -v, --version: output version information and exit
1/15/2014Software Engineering Course 6
7. USE CASES
Name Description
1 Attributes Diff Perform attributes analysis
2 Methods Diff Perform methods analysis
3 Classes Diff Perform classes analysis
4 Quick Diff Perform quick analysis
5 Detailed Diff Perform detailed analysis
6 Full Diff Perform full analysis
7 Show Help Show software manual
8 Show Version Show software version
1/15/2014Software Engineering Course 7
8. SOME USE CASES
Name Classes Diff
Description System usage for classes diff analysis
Actors Max, System
Input -c parameter, class name, 2 files
Pre-condition Files must contain Java source code
Output XML report generated from System
Post-condition
Main scenario 1. Max uses System from cli with inputs
2. System runs classes diff, generates output and exit
Alternative scenario 2a. System show an error message and exit
1/15/2014Software Engineering Course 8
9. SOME USE CASES
Name Full Diff
Description System usage for full diff analysis
Actors Max, System
Input -f parameter, 2 files
Pre-condition Files must contain Java source code
Output XML report generated from System
Post-condition
Main scenario 1. Max uses System from cli with inputs
2. System runs full diff, generates output and exit
Alternative scenario 2a. System show an error message and exit
1/15/2014Software Engineering Course 9
11. srcML: document-oriented XML representation of source code
• Tool included in JavaDiff to convert java source file into XML
documents
• Combination of source code (text) and AST information (tags)
• Simplified management of files content
• Java structures easily identifiable
SRCML TOOLKIT
1/15/2014Software Engineering Course
Class
Main
{
//…
}
<?xml
version
…
</>
Java Code srcML file
11
12. JAVADIFF TOOL
1/15/2014Software Engineering Course
Class
Main
{
//…
}
Java Code
Class
Main
{
//…
}
Java Code
<?xml
version
…
</>
XML report
<?xml
version
…
</>
srcML file
<?xml
version
…
</>
srcML file
12
13. JAVADIFF EXAMPLE
1/15/2014Software Engineering Course 13
…
public double sum
(double a, double b)
{
double result=a+b;
return result;
}
…
…
public int sum (double
a, double b)
{
int result=a+b;
return result;
}
…
<Description>
Differences between
methods "sum" and
"sum" in class
"Calculator“
</Description>
<DiffFactor>
30.0
</DiffFactor>
<DiffResult>
Same name
- Type:
double modified
in int
- statements
DECL_STMT double
result = a + b;
modified in:
DECL_STMT int
result = a + b;
</DiffResult>
Java Code
Java Code
XML Report
18. CLASS DIAGRAMS
• SrcmlModel
• SrcmlClass
• SrcmlAttribute
• SrcmlMethod
• Parameter
• Statement
• Description:
• Object representation for XML file
generated with srcML tool
1/15/2014Software Engineering Course 18
19. CLASS DIAGRAMS
• DiffModel
• DiffElement
• DiffClassElement
• DiffAttributeElement
• DiffMethodElement
• Description:
• Contains elementary
differences between srcML
models
• Added elements
• Removed elements
1/15/2014Software Engineering Course 19
20. CLASS DIAGRAMS
• DiffReport
• DiffReportElement
• DiffElement
• DiffClassElement
• DiffAttributeElement
• DiffMethodElement
• Description:
• Contains DiffElements with:
• Elements info
• Numeric difference factor
• Details about differences between
elements (class, attribute, method)
1/15/2014Software Engineering Course 20
21. TECHNOLOGIES
• Eclipse IDE (Juno SR1) for Java
• Subversive (1.1.0):
• Subversion (SVN) team provider
• Eclipse plug-in
• srcML (Trunk 13990 – 07 Jun 2012):
• document-oriented XML representation of source code
• Platform-dependent binaries integrated with Java’s ProcessBuilder
• jDOM (2.0.4):
• Java-based solution for accessing, manipulating, and outputting XML data from
Java
1/15/2014Software Engineering Course 21
22. SOFTWARE TESTING
• Software Lifecycle Activity which is aimed for evaluating quality of a program
and also for improving it
• Process of verifying and validating that a software application or program
• meets the business and technical requirements, that guided its design and development
and works as expected
• Identifies important errors
• Other software quality factors:
• Reliability
• Performance
• Security
• Maintainability
• …
1/15/2014Software Engineering Course 22
23. • Functional Testing (Black-Box)
• Checks conformance with specifications
• Helps categorize inputs and derive expected outputs
• Helps test case generation and test oracles
• Our approach: Category-Partition Testing
• The system is divided into individual “functions” that can be
independently tested
• The method identifies the parameters of each “function” and, for
each parameter, identifies distinct categories
• Allowable combinations of choices in the categories are
converted in test data and test cases are generated
TESTING APPROACH
1/15/2014Software Engineering Course 23
24. • JavaDiff functions:
Function Parameters
1 Attributes Diff –a “ClassName” “AttributeName” file1 file2
2 Methods Diff –m “ClassName” “MethodName” file1 file2
3 Classes Diff –c “ClassName” file1 file2
4 Quick Diff –q file1 file2 (also only file1 file2)
5 Detailed Diff -d file1 file2
6 Full Diff -f file1 file2
7 Show Help -h (also no parameters)
8 Show Version -v
TESTING APPROACH
1/15/2014Software Engineering Course 24
26. • Other categories for (file1, file2) parameters in [4-6]
functions:
• GitHub1
• Github2
• Some other categories for parameters:
• Input options error
• Too many input options error
• File path error (File not found)
• System exceptions
TESTING APPROACH
1/15/2014Software Engineering Course 26
27. • Formal written test case:
• Known input (Test Data)
• Expected output, worked out before the test is executed
• Automated test case generation with JUnit:
• Open source framework used for writing & running tests
• Test suites:
1. AttributesDiff Test Suite
2. Methods Diff Test Suite
3. Classes Diff Test Suite
4. Quick Diff Test Suite
5. Detailed Diff Test Suite
6. Full Diff Test Suite
7. Help Test Suite
8. Version Test Suite
9. Github Test Suite
10. Error Test Suite
TEST CASES SPECIFICATION
1/15/2014Software Engineering Course 27
28. • Attributes Diff function:
Test Case testAttributesDiff()
Test Description "Attributes Diff" funtion testing
Class’ attribute comparison between (file1, file2)
sources
Actors
Involved Classes JavaDiffApplication
Preconditions
Test Inputs (Data) String[] args = {"-a", "Calculator", "mem", file1, file2};
Expected Outputs Report identical to test oracle’s report
Test Outputs Report
Test Result Success
Error Description
Notes This Test Case was run for all 25 categories of (file1,
file2) parameters
TEST CASES
1/15/2014Software Engineering Course 28
29. • Full Diff function:
Test Case testFullDiff()
Test Description "Full Diff" funtion testing
Full comparison between (file1, file2) sources
Actors
Involved Classes JavaDiffApplication
Preconditions
Test Inputs (Data) String[] args = {"-f", file1, file2};
Expected Outputs Report identical to test oracle’s report
Test Outputs Report
Test Result Success
Error Description
Notes This Test Case was run for all 25 categories of (file1,
file2) parameters
TEST CASES
1/15/2014Software Engineering Course 29
30. • Functional test coverage:
• Test cases must cover system specifications
• Code coverage:
• measure used to describe the degree to which the source code is tested by test suites
• EclEmma:
• free Java code coverage tool for Eclipse
• Coverage data can be exported in HTML, XML or CSV format
• Testing results:
• Covered Instructions: 98.4 %
• Covered branches: 84.6%
1/15/2014Software Engineering Course 30
CODE COVERAGE