diff options
author | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2013-11-22 17:00:50 +0100 |
---|---|---|
committer | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2013-11-22 18:03:32 +0100 |
commit | 497fb3fd0f85b5dd4b7d240f99dc0bc3811d939c (patch) | |
tree | 7fedefdf09ba581c0b8993760fb9bf3e24756b73 /org.jacoco.examples | |
parent | e35a34adcc3a47eac6723533e811e52c288d4dd5 (diff) | |
download | jacoco-497fb3fd0f85b5dd4b7d240f99dc0bc3811d939c.tar.gz |
Unit tests for API examples.
Diffstat (limited to 'org.jacoco.examples')
3 files changed, 123 insertions, 68 deletions
diff --git a/org.jacoco.examples/src/org/jacoco/examples/ClassInfo.java b/org.jacoco.examples/src/org/jacoco/examples/ClassInfo.java index 21c2a588..30daf014 100644 --- a/org.jacoco.examples/src/org/jacoco/examples/ClassInfo.java +++ b/org.jacoco.examples/src/org/jacoco/examples/ClassInfo.java @@ -13,6 +13,7 @@ package org.jacoco.examples; import java.io.File; import java.io.IOException; +import java.io.PrintStream; import org.jacoco.core.analysis.Analyzer; import org.jacoco.core.analysis.IClassCoverage; @@ -20,50 +21,64 @@ import org.jacoco.core.analysis.ICoverageVisitor; import org.jacoco.core.data.ExecutionDataStore; /** - * This example reads given Java class files, directories or JARs and dumps - * information about the classes. + * This example reads Java class files, directories or JARs given as program + * arguments and dumps information about the classes. */ public final class ClassInfo implements ICoverageVisitor { + private final PrintStream out; private final Analyzer analyzer; - private ClassInfo() { + /** + * Creates a new example instance printing to the given stream. + * + * @param out + * stream for outputs + */ + public ClassInfo(final PrintStream out) { + this.out = out; analyzer = new Analyzer(new ExecutionDataStore(), this); } - private void dumpInfo(final String file) throws IOException { - analyzer.analyzeAll(new File(file)); + /** + * Run this example with the given parameters. + * + * @param args + * command line parameters + * @throws IOException + * in case of error reading a input file + */ + public void execute(final String[] args) throws IOException { + for (final String file : args) { + analyzer.analyzeAll(new File(file)); + } } public void visitCoverage(final IClassCoverage coverage) { - System.out.printf("class name: %s%n", coverage.getName()); - System.out.printf("class id: %016x%n", - Long.valueOf(coverage.getId())); - System.out.printf("instructions: %s%n", Integer.valueOf(coverage + out.printf("class name: %s%n", coverage.getName()); + out.printf("class id: %016x%n", Long.valueOf(coverage.getId())); + out.printf("instructions: %s%n", Integer.valueOf(coverage .getInstructionCounter().getTotalCount())); - System.out.printf("branches: %s%n", + out.printf("branches: %s%n", Integer.valueOf(coverage.getBranchCounter().getTotalCount())); - System.out.printf("lines: %s%n", + out.printf("lines: %s%n", Integer.valueOf(coverage.getLineCounter().getTotalCount())); - System.out.printf("methods: %s%n", + out.printf("methods: %s%n", Integer.valueOf(coverage.getMethodCounter().getTotalCount())); - System.out.printf("complexity: %s%n%n", Integer.valueOf(coverage + out.printf("complexity: %s%n%n", Integer.valueOf(coverage .getComplexityCounter().getTotalCount())); } /** - * Reads all class file specified as the arguments and dumps information - * about it to <code>stdout</code>. + * Entry point to run this examples as a Java application. * * @param args - * list of class files + * list of program arguments * @throws IOException + * in case of errors executing the example */ public static void main(final String[] args) throws IOException { - final ClassInfo info = new ClassInfo(); - for (final String file : args) { - info.dumpInfo(file); - } + new ClassInfo(System.out).execute(args); } } diff --git a/org.jacoco.examples/src/org/jacoco/examples/CoreTutorial.java b/org.jacoco.examples/src/org/jacoco/examples/CoreTutorial.java index 55fd6cf2..560eacd6 100644 --- a/org.jacoco.examples/src/org/jacoco/examples/CoreTutorial.java +++ b/org.jacoco.examples/src/org/jacoco/examples/CoreTutorial.java @@ -12,6 +12,7 @@ package org.jacoco.examples; import java.io.InputStream; +import java.io.PrintStream; import java.util.HashMap; import java.util.Map; @@ -39,9 +40,7 @@ public final class CoreTutorial { public static class TestTarget implements Runnable { public void run() { - final int n = 7; - final String status = isPrime(n) ? "prime" : "not prime"; - System.out.printf("%s is %s%n", Integer.valueOf(n), status); + isPrime(7); } private boolean isPrime(final int n) { @@ -86,30 +85,25 @@ public final class CoreTutorial { } - private InputStream getTargetClass(final String name) { - final String resource = '/' + name.replace('.', '/') + ".class"; - return getClass().getResourceAsStream(resource); - } + private final PrintStream out; - private void printCounter(final String unit, final ICounter counter) { - final Integer missed = Integer.valueOf(counter.getMissedCount()); - final Integer total = Integer.valueOf(counter.getTotalCount()); - System.out.printf("%s of %s %s missed%n", missed, total, unit); - } - - private String getColor(final int status) { - switch (status) { - case ICounter.NOT_COVERED: - return "red"; - case ICounter.PARTLY_COVERED: - return "yellow"; - case ICounter.FULLY_COVERED: - return "green"; - } - return ""; + /** + * Creates a new example instance printing to the given stream. + * + * @param out + * stream for outputs + */ + public CoreTutorial(final PrintStream out) { + this.out = out; } - private void runTutorial() throws Exception { + /** + * Run this example. + * + * @throws Exception + * in case of errors + */ + public void execute() throws Exception { final String targetName = TestTarget.class.getName(); // For instrumentation and runtime we need a IRuntime instance @@ -152,7 +146,7 @@ public final class CoreTutorial { // Let's dump some metrics and line coverage information: for (final IClassCoverage cc : coverageBuilder.getClasses()) { - System.out.printf("Coverage of class %s%n", cc.getName()); + out.printf("Coverage of class %s%n", cc.getName()); printCounter("instructions", cc.getInstructionCounter()); printCounter("branches", cc.getBranchCounter()); @@ -161,23 +155,45 @@ public final class CoreTutorial { printCounter("complexity", cc.getComplexityCounter()); for (int i = cc.getFirstLine(); i <= cc.getLastLine(); i++) { - System.out.printf("Line %s: %s%n", Integer.valueOf(i), - getColor(cc.getLine(i).getStatus())); + out.printf("Line %s: %s%n", Integer.valueOf(i), getColor(cc + .getLine(i).getStatus())); } } } + private InputStream getTargetClass(final String name) { + final String resource = '/' + name.replace('.', '/') + ".class"; + return getClass().getResourceAsStream(resource); + } + + private void printCounter(final String unit, final ICounter counter) { + final Integer missed = Integer.valueOf(counter.getMissedCount()); + final Integer total = Integer.valueOf(counter.getTotalCount()); + out.printf("%s of %s %s missed%n", missed, total, unit); + } + + private String getColor(final int status) { + switch (status) { + case ICounter.NOT_COVERED: + return "red"; + case ICounter.PARTLY_COVERED: + return "yellow"; + case ICounter.FULLY_COVERED: + return "green"; + } + return ""; + } + /** - * Execute the example. + * Entry point to run this examples as a Java application. * * @param args + * list of program arguments * @throws Exception + * in case of errors */ public static void main(final String[] args) throws Exception { - new CoreTutorial().runTutorial(); - } - - private CoreTutorial() { + new CoreTutorial(System.out).execute(); } } diff --git a/org.jacoco.examples/src/org/jacoco/examples/ExecDump.java b/org.jacoco.examples/src/org/jacoco/examples/ExecDump.java index 3f28a1ab..30193314 100644 --- a/org.jacoco.examples/src/org/jacoco/examples/ExecDump.java +++ b/org.jacoco.examples/src/org/jacoco/examples/ExecDump.java @@ -13,6 +13,7 @@ package org.jacoco.examples; import java.io.FileInputStream; import java.io.IOException; +import java.io.PrintStream; import java.util.Date; import org.jacoco.core.data.ExecutionData; @@ -22,51 +23,65 @@ import org.jacoco.core.data.ISessionInfoVisitor; import org.jacoco.core.data.SessionInfo; /** - * This example reads given execution data files and dumps their content. + * This example reads execution data files given as program arguments and dumps + * their content. */ public final class ExecDump { + private final PrintStream out; + /** - * Reads all execution data files specified as the arguments and dumps the - * content. + * Creates a new example instance printing to the given stream. + * + * @param out + * stream for outputs + */ + public ExecDump(final PrintStream out) { + this.out = out; + } + + /** + * Run this example with the given parameters. * * @param args - * list of execution data files + * command line parameters * @throws IOException + * in case of error reading a input file */ - public static void main(final String[] args) throws IOException { + public void execute(final String[] args) throws IOException { for (final String file : args) { - dumpContent(file); + dump(file); } } - private static void dumpContent(final String file) throws IOException { - System.out.printf("exec file: %s%n", file); - System.out.println("CLASS ID HITS/PROBES CLASS NAME"); + private void dump(final String file) throws IOException { + out.printf("exec file: %s%n", file); + out.println("CLASS ID HITS/PROBES CLASS NAME"); final FileInputStream in = new FileInputStream(file); final ExecutionDataReader reader = new ExecutionDataReader(in); reader.setSessionInfoVisitor(new ISessionInfoVisitor() { public void visitSessionInfo(final SessionInfo info) { - System.out.printf("Session \"%s\": %s - %s%n", info.getId(), - new Date(info.getStartTimeStamp()), + out.printf("Session \"%s\": %s - %s%n", info.getId(), new Date( + info.getStartTimeStamp()), new Date(info.getDumpTimeStamp())); } }); reader.setExecutionDataVisitor(new IExecutionDataVisitor() { public void visitClassExecution(final ExecutionData data) { - System.out.printf("%016x %3d of %3d %s%n", + out.printf("%016x %3d of %3d %s%n", Long.valueOf(data.getId()), Integer.valueOf(getHitCount(data.getProbes())), - Integer.valueOf(data.getProbes().length), data.getName()); + Integer.valueOf(data.getProbes().length), + data.getName()); } }); reader.read(); in.close(); - System.out.println(); + out.println(); } - private static int getHitCount(final boolean[] data) { + private int getHitCount(final boolean[] data) { int count = 0; for (final boolean hit : data) { if (hit) { @@ -76,6 +91,15 @@ public final class ExecDump { return count; } - private ExecDump() { + /** + * Entry point to run this examples as a Java application. + * + * @param args + * list of program arguments + * @throws IOException + * in case of errors executing the example + */ + public static void main(final String[] args) throws IOException { + new ExecDump(System.out).execute(args); } } |