diff options
author | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2013-11-22 17:00:50 +0100 |
---|---|---|
committer | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2013-11-22 18:03:32 +0100 |
commit | 497fb3fd0f85b5dd4b7d240f99dc0bc3811d939c (patch) | |
tree | 7fedefdf09ba581c0b8993760fb9bf3e24756b73 /org.jacoco.examples.test | |
parent | e35a34adcc3a47eac6723533e811e52c288d4dd5 (diff) | |
download | jacoco-497fb3fd0f85b5dd4b7d240f99dc0bc3811d939c.tar.gz |
Unit tests for API examples.
Diffstat (limited to 'org.jacoco.examples.test')
-rw-r--r-- | org.jacoco.examples.test/.classpath | 7 | ||||
-rw-r--r-- | org.jacoco.examples.test/.gitignore | 2 | ||||
-rw-r--r-- | org.jacoco.examples.test/.project | 35 | ||||
-rw-r--r-- | org.jacoco.examples.test/META-INF/MANIFEST.MF | 12 | ||||
-rw-r--r-- | org.jacoco.examples.test/about.html | 27 | ||||
-rw-r--r-- | org.jacoco.examples.test/build.properties | 4 | ||||
-rw-r--r-- | org.jacoco.examples.test/pom.xml | 41 | ||||
-rw-r--r-- | org.jacoco.examples.test/src/org/jacoco/examples/ClassInfoTest.java | 57 | ||||
-rw-r--r-- | org.jacoco.examples.test/src/org/jacoco/examples/ConsoleOutput.java | 70 | ||||
-rw-r--r-- | org.jacoco.examples.test/src/org/jacoco/examples/CoreTutorialTest.java | 38 | ||||
-rw-r--r-- | org.jacoco.examples.test/src/org/jacoco/examples/ExecDumpTest.java | 59 |
11 files changed, 352 insertions, 0 deletions
diff --git a/org.jacoco.examples.test/.classpath b/org.jacoco.examples.test/.classpath new file mode 100644 index 00000000..64c5e31b --- /dev/null +++ b/org.jacoco.examples.test/.classpath @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="src" path="src"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/org.jacoco.examples.test/.gitignore b/org.jacoco.examples.test/.gitignore new file mode 100644 index 00000000..4dc00917 --- /dev/null +++ b/org.jacoco.examples.test/.gitignore @@ -0,0 +1,2 @@ +/target +/bin diff --git a/org.jacoco.examples.test/.project b/org.jacoco.examples.test/.project new file mode 100644 index 00000000..56dd28db --- /dev/null +++ b/org.jacoco.examples.test/.project @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.jacoco.examples.test</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> + <linkedResources> + <link> + <name>.settings</name> + <type>2</type> + <locationURI>PARENT-1-PROJECT_LOC/org.jacoco.core.test/.settings</locationURI> + </link> + </linkedResources> +</projectDescription> diff --git a/org.jacoco.examples.test/META-INF/MANIFEST.MF b/org.jacoco.examples.test/META-INF/MANIFEST.MF new file mode 100644 index 00000000..70691df3 --- /dev/null +++ b/org.jacoco.examples.test/META-INF/MANIFEST.MF @@ -0,0 +1,12 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: JaCoCo Examples Tests +Bundle-SymbolicName: org.jacoco.examples.test +Bundle-Version: 0.6.4.qualifier +Bundle-Vendor: Mountainminds GmbH & Co. KG +Fragment-Host: org.jacoco.examples +Bundle-RequiredExecutionEnvironment: J2SE-1.5 +Import-Package: org.junit;version="[4.8.0,5.0.0)", + org.junit.matchers;version="[4.8.0,5.0.0)", + org.junit.rules;version="[4.8.0,5.0.0)" +Require-Bundle: org.hamcrest.core;bundle-version="[1.1.0,2.0.0)" diff --git a/org.jacoco.examples.test/about.html b/org.jacoco.examples.test/about.html new file mode 100644 index 00000000..d31112db --- /dev/null +++ b/org.jacoco.examples.test/about.html @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html> +<head> +<title>About</title> +<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> +</head> +<body lang="EN-US"> +<h2>About This Content</h2> + +<p> + @build.date@ +</p> + +<h3>License</h3> + +<p> + All Content in this plug-in is made available by Mountainminds GmbH & Co. + KG, Munich. Unless otherwise indicated below, the Content is provided to you + under the terms and conditions of the Eclipse Public License Version 1.0 + ("EPL"). A copy of the EPL is available at + <a href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a>. + For purposes of the EPL, "Program" will mean the Content. +</p> + +</body> +</html>
\ No newline at end of file diff --git a/org.jacoco.examples.test/build.properties b/org.jacoco.examples.test/build.properties new file mode 100644 index 00000000..34d2e4d2 --- /dev/null +++ b/org.jacoco.examples.test/build.properties @@ -0,0 +1,4 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + . diff --git a/org.jacoco.examples.test/pom.xml b/org.jacoco.examples.test/pom.xml new file mode 100644 index 00000000..a2dbf9d3 --- /dev/null +++ b/org.jacoco.examples.test/pom.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright (c) 2009, 2013 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: + Evgeny Mandrikov - initial API and implementation +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.jacoco</groupId> + <artifactId>org.jacoco.tests</artifactId> + <version>0.6.4-SNAPSHOT</version> + <relativePath>../org.jacoco.tests</relativePath> + </parent> + + <artifactId>org.jacoco.examples.test</artifactId> + + <name>JaCoCo :: Test :: Examples</name> + + <properties> + <jacoco.includes>org.jacoco.examples.*</jacoco.includes> + <jacoco.excludes>org.jacoco.examples.CoreTutorial$TestTarget</jacoco.excludes> + </properties> + + <dependencies> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>org.jacoco.examples</artifactId> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + </dependencies> +</project> diff --git a/org.jacoco.examples.test/src/org/jacoco/examples/ClassInfoTest.java b/org.jacoco.examples.test/src/org/jacoco/examples/ClassInfoTest.java new file mode 100644 index 00000000..bd4c200e --- /dev/null +++ b/org.jacoco.examples.test/src/org/jacoco/examples/ClassInfoTest.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2009, 2013 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.examples; + +import static org.jacoco.examples.ConsoleOutput.containsLine; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.junit.Rule; +import org.junit.Test; + +/** + * Tests for {@link ClassInfo}. + */ +public class ClassInfoTest { + + @Rule + public ConsoleOutput console = new ConsoleOutput(); + + @Test + public void testRunExample() throws Exception { + + final String[] args = new String[] { createClassFile() }; + new ClassInfo(console.stream).execute(args); + + console.expect(containsLine("class name: org/jacoco/examples/ClassInfoTest")); + console.expect(containsLine("methods: 3")); + console.expect(containsLine("branches: 2")); + console.expect(containsLine("complexity: 4")); + } + + private String createClassFile() throws IOException { + InputStream in = getClass().getResource( + getClass().getSimpleName() + ".class").openStream(); + File f = File.createTempFile("Example", ".class"); + FileOutputStream out = new FileOutputStream(f); + int b; + while ((b = in.read()) != -1) { + out.write(b); + } + in.close(); + out.close(); + return f.getPath(); + } +} diff --git a/org.jacoco.examples.test/src/org/jacoco/examples/ConsoleOutput.java b/org.jacoco.examples.test/src/org/jacoco/examples/ConsoleOutput.java new file mode 100644 index 00000000..40ab3d8e --- /dev/null +++ b/org.jacoco.examples.test/src/org/jacoco/examples/ConsoleOutput.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2009, 2013 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.examples; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static org.junit.matchers.JUnitMatchers.containsString; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; + +import org.hamcrest.Matcher; +import org.junit.rules.ExternalResource; + +/** + * In-Memory buffer to assert console output. + */ +public class ConsoleOutput extends ExternalResource { + + private final ByteArrayOutputStream buffer; + + public final PrintStream stream; + + public ConsoleOutput() { + this.buffer = new ByteArrayOutputStream(); + try { + this.stream = new PrintStream(buffer, true, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e.getMessage()); + } + } + + @Override + protected void after() { + buffer.reset(); + } + + public static Matcher<String> containsLine(String line) { + return containsString(String.format("%s\n", line)); + } + + public static Matcher<String> isEmpty() { + return is(""); + } + + public String getContents() { + try { + return new String(buffer.toByteArray(), "UTF-8"); + } catch (UnsupportedEncodingException e) { + fail(e.getMessage()); + return ""; + } + } + + public void expect(Matcher<String> matcher) { + assertThat(getContents(), matcher); + } + +} diff --git a/org.jacoco.examples.test/src/org/jacoco/examples/CoreTutorialTest.java b/org.jacoco.examples.test/src/org/jacoco/examples/CoreTutorialTest.java new file mode 100644 index 00000000..7fea0e4f --- /dev/null +++ b/org.jacoco.examples.test/src/org/jacoco/examples/CoreTutorialTest.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2009, 2013 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.examples; + +import static org.jacoco.examples.ConsoleOutput.containsLine; + +import org.junit.Rule; +import org.junit.Test; + +/** + * Tests for {@link CoreTutorial}. + */ +public class CoreTutorialTest { + + @Rule + public ConsoleOutput console = new ConsoleOutput(); + + @Test + public void testRunExample() throws Exception { + new CoreTutorial(console.stream).execute(); + + console.expect(containsLine("0 of 3 methods missed")); + console.expect(containsLine("1 of 5 complexity missed")); + console.expect(containsLine("Line 46: ")); + console.expect(containsLine("Line 47: green")); + console.expect(containsLine("Line 48: yellow")); + console.expect(containsLine("Line 49: red")); + } +} diff --git a/org.jacoco.examples.test/src/org/jacoco/examples/ExecDumpTest.java b/org.jacoco.examples.test/src/org/jacoco/examples/ExecDumpTest.java new file mode 100644 index 00000000..f35bf89b --- /dev/null +++ b/org.jacoco.examples.test/src/org/jacoco/examples/ExecDumpTest.java @@ -0,0 +1,59 @@ +/******************************************************************************* + * Copyright (c) 2009, 2013 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.examples; + +import static org.jacoco.examples.ConsoleOutput.containsLine; +import static org.junit.matchers.JUnitMatchers.containsString; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +import org.jacoco.core.data.ExecutionData; +import org.jacoco.core.data.ExecutionDataWriter; +import org.jacoco.core.data.SessionInfo; +import org.junit.Rule; +import org.junit.Test; + +/** + * Tests for {@link ExecDump}. + */ +public class ExecDumpTest { + + @Rule + public ConsoleOutput console = new ConsoleOutput(); + + @Test + public void testRunExample() throws Exception { + + final String file = createExecFile(); + final String[] args = new String[] { file }; + new ExecDump(console.stream).execute(args); + + console.expect(containsLine("exec file: " + file)); + console.expect(containsLine("CLASS ID HITS/PROBES CLASS NAME")); + console.expect(containsString("Session \"testid\":")); + console.expect(containsLine("0000000000001234 2 of 3 foo/MyClass")); + } + + private String createExecFile() throws IOException { + File f = File.createTempFile("jacoco", ".exec"); + final FileOutputStream out = new FileOutputStream(f); + final ExecutionDataWriter writer = new ExecutionDataWriter(out); + writer.visitSessionInfo(new SessionInfo("testid", 1, 2)); + writer.visitClassExecution(new ExecutionData(0x1234, "foo/MyClass", + new boolean[] { false, true, true })); + writer.flush(); + out.close(); + return f.getPath(); + } +} |