while (lines.hasNext()) {
coveredLines.add(Integer.parseInt(lines.next()));
}
- TestCase testCase = context.getTestCase(testFile, testCaseName);
+ TestCase testCase = context.newTestCase()
+ .inTestFile(testFile)
+ .name(testCaseName);
if (testCase == null) {
throw new IllegalStateException("No test case with name " + testCaseName + " on file " + testFile);
}
String message = split.next();
String stack = split.next();
long duration = Long.parseLong(split.next());
- context.addTestCase(context.testCaseBuilder(testFile, name)
- .type(TestCase.Type.valueOf(type))
+ context.newTestCase()
+ .inTestFile(testFile)
+ .name(name)
+ .ofType(TestCase.Type.valueOf(type))
.status(TestCase.Status.valueOf(status))
.message(StringUtils.trimToNull(message))
.stackTrace(StringUtils.trimToNull(stack))
.durationInMs(duration)
- .build());
+ .save();
} catch (Exception e) {
throw new IllegalStateException("Error processing line " + lineNumber + " of file " + testplanFile.getAbsolutePath(), e);
}
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
import org.sonar.api.batch.fs.InputFile.Type;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.sensor.SensorContext;
+import org.sonar.api.batch.sensor.SensorStorage;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.test.TestCase;
-import org.sonar.api.batch.sensor.test.internal.DefaultTestCaseBuilder;
+import org.sonar.api.batch.sensor.test.internal.DefaultTestCase;
import java.io.File;
import java.io.IOException;
fileSystem.add(inputFile);
fileSystem.add(testFile);
- TestCase test1 = new DefaultTestCaseBuilder(testFile, "test1").durationInMs(10).build();
- TestCase test2 = new DefaultTestCaseBuilder(testFile, "test2").durationInMs(10).build();
- when(context.getTestCase(testFile, "test1")).thenReturn(test1);
- when(context.getTestCase(testFile, "test2")).thenReturn(test2);
+ final SensorStorage sensorStorage = mock(SensorStorage.class);
+
+ when(context.newTestCase()).thenAnswer(new Answer<TestCase>() {
+ @Override
+ public TestCase answer(InvocationOnMock invocation) throws Throwable {
+ return new DefaultTestCase(sensorStorage);
+ }
+ });
+
+ TestCase test1 = new DefaultTestCase(null).inTestFile(testFile).name("test1");
+ TestCase test2 = new DefaultTestCase(null).inTestFile(testFile).name("test2");
sensor.execute(context);
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
import org.sonar.api.batch.fs.InputFile.Type;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.sensor.SensorContext;
+import org.sonar.api.batch.sensor.SensorStorage;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.test.TestCase;
-import org.sonar.api.batch.sensor.test.internal.DefaultTestCaseBuilder;
+import org.sonar.api.batch.sensor.test.internal.DefaultTestCase;
import java.io.File;
import java.io.IOException;
.setType(Type.TEST);
fileSystem.add(testFile);
- when(context.testCaseBuilder(testFile, "test1")).thenReturn(new DefaultTestCaseBuilder(testFile, "test1"));
- when(context.testCaseBuilder(testFile, "test2")).thenReturn(new DefaultTestCaseBuilder(testFile, "test2"));
+ final SensorStorage sensorStorage = mock(SensorStorage.class);
+
+ when(context.newTestCase()).thenAnswer(new Answer<TestCase>() {
+ @Override
+ public TestCase answer(InvocationOnMock invocation) throws Throwable {
+ return new DefaultTestCase(sensorStorage);
+ }
+ });
sensor.execute(context);
- verify(context).addTestCase(new DefaultTestCaseBuilder(testFile, "test1").durationInMs(10).build());
- verify(context).addTestCase(
- new DefaultTestCaseBuilder(testFile, "test2").type(TestCase.Type.INTEGRATION).status(TestCase.Status.ERROR).message("message").stackTrace("stack").durationInMs(15).build());
+ verify(sensorStorage).store(new DefaultTestCase(null)
+ .inTestFile(testFile)
+ .name("test1")
+ .durationInMs(10));
+ verify(sensorStorage).store(new DefaultTestCase(null)
+ .inTestFile(testFile)
+ .name("test2")
+ .ofType(TestCase.Type.INTEGRATION)
+ .status(TestCase.Status.ERROR)
+ .message("message")
+ .stackTrace("stack")
+ .durationInMs(15));
}
}
}
@Override
- public void addTestCase(TestCase testCase) {
+ public void store(TestCase testCase) {
File testRes = getTestResource(((DefaultTestCase) testCase).testFile());
MutableTestPlan testPlan = perspectives.as(MutableTestPlan.class, testRes);
if (testPlan != null) {
}
}
- @Override
- public TestCase getTestCase(InputFile testFile, String testCaseName) {
- File testRes = getTestResource(testFile);
- MutableTestPlan testPlan = perspectives.as(MutableTestPlan.class, testRes);
- if (testPlan != null) {
- Iterable<MutableTestCase> testCases = testPlan.testCasesByName(testCaseName);
- if (testCases.iterator().hasNext()) {
- MutableTestCase testCase = testCases.iterator().next();
- return new DefaultTestCase(testFile, testCaseName, testCase.durationInMs(), TestCase.Status.of(testCase.status().name()), testCase.message(),
- TestCase.Type.valueOf(testCase
- .type()), testCase.stackTrace());
- }
- }
- return null;
- }
-
@Override
public void saveCoveragePerTest(TestCase testCase, InputFile coveredFile, List<Integer> coveredLines) {
Preconditions.checkArgument(coveredFile.type() == Type.MAIN, "Should be a main file: " + coveredFile);
import org.sonar.api.batch.sensor.measure.Measure;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
import org.sonar.api.batch.sensor.symbol.SymbolTableBuilder;
-import org.sonar.api.batch.sensor.test.TestCaseBuilder;
-import org.sonar.api.batch.sensor.test.internal.DefaultTestCaseBuilder;
+import org.sonar.api.batch.sensor.test.TestCase;
+import org.sonar.api.batch.sensor.test.internal.DefaultTestCase;
import org.sonar.api.config.Settings;
import org.sonar.batch.duplication.BlockCache;
import org.sonar.batch.duplication.DefaultTokenBuilder;
}
@Override
- public TestCaseBuilder testCaseBuilder(InputFile testFile, String testCaseName) {
- return new DefaultTestCaseBuilder(testFile, testCaseName);
+ public TestCase newTestCase() {
+ return new DefaultTestCase(this);
}
}
}
@Override
- public void addTestCase(TestCase testCase) {
+ public void store(TestCase testCase) {
if (testCaseCache.contains(((DefaultTestCase) testCase).testFile(), testCase.name())) {
throw new IllegalArgumentException("There is already a test case with the same name: " + testCase.name());
}
testCaseCache.put(((DefaultTestCase) testCase).testFile(), testCase);
}
- @Override
- public TestCase getTestCase(InputFile testFile, String testCaseName) {
- return testCaseCache.get(testFile, testCaseName);
- }
-
@Override
public void saveCoveragePerTest(TestCase testCase, InputFile coveredFile, List<Integer> coveredLines) {
Preconditions.checkNotNull(testCase);
long duration = value.getLong();
TestCase.Type type = TestCase.Type.values()[value.getInt()];
TestCase.Status status = TestCase.Status.values()[value.getInt()];
- return new DefaultTestCase(testFile, name, duration != -1 ? duration : null, status, message, type, stack);
+ return new DefaultTestCase(null)
+ .inTestFile(testFile)
+ .ofType(type)
+ .name(name)
+ .durationInMs(duration != -1 ? duration : null)
+ .status(status)
+ .message(message)
+ .stackTrace(stack);
}
}
import org.mockito.ArgumentCaptor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.SonarIndex;
+import org.sonar.api.batch.fs.InputDir;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
+import org.sonar.api.batch.fs.internal.DefaultInputDir;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
+import org.sonar.api.batch.sensor.issue.Issue.Severity;
import org.sonar.api.batch.sensor.issue.internal.DefaultIssue;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
import org.sonar.api.component.ResourcePerspectives;
import org.sonar.api.issue.Issue;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.MetricFinder;
+import org.sonar.api.measures.PersistenceMode;
+import org.sonar.api.resources.Directory;
import org.sonar.api.resources.File;
import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Resource;
import org.sonar.api.rule.RuleKey;
import org.sonar.batch.duplication.BlockCache;
import org.sonar.batch.duplication.DuplicationCache;
private SensorContext sensorContext;
private Settings settings;
private ResourcePerspectives resourcePerspectives;
+ private Project project;
@Before
public void prepare() {
fs = new DefaultFileSystem();
MetricFinder metricFinder = mock(MetricFinder.class);
when(metricFinder.findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC);
+ when(metricFinder.findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION);
sensorContext = mock(SensorContext.class);
settings = new Settings();
resourcePerspectives = mock(ResourcePerspectives.class);
ComponentDataCache componentDataCache = mock(ComponentDataCache.class);
BlockCache blockCache = mock(BlockCache.class);
- adaptor = new SensorContextAdapter(sensorContext, metricFinder, new Project("myProject"),
+ project = new Project("myProject");
+ adaptor = new SensorContextAdapter(sensorContext, metricFinder, project,
resourcePerspectives, settings, fs, activeRules, componentDataCache, blockCache, mock(DuplicationCache.class), mock(SonarIndex.class));
}
}
@Test
- public void shouldAddMeasureToSensorContext() {
+ public void shouldFailIfUnknowMetric() {
+ InputFile file = new DefaultInputFile("foo", "src/Foo.php");
+
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("Unknow metric with key: lines");
+
+ adaptor.store(new DefaultMeasure()
+ .onFile(file)
+ .forMetric(CoreMetrics.LINES)
+ .withValue(10));
+ }
+
+ @Test
+ public void shouldSaveFileMeasureToSensorContext() {
InputFile file = new DefaultInputFile("foo", "src/Foo.php");
ArgumentCaptor<org.sonar.api.measures.Measure> argumentCaptor = ArgumentCaptor.forClass(org.sonar.api.measures.Measure.class);
}
@Test
- public void shouldAddIssue() {
+ public void shouldSetAppropriatePersistenceMode() {
+ // Metric FUNCTION_COMPLEXITY_DISTRIBUTION is only persisted on directories.
+
+ InputFile file = new DefaultInputFile("foo", "src/Foo.php");
+
+ ArgumentCaptor<org.sonar.api.measures.Measure> argumentCaptor = ArgumentCaptor.forClass(org.sonar.api.measures.Measure.class);
+ when(sensorContext.saveMeasure(eq(file), argumentCaptor.capture())).thenReturn(null);
+
+ adaptor.store(new DefaultMeasure()
+ .onFile(file)
+ .forMetric(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION)
+ .withValue("foo"));
+
+ org.sonar.api.measures.Measure m = argumentCaptor.getValue();
+ assertThat(m.getData()).isEqualTo("foo");
+ assertThat(m.getMetric()).isEqualTo(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION);
+ assertThat(m.getPersistenceMode()).isEqualTo(PersistenceMode.MEMORY);
+
+ }
+
+ @Test
+ public void shouldSaveProjectMeasureToSensorContext() {
+
+ ArgumentCaptor<org.sonar.api.measures.Measure> argumentCaptor = ArgumentCaptor.forClass(org.sonar.api.measures.Measure.class);
+ when(sensorContext.saveMeasure(argumentCaptor.capture())).thenReturn(null);
+
+ adaptor.store(new DefaultMeasure()
+ .onProject()
+ .forMetric(CoreMetrics.NCLOC)
+ .withValue(10));
+
+ org.sonar.api.measures.Measure m = argumentCaptor.getValue();
+ assertThat(m.getValue()).isEqualTo(10.0);
+ assertThat(m.getMetric()).isEqualTo(CoreMetrics.NCLOC);
+ }
+
+ @Test
+ public void shouldAddIssueOnFile() {
InputFile file = new DefaultInputFile("foo", "src/Foo.php");
ArgumentCaptor<Issue> argumentCaptor = ArgumentCaptor.forClass(Issue.class);
assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("foo", "bar"));
assertThat(issue.message()).isEqualTo("Foo");
assertThat(issue.line()).isEqualTo(3);
+ assertThat(issue.severity()).isNull();
+ assertThat(issue.effortToFix()).isEqualTo(10.0);
+ }
+
+ @Test
+ public void shouldAddIssueOnDirectory() {
+ InputDir dir = new DefaultInputDir("foo", "src");
+
+ ArgumentCaptor<Issue> argumentCaptor = ArgumentCaptor.forClass(Issue.class);
+
+ Issuable issuable = mock(Issuable.class);
+ when(resourcePerspectives.as(Issuable.class, Directory.create("src"))).thenReturn(issuable);
+
+ when(issuable.addIssue(argumentCaptor.capture())).thenReturn(true);
+
+ adaptor.store(new DefaultIssue()
+ .onDir(dir)
+ .ruleKey(RuleKey.of("foo", "bar"))
+ .message("Foo")
+ .effortToFix(10.0));
+
+ Issue issue = argumentCaptor.getValue();
+ assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("foo", "bar"));
+ assertThat(issue.message()).isEqualTo("Foo");
+ assertThat(issue.line()).isNull();
+ assertThat(issue.severity()).isNull();
assertThat(issue.effortToFix()).isEqualTo(10.0);
}
+
+ @Test
+ public void shouldAddIssueOnProject() {
+ ArgumentCaptor<Issue> argumentCaptor = ArgumentCaptor.forClass(Issue.class);
+
+ Issuable issuable = mock(Issuable.class);
+ when(resourcePerspectives.as(Issuable.class, (Resource) project)).thenReturn(issuable);
+
+ when(issuable.addIssue(argumentCaptor.capture())).thenReturn(true);
+
+ adaptor.store(new DefaultIssue()
+ .onProject()
+ .ruleKey(RuleKey.of("foo", "bar"))
+ .message("Foo")
+ .overrideSeverity(Severity.BLOCKER)
+ .effortToFix(10.0));
+
+ Issue issue = argumentCaptor.getValue();
+ assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("foo", "bar"));
+ assertThat(issue.message()).isEqualTo("Foo");
+ assertThat(issue.line()).isNull();
+ assertThat(issue.severity()).isEqualTo("BLOCKER");
+ assertThat(issue.effortToFix()).isEqualTo(10.0);
+ }
+
}
import org.sonar.api.batch.sensor.measure.Measure;
import org.sonar.api.batch.sensor.symbol.SymbolTableBuilder;
import org.sonar.api.batch.sensor.test.TestCase;
-import org.sonar.api.batch.sensor.test.TestCaseBuilder;
import org.sonar.api.config.Settings;
-import javax.annotation.CheckForNull;
-
import java.io.Serializable;
import java.util.List;
/**
* Create a new test case for the given test file.
- * @param testFile An {@link InputFile} with type {@link InputFile.Type#TEST}
- * @param testCaseName name of the test case
- * @since 5.0
- */
- TestCaseBuilder testCaseBuilder(InputFile testFile, String testCaseName);
-
- /**
- * Add a new test case.
- * Use {@link #testCaseBuilder(InputFile, String)} to create a new {@link TestCase}
- * @throws IllegalArgumentException if a test case with same name was already added on the same file.
- * @since 5.0
- */
- void addTestCase(TestCase testCase);
-
- /**
- * Get a {@link TestCase} that has been previously added to the context with {@link #addTestCase(TestCase)}.
+ * Don't forget to call {@link TestCase#save()} once all parameters are provided.
* @since 5.0
*/
- @CheckForNull
- TestCase getTestCase(InputFile testFile, String testCaseName);
+ TestCase newTestCase();
/**
* Register coverage of a given test case on another main file. TestCase should have been registered using {@link #testPlanBuilder(InputFile)}
import org.sonar.api.batch.sensor.issue.Issue;
import org.sonar.api.batch.sensor.measure.Measure;
+import org.sonar.api.batch.sensor.test.TestCase;
/**
* Interface for storing data computed by sensors.
void store(Issue issue);
+ void store(TestCase testCase);
+
}
*/
package org.sonar.api.batch.sensor.test;
+import org.sonar.api.batch.fs.InputFile;
+
import javax.annotation.Nullable;
/**
- * Represents a single test in a test plan.
+ * Represents a single test in a test file.
* @since 5.0
- *
*/
public interface TestCase {
+
+ /**
+ * Test execution status.
+ */
enum Status {
OK, FAILURE, ERROR, SKIPPED;
}
}
+ /**
+ * Test type.
+ */
enum Type {
UNIT, INTEGRATION;
}
+ /**
+ * InputFile where this test is located.
+ */
+ InputFile testFile();
+
+ /**
+ * Set file where this test is located. Mandatory.
+ */
+ TestCase inTestFile(InputFile testFile);
+
/**
* Duration in milliseconds
*/
Long durationInMs();
- Type type();
+ /**
+ * Duration in milliseconds
+ */
+ TestCase durationInMs(long duration);
+
+ /**
+ * Name of this test case.
+ */
+ String name();
+
+ /**
+ * Set name of this test. Name is mandatory.
+ */
+ TestCase name(String name);
/**
* Status of execution of the test.
Status status();
/**
- * Name of this test case.
+ * Status of execution of the test.
*/
- String name();
+ TestCase status(Status status);
/**
* Message (usually in case of {@link Status#ERROR} or {@link Status#FAILURE}).
*/
String message();
+ /**
+ * Message (usually in case of {@link Status#ERROR} or {@link Status#FAILURE}).
+ */
+ TestCase message(String message);
+
+ /**
+ * Type of test.
+ */
+ Type type();
+
+ /**
+ * Type of test.
+ */
+ TestCase ofType(Type type);
+
/**
* Stacktrace (usually in case of {@link Status#ERROR}).
*/
String stackTrace();
+ /**
+ * Set stacktrace (usually in case of {@link Status#ERROR}).
+ */
+ TestCase stackTrace(String stackTrace);
+
+ /**
+ * Call this method only once when your are done with defining the test case.
+ */
+ void save();
+
}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.sensor.test;
-
-import org.sonar.api.batch.sensor.test.TestCase.Status;
-import org.sonar.api.batch.sensor.test.TestCase.Type;
-
-/**
- * Builder to create a new TestCase on a test file.
- * @since 5.0
- */
-public interface TestCaseBuilder {
-
- /**
- * Duration in milliseconds
- */
- TestCaseBuilder durationInMs(long duration);
-
- /**
- * Status of execution of the test.
- */
- TestCaseBuilder status(Status status);
-
- /**
- * Message (usually in case of {@link Status#ERROR} or {@link Status#FAILURE}).
- */
- TestCaseBuilder message(String message);
-
- /**
- * Type of test.
- */
- TestCaseBuilder type(Type type);
-
- /**
- * Stacktrace (usually in case of {@link Status#ERROR}).
- */
- TestCaseBuilder stackTrace(String stackTrace);
-
- /**
- * Call this method only once when your are done with defining the test case.
- */
- TestCase build();
-
-}
*/
package org.sonar.api.batch.sensor.test.internal;
+import com.google.common.base.Preconditions;
+import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.sensor.SensorStorage;
import org.sonar.api.batch.sensor.test.TestCase;
import javax.annotation.CheckForNull;
public class DefaultTestCase implements TestCase {
- private final InputFile testFile;
- private final String name;
- private final Long duration;
- private final Status status;
- private final String message;
- private final Type type;
- private final String stackTrace;
+ private final SensorStorage storage;
+ private InputFile testFile;
+ private String name;
+ private Long duration;
+ private TestCase.Status status = Status.OK;
+ private String message;
+ private TestCase.Type type = Type.UNIT;
+ private String stackTrace;
- public DefaultTestCase(InputFile testFile, String name, @Nullable Long duration, Status status, @Nullable String message, Type type, @Nullable String stackTrace) {
+ public DefaultTestCase(SensorStorage storage) {
+ this.storage = storage;
+ }
+
+ @Override
+ public DefaultTestCase inTestFile(InputFile testFile) {
+ Preconditions.checkNotNull(testFile, "TestFile cannot be null");
+ Preconditions.checkArgument(testFile.type() == InputFile.Type.TEST, "Should be a test file: " + testFile);
this.testFile = testFile;
+ return this;
+ }
+
+ @Override
+ public DefaultTestCase name(String name) {
+ Preconditions.checkArgument(StringUtils.isNotBlank(name), "Test name is mandatory and should not be blank");
this.name = name;
+ return this;
+ }
+
+ @Override
+ public DefaultTestCase durationInMs(long duration) {
+ Preconditions.checkArgument(duration >= 0, "Test duration must be positive (got: " + duration + ")");
this.duration = duration;
+ return this;
+ }
+
+ @Override
+ public DefaultTestCase status(TestCase.Status status) {
+ Preconditions.checkNotNull(status);
this.status = status;
+ return this;
+ }
+
+ @Override
+ public DefaultTestCase message(@Nullable String message) {
this.message = message;
+ return this;
+ }
+
+ @Override
+ public DefaultTestCase ofType(TestCase.Type type) {
+ Preconditions.checkNotNull(type);
this.type = type;
+ return this;
+ }
+
+ @Override
+ public DefaultTestCase stackTrace(@Nullable String stackTrace) {
this.stackTrace = stackTrace;
+ return this;
}
public InputFile testFile() {
return stackTrace;
}
+ @Override
+ public void save() {
+ Preconditions.checkNotNull(this.storage, "No persister on this object");
+ Preconditions.checkNotNull(testFile, "TestFile is mandatory");
+ Preconditions.checkNotNull(name, "TestFile is mandatory");
+ storage.store(this);
+ }
+
// Just for unit tests
@Override
public boolean equals(Object obj) {
.append("stackTrace", stackTrace)
.toString();
}
+
}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.sensor.test.internal;
-
-import com.google.common.base.Preconditions;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.sensor.test.TestCase;
-import org.sonar.api.batch.sensor.test.TestCase.Status;
-import org.sonar.api.batch.sensor.test.TestCase.Type;
-import org.sonar.api.batch.sensor.test.TestCaseBuilder;
-
-import javax.annotation.Nullable;
-
-public class DefaultTestCaseBuilder implements TestCaseBuilder {
-
- private final InputFile testFile;
- private final String name;
- private Long duration;
- private TestCase.Status status = Status.OK;
- private String message;
- private TestCase.Type type = Type.UNIT;
- private String stackTrace;
-
- public DefaultTestCaseBuilder(InputFile testFile, String name) {
- Preconditions.checkArgument(testFile.type() == InputFile.Type.TEST, "Should be a test file: " + testFile);
- Preconditions.checkArgument(StringUtils.isNotBlank(name), "Test name should not be blank");
- this.testFile = testFile;
- this.name = name;
- }
-
- @Override
- public TestCaseBuilder durationInMs(long duration) {
- Preconditions.checkArgument(duration >= 0, "Test duration must be positive (got: " + duration + ")");
- this.duration = duration;
- return this;
- }
-
- @Override
- public TestCaseBuilder status(TestCase.Status status) {
- Preconditions.checkNotNull(status);
- this.status = status;
- return this;
- }
-
- @Override
- public TestCaseBuilder message(@Nullable String message) {
- this.message = message;
- return this;
- }
-
- @Override
- public TestCaseBuilder type(TestCase.Type type) {
- this.type = type;
- return this;
- }
-
- @Override
- public TestCaseBuilder stackTrace(@Nullable String stackTrace) {
- this.stackTrace = stackTrace;
- return this;
- }
-
- @Override
- public TestCase build() {
- return new DefaultTestCase(this.testFile, this.name, this.duration, this.status, this.message, this.type, this.stackTrace);
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.sensor.test.internal;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.sensor.test.TestCase.Status;
-import org.sonar.api.batch.sensor.test.TestCase.Type;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class DefaultTestCaseBuilderTest {
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- private InputFile parent = new DefaultInputFile("foo", "src/Foo.php").setType(InputFile.Type.TEST);
-
- @Test
- public void testBuilder() throws Exception {
- DefaultTestCaseBuilder builder = new DefaultTestCaseBuilder(parent, "myTest");
- assertThat(builder.durationInMs(1)
- .message("message")
- .stackTrace("stack")
- .status(Status.ERROR)
- .type(Type.UNIT)
- .build()).isEqualTo(new DefaultTestCase(parent, "myTest", 1L, Status.ERROR, "message", Type.UNIT, "stack"));
- }
-
- @Test
- public void testBuilderWithDefaultValues() throws Exception {
- DefaultTestCaseBuilder builder = new DefaultTestCaseBuilder(parent, "myTest");
- assertThat(builder.build()).isEqualTo(
- new DefaultTestCase(parent, "myTest", null, Status.OK, null, Type.UNIT, null));
- }
-
- @Test
- public void testInvalidDuration() throws Exception {
- DefaultTestCaseBuilder builder = new DefaultTestCaseBuilder(parent, "myTest");
-
- thrown.expect(IllegalArgumentException.class);
-
- builder.durationInMs(-3)
- .message("message")
- .stackTrace("stack")
- .status(Status.ERROR)
- .type(Type.UNIT)
- .build();
- }
-
-}
*/
package org.sonar.api.batch.sensor.test.internal;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.sensor.test.TestCase.Status;
public class DefaultTestCaseTest {
- private InputFile parent = new DefaultInputFile("foo", "src/Foo.php");
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private InputFile parent = new DefaultInputFile("foo", "src/Foo.php").setType(InputFile.Type.TEST);
+
+ @Test
+ public void testCreation() throws Exception {
+ DefaultTestCase testCase = new DefaultTestCase(null)
+ .inTestFile(parent)
+ .name("myTest")
+ .durationInMs(1)
+ .message("message")
+ .stackTrace("stack")
+ .status(Status.ERROR)
+ .ofType(Type.UNIT);
+
+ assertThat(testCase.name()).isEqualTo("myTest");
+ assertThat(testCase.testFile()).isEqualTo(parent);
+ assertThat(testCase.durationInMs()).isEqualTo(1L);
+ assertThat(testCase.message()).isEqualTo("message");
+ assertThat(testCase.stackTrace()).isEqualTo("stack");
+ assertThat(testCase.status()).isEqualTo(Status.ERROR);
+ assertThat(testCase.type()).isEqualTo(Type.UNIT);
+ }
@Test
- public void testDefaultTestCaseTest() {
- DefaultTestCase testCase1 = new DefaultTestCase(parent, "myTest", 1L, Status.ERROR, "message", Type.UNIT, "stack");
-
- assertThat(testCase1.name()).isEqualTo("myTest");
- assertThat(testCase1.durationInMs()).isEqualTo(1L);
- assertThat(testCase1.status()).isEqualTo(Status.ERROR);
- assertThat(testCase1.message()).isEqualTo("message");
- assertThat(testCase1.type()).isEqualTo(Type.UNIT);
- assertThat(testCase1.stackTrace()).isEqualTo("stack");
+ public void testCreationWithDefaultValues() throws Exception {
+ DefaultTestCase testCase = new DefaultTestCase(null)
+ .inTestFile(parent)
+ .name("myTest");
+
+ assertThat(testCase.name()).isEqualTo("myTest");
+ assertThat(testCase.testFile()).isEqualTo(parent);
+ assertThat(testCase.durationInMs()).isNull();
+ assertThat(testCase.message()).isNull();
+ assertThat(testCase.stackTrace()).isNull();
+ assertThat(testCase.status()).isEqualTo(Status.OK);
+ assertThat(testCase.type()).isEqualTo(Type.UNIT);
+ }
+
+ @Test
+ public void testInvalidDuration() throws Exception {
+ DefaultTestCase builder = new DefaultTestCase(null)
+ .inTestFile(parent)
+ .name("myTest");
+
+ thrown.expect(IllegalArgumentException.class);
+
+ builder.durationInMs(-3);
}
@Test
public void testEqualsHashCodeToString() {
- DefaultTestCase testCase1 = new DefaultTestCase(parent, "myTest", 1L, Status.ERROR, "message", Type.UNIT, "stack");
- DefaultTestCase testCase1a = new DefaultTestCase(parent, "myTest", 1L, Status.ERROR, "message", Type.UNIT, "stack");
- DefaultTestCase testCase2 = new DefaultTestCase(new DefaultInputFile("foo2", "src/Foo.php"), "myTest2", 2L, Status.FAILURE, "message2", Type.INTEGRATION, null);
+ DefaultTestCase testCase1 = new DefaultTestCase(null)
+ .inTestFile(parent)
+ .name("myTest")
+ .durationInMs(1)
+ .message("message")
+ .stackTrace("stack")
+ .status(Status.ERROR)
+ .ofType(Type.UNIT);
+ DefaultTestCase testCase1a = new DefaultTestCase(null)
+ .inTestFile(parent)
+ .name("myTest")
+ .durationInMs(1)
+ .message("message")
+ .stackTrace("stack")
+ .status(Status.ERROR)
+ .ofType(Type.UNIT);
+ DefaultTestCase testCase2 = new DefaultTestCase(null)
+ .inTestFile(new DefaultInputFile("foo2", "src/Foo.php").setType(InputFile.Type.TEST))
+ .name("myTest2")
+ .durationInMs(2)
+ .message("message2")
+ .stackTrace("null")
+ .status(Status.FAILURE)
+ .ofType(Type.INTEGRATION);
assertThat(testCase1).isEqualTo(testCase1);
assertThat(testCase1).isEqualTo(testCase1a);