code coverage analysis for eclipse - eclipsecon

28
10.03.2006 Code Coverage Analysis for Eclipse © 2008 Marc R. Hoffmann, made available under EPL v1.0 Code Coverage Analysis for Eclipse EclipseCon 2008, Santa Clara Marc R. Hoffmann, [email protected] Gilles Iachelini, [email protected] 17.03.2008

Upload: others

Post on 03-Feb-2022

8 views

Category:

Documents


0 download

TRANSCRIPT

10.03.2006

Code Coverage Analysis for Eclipse

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

EclipseCon 2008, Santa ClaraMarc R. Hoffmann, [email protected]

Gilles Iachelini, [email protected]

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

2

Focus

Technical

Java

Eclipse

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

3

Topics

Principles and TechniquesCode Coverage Tools in EclipseCoverage Analysis of Eclipse Apps

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

4

Tutorial Prerequisites

Eclipse 3.3.xTutorial Data

http://www.eclemma.org/research

Tutorial USB Sticks

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

5

Usage Scenario

Unit Tests

White Box Testing

Automated UI Test

System Test

Manual Execution

Black Box Testing

INDIRECT METRIC

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

6

Coverage Units

Control Flow CoverageClassesMethodsLinesStatementsBranchesPaths

Coverage Ratio =Covered Units

Total Units

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

7

Statement Coverage

public int clip(int lower, int upper, int x) {if (x < lower) {

x = lower;}if (x > upper) {

x = upper;}return x;

}

Test Set for Full Statement Coverage: clip(1, 9, 0)clip(1, 9, 10)

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

8

Branch Coverage

public int clip(int lower, int upper, int x) {if (x < lower) {

x = lower;}if (x > upper) {

x = upper;}return x;

}

clip(1, 9, 0)clip(1, 9, 10)

Test Set for Full Branch Coverage:

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

9

Path Coverage

public int clip(int lower, int upper, int x) {if (x < lower) {

x = lower;}if (x > upper) {

x = upper;}return x;

}

clip(1, 9, 0)clip(1, 9, 10)clip(1, 9, 5)clip(9, 1, 5)

Test Set for Full Path Coverage:

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

10

Granularity

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

11

Tool Matrix

TPTP

EMMA/EclEmma

Cobertura

Coverlipse

Clover

EclipseAntCmd

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

12

Coverage While You Work

Install EclEmma02_EclEmma_1.3.1/eclemma-1.3.1.zip

Import sample application03_Exercise_1/...simplemath.zip

Run FormulasTest as JUnit test

Identify lines in class Formulas not covered by the test cases.

Add test cases to get full coverage

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

13

Coverage Data Analysis

Import Apache Collections Library04_Exercise_2/...collections.zip

Run TestAllPackes as JUnit test

Explore Results in Coverage view

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

14

Feedback Loop

Write Tests Fix It

JUJU

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

15

Implementation Strategies

J 010Compiler Class Loader

JVM

Instrumentation Runtime Profiling

Java Source

Offline

Byte Code

On-the-Fly

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

16

Why Byte Code Instrumentation?

Performance Issues with JVM ProfilersNo Source RequiredWorks on 3rd party JAR‘sPlatform independent

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

17

Byte Code Instrumentation

JVM

CoverageRuntime

Instrumentation010

StructureInformation

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

18

Byte Code Probes// access flags 9public static clip(III)IL0LINENUMBER 6 L0ILOAD 2ILOAD 0IF_ICMPGE L1

L2LINENUMBER 7 L2ILOAD 0ISTORE 2

L1LINENUMBER 9 L1ILOAD 2ILOAD 1IF_ICMPLE L3

L4LINENUMBER 10 L4ILOAD 1ISTORE 2

L3LINENUMBER 12 L3ILOAD 2IRETURN

// access flags 9public static clip(III)IL0GETSTATIC Formulas.$VRc : [[Z

L1ICONST_1AALOADASTORE 3

L2LINENUMBER 6 L2ILOAD 2ILOAD 0

L3ALOAD 3ICONST_0ICONST_1BASTOREIF_ICMPGE L4

L5LINENUMBER 7 L5ILOAD 0ISTORE 2ALOAD 3ICONST_1ICONST_1BASTORE

[…]

// access flags 9public static clip(III)I

L0LINENUMBER 6 L0ILOAD 2ILOAD 0

IF_ICMPGE L1L2LINENUMBER 7 L2ILOAD 0ISTORE 2

[…]

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

19

Code Coverage for Eclipse/OSGi Bundles

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

20

Code Coverage for JUnit Plug-In Tests

Import EclEmma Plug-ins05_Exercise_3/eclemma_core.zip

Run AllEclEmmaCoreTests JUnit plug-in test

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

21

Headless?

Eclipse/OSGi

IDE Target

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

22

Class Loading via OSGi

J 010Compiler Class Loader

JVM

OSGi FrameworkImplementation

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

23

Equinox Adapter Hooks

Equinox OSGi Runtime

Bun

dle

Framework ExtensionB

undl

e

Bun

dle

Bun

dle

Bun

dle

Instrumentation

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

24

Code Coverage for Equinox

Copy to your plugins folder 06_Exercise_4/…osgihook

Insert into config.ini

osgi.framework.extensions=com.mountainminds.eclemma.osgihook

Start/Stop Eclipse

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

25

TDD/Test First ↔ Code Coverage

CodeCoverage

TDDTest First

superseeds

verifies

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

26

Thank You!

Use Code Coverage

It‘s Easy and Helpful

OSGi/Equinox is Code Coverage Ready

Makes Writing Unit Tests Fun

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

27

References

Code Coverage ToolsClover, http://www.atlassian.com/software/clover/Coverlipse, http://coverlipse.sourceforge.net/Cobertura, http://cobertura.sourceforge.net/CodeCover, http://codecover.org/EclEmma, http://www.eclemma.org/EMMA, http://emma.sourceforge.net/TPTP, http://www.eclipse.org/tptp/

Byte Code Library and Outline Plug-in:ASM, http://asm.objectweb.org/

Code Coverage for Equinoxhttp://www.eclemma.org/research/instrumentingosgi/index.html

© 2008 Marc R. Hoffmann, made available under EPL v1.0

Code Coverage Analysis for Eclipse

28

Copy Right Info

This presentation is contributed by Marc R. Hoffmann, Mountainminds GmbH & Co. KG, made available under EPL 1.0Some example code taken from the Apache Jakarta Commons project, provided under Apache License Version 2.0. All pictures in this presentation taken from stock.xching, http://sxc.hu/