Browse Source

SONAR-19050 Add tests to fix coverage

tags/10.1.0.73491
Léo Geoffroy 1 year ago
parent
commit
04039ecb04

+ 11
- 1
sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java View File

import org.junit.Test; import org.junit.Test;
import org.sonar.api.issue.Issue; import org.sonar.api.issue.Issue;
import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.RuleType;
import org.sonar.api.utils.Duration; import org.sonar.api.utils.Duration;


import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
.setUpdateDate(new SimpleDateFormat("yyyy-MM-dd").parse("2013-08-20")) .setUpdateDate(new SimpleDateFormat("yyyy-MM-dd").parse("2013-08-20"))
.setCloseDate(new SimpleDateFormat("yyyy-MM-dd").parse("2013-08-21")) .setCloseDate(new SimpleDateFormat("yyyy-MM-dd").parse("2013-08-21"))
.setSelectedAt(1400000000000L) .setSelectedAt(1400000000000L)
.setRuleDescriptionContextKey(TEST_CONTEXT_KEY);
.setRuleDescriptionContextKey(TEST_CONTEXT_KEY)
.setType(RuleType.BUG);


assertThat((Object) issue.getLocations()).isEqualTo("loc"); assertThat((Object) issue.getLocations()).isEqualTo("loc");
assertThat(issue.locationsChanged()).isTrue(); assertThat(issue.locationsChanged()).isTrue();
assertThat(issue.closeDate()).isEqualTo(new SimpleDateFormat("yyyy-MM-dd").parse("2013-08-21")); assertThat(issue.closeDate()).isEqualTo(new SimpleDateFormat("yyyy-MM-dd").parse("2013-08-21"));
assertThat(issue.selectedAt()).isEqualTo(1400000000000L); assertThat(issue.selectedAt()).isEqualTo(1400000000000L);
assertThat(issue.getRuleDescriptionContextKey()).contains(TEST_CONTEXT_KEY); assertThat(issue.getRuleDescriptionContextKey()).contains(TEST_CONTEXT_KEY);
assertThat(issue.type()).isEqualTo(RuleType.BUG);
} }


@Test @Test


assertThat(defaultIssue.isQuickFixAvailable()).isFalse(); assertThat(defaultIssue.isQuickFixAvailable()).isFalse();
} }

@Test
public void characteristic_shouldThrowIllegalStateException() {
DefaultIssue defaultIssue = new DefaultIssue();
assertThatThrownBy(() -> defaultIssue.characteristic()).isInstanceOf(IllegalStateException.class);
}
} }

+ 9
- 0
sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultExternalIssueTest.java View File

import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.rule.Severity; import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.internal.SensorStorage; import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.code.CodeCharacteristic;
import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.RuleType; import org.sonar.api.rules.RuleType;


.hasMessageContaining("Severity is mandatory"); .hasMessageContaining("Severity is mandatory");
} }


@Test
public void characteristic_shouldThrowIllegalStateException() {
SensorStorage storage = mock(SensorStorage.class);
DefaultExternalIssue issue = new DefaultExternalIssue(project, storage);
assertThatThrownBy(issue::characteristic).isInstanceOf(IllegalStateException.class);
assertThatThrownBy(() -> issue.characteristic(CodeCharacteristic.CLEAR)).isInstanceOf(IllegalStateException.class);
}

} }

+ 9
- 1
sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/rule/internal/DefaultAdHocRuleTest.java View File

import org.sonar.api.batch.rule.Severity; import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.internal.SensorStorage; import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.batch.sensor.rule.NewAdHocRule; import org.sonar.api.batch.sensor.rule.NewAdHocRule;
import org.sonar.api.code.CodeCharacteristic;
import org.sonar.api.rules.RuleType; import org.sonar.api.rules.RuleType;


import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
.description("desc") .description("desc")
.severity(Severity.BLOCKER) .severity(Severity.BLOCKER)
.type(RuleType.CODE_SMELL); .type(RuleType.CODE_SMELL);
rule.save();
rule.save();


assertThat(rule.engineId()).isEqualTo("engine"); assertThat(rule.engineId()).isEqualTo("engine");
assertThat(rule.ruleId()).isEqualTo("ruleId"); assertThat(rule.ruleId()).isEqualTo("ruleId");
.hasMessageContaining("Type is mandatory"); .hasMessageContaining("Type is mandatory");
} }


@Test
public void characteristic_shouldThrowIllegalStateException() {
SensorStorage storage = mock(SensorStorage.class);
DefaultAdHocRule rule = new DefaultAdHocRule(storage);
assertThatThrownBy(() -> rule.characteristic(CodeCharacteristic.CLEAR)).isInstanceOf(IllegalStateException.class);
assertThatThrownBy(() -> rule.characteristic()).isInstanceOf(IllegalStateException.class);
}
} }

Loading…
Cancel
Save