From e69ba4dbb015949c5d84ba7bbb0b53efac28bb23 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Thu, 30 Aug 2012 22:13:04 +0600 Subject: Fix EOLs --- .../src/org/jacoco/core/test/TargetLoader.java | 184 ++++----- .../core/test/perf/AnalysisTimeScenario.java | 104 ++--- .../test/perf/ExecuteInstrumentedCodeScenario.java | 102 ++--- .../src/org/jacoco/core/test/perf/IPerfOutput.java | 96 ++--- .../org/jacoco/core/test/perf/IPerfScenario.java | 54 +-- .../test/perf/InstrumentationSizeSzenario.java | 82 ++-- .../test/perf/InstrumentationTimeScenario.java | 92 ++--- .../jacoco/core/test/perf/PerfOutputWriter.java | 160 ++++---- .../jacoco/core/test/perf/PerformanceSuite.java | 98 ++--- .../org/jacoco/core/test/perf/TimedScenario.java | 134 +++---- .../jacoco/core/test/perf/targets/Target01.java | 252 ++++++------ .../jacoco/core/test/perf/targets/Target02.java | 54 +-- .../jacoco/core/test/perf/targets/Target03.java | 306 +++++++-------- .../test/validation/BooleanExpressionsTest.java | 170 ++++---- .../test/validation/ControlStructuresTest.java | 278 ++++++------- .../core/test/validation/ExceptionsTest.java | 196 +++++----- .../org/jacoco/core/test/validation/Source.java | 232 +++++------ .../jacoco/core/test/validation/SourceTest.java | 174 ++++----- .../core/test/validation/ValidationTestBase.java | 246 ++++++------ .../jacoco/core/test/validation/targets/Stubs.java | 214 +++++----- .../core/test/validation/targets/Target01.java | 432 ++++++++++----------- .../core/test/validation/targets/Target02.java | 248 ++++++------ 22 files changed, 1954 insertions(+), 1954 deletions(-) (limited to 'org.jacoco.core.test/src/org/jacoco/core/test') diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/TargetLoader.java b/org.jacoco.core.test/src/org/jacoco/core/test/TargetLoader.java index bb216a27..4fa9f69b 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/TargetLoader.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/TargetLoader.java @@ -1,92 +1,92 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; - -/** - * Loads a single class from a byte array. - */ -public class TargetLoader extends ClassLoader { - - private final String sourcename; - - private final byte[] bytes; - - private final Class clazz; - - public TargetLoader(final String name, final byte[] bytes) { - super(TargetLoader.class.getClassLoader()); - this.sourcename = name; - this.bytes = bytes; - clazz = load(name); - } - - public TargetLoader(final Class source, final byte[] bytes) { - super(TargetLoader.class.getClassLoader()); - this.sourcename = source.getName(); - this.bytes = bytes; - clazz = load(source.getName()); - } - - private Class load(final String sourcename) { - try { - return loadClass(sourcename); - } catch (ClassNotFoundException e) { - // must not happen - throw new RuntimeException(e); - } - } - - public Class getTargetClass() { - return clazz; - } - - public Object newTargetInstance() throws InstantiationException, - IllegalAccessException { - return clazz.newInstance(); - } - - public static InputStream getClassData(Class clazz) { - final String resource = "/" + clazz.getName().replace('.', '/') - + ".class"; - return clazz.getResourceAsStream(resource); - } - - public static byte[] getClassDataAsBytes(Class clazz) throws IOException { - InputStream in = getClassData(clazz); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] buffer = new byte[0x100]; - int len; - while ((len = in.read(buffer)) != -1) { - out.write(buffer, 0, len); - } - in.close(); - return out.toByteArray(); - } - - @Override - protected synchronized Class loadClass(String name, boolean resolve) - throws ClassNotFoundException { - if (sourcename.equals(name)) { - Class c = defineClass(name, bytes, 0, bytes.length); - if (resolve) { - resolveClass(c); - } - return c; - } - return super.loadClass(name, resolve); - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * Loads a single class from a byte array. + */ +public class TargetLoader extends ClassLoader { + + private final String sourcename; + + private final byte[] bytes; + + private final Class clazz; + + public TargetLoader(final String name, final byte[] bytes) { + super(TargetLoader.class.getClassLoader()); + this.sourcename = name; + this.bytes = bytes; + clazz = load(name); + } + + public TargetLoader(final Class source, final byte[] bytes) { + super(TargetLoader.class.getClassLoader()); + this.sourcename = source.getName(); + this.bytes = bytes; + clazz = load(source.getName()); + } + + private Class load(final String sourcename) { + try { + return loadClass(sourcename); + } catch (ClassNotFoundException e) { + // must not happen + throw new RuntimeException(e); + } + } + + public Class getTargetClass() { + return clazz; + } + + public Object newTargetInstance() throws InstantiationException, + IllegalAccessException { + return clazz.newInstance(); + } + + public static InputStream getClassData(Class clazz) { + final String resource = "/" + clazz.getName().replace('.', '/') + + ".class"; + return clazz.getResourceAsStream(resource); + } + + public static byte[] getClassDataAsBytes(Class clazz) throws IOException { + InputStream in = getClassData(clazz); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buffer = new byte[0x100]; + int len; + while ((len = in.read(buffer)) != -1) { + out.write(buffer, 0, len); + } + in.close(); + return out.toByteArray(); + } + + @Override + protected synchronized Class loadClass(String name, boolean resolve) + throws ClassNotFoundException { + if (sourcename.equals(name)) { + Class c = defineClass(name, bytes, 0, bytes.length); + if (resolve) { + resolveClass(c); + } + return c; + } + return super.loadClass(name, resolve); + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/perf/AnalysisTimeScenario.java b/org.jacoco.core.test/src/org/jacoco/core/test/perf/AnalysisTimeScenario.java index ffe3df60..f5a5b6c8 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/perf/AnalysisTimeScenario.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/perf/AnalysisTimeScenario.java @@ -1,52 +1,52 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.perf; - -import org.jacoco.core.analysis.Analyzer; -import org.jacoco.core.analysis.IClassCoverage; -import org.jacoco.core.analysis.ICoverageVisitor; -import org.jacoco.core.data.ExecutionDataStore; -import org.jacoco.core.test.TargetLoader; - -/** - * Scenario to measure the time taken by the instrumentation process itself. - */ -public class AnalysisTimeScenario extends TimedScenario { - - private final Class target; - - private final int count; - - protected AnalysisTimeScenario(Class target, int count) { - super(String.format("analysing %s classes", Integer.valueOf(count))); - this.target = target; - this.count = count; - } - - @Override - protected Runnable getInstrumentedRunnable() throws Exception { - final byte[] bytes = TargetLoader.getClassDataAsBytes(target); - final ExecutionDataStore executionData = new ExecutionDataStore(); - ICoverageVisitor visitor = new ICoverageVisitor() { - public void visitCoverage(IClassCoverage coverage) { - } - }; - final Analyzer analyzer = new Analyzer(executionData, visitor); - return new Runnable() { - public void run() { - for (int i = 0; i < count; i++) { - analyzer.analyzeClass(bytes); - } - } - }; - } -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.perf; + +import org.jacoco.core.analysis.Analyzer; +import org.jacoco.core.analysis.IClassCoverage; +import org.jacoco.core.analysis.ICoverageVisitor; +import org.jacoco.core.data.ExecutionDataStore; +import org.jacoco.core.test.TargetLoader; + +/** + * Scenario to measure the time taken by the instrumentation process itself. + */ +public class AnalysisTimeScenario extends TimedScenario { + + private final Class target; + + private final int count; + + protected AnalysisTimeScenario(Class target, int count) { + super(String.format("analysing %s classes", Integer.valueOf(count))); + this.target = target; + this.count = count; + } + + @Override + protected Runnable getInstrumentedRunnable() throws Exception { + final byte[] bytes = TargetLoader.getClassDataAsBytes(target); + final ExecutionDataStore executionData = new ExecutionDataStore(); + ICoverageVisitor visitor = new ICoverageVisitor() { + public void visitCoverage(IClassCoverage coverage) { + } + }; + final Analyzer analyzer = new Analyzer(executionData, visitor); + return new Runnable() { + public void run() { + for (int i = 0; i < count; i++) { + analyzer.analyzeClass(bytes); + } + } + }; + } +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/perf/ExecuteInstrumentedCodeScenario.java b/org.jacoco.core.test/src/org/jacoco/core/test/perf/ExecuteInstrumentedCodeScenario.java index 92b219f1..513de2a8 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/perf/ExecuteInstrumentedCodeScenario.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/perf/ExecuteInstrumentedCodeScenario.java @@ -1,51 +1,51 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.perf; - -import org.jacoco.core.instr.Instrumenter; -import org.jacoco.core.runtime.IRuntime; -import org.jacoco.core.runtime.LoggerRuntime; -import org.jacoco.core.test.TargetLoader; -import org.objectweb.asm.ClassReader; - -/** - * This scenario runs a given scenario twice and reports the execution time: - * Once on its original version, once in a instrumented version. - */ -public class ExecuteInstrumentedCodeScenario extends TimedScenario { - - private final Class target; - - protected ExecuteInstrumentedCodeScenario(String description, - Class target) { - super(description); - this.target = target; - } - - @Override - protected Runnable getInstrumentedRunnable() throws Exception { - ClassReader reader = new ClassReader(TargetLoader.getClassData(target)); - IRuntime runtime = new LoggerRuntime(); - runtime.startup(); - final Instrumenter instr = new Instrumenter(runtime); - final byte[] instrumentedBuffer = instr.instrument(reader); - final TargetLoader loader = new TargetLoader(target, instrumentedBuffer); - - return (Runnable) loader.newTargetInstance(); - } - - @Override - protected Runnable getReferenceRunnable() throws Exception { - return target.newInstance(); - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.perf; + +import org.jacoco.core.instr.Instrumenter; +import org.jacoco.core.runtime.IRuntime; +import org.jacoco.core.runtime.LoggerRuntime; +import org.jacoco.core.test.TargetLoader; +import org.objectweb.asm.ClassReader; + +/** + * This scenario runs a given scenario twice and reports the execution time: + * Once on its original version, once in a instrumented version. + */ +public class ExecuteInstrumentedCodeScenario extends TimedScenario { + + private final Class target; + + protected ExecuteInstrumentedCodeScenario(String description, + Class target) { + super(description); + this.target = target; + } + + @Override + protected Runnable getInstrumentedRunnable() throws Exception { + ClassReader reader = new ClassReader(TargetLoader.getClassData(target)); + IRuntime runtime = new LoggerRuntime(); + runtime.startup(); + final Instrumenter instr = new Instrumenter(runtime); + final byte[] instrumentedBuffer = instr.instrument(reader); + final TargetLoader loader = new TargetLoader(target, instrumentedBuffer); + + return (Runnable) loader.newTargetInstance(); + } + + @Override + protected Runnable getReferenceRunnable() throws Exception { + return target.newInstance(); + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/perf/IPerfOutput.java b/org.jacoco.core.test/src/org/jacoco/core/test/perf/IPerfOutput.java index 8a6cafa0..72e1f1eb 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/perf/IPerfOutput.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/perf/IPerfOutput.java @@ -1,48 +1,48 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.perf; - -/** - * Interface to report performance figures to. - */ -public interface IPerfOutput { - - /** Indicator for no reference time given */ - public static final long NO_REFERENCE = Long.MIN_VALUE; - - /** - * Reports the result of a time measurement with a optional reference time - * for comparison. - * - * @param description - * textual description of the test case - * @param duration - * duration in nano seconds - * @param reference - * optional reference time in nano seconds - */ - void writeTimeResult(String description, long duration, long reference); - - /** - * Reports the result of a byte size measurement with a optional reference - * size for comparison. - * - * @param description - * textual description of the test case - * @param size - * size in bytes - * @param reference - * optional reference size in bytes - */ - void writeByteResult(String description, long size, long reference); - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.perf; + +/** + * Interface to report performance figures to. + */ +public interface IPerfOutput { + + /** Indicator for no reference time given */ + public static final long NO_REFERENCE = Long.MIN_VALUE; + + /** + * Reports the result of a time measurement with a optional reference time + * for comparison. + * + * @param description + * textual description of the test case + * @param duration + * duration in nano seconds + * @param reference + * optional reference time in nano seconds + */ + void writeTimeResult(String description, long duration, long reference); + + /** + * Reports the result of a byte size measurement with a optional reference + * size for comparison. + * + * @param description + * textual description of the test case + * @param size + * size in bytes + * @param reference + * optional reference size in bytes + */ + void writeByteResult(String description, long size, long reference); + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/perf/IPerfScenario.java b/org.jacoco.core.test/src/org/jacoco/core/test/perf/IPerfScenario.java index ed0d02cb..adfafab2 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/perf/IPerfScenario.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/perf/IPerfScenario.java @@ -1,27 +1,27 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.perf; - -/** - * Interface for a performance scenario. - */ -public interface IPerfScenario { - - /** - * Runs the performance scenario and reports the result to the given - * interface. - * - * @param output - */ - public void run(IPerfOutput output) throws Exception; - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.perf; + +/** + * Interface for a performance scenario. + */ +public interface IPerfScenario { + + /** + * Runs the performance scenario and reports the result to the given + * interface. + * + * @param output + */ + public void run(IPerfOutput output) throws Exception; + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/perf/InstrumentationSizeSzenario.java b/org.jacoco.core.test/src/org/jacoco/core/test/perf/InstrumentationSizeSzenario.java index 6453f5fd..9568f555 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/perf/InstrumentationSizeSzenario.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/perf/InstrumentationSizeSzenario.java @@ -1,41 +1,41 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.perf; - -import org.jacoco.core.instr.Instrumenter; -import org.jacoco.core.runtime.IRuntime; -import org.jacoco.core.runtime.LoggerRuntime; -import org.jacoco.core.test.TargetLoader; -import org.objectweb.asm.ClassReader; - -/** - * Scenario to measure the overhead in terms of additional byte code size - * through instrumentation. - */ -public class InstrumentationSizeSzenario implements IPerfScenario { - - private final Class target; - - public InstrumentationSizeSzenario(Class target) { - this.target = target; - } - - public void run(IPerfOutput output) throws Exception { - final IRuntime runtime = new LoggerRuntime(); - ClassReader reader = new ClassReader(TargetLoader.getClassData(target)); - final Instrumenter instr = new Instrumenter(runtime); - instr.instrument(reader); - output.writeByteResult("instrumented class", - instr.instrument(reader).length, reader.b.length); - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.perf; + +import org.jacoco.core.instr.Instrumenter; +import org.jacoco.core.runtime.IRuntime; +import org.jacoco.core.runtime.LoggerRuntime; +import org.jacoco.core.test.TargetLoader; +import org.objectweb.asm.ClassReader; + +/** + * Scenario to measure the overhead in terms of additional byte code size + * through instrumentation. + */ +public class InstrumentationSizeSzenario implements IPerfScenario { + + private final Class target; + + public InstrumentationSizeSzenario(Class target) { + this.target = target; + } + + public void run(IPerfOutput output) throws Exception { + final IRuntime runtime = new LoggerRuntime(); + ClassReader reader = new ClassReader(TargetLoader.getClassData(target)); + final Instrumenter instr = new Instrumenter(runtime); + instr.instrument(reader); + output.writeByteResult("instrumented class", + instr.instrument(reader).length, reader.b.length); + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/perf/InstrumentationTimeScenario.java b/org.jacoco.core.test/src/org/jacoco/core/test/perf/InstrumentationTimeScenario.java index 581df93c..22503307 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/perf/InstrumentationTimeScenario.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/perf/InstrumentationTimeScenario.java @@ -1,46 +1,46 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.perf; - -import org.jacoco.core.instr.Instrumenter; -import org.jacoco.core.runtime.LoggerRuntime; -import org.jacoco.core.test.TargetLoader; - -/** - * Scenario to measure the time taken by the instrumentation process itself. - */ -public class InstrumentationTimeScenario extends TimedScenario { - - private final Class target; - - private final int count; - - protected InstrumentationTimeScenario(Class target, int count) { - super(String.format("instrumenting %s classes", Integer.valueOf(count))); - this.target = target; - this.count = count; - } - - @Override - protected Runnable getInstrumentedRunnable() throws Exception { - final byte[] bytes = TargetLoader.getClassDataAsBytes(target); - final Instrumenter instr = new Instrumenter(new LoggerRuntime()); - return new Runnable() { - public void run() { - for (int i = 0; i < count; i++) { - instr.instrument(bytes); - } - } - }; - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.perf; + +import org.jacoco.core.instr.Instrumenter; +import org.jacoco.core.runtime.LoggerRuntime; +import org.jacoco.core.test.TargetLoader; + +/** + * Scenario to measure the time taken by the instrumentation process itself. + */ +public class InstrumentationTimeScenario extends TimedScenario { + + private final Class target; + + private final int count; + + protected InstrumentationTimeScenario(Class target, int count) { + super(String.format("instrumenting %s classes", Integer.valueOf(count))); + this.target = target; + this.count = count; + } + + @Override + protected Runnable getInstrumentedRunnable() throws Exception { + final byte[] bytes = TargetLoader.getClassDataAsBytes(target); + final Instrumenter instr = new Instrumenter(new LoggerRuntime()); + return new Runnable() { + public void run() { + for (int i = 0; i < count; i++) { + instr.instrument(bytes); + } + } + }; + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/perf/PerfOutputWriter.java b/org.jacoco.core.test/src/org/jacoco/core/test/perf/PerfOutputWriter.java index 6586a97e..47652fe8 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/perf/PerfOutputWriter.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/perf/PerfOutputWriter.java @@ -1,80 +1,80 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.perf; - -import static java.lang.String.format; - -import java.io.PrintWriter; - -import org.jacoco.core.JaCoCo; - -/** - * Formatted text output. - */ -public class PerfOutputWriter implements IPerfOutput { - - private final PrintWriter writer; - - public PerfOutputWriter(final PrintWriter writer) { - this.writer = writer; - writeHeader(); - } - - private void writeHeader() { - writer.printf("JaCoCo Performance Data%n%n"); - writer.printf("JaCoCo Version: %s%n", JaCoCo.VERSION); - writer.printf("JVM Vendor: %s%n", - System.getProperty("java.vm.vendor")); - writer.printf("JVM Version: %s%n%n", - System.getProperty("java.vm.version")); - writer.println("scenario instr ref overhead"); - writer.println("----------------------------------------------------------"); - } - - public void writeTimeResult(final String description, final long duration, - final long reference) { - final double dms = (double) duration / 1000000; - if (reference == NO_REFERENCE) { - writeResult(description, dms, "%.2f", "ms"); - } else { - final double rms = (double) reference / 1000000; - writeResult(description, dms, rms, "%.2f", "ms"); - } - } - - public void writeByteResult(String description, long size, long reference) { - if (size == 0) { - return; - } - if (reference == NO_REFERENCE) { - writeResult(description, size, "%.0f", "bytes"); - } else { - writeResult(description, size, reference, "%.0f", "bytes"); - } - } - - private void writeResult(final String description, final double subject, - String fmt, String unit) { - writer.printf("%-30s%8s %-6s%n", description, - format(fmt, Double.valueOf(subject)), unit); - } - - private void writeResult(final String description, final double subject, - final double reference, String fmt, String unit) { - double overhead = 100 * (subject - reference) / reference; - writer.printf("%-30s%8s%8s %-6s%4.0f%%%n", description, - format(fmt, Double.valueOf(subject)), - format(fmt, Double.valueOf(reference)), unit, - Double.valueOf(overhead)); - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.perf; + +import static java.lang.String.format; + +import java.io.PrintWriter; + +import org.jacoco.core.JaCoCo; + +/** + * Formatted text output. + */ +public class PerfOutputWriter implements IPerfOutput { + + private final PrintWriter writer; + + public PerfOutputWriter(final PrintWriter writer) { + this.writer = writer; + writeHeader(); + } + + private void writeHeader() { + writer.printf("JaCoCo Performance Data%n%n"); + writer.printf("JaCoCo Version: %s%n", JaCoCo.VERSION); + writer.printf("JVM Vendor: %s%n", + System.getProperty("java.vm.vendor")); + writer.printf("JVM Version: %s%n%n", + System.getProperty("java.vm.version")); + writer.println("scenario instr ref overhead"); + writer.println("----------------------------------------------------------"); + } + + public void writeTimeResult(final String description, final long duration, + final long reference) { + final double dms = (double) duration / 1000000; + if (reference == NO_REFERENCE) { + writeResult(description, dms, "%.2f", "ms"); + } else { + final double rms = (double) reference / 1000000; + writeResult(description, dms, rms, "%.2f", "ms"); + } + } + + public void writeByteResult(String description, long size, long reference) { + if (size == 0) { + return; + } + if (reference == NO_REFERENCE) { + writeResult(description, size, "%.0f", "bytes"); + } else { + writeResult(description, size, reference, "%.0f", "bytes"); + } + } + + private void writeResult(final String description, final double subject, + String fmt, String unit) { + writer.printf("%-30s%8s %-6s%n", description, + format(fmt, Double.valueOf(subject)), unit); + } + + private void writeResult(final String description, final double subject, + final double reference, String fmt, String unit) { + double overhead = 100 * (subject - reference) / reference; + writer.printf("%-30s%8s%8s %-6s%4.0f%%%n", description, + format(fmt, Double.valueOf(subject)), + format(fmt, Double.valueOf(reference)), unit, + Double.valueOf(overhead)); + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/perf/PerformanceSuite.java b/org.jacoco.core.test/src/org/jacoco/core/test/perf/PerformanceSuite.java index d6b2f19d..c51f6260 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/perf/PerformanceSuite.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/perf/PerformanceSuite.java @@ -1,49 +1,49 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.perf; - -import java.io.PrintWriter; - -import org.jacoco.core.test.perf.targets.Target01; -import org.jacoco.core.test.perf.targets.Target02; -import org.jacoco.core.test.perf.targets.Target03; - -/** - * The main test suite. - */ -public class PerformanceSuite implements IPerfScenario { - - public void run(IPerfOutput output) throws Exception { - new ExecuteInstrumentedCodeScenario("plain method calls", - Target01.class).run(output); - new ExecuteInstrumentedCodeScenario("loop only", Target02.class) - .run(output); - new ExecuteInstrumentedCodeScenario("game of life", Target03.class) - .run(output); - new InstrumentationSizeSzenario(Target03.class).run(output); - new InstrumentationTimeScenario(Target03.class, 1000).run(output); - new AnalysisTimeScenario(Target03.class, 1000).run(output); - } - - public static void main(String[] args) throws Exception { - final PrintWriter writer; - if (args.length == 0) { - writer = new PrintWriter(System.out, true); - } else { - writer = new PrintWriter(args[0]); - } - IPerfOutput output = new PerfOutputWriter(writer); - new PerformanceSuite().run(output); - writer.close(); - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.perf; + +import java.io.PrintWriter; + +import org.jacoco.core.test.perf.targets.Target01; +import org.jacoco.core.test.perf.targets.Target02; +import org.jacoco.core.test.perf.targets.Target03; + +/** + * The main test suite. + */ +public class PerformanceSuite implements IPerfScenario { + + public void run(IPerfOutput output) throws Exception { + new ExecuteInstrumentedCodeScenario("plain method calls", + Target01.class).run(output); + new ExecuteInstrumentedCodeScenario("loop only", Target02.class) + .run(output); + new ExecuteInstrumentedCodeScenario("game of life", Target03.class) + .run(output); + new InstrumentationSizeSzenario(Target03.class).run(output); + new InstrumentationTimeScenario(Target03.class, 1000).run(output); + new AnalysisTimeScenario(Target03.class, 1000).run(output); + } + + public static void main(String[] args) throws Exception { + final PrintWriter writer; + if (args.length == 0) { + writer = new PrintWriter(System.out, true); + } else { + writer = new PrintWriter(args[0]); + } + IPerfOutput output = new PerfOutputWriter(writer); + new PerformanceSuite().run(output); + writer.close(); + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/perf/TimedScenario.java b/org.jacoco.core.test/src/org/jacoco/core/test/perf/TimedScenario.java index a68da4da..5db65a13 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/perf/TimedScenario.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/perf/TimedScenario.java @@ -1,67 +1,67 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.perf; - -/** - * Base class for execution time test scenarios. - */ -public abstract class TimedScenario implements IPerfScenario { - - private static final int RUNS = 10; - - private final String description; - - protected TimedScenario(final String description) { - this.description = description; - } - - public void run(final IPerfOutput output) throws Exception { - final long time = getMinimumTime(getInstrumentedRunnable()); - final Runnable refRunnable = getReferenceRunnable(); - final long reftime; - if (refRunnable == null) { - reftime = IPerfOutput.NO_REFERENCE; - } else { - reftime = getMinimumTime(refRunnable); - } - output.writeTimeResult(description, time, reftime); - } - - /** - * Runs the given subject several times and returns the minimum execution - * time. - * - * @param subject - * @return minimum execution time in nano seconds - */ - private long getMinimumTime(final Runnable subject) { - long min = Long.MAX_VALUE; - for (int i = 0; i < RUNS; i++) { - final long t = getTime(subject); - min = Math.min(min, t); - } - return min; - } - - private long getTime(final Runnable subject) { - long start = System.nanoTime(); - subject.run(); - return System.nanoTime() - start; - } - - protected abstract Runnable getInstrumentedRunnable() throws Exception; - - protected Runnable getReferenceRunnable() throws Exception { - return null; - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.perf; + +/** + * Base class for execution time test scenarios. + */ +public abstract class TimedScenario implements IPerfScenario { + + private static final int RUNS = 10; + + private final String description; + + protected TimedScenario(final String description) { + this.description = description; + } + + public void run(final IPerfOutput output) throws Exception { + final long time = getMinimumTime(getInstrumentedRunnable()); + final Runnable refRunnable = getReferenceRunnable(); + final long reftime; + if (refRunnable == null) { + reftime = IPerfOutput.NO_REFERENCE; + } else { + reftime = getMinimumTime(refRunnable); + } + output.writeTimeResult(description, time, reftime); + } + + /** + * Runs the given subject several times and returns the minimum execution + * time. + * + * @param subject + * @return minimum execution time in nano seconds + */ + private long getMinimumTime(final Runnable subject) { + long min = Long.MAX_VALUE; + for (int i = 0; i < RUNS; i++) { + final long t = getTime(subject); + min = Math.min(min, t); + } + return min; + } + + private long getTime(final Runnable subject) { + long start = System.nanoTime(); + subject.run(); + return System.nanoTime() - start; + } + + protected abstract Runnable getInstrumentedRunnable() throws Exception; + + protected Runnable getReferenceRunnable() throws Exception { + return null; + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/perf/targets/Target01.java b/org.jacoco.core.test/src/org/jacoco/core/test/perf/targets/Target01.java index 4879b6a0..632c149e 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/perf/targets/Target01.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/perf/targets/Target01.java @@ -1,126 +1,126 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.perf.targets; - -/** - * Plain method calls. - */ -public class Target01 implements Runnable { - - @SuppressWarnings("unused") - private int c; - - // 4 ^ 0 = 1 times - public void run() { - m1(); - m1(); - m1(); - m1(); - c++; // some side effect, otherwise the JIT will remove the method - } - - // 4 ^ 1 = 4 times - public void m1() { - m2(); - m2(); - m2(); - m2(); - c++; // some side effect, otherwise the JIT will remove the method - } - - // 4 ^ 2 == 16 times - public void m2() { - m3(); - m3(); - m3(); - m3(); - c++; // some side effect, otherwise the JIT will remove the method - } - - // 4 ^ 3 == 64 times - public void m3() { - m4(); - m4(); - m4(); - m4(); - c++; // some side effect, otherwise the JIT will remove the method - } - - // 4 ^ 4 == 256 times - public void m4() { - m5(); - m5(); - m5(); - m5(); - c++; // some side effect, otherwise the JIT will remove the method - } - - // 4 ^ 5 == 1,024 times - public void m5() { - m6(); - m6(); - m6(); - m6(); - c++; // some side effect, otherwise the JIT will remove the method - } - - // 4 ^ 6 == 4,096 times - public void m6() { - m7(); - m7(); - m7(); - m7(); - c++; // some side effect, otherwise the JIT will remove the method - } - - // 4 ^ 7 == 16,384 times - public void m7() { - m8(); - m8(); - m8(); - m8(); - c++; // some side effect, otherwise the JIT will remove the method - } - - // 4 ^ 8 == 65,536 times - public void m8() { - m9(); - m9(); - m9(); - m9(); - c++; // some side effect, otherwise the JIT will remove the method - } - - // 4 ^ 9 == 262,144 times - public void m9() { - m10(); - m10(); - m10(); - m10(); - c++; // some side effect, otherwise the JIT will remove the method - } - - // 4 ^ 10 == 1,048,576 times - public void m10() { - m11(); - m11(); - m11(); - m11(); - c++; // some side effect, otherwise the JIT will remove the method - } - - // 4 ^ 11 == 4,194,304 times - public void m11() { - c++; // some side effect, otherwise the JIT will remove the method - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.perf.targets; + +/** + * Plain method calls. + */ +public class Target01 implements Runnable { + + @SuppressWarnings("unused") + private int c; + + // 4 ^ 0 = 1 times + public void run() { + m1(); + m1(); + m1(); + m1(); + c++; // some side effect, otherwise the JIT will remove the method + } + + // 4 ^ 1 = 4 times + public void m1() { + m2(); + m2(); + m2(); + m2(); + c++; // some side effect, otherwise the JIT will remove the method + } + + // 4 ^ 2 == 16 times + public void m2() { + m3(); + m3(); + m3(); + m3(); + c++; // some side effect, otherwise the JIT will remove the method + } + + // 4 ^ 3 == 64 times + public void m3() { + m4(); + m4(); + m4(); + m4(); + c++; // some side effect, otherwise the JIT will remove the method + } + + // 4 ^ 4 == 256 times + public void m4() { + m5(); + m5(); + m5(); + m5(); + c++; // some side effect, otherwise the JIT will remove the method + } + + // 4 ^ 5 == 1,024 times + public void m5() { + m6(); + m6(); + m6(); + m6(); + c++; // some side effect, otherwise the JIT will remove the method + } + + // 4 ^ 6 == 4,096 times + public void m6() { + m7(); + m7(); + m7(); + m7(); + c++; // some side effect, otherwise the JIT will remove the method + } + + // 4 ^ 7 == 16,384 times + public void m7() { + m8(); + m8(); + m8(); + m8(); + c++; // some side effect, otherwise the JIT will remove the method + } + + // 4 ^ 8 == 65,536 times + public void m8() { + m9(); + m9(); + m9(); + m9(); + c++; // some side effect, otherwise the JIT will remove the method + } + + // 4 ^ 9 == 262,144 times + public void m9() { + m10(); + m10(); + m10(); + m10(); + c++; // some side effect, otherwise the JIT will remove the method + } + + // 4 ^ 10 == 1,048,576 times + public void m10() { + m11(); + m11(); + m11(); + m11(); + c++; // some side effect, otherwise the JIT will remove the method + } + + // 4 ^ 11 == 4,194,304 times + public void m11() { + c++; // some side effect, otherwise the JIT will remove the method + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/perf/targets/Target02.java b/org.jacoco.core.test/src/org/jacoco/core/test/perf/targets/Target02.java index 55ffbfe9..c23b50bc 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/perf/targets/Target02.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/perf/targets/Target02.java @@ -1,27 +1,27 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.perf.targets; - -/** - * Simple Loop. - */ -public class Target02 implements Runnable { - - public void run() { - @SuppressWarnings("unused") - int count = 0; - for (int i = 0; i < 10000000; i++) { - count++; - } - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.perf.targets; + +/** + * Simple Loop. + */ +public class Target02 implements Runnable { + + public void run() { + @SuppressWarnings("unused") + int count = 0; + for (int i = 0; i < 10000000; i++) { + count++; + } + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/perf/targets/Target03.java b/org.jacoco.core.test/src/org/jacoco/core/test/perf/targets/Target03.java index 689d275e..124f48ab 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/perf/targets/Target03.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/perf/targets/Target03.java @@ -1,153 +1,153 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.perf.targets; - -import java.util.Random; - -/** - * "Game of Life" implementation as a more reference scenario. Obviously the - * implementation could be more elegant using several classes, but the test - * runner targets one class only. Also one could think about more efficient - * implementations which again is not the focus here. - */ -public class Target03 implements Runnable { - - private final int width; - - private final int height; - - private boolean[][] field; - - public Target03(int width, int height) { - this.width = width; - this.height = height; - this.field = createField(); - } - - public Target03() { - this(64, 64); - } - - private boolean[][] createField() { - boolean[][] f = new boolean[height][]; - for (int i = 0; i < height; i++) { - f[i] = new boolean[width]; - } - return f; - } - - public void set(int x, int y, boolean flag) { - field[wrap(x, width)][wrap(y, height)] = flag; - } - - public boolean get(int x, int y) { - return field[wrap(x, width)][wrap(y, height)]; - } - - public void clear() { - field = createField(); - } - - public void randomFill(long seed, int count) { - Random r = new Random(seed); - for (int i = 0; i < count; i++) { - set(r.nextInt(), r.nextInt(), true); - } - - } - - public void tick() { - boolean[][] next = createField(); - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - final int n = getNeighbors(x, y); - if (get(x, y)) { - next[x][y] = 2 <= n && n <= 3; - } else { - next[x][y] = n == 3; - } - } - } - field = next; - } - - // Neighbor - private int getNeighbors(int x, int y) { - int count = 0; - if (get(x - 1, y - 1)) { - count++; - } - if (get(x + 0, y - 1)) { - count++; - } - if (get(x + 1, y - 1)) { - count++; - } - if (get(x + 1, y + 0)) { - count++; - } - if (get(x + 1, y + 1)) { - count++; - } - if (get(x + 0, y + 1)) { - count++; - } - if (get(x - 1, y + 1)) { - count++; - } - if (get(x - 1, y + 0)) { - count++; - } - return count; - } - - private int wrap(int value, int size) { - int res = value % size; - if (res < 0) { - res += size; - } - return res; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - sb.append(get(x, y) ? 'O' : '.'); - } - sb.append('\n'); - } - return sb.toString(); - } - - public void run() { - clear(); - randomFill(123, width * height / 2); - for (int i = 0; i < 20; i++) { - tick(); - } - } - - // Demo - public static void main(String[] args) { - Target03 t = new Target03(10, 10); - t.randomFill(123, 20); - - for (int i = 0; i < 10; i++) { - System.out.println("Generation " + i + ":"); - System.out.println(t); - t.tick(); - } - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.perf.targets; + +import java.util.Random; + +/** + * "Game of Life" implementation as a more reference scenario. Obviously the + * implementation could be more elegant using several classes, but the test + * runner targets one class only. Also one could think about more efficient + * implementations which again is not the focus here. + */ +public class Target03 implements Runnable { + + private final int width; + + private final int height; + + private boolean[][] field; + + public Target03(int width, int height) { + this.width = width; + this.height = height; + this.field = createField(); + } + + public Target03() { + this(64, 64); + } + + private boolean[][] createField() { + boolean[][] f = new boolean[height][]; + for (int i = 0; i < height; i++) { + f[i] = new boolean[width]; + } + return f; + } + + public void set(int x, int y, boolean flag) { + field[wrap(x, width)][wrap(y, height)] = flag; + } + + public boolean get(int x, int y) { + return field[wrap(x, width)][wrap(y, height)]; + } + + public void clear() { + field = createField(); + } + + public void randomFill(long seed, int count) { + Random r = new Random(seed); + for (int i = 0; i < count; i++) { + set(r.nextInt(), r.nextInt(), true); + } + + } + + public void tick() { + boolean[][] next = createField(); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + final int n = getNeighbors(x, y); + if (get(x, y)) { + next[x][y] = 2 <= n && n <= 3; + } else { + next[x][y] = n == 3; + } + } + } + field = next; + } + + // Neighbor + private int getNeighbors(int x, int y) { + int count = 0; + if (get(x - 1, y - 1)) { + count++; + } + if (get(x + 0, y - 1)) { + count++; + } + if (get(x + 1, y - 1)) { + count++; + } + if (get(x + 1, y + 0)) { + count++; + } + if (get(x + 1, y + 1)) { + count++; + } + if (get(x + 0, y + 1)) { + count++; + } + if (get(x - 1, y + 1)) { + count++; + } + if (get(x - 1, y + 0)) { + count++; + } + return count; + } + + private int wrap(int value, int size) { + int res = value % size; + if (res < 0) { + res += size; + } + return res; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + sb.append(get(x, y) ? 'O' : '.'); + } + sb.append('\n'); + } + return sb.toString(); + } + + public void run() { + clear(); + randomFill(123, width * height / 2); + for (int i = 0; i < 20; i++) { + tick(); + } + } + + // Demo + public static void main(String[] args) { + Target03 t = new Target03(10, 10); + t.randomFill(123, 20); + + for (int i = 0; i < 10; i++) { + System.out.println("Generation " + i + ":"); + System.out.println(t); + t.tick(); + } + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/validation/BooleanExpressionsTest.java b/org.jacoco.core.test/src/org/jacoco/core/test/validation/BooleanExpressionsTest.java index eb1f836a..e59dcf50 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/validation/BooleanExpressionsTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/validation/BooleanExpressionsTest.java @@ -1,85 +1,85 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.validation; - -import org.jacoco.core.analysis.ICounter; -import org.jacoco.core.test.validation.targets.Target02; -import org.junit.Test; - -/** - * Tests of basic Java boolean expressions. - */ -public class BooleanExpressionsTest extends ValidationTestBase { - - public BooleanExpressionsTest() { - super(Target02.class); - } - - @Override - protected void run(final Class targetClass) throws Exception { - final Object instance = targetClass.newInstance(); - ((Runnable) instance).run(); - } - - @Test - public void testCoverageResult() { - - // 1. Boolean comparison result (one case) - assertLine("booleancmp1", ICounter.PARTLY_COVERED, 1, 1); - - // 2. Boolean comparison result (both cases) - assertLine("booleancmp2", ICounter.FULLY_COVERED, 0, 2); - - // 3. And - assertLine("andFF", ICounter.FULLY_COVERED, 1, 1); - assertLine("andFT", ICounter.FULLY_COVERED, 1, 1); - assertLine("andTF", ICounter.FULLY_COVERED, 1, 1); - assertLine("andTT", ICounter.FULLY_COVERED, 1, 1); - - // 4. Conditional And - assertLine("conditionalandFF", ICounter.PARTLY_COVERED, 3, 1); - assertLine("conditionalandFT", ICounter.PARTLY_COVERED, 3, 1); - assertLine("conditionalandTF", ICounter.FULLY_COVERED, 2, 2); - assertLine("conditionalandTT", ICounter.FULLY_COVERED, 2, 2); - - // 5. Or - assertLine("orFF", ICounter.FULLY_COVERED, 1, 1); - assertLine("orFT", ICounter.FULLY_COVERED, 1, 1); - assertLine("orTF", ICounter.FULLY_COVERED, 1, 1); - assertLine("orTT", ICounter.FULLY_COVERED, 1, 1); - - // 6. Conditional Or - assertLine("conditionalorFF", ICounter.FULLY_COVERED, 2, 2); - assertLine("conditionalorFT", ICounter.FULLY_COVERED, 2, 2); - assertLine("conditionalorTF", ICounter.PARTLY_COVERED, 3, 1); - assertLine("conditionalorTT", ICounter.PARTLY_COVERED, 3, 1); - - // 7. Exclusive Or - assertLine("xorFF", ICounter.FULLY_COVERED, 1, 1); - assertLine("xorFT", ICounter.FULLY_COVERED, 1, 1); - assertLine("xorTF", ICounter.FULLY_COVERED, 1, 1); - assertLine("xorTT", ICounter.FULLY_COVERED, 1, 1); - - // 8. Conditional Operator - assertLine("condT", ICounter.PARTLY_COVERED, 1, 1); - assertLine("condF", ICounter.PARTLY_COVERED, 1, 1); - - // 9. Not (one case) - assertLine("notT", ICounter.PARTLY_COVERED, 1, 1); - assertLine("notF", ICounter.PARTLY_COVERED, 1, 1); - - // 10. Not (both cases) - assertLine("notTF", ICounter.FULLY_COVERED, 0, 2); - - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.validation; + +import org.jacoco.core.analysis.ICounter; +import org.jacoco.core.test.validation.targets.Target02; +import org.junit.Test; + +/** + * Tests of basic Java boolean expressions. + */ +public class BooleanExpressionsTest extends ValidationTestBase { + + public BooleanExpressionsTest() { + super(Target02.class); + } + + @Override + protected void run(final Class targetClass) throws Exception { + final Object instance = targetClass.newInstance(); + ((Runnable) instance).run(); + } + + @Test + public void testCoverageResult() { + + // 1. Boolean comparison result (one case) + assertLine("booleancmp1", ICounter.PARTLY_COVERED, 1, 1); + + // 2. Boolean comparison result (both cases) + assertLine("booleancmp2", ICounter.FULLY_COVERED, 0, 2); + + // 3. And + assertLine("andFF", ICounter.FULLY_COVERED, 1, 1); + assertLine("andFT", ICounter.FULLY_COVERED, 1, 1); + assertLine("andTF", ICounter.FULLY_COVERED, 1, 1); + assertLine("andTT", ICounter.FULLY_COVERED, 1, 1); + + // 4. Conditional And + assertLine("conditionalandFF", ICounter.PARTLY_COVERED, 3, 1); + assertLine("conditionalandFT", ICounter.PARTLY_COVERED, 3, 1); + assertLine("conditionalandTF", ICounter.FULLY_COVERED, 2, 2); + assertLine("conditionalandTT", ICounter.FULLY_COVERED, 2, 2); + + // 5. Or + assertLine("orFF", ICounter.FULLY_COVERED, 1, 1); + assertLine("orFT", ICounter.FULLY_COVERED, 1, 1); + assertLine("orTF", ICounter.FULLY_COVERED, 1, 1); + assertLine("orTT", ICounter.FULLY_COVERED, 1, 1); + + // 6. Conditional Or + assertLine("conditionalorFF", ICounter.FULLY_COVERED, 2, 2); + assertLine("conditionalorFT", ICounter.FULLY_COVERED, 2, 2); + assertLine("conditionalorTF", ICounter.PARTLY_COVERED, 3, 1); + assertLine("conditionalorTT", ICounter.PARTLY_COVERED, 3, 1); + + // 7. Exclusive Or + assertLine("xorFF", ICounter.FULLY_COVERED, 1, 1); + assertLine("xorFT", ICounter.FULLY_COVERED, 1, 1); + assertLine("xorTF", ICounter.FULLY_COVERED, 1, 1); + assertLine("xorTT", ICounter.FULLY_COVERED, 1, 1); + + // 8. Conditional Operator + assertLine("condT", ICounter.PARTLY_COVERED, 1, 1); + assertLine("condF", ICounter.PARTLY_COVERED, 1, 1); + + // 9. Not (one case) + assertLine("notT", ICounter.PARTLY_COVERED, 1, 1); + assertLine("notF", ICounter.PARTLY_COVERED, 1, 1); + + // 10. Not (both cases) + assertLine("notTF", ICounter.FULLY_COVERED, 0, 2); + + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/validation/ControlStructuresTest.java b/org.jacoco.core.test/src/org/jacoco/core/test/validation/ControlStructuresTest.java index 5d2a5ab8..a512f93f 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/validation/ControlStructuresTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/validation/ControlStructuresTest.java @@ -1,139 +1,139 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.validation; - -import org.jacoco.core.analysis.ICounter; -import org.jacoco.core.test.validation.targets.Target01; -import org.junit.Test; - -/** - * Tests of basic Java control structures. - */ -public class ControlStructuresTest extends ValidationTestBase { - - public ControlStructuresTest() { - super(Target01.class); - } - - @Override - protected void run(final Class targetClass) throws Exception { - final Object instance = targetClass.newInstance(); - ((Runnable) instance).run(); - } - - @Test - public void testCoverageResult() { - - // 1. Direct unconditional execution - assertLine("unconditional", ICounter.FULLY_COVERED); - - // 2. Missed if block - assertLine("iffalse", ICounter.FULLY_COVERED, 1, 1); - assertLine("missedif", ICounter.NOT_COVERED); - assertLine("executedelse", ICounter.FULLY_COVERED); - - // 3. Executed if block - assertLine("iftrue", ICounter.FULLY_COVERED, 1, 1); - assertLine("executedif", ICounter.FULLY_COVERED); - assertLine("missedelse", ICounter.NOT_COVERED); - - // 4. Missed while block - // ECJ and javac produce different status here - assertLine("whilefalse", 1, 1); - assertLine("missedwhile", ICounter.NOT_COVERED); - - // 5. Always true while block - assertLine("whiletrue", ICounter.FULLY_COVERED, 1, 1); - - // 6. Executed while block - assertLine("whiletruefalse", ICounter.FULLY_COVERED, 0, 2); - assertLine("executedwhile", ICounter.FULLY_COVERED); - - // 7. Executed do while block - assertLine("executeddowhile", ICounter.FULLY_COVERED); - - // 8. Missed for block - assertLine("missedforincrementer", ICounter.PARTLY_COVERED, 1, 1); - assertLine("missedfor", ICounter.NOT_COVERED); - - // 9. Executed for block - assertLine("executedforincrementer", ICounter.FULLY_COVERED, 0, 2); - assertLine("executedfor", ICounter.FULLY_COVERED); - - // 10. Missed for each block - assertLine("missedforeachincrementer", ICounter.PARTLY_COVERED, 1, 1); - assertLine("missedforeach", ICounter.NOT_COVERED); - - // 11. Executed for each block - assertLine("executedforeachincrementer", ICounter.FULLY_COVERED, 0, 2); - assertLine("executedforeach", ICounter.FULLY_COVERED); - - // 12. Table switch with hit - assertLine("tswitch1", ICounter.FULLY_COVERED, 3, 1); - assertLine("tswitch1case1", ICounter.NOT_COVERED); - assertLine("tswitch1case2", ICounter.FULLY_COVERED); - assertLine("tswitch1case3", ICounter.NOT_COVERED); - assertLine("tswitch1default", ICounter.NOT_COVERED); - - // 13. Continued table switch with hit - assertLine("tswitch2", ICounter.FULLY_COVERED, 3, 1); - assertLine("tswitch2case1", ICounter.NOT_COVERED); - assertLine("tswitch2case2", ICounter.FULLY_COVERED); - assertLine("tswitch2case3", ICounter.FULLY_COVERED); - assertLine("tswitch2default", ICounter.FULLY_COVERED); - - // 14. Table switch without hit - assertLine("tswitch2", ICounter.FULLY_COVERED, 3, 1); - assertLine("tswitch3case1", ICounter.NOT_COVERED); - assertLine("tswitch3case2", ICounter.NOT_COVERED); - assertLine("tswitch3case3", ICounter.NOT_COVERED); - assertLine("tswitch3default", ICounter.FULLY_COVERED); - - // 15. Lookup switch with hit - assertLine("lswitch1", ICounter.FULLY_COVERED, 3, 1); - assertLine("lswitch1case1", ICounter.NOT_COVERED); - assertLine("lswitch1case2", ICounter.FULLY_COVERED); - assertLine("lswitch1case3", ICounter.NOT_COVERED); - assertLine("lswitch1default", ICounter.NOT_COVERED); - - // 16. Continued lookup switch with hit - assertLine("lswitch2", ICounter.FULLY_COVERED, 3, 1); - assertLine("lswitch2case1", ICounter.NOT_COVERED); - assertLine("lswitch2case2", ICounter.FULLY_COVERED); - assertLine("lswitch2case3", ICounter.FULLY_COVERED); - assertLine("lswitch2default", ICounter.FULLY_COVERED); - - // 17. Lookup switch without hit - assertLine("lswitch3", ICounter.FULLY_COVERED, 3, 1); - assertLine("lswitch3case1", ICounter.NOT_COVERED); - assertLine("lswitch3case2", ICounter.NOT_COVERED); - assertLine("lswitch3case3", ICounter.NOT_COVERED); - assertLine("lswitch3default", ICounter.FULLY_COVERED); - - // 18. Break statement - assertLine("executedbreak", ICounter.FULLY_COVERED); - assertLine("missedafterbreak", ICounter.NOT_COVERED); - - // 19. Continue statement - assertLine("executedcontinue", ICounter.FULLY_COVERED); - assertLine("missedaftercontinue", ICounter.NOT_COVERED); - - // 20. Return statement - assertLine("return", ICounter.FULLY_COVERED); - assertLine("afterreturn", ICounter.NOT_COVERED); - - // 21. Implicit return - assertLine("implicitreturn", ICounter.FULLY_COVERED); - - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.validation; + +import org.jacoco.core.analysis.ICounter; +import org.jacoco.core.test.validation.targets.Target01; +import org.junit.Test; + +/** + * Tests of basic Java control structures. + */ +public class ControlStructuresTest extends ValidationTestBase { + + public ControlStructuresTest() { + super(Target01.class); + } + + @Override + protected void run(final Class targetClass) throws Exception { + final Object instance = targetClass.newInstance(); + ((Runnable) instance).run(); + } + + @Test + public void testCoverageResult() { + + // 1. Direct unconditional execution + assertLine("unconditional", ICounter.FULLY_COVERED); + + // 2. Missed if block + assertLine("iffalse", ICounter.FULLY_COVERED, 1, 1); + assertLine("missedif", ICounter.NOT_COVERED); + assertLine("executedelse", ICounter.FULLY_COVERED); + + // 3. Executed if block + assertLine("iftrue", ICounter.FULLY_COVERED, 1, 1); + assertLine("executedif", ICounter.FULLY_COVERED); + assertLine("missedelse", ICounter.NOT_COVERED); + + // 4. Missed while block + // ECJ and javac produce different status here + assertLine("whilefalse", 1, 1); + assertLine("missedwhile", ICounter.NOT_COVERED); + + // 5. Always true while block + assertLine("whiletrue", ICounter.FULLY_COVERED, 1, 1); + + // 6. Executed while block + assertLine("whiletruefalse", ICounter.FULLY_COVERED, 0, 2); + assertLine("executedwhile", ICounter.FULLY_COVERED); + + // 7. Executed do while block + assertLine("executeddowhile", ICounter.FULLY_COVERED); + + // 8. Missed for block + assertLine("missedforincrementer", ICounter.PARTLY_COVERED, 1, 1); + assertLine("missedfor", ICounter.NOT_COVERED); + + // 9. Executed for block + assertLine("executedforincrementer", ICounter.FULLY_COVERED, 0, 2); + assertLine("executedfor", ICounter.FULLY_COVERED); + + // 10. Missed for each block + assertLine("missedforeachincrementer", ICounter.PARTLY_COVERED, 1, 1); + assertLine("missedforeach", ICounter.NOT_COVERED); + + // 11. Executed for each block + assertLine("executedforeachincrementer", ICounter.FULLY_COVERED, 0, 2); + assertLine("executedforeach", ICounter.FULLY_COVERED); + + // 12. Table switch with hit + assertLine("tswitch1", ICounter.FULLY_COVERED, 3, 1); + assertLine("tswitch1case1", ICounter.NOT_COVERED); + assertLine("tswitch1case2", ICounter.FULLY_COVERED); + assertLine("tswitch1case3", ICounter.NOT_COVERED); + assertLine("tswitch1default", ICounter.NOT_COVERED); + + // 13. Continued table switch with hit + assertLine("tswitch2", ICounter.FULLY_COVERED, 3, 1); + assertLine("tswitch2case1", ICounter.NOT_COVERED); + assertLine("tswitch2case2", ICounter.FULLY_COVERED); + assertLine("tswitch2case3", ICounter.FULLY_COVERED); + assertLine("tswitch2default", ICounter.FULLY_COVERED); + + // 14. Table switch without hit + assertLine("tswitch2", ICounter.FULLY_COVERED, 3, 1); + assertLine("tswitch3case1", ICounter.NOT_COVERED); + assertLine("tswitch3case2", ICounter.NOT_COVERED); + assertLine("tswitch3case3", ICounter.NOT_COVERED); + assertLine("tswitch3default", ICounter.FULLY_COVERED); + + // 15. Lookup switch with hit + assertLine("lswitch1", ICounter.FULLY_COVERED, 3, 1); + assertLine("lswitch1case1", ICounter.NOT_COVERED); + assertLine("lswitch1case2", ICounter.FULLY_COVERED); + assertLine("lswitch1case3", ICounter.NOT_COVERED); + assertLine("lswitch1default", ICounter.NOT_COVERED); + + // 16. Continued lookup switch with hit + assertLine("lswitch2", ICounter.FULLY_COVERED, 3, 1); + assertLine("lswitch2case1", ICounter.NOT_COVERED); + assertLine("lswitch2case2", ICounter.FULLY_COVERED); + assertLine("lswitch2case3", ICounter.FULLY_COVERED); + assertLine("lswitch2default", ICounter.FULLY_COVERED); + + // 17. Lookup switch without hit + assertLine("lswitch3", ICounter.FULLY_COVERED, 3, 1); + assertLine("lswitch3case1", ICounter.NOT_COVERED); + assertLine("lswitch3case2", ICounter.NOT_COVERED); + assertLine("lswitch3case3", ICounter.NOT_COVERED); + assertLine("lswitch3default", ICounter.FULLY_COVERED); + + // 18. Break statement + assertLine("executedbreak", ICounter.FULLY_COVERED); + assertLine("missedafterbreak", ICounter.NOT_COVERED); + + // 19. Continue statement + assertLine("executedcontinue", ICounter.FULLY_COVERED); + assertLine("missedaftercontinue", ICounter.NOT_COVERED); + + // 20. Return statement + assertLine("return", ICounter.FULLY_COVERED); + assertLine("afterreturn", ICounter.NOT_COVERED); + + // 21. Implicit return + assertLine("implicitreturn", ICounter.FULLY_COVERED); + + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/validation/ExceptionsTest.java b/org.jacoco.core.test/src/org/jacoco/core/test/validation/ExceptionsTest.java index 1a8a04f0..885ee1a9 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/validation/ExceptionsTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/validation/ExceptionsTest.java @@ -1,98 +1,98 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.validation; - -import org.jacoco.core.analysis.ICounter; -import org.jacoco.core.test.validation.targets.Target03; -import org.junit.Test; - -/** - * Tests of exception based control flow. - */ -public class ExceptionsTest extends ValidationTestBase { - - public ExceptionsTest() { - super(Target03.class); - } - - @Override - protected void run(final Class targetClass) throws Exception { - final Object instance = targetClass.newInstance(); - ((Runnable) instance).run(); - } - - @Test - public void testCoverageResult() { - - // 1. Implicit Exception - // Currently no coverage at all, as we don't see when a block aborts - // somewhere in the middle. - assertLine("implicitException.before", ICounter.NOT_COVERED); - assertLine("implicitException.exception", ICounter.NOT_COVERED); - assertLine("implicitException.after", ICounter.NOT_COVERED); - - // 2. Explicit Exception - // Full coverage, as we recognize throw statements as block boundaries. - assertLine("explicitException.before", ICounter.FULLY_COVERED); - assertLine("explicitException.throw", ICounter.FULLY_COVERED); - - // 3. Try/Catch Block Without Exception Thrown - assertLine("noExceptionTryCatch.beforeBlock", ICounter.FULLY_COVERED); - assertLine("noExceptionTryCatch.tryBlock", ICounter.FULLY_COVERED); - assertLine("noExceptionTryCatch.catchBlock", ICounter.NOT_COVERED); - - // 4. Try/Catch Block Without a Implicit Exception Thrown - // As always with implicit exceptions we don't see when a block aborts - // somewhere in the middle. - assertLine("implicitExceptionTryCatch.beforeBlock", - ICounter.FULLY_COVERED); - assertLine("implicitExceptionTryCatch.before", ICounter.NOT_COVERED); - assertLine("implicitExceptionTryCatch.exception", ICounter.NOT_COVERED); - assertLine("implicitExceptionTryCatch.after", ICounter.NOT_COVERED); - assertLine("implicitExceptionTryCatch.catchBlock", - ICounter.FULLY_COVERED); - - // 5. Try/Catch Block With Exception Thrown Explicitly - assertLine("explicitExceptionTryCatch.beforeBlock", - ICounter.FULLY_COVERED); - assertLine("explicitExceptionTryCatch.before", ICounter.FULLY_COVERED); - assertLine("explicitExceptionTryCatch.throw", ICounter.FULLY_COVERED); - assertLine("explicitExceptionTryCatch.catchBlock", - ICounter.FULLY_COVERED); - - // 6. Finally Block Without Exception Thrown - // Finally block is yellow as the exception path is missing. - assertLine("noExceptionFinally.beforeBlock", ICounter.FULLY_COVERED); - assertLine("noExceptionFinally.tryBlock", ICounter.FULLY_COVERED); - assertLine("noExceptionFinally.finallyBlock", ICounter.PARTLY_COVERED); - - // 7. Finally Block With Implicit Exception - // Finally block is yellow as the non-exception path is missing. - assertLine("implicitExceptionFinally.beforeBlock", - ICounter.FULLY_COVERED); - assertLine("implicitExceptionFinally.before", ICounter.NOT_COVERED); - assertLine("implicitExceptionFinally.exception", ICounter.NOT_COVERED); - assertLine("implicitExceptionFinally.after", ICounter.NOT_COVERED); - assertLine("implicitExceptionFinally.finallyBlock", - ICounter.PARTLY_COVERED); - - // 8. Finally Block With Exception Thrown Explicitly - assertLine("explicitExceptionFinally.beforeBlock", - ICounter.FULLY_COVERED); - assertLine("explicitExceptionFinally.before", ICounter.FULLY_COVERED); - assertLine("explicitExceptionFinally.throw", ICounter.FULLY_COVERED); - assertLine("explicitExceptionFinally.finallyBlock", - ICounter.FULLY_COVERED); - - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.validation; + +import org.jacoco.core.analysis.ICounter; +import org.jacoco.core.test.validation.targets.Target03; +import org.junit.Test; + +/** + * Tests of exception based control flow. + */ +public class ExceptionsTest extends ValidationTestBase { + + public ExceptionsTest() { + super(Target03.class); + } + + @Override + protected void run(final Class targetClass) throws Exception { + final Object instance = targetClass.newInstance(); + ((Runnable) instance).run(); + } + + @Test + public void testCoverageResult() { + + // 1. Implicit Exception + // Currently no coverage at all, as we don't see when a block aborts + // somewhere in the middle. + assertLine("implicitException.before", ICounter.NOT_COVERED); + assertLine("implicitException.exception", ICounter.NOT_COVERED); + assertLine("implicitException.after", ICounter.NOT_COVERED); + + // 2. Explicit Exception + // Full coverage, as we recognize throw statements as block boundaries. + assertLine("explicitException.before", ICounter.FULLY_COVERED); + assertLine("explicitException.throw", ICounter.FULLY_COVERED); + + // 3. Try/Catch Block Without Exception Thrown + assertLine("noExceptionTryCatch.beforeBlock", ICounter.FULLY_COVERED); + assertLine("noExceptionTryCatch.tryBlock", ICounter.FULLY_COVERED); + assertLine("noExceptionTryCatch.catchBlock", ICounter.NOT_COVERED); + + // 4. Try/Catch Block Without a Implicit Exception Thrown + // As always with implicit exceptions we don't see when a block aborts + // somewhere in the middle. + assertLine("implicitExceptionTryCatch.beforeBlock", + ICounter.FULLY_COVERED); + assertLine("implicitExceptionTryCatch.before", ICounter.NOT_COVERED); + assertLine("implicitExceptionTryCatch.exception", ICounter.NOT_COVERED); + assertLine("implicitExceptionTryCatch.after", ICounter.NOT_COVERED); + assertLine("implicitExceptionTryCatch.catchBlock", + ICounter.FULLY_COVERED); + + // 5. Try/Catch Block With Exception Thrown Explicitly + assertLine("explicitExceptionTryCatch.beforeBlock", + ICounter.FULLY_COVERED); + assertLine("explicitExceptionTryCatch.before", ICounter.FULLY_COVERED); + assertLine("explicitExceptionTryCatch.throw", ICounter.FULLY_COVERED); + assertLine("explicitExceptionTryCatch.catchBlock", + ICounter.FULLY_COVERED); + + // 6. Finally Block Without Exception Thrown + // Finally block is yellow as the exception path is missing. + assertLine("noExceptionFinally.beforeBlock", ICounter.FULLY_COVERED); + assertLine("noExceptionFinally.tryBlock", ICounter.FULLY_COVERED); + assertLine("noExceptionFinally.finallyBlock", ICounter.PARTLY_COVERED); + + // 7. Finally Block With Implicit Exception + // Finally block is yellow as the non-exception path is missing. + assertLine("implicitExceptionFinally.beforeBlock", + ICounter.FULLY_COVERED); + assertLine("implicitExceptionFinally.before", ICounter.NOT_COVERED); + assertLine("implicitExceptionFinally.exception", ICounter.NOT_COVERED); + assertLine("implicitExceptionFinally.after", ICounter.NOT_COVERED); + assertLine("implicitExceptionFinally.finallyBlock", + ICounter.PARTLY_COVERED); + + // 8. Finally Block With Exception Thrown Explicitly + assertLine("explicitExceptionFinally.beforeBlock", + ICounter.FULLY_COVERED); + assertLine("explicitExceptionFinally.before", ICounter.FULLY_COVERED); + assertLine("explicitExceptionFinally.throw", ICounter.FULLY_COVERED); + assertLine("explicitExceptionFinally.finallyBlock", + ICounter.FULLY_COVERED); + + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/validation/Source.java b/org.jacoco.core.test/src/org/jacoco/core/test/validation/Source.java index 156fb78d..ec7a5c38 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/validation/Source.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/validation/Source.java @@ -1,116 +1,116 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.validation; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; -import java.io.Reader; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Reads a single source file and allows access to it through special probe - * comments in the following format //$line-tag$. - */ -public class Source { - - /** - * Reads the source for the given type from the ./src/ folder - * relative to the working directory. - * - * @param type - * type to load the source file for - * @throws IOException - * @throws - */ - public static Source getSourceFor(final Class type) throws IOException { - String file = "src/" + type.getName().replace('.', '/') + ".java"; - return new Source(new FileReader(file)); - } - - private static final Pattern TAG_PATTERN = Pattern - .compile("\\$line-(.*)\\$"); - - private final List lines = new ArrayList(); - - private final Map tags = new HashMap(); - - /** - * Reads a source file from the given reader. - * - * @param reader - * @throws IOException - */ - public Source(final Reader reader) throws IOException { - final BufferedReader buffer = new BufferedReader(reader); - for (String l = buffer.readLine(); l != null; l = buffer.readLine()) { - addLine(l); - } - buffer.close(); - } - - private void addLine(final String l) { - lines.add(l); - final Matcher m = TAG_PATTERN.matcher(l); - if (m.find()) { - final String tag = m.group(1); - if (tags.put(tag, Integer.valueOf(lines.size())) != null) { - throw new IllegalArgumentException("Duplicate tag: " + tag); - } - } - } - - /** - * Returns all lines of the source file as a list. - * - * @return all lines of the source file - */ - public List getLines() { - return Collections.unmodifiableList(lines); - } - - /** - * Returns the line with the given number - * - * @param nr - * line number (first line is 1) - * @return line content - */ - public String getLine(int nr) { - return lines.get(nr - 1); - } - - /** - * Returns the line number with the given tag - * - * @param tag - * tag from a //$line-tag$ marker - * @return line number (first line is 1) - * @throws NoSuchElementException - * if there is no such tag - */ - public int getLineNumber(String tag) throws NoSuchElementException { - final Integer nr = tags.get(tag); - if (nr == null) { - throw new NoSuchElementException("Unknown tag: " + tag); - } - return nr.intValue(); - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.validation; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Reads a single source file and allows access to it through special probe + * comments in the following format //$line-tag$. + */ +public class Source { + + /** + * Reads the source for the given type from the ./src/ folder + * relative to the working directory. + * + * @param type + * type to load the source file for + * @throws IOException + * @throws + */ + public static Source getSourceFor(final Class type) throws IOException { + String file = "src/" + type.getName().replace('.', '/') + ".java"; + return new Source(new FileReader(file)); + } + + private static final Pattern TAG_PATTERN = Pattern + .compile("\\$line-(.*)\\$"); + + private final List lines = new ArrayList(); + + private final Map tags = new HashMap(); + + /** + * Reads a source file from the given reader. + * + * @param reader + * @throws IOException + */ + public Source(final Reader reader) throws IOException { + final BufferedReader buffer = new BufferedReader(reader); + for (String l = buffer.readLine(); l != null; l = buffer.readLine()) { + addLine(l); + } + buffer.close(); + } + + private void addLine(final String l) { + lines.add(l); + final Matcher m = TAG_PATTERN.matcher(l); + if (m.find()) { + final String tag = m.group(1); + if (tags.put(tag, Integer.valueOf(lines.size())) != null) { + throw new IllegalArgumentException("Duplicate tag: " + tag); + } + } + } + + /** + * Returns all lines of the source file as a list. + * + * @return all lines of the source file + */ + public List getLines() { + return Collections.unmodifiableList(lines); + } + + /** + * Returns the line with the given number + * + * @param nr + * line number (first line is 1) + * @return line content + */ + public String getLine(int nr) { + return lines.get(nr - 1); + } + + /** + * Returns the line number with the given tag + * + * @param tag + * tag from a //$line-tag$ marker + * @return line number (first line is 1) + * @throws NoSuchElementException + * if there is no such tag + */ + public int getLineNumber(String tag) throws NoSuchElementException { + final Integer nr = tags.get(tag); + if (nr == null) { + throw new NoSuchElementException("Unknown tag: " + tag); + } + return nr.intValue(); + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/validation/SourceTest.java b/org.jacoco.core.test/src/org/jacoco/core/test/validation/SourceTest.java index 9a5e2392..1e7f1e02 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/validation/SourceTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/validation/SourceTest.java @@ -1,87 +1,87 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.validation; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.io.StringReader; -import java.util.Arrays; -import java.util.NoSuchElementException; - -import org.junit.Test; - -/** - * Unit tests for {@link Source}. - */ -public class SourceTest { - - @Test - public void testGetLines1() throws IOException { - String src = "\na\nbb\n"; - final Source s = new Source(new StringReader(src)); - assertEquals(Arrays.asList("", "a", "bb"), s.getLines()); - } - - @Test - public void testGetLines2() throws IOException { - String src = "aa\nbb\n;"; - final Source s = new Source(new StringReader(src)); - assertEquals(Arrays.asList("aa", "bb", ";"), s.getLines()); - } - - @Test - public void testGetLines3() throws IOException { - String src = "xx\r\nyy"; - final Source s = new Source(new StringReader(src)); - assertEquals(Arrays.asList("xx", "yy"), s.getLines()); - } - - @Test - public void testGetLine() throws IOException { - String src = "Hello\n\nWorld!"; - final Source s = new Source(new StringReader(src)); - assertEquals("Hello", s.getLine(1)); - assertEquals("", s.getLine(2)); - assertEquals("World!", s.getLine(3)); - } - - @Test - public void testGetLineNumber() throws IOException { - String src = "a\nb$line-tag$\nc\nd\ne$line-tagx$\nf"; - final Source s = new Source(new StringReader(src)); - assertEquals(2, s.getLineNumber("tag"), 0.0); - } - - @Test(expected = NoSuchElementException.class) - public void testGetLineNumberNegative() throws IOException { - String src = "a\nb$line-tag$\nc\nd\ne\nf"; - final Source s = new Source(new StringReader(src)); - s.getLineNumber("ag"); - } - - @Test(expected = IllegalArgumentException.class) - public void testDuplicateTag() throws IOException { - String src = "a\nb$line-tag$\nc\nd\ne$line-tag$\nf"; - new Source(new StringReader(src)); - } - - @Test - public void testGetSourceFor() throws IOException { - final Source s = Source.getSourceFor(SourceTest.class); - // Here we are. $line-testGetSourceFor$ - final String l = s.getLine(s.getLineNumber("testGetSourceFor")); - assertTrue(l, l.contains("Here we are.")); - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.validation; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.io.StringReader; +import java.util.Arrays; +import java.util.NoSuchElementException; + +import org.junit.Test; + +/** + * Unit tests for {@link Source}. + */ +public class SourceTest { + + @Test + public void testGetLines1() throws IOException { + String src = "\na\nbb\n"; + final Source s = new Source(new StringReader(src)); + assertEquals(Arrays.asList("", "a", "bb"), s.getLines()); + } + + @Test + public void testGetLines2() throws IOException { + String src = "aa\nbb\n;"; + final Source s = new Source(new StringReader(src)); + assertEquals(Arrays.asList("aa", "bb", ";"), s.getLines()); + } + + @Test + public void testGetLines3() throws IOException { + String src = "xx\r\nyy"; + final Source s = new Source(new StringReader(src)); + assertEquals(Arrays.asList("xx", "yy"), s.getLines()); + } + + @Test + public void testGetLine() throws IOException { + String src = "Hello\n\nWorld!"; + final Source s = new Source(new StringReader(src)); + assertEquals("Hello", s.getLine(1)); + assertEquals("", s.getLine(2)); + assertEquals("World!", s.getLine(3)); + } + + @Test + public void testGetLineNumber() throws IOException { + String src = "a\nb$line-tag$\nc\nd\ne$line-tagx$\nf"; + final Source s = new Source(new StringReader(src)); + assertEquals(2, s.getLineNumber("tag"), 0.0); + } + + @Test(expected = NoSuchElementException.class) + public void testGetLineNumberNegative() throws IOException { + String src = "a\nb$line-tag$\nc\nd\ne\nf"; + final Source s = new Source(new StringReader(src)); + s.getLineNumber("ag"); + } + + @Test(expected = IllegalArgumentException.class) + public void testDuplicateTag() throws IOException { + String src = "a\nb$line-tag$\nc\nd\ne$line-tag$\nf"; + new Source(new StringReader(src)); + } + + @Test + public void testGetSourceFor() throws IOException { + final Source s = Source.getSourceFor(SourceTest.class); + // Here we are. $line-testGetSourceFor$ + final String l = s.getLine(s.getLineNumber("testGetSourceFor")); + assertTrue(l, l.contains("Here we are.")); + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/validation/ValidationTestBase.java b/org.jacoco.core.test/src/org/jacoco/core/test/validation/ValidationTestBase.java index ab07913b..0485ac51 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/validation/ValidationTestBase.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/validation/ValidationTestBase.java @@ -1,123 +1,123 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.validation; - -import static org.junit.Assert.assertEquals; - -import java.util.Collection; - -import org.jacoco.core.analysis.Analyzer; -import org.jacoco.core.analysis.CoverageBuilder; -import org.jacoco.core.analysis.IClassCoverage; -import org.jacoco.core.analysis.ICounter; -import org.jacoco.core.analysis.ILine; -import org.jacoco.core.analysis.ISourceFileCoverage; -import org.jacoco.core.data.ExecutionDataStore; -import org.jacoco.core.instr.Instrumenter; -import org.jacoco.core.internal.analysis.CounterImpl; -import org.jacoco.core.runtime.IRuntime; -import org.jacoco.core.runtime.SystemPropertiesRuntime; -import org.jacoco.core.test.TargetLoader; -import org.junit.Before; -import org.objectweb.asm.ClassReader; - -/** - * Base class for validation tests. It executes the given class under code - * coverage and provides the coverage results for validation. - */ -public abstract class ValidationTestBase { - - private static final String[] STATUS_NAME = new String[4]; - - { - STATUS_NAME[ICounter.EMPTY] = "NO_CODE"; - STATUS_NAME[ICounter.NOT_COVERED] = "NOT_COVERED"; - STATUS_NAME[ICounter.FULLY_COVERED] = "FULLY_COVERED"; - STATUS_NAME[ICounter.PARTLY_COVERED] = "PARTLY_COVERED"; - } - - protected final Class target; - - protected IClassCoverage classCoverage; - - protected ISourceFileCoverage sourceCoverage; - - protected Source source; - - protected ValidationTestBase(final Class target) { - this.target = target; - } - - @Before - public void setup() throws Exception { - final ClassReader reader = new ClassReader( - TargetLoader.getClassData(target)); - final ExecutionDataStore store = execute(reader); - analyze(reader, store); - source = Source.getSourceFor(target); - } - - private ExecutionDataStore execute(final ClassReader reader) - throws Exception { - IRuntime runtime = new SystemPropertiesRuntime(); - runtime.startup(); - final byte[] bytes = new Instrumenter(runtime).instrument(reader); - final TargetLoader loader = new TargetLoader(target, bytes); - run(loader.getTargetClass()); - final ExecutionDataStore store = new ExecutionDataStore(); - runtime.collect(store, null, false); - runtime.shutdown(); - return store; - } - - protected abstract void run(final Class targetClass) throws Exception; - - private void analyze(final ClassReader reader, - final ExecutionDataStore store) { - final CoverageBuilder builder = new CoverageBuilder(); - final Analyzer analyzer = new Analyzer(store, builder); - analyzer.analyzeClass(reader); - final Collection classes = builder.getClasses(); - assertEquals(1, classes.size(), 0.0); - classCoverage = classes.iterator().next(); - final Collection files = builder.getSourceFiles(); - assertEquals(1, files.size(), 0.0); - sourceCoverage = files.iterator().next(); - } - - protected void assertLine(final String tag, final int status) { - final int nr = source.getLineNumber(tag); - final ILine line = sourceCoverage.getLine(nr); - final String msg = String.format("Status in line %s: %s", - Integer.valueOf(nr), source.getLine(nr)); - final int insnStatus = line.getInstructionCounter().getStatus(); - assertEquals(msg, STATUS_NAME[status], STATUS_NAME[insnStatus]); - } - - protected void assertLine(final String tag, final int missedBranches, - final int coveredBranches) { - final int nr = source.getLineNumber(tag); - final ILine line = sourceCoverage.getLine(nr); - final String msg = String.format("Branches in line %s: %s", - Integer.valueOf(nr), source.getLine(nr)); - assertEquals(msg + " branches", - CounterImpl.getInstance(missedBranches, coveredBranches), - line.getBranchCounter()); - } - - protected void assertLine(final String tag, final int status, - final int missedBranches, final int coveredBranches) { - assertLine(tag, status); - assertLine(tag, missedBranches, coveredBranches); - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.validation; + +import static org.junit.Assert.assertEquals; + +import java.util.Collection; + +import org.jacoco.core.analysis.Analyzer; +import org.jacoco.core.analysis.CoverageBuilder; +import org.jacoco.core.analysis.IClassCoverage; +import org.jacoco.core.analysis.ICounter; +import org.jacoco.core.analysis.ILine; +import org.jacoco.core.analysis.ISourceFileCoverage; +import org.jacoco.core.data.ExecutionDataStore; +import org.jacoco.core.instr.Instrumenter; +import org.jacoco.core.internal.analysis.CounterImpl; +import org.jacoco.core.runtime.IRuntime; +import org.jacoco.core.runtime.SystemPropertiesRuntime; +import org.jacoco.core.test.TargetLoader; +import org.junit.Before; +import org.objectweb.asm.ClassReader; + +/** + * Base class for validation tests. It executes the given class under code + * coverage and provides the coverage results for validation. + */ +public abstract class ValidationTestBase { + + private static final String[] STATUS_NAME = new String[4]; + + { + STATUS_NAME[ICounter.EMPTY] = "NO_CODE"; + STATUS_NAME[ICounter.NOT_COVERED] = "NOT_COVERED"; + STATUS_NAME[ICounter.FULLY_COVERED] = "FULLY_COVERED"; + STATUS_NAME[ICounter.PARTLY_COVERED] = "PARTLY_COVERED"; + } + + protected final Class target; + + protected IClassCoverage classCoverage; + + protected ISourceFileCoverage sourceCoverage; + + protected Source source; + + protected ValidationTestBase(final Class target) { + this.target = target; + } + + @Before + public void setup() throws Exception { + final ClassReader reader = new ClassReader( + TargetLoader.getClassData(target)); + final ExecutionDataStore store = execute(reader); + analyze(reader, store); + source = Source.getSourceFor(target); + } + + private ExecutionDataStore execute(final ClassReader reader) + throws Exception { + IRuntime runtime = new SystemPropertiesRuntime(); + runtime.startup(); + final byte[] bytes = new Instrumenter(runtime).instrument(reader); + final TargetLoader loader = new TargetLoader(target, bytes); + run(loader.getTargetClass()); + final ExecutionDataStore store = new ExecutionDataStore(); + runtime.collect(store, null, false); + runtime.shutdown(); + return store; + } + + protected abstract void run(final Class targetClass) throws Exception; + + private void analyze(final ClassReader reader, + final ExecutionDataStore store) { + final CoverageBuilder builder = new CoverageBuilder(); + final Analyzer analyzer = new Analyzer(store, builder); + analyzer.analyzeClass(reader); + final Collection classes = builder.getClasses(); + assertEquals(1, classes.size(), 0.0); + classCoverage = classes.iterator().next(); + final Collection files = builder.getSourceFiles(); + assertEquals(1, files.size(), 0.0); + sourceCoverage = files.iterator().next(); + } + + protected void assertLine(final String tag, final int status) { + final int nr = source.getLineNumber(tag); + final ILine line = sourceCoverage.getLine(nr); + final String msg = String.format("Status in line %s: %s", + Integer.valueOf(nr), source.getLine(nr)); + final int insnStatus = line.getInstructionCounter().getStatus(); + assertEquals(msg, STATUS_NAME[status], STATUS_NAME[insnStatus]); + } + + protected void assertLine(final String tag, final int missedBranches, + final int coveredBranches) { + final int nr = source.getLineNumber(tag); + final ILine line = sourceCoverage.getLine(nr); + final String msg = String.format("Branches in line %s: %s", + Integer.valueOf(nr), source.getLine(nr)); + assertEquals(msg + " branches", + CounterImpl.getInstance(missedBranches, coveredBranches), + line.getBranchCounter()); + } + + protected void assertLine(final String tag, final int status, + final int missedBranches, final int coveredBranches) { + assertLine(tag, status); + assertLine(tag, missedBranches, coveredBranches); + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Stubs.java b/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Stubs.java index 110664b5..15889173 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Stubs.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Stubs.java @@ -1,107 +1,107 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.validation.targets; - -/** - * Collection of stub methods that are called from the coverage targets. * - */ -public class Stubs { - - /** - * Exception stub. - */ - public static class StubException extends RuntimeException { - - static final long serialVersionUID = 0L; - - } - - /** - * Superclass stub. - */ - public static class SuperClass { - - public SuperClass(boolean arg) { - } - - } - - /** - * Dummy method. - */ - public static void nop() { - } - - /** - * Dummy method. - */ - public static void nop(int i) { - } - - /** - * Dummy method. - */ - public static void nop(boolean b) { - } - - /** - * Dummy method. - */ - public static void nop(Object o) { - } - - /** - * @return always true - */ - public static boolean t() { - return true; - } - - /** - * @return always false - */ - public static boolean f() { - return false; - } - - /** - * @return always 1 - */ - public static int i1() { - return 1; - } - - /** - * @return always 3 - */ - public static int i2() { - return 2; - } - - /** - * @return always 3 - */ - public static int i3() { - return 3; - } - - /** - * Always throws a {@link RuntimeException}. - * - * @throws StubException - * always thrown - */ - public static void ex() throws StubException { - throw new StubException(); - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.validation.targets; + +/** + * Collection of stub methods that are called from the coverage targets. * + */ +public class Stubs { + + /** + * Exception stub. + */ + public static class StubException extends RuntimeException { + + static final long serialVersionUID = 0L; + + } + + /** + * Superclass stub. + */ + public static class SuperClass { + + public SuperClass(boolean arg) { + } + + } + + /** + * Dummy method. + */ + public static void nop() { + } + + /** + * Dummy method. + */ + public static void nop(int i) { + } + + /** + * Dummy method. + */ + public static void nop(boolean b) { + } + + /** + * Dummy method. + */ + public static void nop(Object o) { + } + + /** + * @return always true + */ + public static boolean t() { + return true; + } + + /** + * @return always false + */ + public static boolean f() { + return false; + } + + /** + * @return always 1 + */ + public static int i1() { + return 1; + } + + /** + * @return always 3 + */ + public static int i2() { + return 2; + } + + /** + * @return always 3 + */ + public static int i3() { + return 3; + } + + /** + * Always throws a {@link RuntimeException}. + * + * @throws StubException + * always thrown + */ + public static void ex() throws StubException { + throw new StubException(); + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Target01.java b/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Target01.java index f26b4805..4ad3e493 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Target01.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Target01.java @@ -1,216 +1,216 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.validation.targets; - -import static org.jacoco.core.test.validation.targets.Stubs.f; -import static org.jacoco.core.test.validation.targets.Stubs.i2; -import static org.jacoco.core.test.validation.targets.Stubs.nop; -import static org.jacoco.core.test.validation.targets.Stubs.t; - -import java.util.Collections; - -/** - * This target exercises a set of common Java control structures. - */ -public class Target01 implements Runnable { - - public void run() { - - // 1. Unconditional execution - nop(); // $line-unconditional$ - - // 2. Missed if block - if (f()) { // $line-iffalse$ - nop(); // $line-missedif$ - } else { - nop(); // $line-executedelse$ - } - - // 3. Executed if block - if (t()) { // $line-iftrue$ - nop(); // $line-executedif$ - } else { - nop(); // $line-missedelse$ - } - - // 4. Missed while block - while (f()) { // $line-whilefalse$ - nop(); // $line-missedwhile$ - } - - // 5. Always executed while block - while (t()) { // $line-whiletrue$ - if (t()) { - break; - } - } - - // 6. Executed while block - int i = 0; - while (i++ < 3) { // $line-whiletruefalse$ - nop(); // $line-executedwhile$ - } - - // 7. Executed do while block - do { - nop(); // $line-executeddowhile$ - } while (f()); - - // 8. Missed for block - for (nop(); f(); nop()) { // $line-missedforincrementer$ - nop(); // $line-missedfor$ - } - - // 9. Executed for block - for (int j = 0; j < 1; j++) { // $line-executedforincrementer$ - nop(); // $line-executedfor$ - } - - // 10. Missed for each block - for (Object o : Collections.emptyList()) { // $line-missedforeachincrementer$ - nop(o); // $line-missedforeach$ - } - - // 11. Executed for each block - for (Object o : Collections.singleton(new Object())) { // $line-executedforeachincrementer$ - nop(o); // $line-executedforeach$ - } - - // 12. Table switch with hit - switch (i2()) { // $line-tswitch1$ - case 1: - nop(); // $line-tswitch1case1$ - break; - case 2: - nop(); // $line-tswitch1case2$ - break; - case 3: - nop(); // $line-tswitch1case3$ - break; - default: - nop(); // $line-tswitch1default$ - break; - } - - // 13. Continued table switch with hit - switch (i2()) { // $line-tswitch2$ - case 1: - nop(); // $line-tswitch2case1$ - case 2: - nop(); // $line-tswitch2case2$ - case 3: - nop(); // $line-tswitch2case3$ - default: - nop(); // $line-tswitch2default$ - } - - // 14. Table switch without hit - switch (i2()) { // $line-tswitch3$ - case 3: - nop(); // $line-tswitch3case1$ - break; - case 4: - nop(); // $line-tswitch3case2$ - break; - case 5: - nop(); // $line-tswitch3case3$ - break; - default: - nop(); // $line-tswitch3default$ - break; - } - - // 15. Lookup switch with hit - switch (i2()) { // $line-lswitch1$ - case -123: - nop(); // $line-lswitch1case1$ - break; - case 2: - nop(); // $line-lswitch1case2$ - break; - case 456: - nop(); // $line-lswitch1case3$ - break; - default: - nop(); // $line-lswitch1default$ - break; - } - - // 16. Continued lookup switch with hit - switch (i2()) { // $line-lswitch2$ - case -123: - nop(); // $line-lswitch2case1$ - case 2: - nop(); // $line-lswitch2case2$ - case 456: - nop(); // $line-lswitch2case3$ - default: - nop(); // $line-lswitch2default$ - } - - // 17. Lookup switch without hit - switch (i2()) { // $line-lswitch3$ - case -123: - nop(); // $line-lswitch3case1$ - break; - case 456: - nop(); // $line-lswitch3case2$ - break; - case 789: - nop(); // $line-lswitch3case3$ - break; - default: - nop(); // $line-lswitch3default$ - break; - } - - // 18. Break statement - while (true) { - if (t()) { - break; // $line-executedbreak$ - } - nop(); // $line-missedafterbreak$ - } - - // 19. Continue statement - for (int j = 0; j < 1; j++) { - if (t()) { - continue; // $line-executedcontinue$ - } - nop(); // $line-missedaftercontinue$ - } - - runReturn(); - runImplicitReturn(); - - } - - private void runReturn() { - - // 20. Return statement - if (t()) { - return; // $line-return$ - } - nop(); // $line-afterreturn$ - - } - - private void runImplicitReturn() { - - // 21. Implicit return - } // $line-implicitreturn$ - - public static void main(String[] args) { - new Target01().run(); - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.validation.targets; + +import static org.jacoco.core.test.validation.targets.Stubs.f; +import static org.jacoco.core.test.validation.targets.Stubs.i2; +import static org.jacoco.core.test.validation.targets.Stubs.nop; +import static org.jacoco.core.test.validation.targets.Stubs.t; + +import java.util.Collections; + +/** + * This target exercises a set of common Java control structures. + */ +public class Target01 implements Runnable { + + public void run() { + + // 1. Unconditional execution + nop(); // $line-unconditional$ + + // 2. Missed if block + if (f()) { // $line-iffalse$ + nop(); // $line-missedif$ + } else { + nop(); // $line-executedelse$ + } + + // 3. Executed if block + if (t()) { // $line-iftrue$ + nop(); // $line-executedif$ + } else { + nop(); // $line-missedelse$ + } + + // 4. Missed while block + while (f()) { // $line-whilefalse$ + nop(); // $line-missedwhile$ + } + + // 5. Always executed while block + while (t()) { // $line-whiletrue$ + if (t()) { + break; + } + } + + // 6. Executed while block + int i = 0; + while (i++ < 3) { // $line-whiletruefalse$ + nop(); // $line-executedwhile$ + } + + // 7. Executed do while block + do { + nop(); // $line-executeddowhile$ + } while (f()); + + // 8. Missed for block + for (nop(); f(); nop()) { // $line-missedforincrementer$ + nop(); // $line-missedfor$ + } + + // 9. Executed for block + for (int j = 0; j < 1; j++) { // $line-executedforincrementer$ + nop(); // $line-executedfor$ + } + + // 10. Missed for each block + for (Object o : Collections.emptyList()) { // $line-missedforeachincrementer$ + nop(o); // $line-missedforeach$ + } + + // 11. Executed for each block + for (Object o : Collections.singleton(new Object())) { // $line-executedforeachincrementer$ + nop(o); // $line-executedforeach$ + } + + // 12. Table switch with hit + switch (i2()) { // $line-tswitch1$ + case 1: + nop(); // $line-tswitch1case1$ + break; + case 2: + nop(); // $line-tswitch1case2$ + break; + case 3: + nop(); // $line-tswitch1case3$ + break; + default: + nop(); // $line-tswitch1default$ + break; + } + + // 13. Continued table switch with hit + switch (i2()) { // $line-tswitch2$ + case 1: + nop(); // $line-tswitch2case1$ + case 2: + nop(); // $line-tswitch2case2$ + case 3: + nop(); // $line-tswitch2case3$ + default: + nop(); // $line-tswitch2default$ + } + + // 14. Table switch without hit + switch (i2()) { // $line-tswitch3$ + case 3: + nop(); // $line-tswitch3case1$ + break; + case 4: + nop(); // $line-tswitch3case2$ + break; + case 5: + nop(); // $line-tswitch3case3$ + break; + default: + nop(); // $line-tswitch3default$ + break; + } + + // 15. Lookup switch with hit + switch (i2()) { // $line-lswitch1$ + case -123: + nop(); // $line-lswitch1case1$ + break; + case 2: + nop(); // $line-lswitch1case2$ + break; + case 456: + nop(); // $line-lswitch1case3$ + break; + default: + nop(); // $line-lswitch1default$ + break; + } + + // 16. Continued lookup switch with hit + switch (i2()) { // $line-lswitch2$ + case -123: + nop(); // $line-lswitch2case1$ + case 2: + nop(); // $line-lswitch2case2$ + case 456: + nop(); // $line-lswitch2case3$ + default: + nop(); // $line-lswitch2default$ + } + + // 17. Lookup switch without hit + switch (i2()) { // $line-lswitch3$ + case -123: + nop(); // $line-lswitch3case1$ + break; + case 456: + nop(); // $line-lswitch3case2$ + break; + case 789: + nop(); // $line-lswitch3case3$ + break; + default: + nop(); // $line-lswitch3default$ + break; + } + + // 18. Break statement + while (true) { + if (t()) { + break; // $line-executedbreak$ + } + nop(); // $line-missedafterbreak$ + } + + // 19. Continue statement + for (int j = 0; j < 1; j++) { + if (t()) { + continue; // $line-executedcontinue$ + } + nop(); // $line-missedaftercontinue$ + } + + runReturn(); + runImplicitReturn(); + + } + + private void runReturn() { + + // 20. Return statement + if (t()) { + return; // $line-return$ + } + nop(); // $line-afterreturn$ + + } + + private void runImplicitReturn() { + + // 21. Implicit return + } // $line-implicitreturn$ + + public static void main(String[] args) { + new Target01().run(); + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Target02.java b/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Target02.java index fb626b37..1725f4a5 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Target02.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/validation/targets/Target02.java @@ -1,124 +1,124 @@ -/******************************************************************************* - * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Marc R. Hoffmann - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.test.validation.targets; - -import static org.jacoco.core.test.validation.targets.Stubs.f; -import static org.jacoco.core.test.validation.targets.Stubs.i1; -import static org.jacoco.core.test.validation.targets.Stubs.i2; -import static org.jacoco.core.test.validation.targets.Stubs.nop; -import static org.jacoco.core.test.validation.targets.Stubs.t; - -/** - * This target exercises boolean expressions. - */ -public class Target02 implements Runnable { - - public void run() { - - // 1. Boolean comparison result (one case) - nop(i2() > 3); // $line-booleancmp1$ - - // 2. Boolean comparison result (both cases) - for (int i = 0; i < 2; i++) { - nop(i < 1); // $line-booleancmp2$ - } - - // 3. And - if (f() & f()) { // $line-andFF$ - nop(); - } - if (f() & t()) { // $line-andFT$ - nop(); - } - if (t() & f()) { // $line-andTF$ - nop(); - } - if (t() & t()) { // $line-andTT$ - nop(); - } - - // 4. Conditional And - if (f() && f()) { // $line-conditionalandFF$ - nop(); - } - if (f() && t()) { // $line-conditionalandFT$ - nop(); - } - if (t() && f()) { // $line-conditionalandTF$ - nop(); - } - if (t() && t()) { // $line-conditionalandTT$ - nop(); - } - - // 5. Or - if (f() | f()) { // $line-orFF$ - nop(); - } - if (f() | t()) { // $line-orFT$ - nop(); - } - if (t() | f()) { // $line-orTF$ - nop(); - } - if (t() | t()) { // $line-orTT$ - nop(); - } - - // 6. Conditional Or - if (f() || f()) { // $line-conditionalorFF$ - nop(); - } - if (f() || t()) { // $line-conditionalorFT$ - nop(); - } - if (t() || f()) { // $line-conditionalorTF$ - nop(); - } - if (t() || t()) { // $line-conditionalorTT$ - nop(); - } - - // 7. Exclusive Or - if (f() ^ f()) { // $line-xorFF$ - nop(); - } - if (f() ^ t()) { // $line-xorFT$ - nop(); - } - if (t() ^ f()) { // $line-xorTF$ - nop(); - } - if (t() ^ t()) { // $line-xorTT$ - nop(); - } - - // 8. Conditional Operator - nop(t() ? i1() : i2()); // $line-condT$ - nop(f() ? i1() : i2()); // $line-condF$ - - // 9. Not (one case) - nop(!t()); // $line-notT$ - nop(!f()); // $line-notF$ - - // 10. Not (both cases) - for (boolean b : new boolean[] { true, false }) { - nop(!b); // $line-notTF$ - } - - } - - public static void main(String[] args) { - new Target02().run(); - } - -} +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.validation.targets; + +import static org.jacoco.core.test.validation.targets.Stubs.f; +import static org.jacoco.core.test.validation.targets.Stubs.i1; +import static org.jacoco.core.test.validation.targets.Stubs.i2; +import static org.jacoco.core.test.validation.targets.Stubs.nop; +import static org.jacoco.core.test.validation.targets.Stubs.t; + +/** + * This target exercises boolean expressions. + */ +public class Target02 implements Runnable { + + public void run() { + + // 1. Boolean comparison result (one case) + nop(i2() > 3); // $line-booleancmp1$ + + // 2. Boolean comparison result (both cases) + for (int i = 0; i < 2; i++) { + nop(i < 1); // $line-booleancmp2$ + } + + // 3. And + if (f() & f()) { // $line-andFF$ + nop(); + } + if (f() & t()) { // $line-andFT$ + nop(); + } + if (t() & f()) { // $line-andTF$ + nop(); + } + if (t() & t()) { // $line-andTT$ + nop(); + } + + // 4. Conditional And + if (f() && f()) { // $line-conditionalandFF$ + nop(); + } + if (f() && t()) { // $line-conditionalandFT$ + nop(); + } + if (t() && f()) { // $line-conditionalandTF$ + nop(); + } + if (t() && t()) { // $line-conditionalandTT$ + nop(); + } + + // 5. Or + if (f() | f()) { // $line-orFF$ + nop(); + } + if (f() | t()) { // $line-orFT$ + nop(); + } + if (t() | f()) { // $line-orTF$ + nop(); + } + if (t() | t()) { // $line-orTT$ + nop(); + } + + // 6. Conditional Or + if (f() || f()) { // $line-conditionalorFF$ + nop(); + } + if (f() || t()) { // $line-conditionalorFT$ + nop(); + } + if (t() || f()) { // $line-conditionalorTF$ + nop(); + } + if (t() || t()) { // $line-conditionalorTT$ + nop(); + } + + // 7. Exclusive Or + if (f() ^ f()) { // $line-xorFF$ + nop(); + } + if (f() ^ t()) { // $line-xorFT$ + nop(); + } + if (t() ^ f()) { // $line-xorTF$ + nop(); + } + if (t() ^ t()) { // $line-xorTT$ + nop(); + } + + // 8. Conditional Operator + nop(t() ? i1() : i2()); // $line-condT$ + nop(f() ? i1() : i2()); // $line-condF$ + + // 9. Not (one case) + nop(!t()); // $line-notT$ + nop(!f()); // $line-notF$ + + // 10. Not (both cases) + for (boolean b : new boolean[] { true, false }) { + nop(!b); // $line-notTF$ + } + + } + + public static void main(String[] args) { + new Target02().run(); + } + +} -- cgit v1.2.3