Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlena Laskavaia2015-11-27 14:36:56 +0000
committerAlexander Kurtakov2015-12-11 13:03:25 +0000
commite1703cbd89b9ca6d9bc5614fc413a8276cffc698 (patch)
tree5685188e77f978c687a441f5ff1d78a1a9501b2a /valgrind
parentf6be12306d2804633e7ada1e5cb39c07a74c07b8 (diff)
downloadorg.eclipse.linuxtools-e1703cbd89b9ca6d9bc5614fc413a8276cffc698.tar.gz
org.eclipse.linuxtools-e1703cbd89b9ca6d9bc5614fc413a8276cffc698.tar.xz
org.eclipse.linuxtools-e1703cbd89b9ca6d9bc5614fc413a8276cffc698.zip
valgrind parser tests using mockito
Change-Id: Ic5da7c4de9c5be4ad439c300580da5ccfa894205 Signed-off-by: Alena Laskavaia <elaskavaia.cdt@gmail.com> Reviewed-on: https://git.eclipse.org/r/61461 Tested-by: Hudson CI Reviewed-by: Alexander Kurtakov <akurtako@redhat.com>
Diffstat (limited to 'valgrind')
-rw-r--r--valgrind/org.eclipse.linuxtools.valgrind.core.tests/.settings/org.eclipse.jdt.core.prefs108
-rw-r--r--valgrind/org.eclipse.linuxtools.valgrind.core.tests/META-INF/MANIFEST.MF4
-rw-r--r--valgrind/org.eclipse.linuxtools.valgrind.core.tests/build.properties3
-rw-r--r--valgrind/org.eclipse.linuxtools.valgrind.core.tests/src/org/eclipse/linuxtools/internal/valgrind/core/ValgrindCoreParserTest.java73
-rw-r--r--valgrind/org.eclipse.linuxtools.valgrind.core.tests/src/org/eclipse/linuxtools/valgrind/core/tests/AbstractInlineDataTest.java101
-rw-r--r--valgrind/org.eclipse.linuxtools.valgrind.core.tests/src/org/eclipse/linuxtools/valgrind/core/tests/TestSourceReader.java160
6 files changed, 443 insertions, 6 deletions
diff --git a/valgrind/org.eclipse.linuxtools.valgrind.core.tests/.settings/org.eclipse.jdt.core.prefs b/valgrind/org.eclipse.linuxtools.valgrind.core.tests/.settings/org.eclipse.jdt.core.prefs
index 0c68a61dca..5ed59cda7f 100644
--- a/valgrind/org.eclipse.linuxtools.valgrind.core.tests/.settings/org.eclipse.jdt.core.prefs
+++ b/valgrind/org.eclipse.linuxtools.valgrind.core.tests/.settings/org.eclipse.jdt.core.prefs
@@ -1,7 +1,115 @@
eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.annotation.inheritNullAnnotations=disabled
+org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore
+org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull
+org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault
+org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable
+org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.doc.comment.support=enabled
+org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.autoboxing=ignore
+org.eclipse.jdt.core.compiler.problem.comparingIdentical=error
+org.eclipse.jdt.core.compiler.problem.deadCode=warning
+org.eclipse.jdt.core.compiler.problem.deprecation=warning
+org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled
+org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled
+org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
+org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
+org.eclipse.jdt.core.compiler.problem.fallthroughCase=warning
+org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
+org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore
+org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning
+org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=error
+org.eclipse.jdt.core.compiler.problem.forbiddenReference=error
+org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning
+org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled
+org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning
+org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=warning
+org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=warning
+org.eclipse.jdt.core.compiler.problem.invalidJavadoc=warning
+org.eclipse.jdt.core.compiler.problem.invalidJavadocTags=enabled
+org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsDeprecatedRef=disabled
+org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsNotVisibleRef=disabled
+org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsVisibility=default
+org.eclipse.jdt.core.compiler.problem.localVariableHiding=ignore
+org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=error
+org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore
+org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=warning
+org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled
+org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=error
+org.eclipse.jdt.core.compiler.problem.missingJavadocComments=ignore
+org.eclipse.jdt.core.compiler.problem.missingJavadocCommentsOverriding=disabled
+org.eclipse.jdt.core.compiler.problem.missingJavadocCommentsVisibility=public
+org.eclipse.jdt.core.compiler.problem.missingJavadocTagDescription=all_standard_tags
+org.eclipse.jdt.core.compiler.problem.missingJavadocTags=warning
+org.eclipse.jdt.core.compiler.problem.missingJavadocTagsMethodTypeParameters=enabled
+org.eclipse.jdt.core.compiler.problem.missingJavadocTagsOverriding=enabled
+org.eclipse.jdt.core.compiler.problem.missingJavadocTagsVisibility=default
+org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=error
+org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled
+org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning
+org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=warning
+org.eclipse.jdt.core.compiler.problem.noEffectAssignment=error
+org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=error
+org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore
+org.eclipse.jdt.core.compiler.problem.nonnullParameterAnnotationDropped=warning
+org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error
+org.eclipse.jdt.core.compiler.problem.nullReference=warning
+org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error
+org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning
+org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning
+org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore
+org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=error
+org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore
+org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=warning
+org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning
+org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning
+org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore
+org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=warning
+org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=warning
+org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore
+org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore
+org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled
+org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning
+org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled
+org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
+org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=disabled
+org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore
+org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning
+org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled
+org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning
+org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning
+org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore
+org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning
+org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore
+org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=warning
+org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=warning
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=disabled
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=disabled
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=enabled
+org.eclipse.jdt.core.compiler.problem.unusedExceptionParameter=ignore
+org.eclipse.jdt.core.compiler.problem.unusedImport=error
+org.eclipse.jdt.core.compiler.problem.unusedLabel=warning
+org.eclipse.jdt.core.compiler.problem.unusedLocal=warning
+org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=warning
+org.eclipse.jdt.core.compiler.problem.unusedParameter=warning
+org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=disabled
+org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled
+org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled
+org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning
+org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=warning
+org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
+org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
org.eclipse.jdt.core.compiler.source=1.8
diff --git a/valgrind/org.eclipse.linuxtools.valgrind.core.tests/META-INF/MANIFEST.MF b/valgrind/org.eclipse.linuxtools.valgrind.core.tests/META-INF/MANIFEST.MF
index 7589660685..4748e75453 100644
--- a/valgrind/org.eclipse.linuxtools.valgrind.core.tests/META-INF/MANIFEST.MF
+++ b/valgrind/org.eclipse.linuxtools.valgrind.core.tests/META-INF/MANIFEST.MF
@@ -5,5 +5,7 @@ Bundle-SymbolicName: org.eclipse.linuxtools.valgrind.core.tests;singleton:=true
Bundle-Version: 1.0.0.qualifier
Fragment-Host: org.eclipse.linuxtools.valgrind.core;bundle-version="1.0.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
-Require-Bundle: org.junit;bundle-version="4.12.0"
+Require-Bundle: org.eclipse.linuxtools.docker.tests.hamcrest-wrap;bundle-version="1.2.0",
+ org.junit;bundle-version="4.12.0",
+ org.mockito
Bundle-Vendor: Eclipse Linux Tools
diff --git a/valgrind/org.eclipse.linuxtools.valgrind.core.tests/build.properties b/valgrind/org.eclipse.linuxtools.valgrind.core.tests/build.properties
index 56d7765555..ff2b6d705c 100644
--- a/valgrind/org.eclipse.linuxtools.valgrind.core.tests/build.properties
+++ b/valgrind/org.eclipse.linuxtools.valgrind.core.tests/build.properties
@@ -1,4 +1,5 @@
source.. = src/
output.. = target/classes/
bin.includes = META-INF/,\
- .
+ .,\
+ src/
diff --git a/valgrind/org.eclipse.linuxtools.valgrind.core.tests/src/org/eclipse/linuxtools/internal/valgrind/core/ValgrindCoreParserTest.java b/valgrind/org.eclipse.linuxtools.valgrind.core.tests/src/org/eclipse/linuxtools/internal/valgrind/core/ValgrindCoreParserTest.java
index ea223c4a49..688f4d5294 100644
--- a/valgrind/org.eclipse.linuxtools.valgrind.core.tests/src/org/eclipse/linuxtools/internal/valgrind/core/ValgrindCoreParserTest.java
+++ b/valgrind/org.eclipse.linuxtools.valgrind.core.tests/src/org/eclipse/linuxtools/internal/valgrind/core/ValgrindCoreParserTest.java
@@ -10,17 +10,82 @@
*******************************************************************************/
package org.eclipse.linuxtools.internal.valgrind.core;
+import static org.junit.Assert.*;
+
import java.io.File;
import java.io.IOException;
import org.eclipse.debug.core.ILaunch;
+import org.eclipse.linuxtools.valgrind.core.IValgrindMessage;
+import org.eclipse.linuxtools.valgrind.core.tests.AbstractInlineDataTest;
+import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mockito;
+
+public class ValgrindCoreParserTest extends AbstractInlineDataTest {
+ private static final String VALGRIND_OUT1 = "valgrind_01.txt";
+ private static final String VALGRIND_OUT2 = "valgrind_02.txt";
+ private IValgrindMessage[] messages;
+ private ILaunch launchMock;
+ private File file;
-public class ValgrindCoreParserTest {
- @Test(expected=IOException.class)
+ @Before
+ public void setUp() throws IOException {
+ launchMock = Mockito.mock(ILaunch.class);
+ file = tmpfiles.newFile(VALGRIND_OUT1);
+ }
+
+ private void parse(File file, ILaunch l) throws IOException {
+ ValgrindCoreParser valgrindCoreParser = new ValgrindCoreParser(file, l);
+ messages = valgrindCoreParser.getMessages();
+ assertNotNull(messages);
+ }
+
+ private void parse() throws IOException {
+ parse(file, launchMock);
+ }
+
+ private void parseComment() throws IOException {
+ file = getAboveCommentAndSaveFile(VALGRIND_OUT2);
+ parse();
+ }
+ private void checkMessage(int index, IValgrindMessage[] messages, String string) {
+ String text = messages[index].getText();
+ text = text.replaceFirst(" \\[PID: \\d+\\]", "");
+ assertEquals(string, text);
+ }
+
+ @Test(expected = IOException.class)
public void test() throws IOException {
- ValgrindCoreParser valgrindCoreParser = new ValgrindCoreParser(new File("/tmp/valgrind_aaa01.txt"), (ILaunch) null);
- valgrindCoreParser.getMessages();
+ file.delete();
+ launchMock = null;
+ parse();
+ }
+
+ //this is pretend message which should be ignored
+ @Test
+ public void testLaunch() throws IOException {
+ parseComment();
+ assertEquals(0, messages.length);
+ }
+
+ //==00:00:00:01.175 52756728== bla bla
+ @Test
+ public void testTimestamp() throws IOException {
+ parseComment();
+ assertEquals(1, messages.length);
+ assertEquals("bla bla [PID: 2]", messages[0].getText()); // TODO should be PID 52756728
+ }
+
+ //==2== one
+ //==2== two
+ @Test
+ public void testIndent() throws IOException {
+ parseComment();
+ assertEquals(1, messages.length);
+ assertEquals(1, messages[0].getChildren().length);
+ checkMessage(0, messages, "one");
+ checkMessage(0, messages[0].getChildren(), "two");
}
}
diff --git a/valgrind/org.eclipse.linuxtools.valgrind.core.tests/src/org/eclipse/linuxtools/valgrind/core/tests/AbstractInlineDataTest.java b/valgrind/org.eclipse.linuxtools.valgrind.core.tests/src/org/eclipse/linuxtools/valgrind/core/tests/AbstractInlineDataTest.java
new file mode 100644
index 0000000000..7ff6f0ee1a
--- /dev/null
+++ b/valgrind/org.eclipse.linuxtools.valgrind.core.tests/src/org/eclipse/linuxtools/valgrind/core/tests/AbstractInlineDataTest.java
@@ -0,0 +1,101 @@
+/*******************************************************************************
+ * Copyright (c) 2015 QNX Software Systems
+ * 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:
+ * Alena Laskavaia - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.linuxtools.valgrind.core.tests;
+
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.charset.Charset;
+
+import org.junit.Rule;
+import org.junit.rules.TemporaryFolder;
+import org.junit.rules.TestName;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.FrameworkUtil;
+
+/**
+ * This is abstract class for juni4 test classes that use line method comments to extract test data.
+ * Test plugin must have "src" folder on its bundle class path.
+ */
+public abstract class AbstractInlineDataTest {
+ public static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
+ public static final Charset CHARSET_UTF_8 = Charset.forName(UTF_8);
+ @Rule public TemporaryFolder tmpfiles = new TemporaryFolder();
+ @Rule public TestName testName = new TestName();
+
+ /**
+ * Gets last comment section (comments that use //) above current test method
+ * @return comment section without leading // and leading whitespaces
+ */
+ protected String getAboveComment() {
+ return getAboveComment(getName());
+ }
+
+ /**
+ * @return current test method name
+ */
+ protected String getName() {
+ return testName.getMethodName();
+ }
+
+ /**
+ * Gets last comment section (comments that use //) above method with a given name
+ * @param name method name
+ * @return comment section without leading // and leading whitespaces
+ */
+ protected String getAboveComment(String name) {
+ return getContents(1, name)[0].toString();
+ }
+
+ /**
+ * Saves comment above test method in the named file in temp dir, if fileName is null it will get a random name.
+ * File will be auto-removed after test is finished
+ * @param fileName - base file name of the temp file or null
+ * @return temp file
+ * @throws IOException if failed to write file
+ */
+ protected File getAboveCommentAndSaveFile(String fileName) throws IOException {
+ String value = getAboveComment(getName());
+ File file = fileName == null ? tmpfiles.newFile() : tmpfiles.newFile(fileName);
+ saveToFile(value, file);
+ return file;
+ }
+
+ protected void saveToFile(String value, File file) throws IOException, FileNotFoundException {
+ try (FileOutputStream st = new FileOutputStream(file)) {
+ st.write(value.getBytes(CHARSET_UTF_8));
+ }
+ }
+
+ protected StringBuilder[] getContents(int sections, String name) {
+ try {
+ return TestSourceReader.getContentsForTest(getBundle(), getSourcePrefix(), getClass(), name, sections);
+ } catch (IOException e) {
+ fail(e.getMessage());
+ return null;
+ }
+ }
+
+ /**
+ * Source directory of the test bundle. Can be overriden if source files that are used to pull comments are not in src.
+ * @return source dir prefix
+ */
+ protected String getSourcePrefix() {
+ return "src";
+ }
+
+ protected Bundle getBundle() {
+ return FrameworkUtil.getBundle(getClass());
+ }
+}
diff --git a/valgrind/org.eclipse.linuxtools.valgrind.core.tests/src/org/eclipse/linuxtools/valgrind/core/tests/TestSourceReader.java b/valgrind/org.eclipse.linuxtools.valgrind.core.tests/src/org/eclipse/linuxtools/valgrind/core/tests/TestSourceReader.java
new file mode 100644
index 0000000000..bfb15457c6
--- /dev/null
+++ b/valgrind/org.eclipse.linuxtools.valgrind.core.tests/src/org/eclipse/linuxtools/valgrind/core/tests/TestSourceReader.java
@@ -0,0 +1,160 @@
+/*******************************************************************************
+ * Copyright (c) 2015 QNX Software Systems and others
+ * 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:
+ * Alena Laskavaia - Initial API and Implementation
+ *******************************************************************************/
+package org.eclipse.linuxtools.valgrind.core.tests;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.junit.Assert;
+import org.osgi.framework.Bundle;
+
+/**
+ * Utilities for reading test source code from plug-in .java sources
+ */
+public class TestSourceReader {
+ private final Bundle bundle;
+ private final String srcRoot;
+ private final Class<?> clazz;
+ private final int numSections;
+
+ /**
+ * @param bundle
+ * the bundle containing the source, if {@code null} can try to load using classpath (source folder has
+ * to be in the classpath for this to work)
+ * @param srcRoot
+ * the directory inside the bundle containing the packages
+ * @param clazz
+ * the name of the class containing the test
+ */
+ public TestSourceReader(Bundle bundle, String srcRoot, Class<?> clazz) {
+ this(bundle, srcRoot, clazz, 0);
+ }
+
+ /**
+ * @param bundle
+ * the bundle containing the source, if {@code null} can try to load using classpath (source folder has
+ * to be in the classpath for this to work)
+ * @param srcRoot
+ * the directory inside the bundle containing the packages
+ * @param clazz
+ * the name of the class containing the test
+ * @param numSections
+ * the number of comment sections preceding the named test to return. Pass zero to get all available
+ * sections.
+ */
+ public TestSourceReader(Bundle bundle, String srcRoot, Class<?> clazz, int numSections) {
+ this.bundle = bundle;
+ this.srcRoot = srcRoot;
+ this.clazz = clazz;
+ this.numSections = numSections;
+ }
+
+ public StringBuilder[] getContentsForTest(final String testName) throws IOException {
+ return getContentsForTest(bundle, srcRoot, clazz, testName, numSections);
+ }
+ /**
+ * Returns an array of StringBuilder objects for each comment section found preceding the named test in the source
+ * code.
+ *
+ * @param bundle
+ * the bundle containing the source, if {@code null} can try to load using classpath (source folder has
+ * to be in the classpath for this to work)
+ * @param srcRoot
+ * the directory inside the bundle containing the packages
+ * @param clazz
+ * the name of the class containing the test
+ * @param testName
+ * the name of the test
+ * @param numSections
+ * the number of comment sections preceding the named test to return. Pass zero to get all available
+ * sections.
+ * @return an array of StringBuilder objects for each comment section found preceding the named test in the source
+ * code.
+ * @throws IOException if a source file is not found
+ */
+ public static StringBuilder[] getContentsForTest(Bundle bundle, String srcRoot, Class<?> clazz, final String testName,
+ int numSections) throws IOException {
+ // Walk up the class inheritance chain until we find the test method.
+ try {
+ while (clazz.getMethod(testName).getDeclaringClass() != clazz) {
+ clazz = clazz.getSuperclass();
+ }
+ } catch (SecurityException e) {
+ Assert.fail(e.getMessage());
+ } catch (NoSuchMethodException e) {
+ Assert.fail(e.getMessage());
+ }
+
+ while (true) {
+ // Find and open the .java file for the class clazz.
+ String fqn = clazz.getName().replace('.', '/');
+ fqn = fqn.indexOf("$") == -1 ? fqn : fqn.substring(0, fqn.indexOf("$"));
+ String classFile = fqn + ".java";
+ IPath filePath = new Path(srcRoot + '/' + classFile);
+
+ InputStream in;
+ Class<?> superclass = clazz.getSuperclass();
+ try {
+ if (bundle != null) {
+ in = FileLocator.openStream(bundle, filePath, false);
+ } else {
+ in = clazz.getResourceAsStream('/' + classFile);
+ }
+ } catch (IOException e) {
+ if (superclass == null || !superclass.getPackage().equals(clazz.getPackage())) {
+ throw e;
+ }
+ clazz = superclass;
+ continue;
+ }
+
+ try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
+ // Read the java file collecting comments until we encounter the test method.
+ List<StringBuilder> contents = new ArrayList<>();
+ StringBuilder content = new StringBuilder();
+ for (String line = br.readLine(); line != null; line = br.readLine()) {
+ line = line.replaceFirst("^\\s*", ""); // Replace leading whitespace, preserve trailing
+ if (line.startsWith("//")) {
+ content.append(line.substring(2) + "\n");
+ } else {
+ if (!line.startsWith("@") && content.length() > 0) {
+ contents.add(content);
+ if (numSections > 0 && contents.size() == numSections + 1)
+ contents.remove(0);
+ content = new StringBuilder();
+ }
+ if (line.length() > 0 && !contents.isEmpty()) {
+ int idx = line.indexOf(testName);
+ if (idx != -1 && !Character.isJavaIdentifierPart(line.charAt(idx + testName.length()))) {
+ return contents.toArray(new StringBuilder[contents.size()]);
+ }
+ if (!line.startsWith("@")) {
+ contents.clear();
+ }
+ }
+ }
+ }
+ }
+
+ if (superclass == null || !superclass.getPackage().equals(clazz.getPackage())) {
+ throw new IOException("Test data not found for " + clazz.getName() + "." + testName);
+ }
+ clazz = superclass;
+ }
+ }
+}

Back to the top