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/src | |
parent | e35a34adcc3a47eac6723533e811e52c288d4dd5 (diff) | |
download | jacoco-497fb3fd0f85b5dd4b7d240f99dc0bc3811d939c.tar.gz |
Unit tests for API examples.
Diffstat (limited to 'org.jacoco.examples.test/src')
4 files changed, 224 insertions, 0 deletions
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(); + } +} |