Skip to content

Commit

Permalink
Fix test stubs.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Nov 14, 2023
1 parent bc7d2f5 commit b8617a2
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ private static boolean containsNewline(final String expression) {
public boolean isValid() {
DescriptorImpl d = new DescriptorImpl(getJenkinsFacade());

return d.doCheckScript(script).kind == Kind.OK
&& d.doCheckRegexp(regexp).kind == Kind.OK
&& d.doCheckName(name).kind == Kind.OK;
return d.checkScript(script).kind == Kind.OK
&& d.checkRegexp(regexp).kind == Kind.OK
&& d.checkName(name).kind == Kind.OK;
}

public String getId() {
Expand Down Expand Up @@ -180,22 +180,22 @@ public int hashCode() {
*
* @return a new parser instance
* @throws IllegalArgumentException
* if this parsers configuration is not valid
* if this parser configuration is not valid
*/
public IssueParser createParser() {
DescriptorImpl descriptor = new DescriptorImpl(getJenkinsFacade());

FormValidation nameCheck = descriptor.doCheckName(name);
FormValidation nameCheck = descriptor.checkName(name);
if (nameCheck.kind == Kind.ERROR) {
throw new IllegalArgumentException("Name is not valid: " + nameCheck.getMessage());
}

FormValidation scriptCheck = descriptor.doCheckScript(script);
FormValidation scriptCheck = descriptor.checkScript(script);
if (scriptCheck.kind == Kind.ERROR) {
throw new IllegalArgumentException("Script is not valid: " + scriptCheck.getMessage());
}

FormValidation regexpCheck = descriptor.doCheckRegexp(regexp);
FormValidation regexpCheck = descriptor.checkRegexp(regexp);
if (regexpCheck.kind == Kind.ERROR) {
throw new IllegalArgumentException("RegExp is not valid: " + regexpCheck.getMessage());
}
Expand Down Expand Up @@ -280,11 +280,10 @@ public FormValidation doCheckName(@AncestorInPath final BuildableItem project,
return FormValidation.ok();
}

return doCheckName(name);
return checkName(name);

Check warning on line 283 in plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParser.java

View check run for this annotation

Codecov / codecov/patch

plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParser.java#L283

Added line #L283 was not covered by tests
}

@VisibleForTesting
FormValidation doCheckName(final String name) {
FormValidation checkName(final String name) {
if (StringUtils.isBlank(name)) {
return FormValidation.error(Messages.GroovyParser_Error_Name_isEmpty());
}
Expand All @@ -308,11 +307,10 @@ public FormValidation doCheckRegexp(@AncestorInPath final BuildableItem project,
return FormValidation.ok();
}

return doCheckRegexp(regexp);
return checkRegexp(regexp);

Check warning on line 310 in plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParser.java

View check run for this annotation

Codecov / codecov/patch

plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParser.java#L310

Added line #L310 was not covered by tests
}

@VisibleForTesting
FormValidation doCheckRegexp(final String regexp) {
FormValidation checkRegexp(final String regexp) {
try {
if (StringUtils.isBlank(regexp)) {
return FormValidation.error(Messages.GroovyParser_Error_Regexp_isEmpty());
Expand Down Expand Up @@ -347,11 +345,10 @@ public FormValidation doCheckScript(@AncestorInPath final BuildableItem project,
if (!jenkinsFacade.hasPermission(Jenkins.ADMINISTER)) {
return NO_RUN_SCRIPT_PERMISSION_WARNING;
}
return doCheckScript(script);
return checkScript(script);

Check warning on line 348 in plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParser.java

View check run for this annotation

Codecov / codecov/patch

plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParser.java#L348

Added line #L348 was not covered by tests
}

@VisibleForTesting
FormValidation doCheckScript(final String script) {
FormValidation checkScript(final String script) {
try {
if (StringUtils.isBlank(script)) {
return FormValidation.error(Messages.GroovyParser_Error_Script_isEmpty());
Expand Down Expand Up @@ -393,11 +390,10 @@ public FormValidation doCheckExample(@AncestorInPath final BuildableItem project
if (!jenkinsFacade.hasPermission(Jenkins.ADMINISTER)) {
return NO_RUN_SCRIPT_PERMISSION_WARNING;
}
return doCheckExample(example, regexp, script);
return checkExample(example, regexp, script);

Check warning on line 393 in plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParser.java

View check run for this annotation

Codecov / codecov/patch

plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/GroovyParser.java#L393

Added line #L393 was not covered by tests
}

@VisibleForTesting
FormValidation doCheckExample(final String example, final String regexp, final String script) {
FormValidation checkExample(final String example, final String regexp, final String script) {
if (StringUtils.isNotBlank(example) && StringUtils.isNotBlank(regexp) && StringUtils.isNotBlank(script)) {
FormValidation response = parseExample(script, example, regexp, containsNewline(regexp));
if (example.length() <= MAX_EXAMPLE_SIZE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.Report.IssueFilterBuilder;

import hudson.model.AbstractProject;
import hudson.model.BuildableItem;
import hudson.model.Item;

import io.jenkins.plugins.analysis.core.filter.IncludeType.DescriptorImpl;
Expand Down Expand Up @@ -48,7 +48,7 @@ void issue54035() {
@Test
void shouldValidatePattern() {
JenkinsFacade jenkinsFacade = mock(JenkinsFacade.class);
when(jenkinsFacade.hasPermission(Item.CONFIGURE, (AbstractProject<?, ?>) null)).thenReturn(true);
when(jenkinsFacade.hasPermission(Item.CONFIGURE, (BuildableItem) null)).thenReturn(true);

RegexpFilterDescriptor descriptor = new DescriptorImpl(jenkinsFacade);
assertThat(descriptor.doCheckPattern(null, null)).isOk().hasMessage(Messages.pattern_blank());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import hudson.model.AbstractProject;
import hudson.model.BuildableItem;
import hudson.model.Item;
import hudson.model.Job;
import hudson.util.ComboBoxModel;
Expand Down Expand Up @@ -64,7 +64,7 @@ private ToolSelectionDescriptor createDescriptor(final String[] elements, final
JenkinsFacade jenkinsFacade = mock(JenkinsFacade.class);
when(jenkinsFacade.getAllJobs()).thenReturn(Lists.list(job));
ToolSelectionDescriptor.setJenkinsFacade(jenkinsFacade);
when(jenkinsFacade.hasPermission(Item.CONFIGURE, (AbstractProject<?, ?>) null)).thenReturn(hasPermission);
when(jenkinsFacade.hasPermission(Item.CONFIGURE, (BuildableItem) null)).thenReturn(hasPermission);
return toolSelectionDescriptor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import edu.hm.hafner.util.SerializableTest;

import hudson.model.AbstractProject;
import hudson.model.BuildableItem;
import hudson.model.Item;

import io.jenkins.plugins.analysis.core.util.QualityGate.QualityGateDescriptor;
Expand All @@ -24,7 +24,7 @@ class QualityGateTest extends SerializableTest<QualityGate> {
@Test
void shouldValidateThreshold() {
JenkinsFacade jenkinsFacade = mock(JenkinsFacade.class);
when(jenkinsFacade.hasPermission(Item.CONFIGURE, (AbstractProject<?, ?>) null)).thenReturn(true);
when(jenkinsFacade.hasPermission(Item.CONFIGURE, (BuildableItem) null)).thenReturn(true);

QualityGateDescriptor descriptor = new QualityGateDescriptor(jenkinsFacade);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import edu.hm.hafner.analysis.parser.checkstyle.CheckStyleParser;

import hudson.model.AbstractProject;
import hudson.model.Item;
import jenkins.model.Jenkins;

import io.jenkins.plugins.analysis.warnings.RegisteredParser.Descriptor;
import io.jenkins.plugins.util.JenkinsFacade;
Expand Down Expand Up @@ -73,14 +72,13 @@ class DescriptorTest {
@Test
void shouldPopulateListOfParsers() {
JenkinsFacade jenkins = mock(JenkinsFacade.class);
AbstractProject<?, ?> job = mock(AbstractProject.class);
when(jenkins.hasPermission(Item.CONFIGURE, job)).thenReturn(true);
when(jenkins.hasPermission(Jenkins.READ)).thenReturn(true);

Descriptor descriptor = new Descriptor(jenkins);
assertThat(descriptor.getId()).isEqualTo(Descriptor.ANALYSIS_MODEL_ID);
assertThat(descriptor.doFillAnalysisModelIdItems()).extracting(o -> o.value).first().isEqualTo("acu-cobol");

when(jenkins.hasPermission(Item.CONFIGURE, job)).thenReturn(false);
when(jenkins.hasPermission(Jenkins.READ)).thenReturn(false);
assertThat(descriptor.doFillAnalysisModelIdItems()).isEmpty();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void matchMultiLine(final String multiLineRegexp) throws IOException {
assertThat(parser.hasMultiLineSupport()).as("Wrong multi line support guess").isTrue();

DescriptorImpl descriptor = createDescriptor();
assertThat(descriptor.doCheckExample(textToMatch, multiLineRegexp, script)).isOk();
assertThat(descriptor.checkExample(textToMatch, multiLineRegexp, script)).isOk();

IssueParser instance = parser.createParser();
Run<?, ?> run = mock(Run.class);
Expand Down Expand Up @@ -112,8 +112,6 @@ void shouldThrowExceptionDueToMissingName() {
assertThat(groovyParser.isValid()).isFalse();
assertThatIllegalArgumentException().isThrownBy(groovyParser::createParser)
.withMessageContaining("Name is not valid");

verifyThatValidationIsSkippedIfUserHasNoPermissions(groovyParser);
}

@Test
Expand All @@ -122,8 +120,6 @@ void shouldThrowExceptionDueToBrokenScript() {
assertThat(groovyParser.isValid()).isFalse();
assertThatIllegalArgumentException().isThrownBy(groovyParser::createParser)
.withMessageContaining("Script is not valid");

verifyThatValidationIsSkippedIfUserHasNoPermissions(groovyParser);
}

@Test
Expand All @@ -132,15 +128,6 @@ void shouldThrowExceptionDueToBrokenRegExp() {
assertThat(groovyParser.isValid()).isFalse();
assertThatIllegalArgumentException().isThrownBy(groovyParser::createParser)
.withMessageContaining("RegExp is not valid");

groovyParser.setJenkinsFacade(mock(JenkinsFacade.class));
assertThat(groovyParser.isValid()).isTrue();
}

private void verifyThatValidationIsSkippedIfUserHasNoPermissions(final GroovyParser groovyParser) {
groovyParser.setJenkinsFacade(mock(JenkinsFacade.class));
assertThat(groovyParser.isValid()).isTrue();
assertThatNoException().isThrownBy(groovyParser::createParser);
}

@Test
Expand All @@ -164,63 +151,63 @@ void shouldDetectSingleLineRegularExpression() {
void shouldAcceptOnlyNonEmptyStringsAsName() {
DescriptorImpl descriptor = createDescriptor();

assertThat(descriptor.doCheckName(null)).isError();
assertThat(descriptor.doCheckName(StringUtils.EMPTY)).isError();
assertThat(descriptor.doCheckName("Java Parser 2")).isOk();
assertThat(descriptor.checkName(null)).isError();
assertThat(descriptor.checkName(StringUtils.EMPTY)).isError();
assertThat(descriptor.checkName("Java Parser 2")).isOk();
}

@Test
void shouldRejectInvalidRegularExpressions() {
DescriptorImpl descriptor = createDescriptor();

assertThat(descriptor.doCheckRegexp(null)).isError();
assertThat(descriptor.doCheckRegexp(StringUtils.EMPTY)).isError();
assertThat(descriptor.doCheckRegexp("one brace (")).isError();
assertThat(descriptor.doCheckRegexp("backslash \\")).isError();
assertThat(descriptor.checkRegexp(null)).isError();
assertThat(descriptor.checkRegexp(StringUtils.EMPTY)).isError();
assertThat(descriptor.checkRegexp("one brace (")).isError();
assertThat(descriptor.checkRegexp("backslash \\")).isError();

assertThat(descriptor.doCheckRegexp("^.*[a-z]")).isOk();
assertThat(descriptor.checkRegexp("^.*[a-z]")).isOk();
}

@Test
void shouldRejectInvalidScripts() {
DescriptorImpl descriptor = createDescriptor();

assertThat(descriptor.doCheckScript(null)).isError();
assertThat(descriptor.doCheckScript(StringUtils.EMPTY)).isError();
assertThat(descriptor.doCheckScript("Hello World")).isError();
assertThat(descriptor.checkScript(null)).isError();
assertThat(descriptor.checkScript(StringUtils.EMPTY)).isError();
assertThat(descriptor.checkScript("Hello World")).isError();

assertThat(descriptor.doCheckScript(toString("parser.groovy"))).isOk();
assertThat(descriptor.checkScript(toString("parser.groovy"))).isOk();
}

@Test
void shouldFindOneIssueWithValidScriptAndRegularExpression() {
DescriptorImpl descriptor = createDescriptor();

assertThat(descriptor.doCheckExample(SINGLE_LINE_EXAMPLE, SINGLE_LINE_REGEXP,
assertThat(descriptor.checkExample(SINGLE_LINE_EXAMPLE, SINGLE_LINE_REGEXP,
toString("parser.groovy"))).isOk();
}

@Test
void shouldReportErrorWhenNoMatchesAreFoundInExample() {
DescriptorImpl descriptor = createDescriptor();

assertThat(descriptor.doCheckExample("this is a warning message", SINGLE_LINE_REGEXP,
assertThat(descriptor.checkExample("this is a warning message", SINGLE_LINE_REGEXP,
toString("parser.groovy"))).isError();
}

@Test
void shouldReportErrorWhenRegularExpressionHasIllegalMatchAccess() {
DescriptorImpl descriptor = createDescriptor();

assertThat(descriptor.doCheckExample(SINGLE_LINE_EXAMPLE, "^\\s*(.*):(\\d+):(.*)$",
assertThat(descriptor.checkExample(SINGLE_LINE_EXAMPLE, "^\\s*(.*):(\\d+):(.*)$",
toString("parser.groovy"))).isError();
}

@Test
void shouldAcceptMultiLineRegularExpression() {
DescriptorImpl descriptor = createDescriptor();

assertThat(descriptor.doCheckExample(MULTI_LINE_EXAMPLE, MULTI_LINE_REGEXP,
assertThat(descriptor.checkExample(MULTI_LINE_EXAMPLE, MULTI_LINE_REGEXP,
toString("multiline.groovy"))).isOk();
}

Expand Down

0 comments on commit b8617a2

Please sign in to comment.