forked from v1v/jenkinslint-plugin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-42268] Added CyclomaticComplexity test cases (WiP)
- Loading branch information
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...st/java/org/jenkins/ci/plugins/jenkinslint/check/CyclomaticComplexityCheckerTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,87 @@ | |||
package org.jenkins.ci.plugins.jenkinslint.check; | |||
|
|||
import hudson.matrix.MatrixProject; | |||
import hudson.maven.MavenModuleSet; | |||
import hudson.model.FreeStyleProject; | |||
import hudson.tasks.BuildStep; | |||
import hudson.tasks.Shell; | |||
import org.jenkins_ci.plugins.run_condition.core.AlwaysRun; | |||
import org.jenkins_ci.plugins.run_condition.core.NeverRun; | |||
import org.jenkins_ci.plugins.run_condition.core.StringsMatchCondition; | |||
import org.jenkins_ci.plugins.run_condition.logic.ConditionContainer; | |||
import org.jenkins_ci.plugins.run_condition.logic.Or; | |||
import org.jenkinsci.plugins.conditionalbuildstep.ConditionalBuilder; | |||
import org.jenkins_ci.plugins.run_condition.BuildStepRunner; | |||
import org.jenkins_ci.plugins.run_condition.core.BooleanCondition; | |||
import org.jenkins_ci.plugins.run_condition.logic.And; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.jvnet.hudson.test.JenkinsRule; | |||
|
|||
import java.util.ArrayList; | |||
|
|||
import static org.junit.Assert.assertFalse; | |||
import static org.junit.Assert.assertTrue; | |||
|
|||
/** | |||
* CyclomaticComplexityChecker Test Case. | |||
* | |||
* @author Victor Martinez | |||
*/ | |||
public class CyclomaticComplexityCheckerTestCase { | |||
private CyclomaticComplexityChecker checker = new CyclomaticComplexityChecker(); | |||
|
|||
@Rule public JenkinsRule j = new JenkinsRule(); | |||
|
|||
private ConditionalBuilder booleanCondition() { | |||
ArrayList<BuildStep> list = new ArrayList<BuildStep>(); | |||
list.add(new Shell("ls")); | |||
list.add(new Shell("ls")); | |||
return new ConditionalBuilder(new BooleanCondition("true"), new BuildStepRunner.Run(), list); | |||
} | |||
private ConditionalBuilder andCondition() { | |||
ArrayList<BuildStep> list = new ArrayList<BuildStep>(); | |||
list.add(new Shell("ls")); | |||
list.add(new Shell("ls")); | |||
ArrayList<ConditionContainer> conditions = new ArrayList<ConditionContainer>(); | |||
conditions.add(new ConditionContainer(new AlwaysRun())); | |||
conditions.add(new ConditionContainer(new StringsMatchCondition("test", "test", false))); | |||
return new ConditionalBuilder(new And(conditions), new BuildStepRunner.Run(), list); | |||
} | |||
private ConditionalBuilder orCondition() { | |||
ArrayList<BuildStep> list = new ArrayList<BuildStep>(); | |||
list.add(new Shell("ls")); | |||
list.add(new Shell("ls")); | |||
ArrayList<ConditionContainer> conditions = new ArrayList<ConditionContainer>(); | |||
conditions.add(new ConditionContainer(new AlwaysRun())); | |||
conditions.add(new ConditionContainer(new StringsMatchCondition("test", "test", false))); | |||
return new ConditionalBuilder(new Or(conditions), new BuildStepRunner.Run(), list); | |||
} | |||
@Test public void testEmptyJob() throws Exception { | |||
FreeStyleProject project = j.createFreeStyleProject(); | |||
assertFalse(checker.executeCheck(project)); | |||
} | |||
@Test public void testConditionJob() throws Exception { | |||
FreeStyleProject project = j.createFreeStyleProject(); | |||
project.getBuildersList().add(booleanCondition()); | |||
project.getBuildersList().add(booleanCondition()); | |||
project.getBuildersList().add(andCondition()); | |||
project.getBuildersList().add(andCondition()); | |||
project.getBuildersList().add(orCondition()); | |||
project.getBuildersList().add(orCondition()); | |||
assertTrue(checker.executeCheck(project)); | |||
} | |||
@Test public void testMavenModuleJob() throws Exception { | |||
MavenModuleSet project = j.createMavenProject(); | |||
assertFalse(checker.executeCheck(project)); | |||
} | |||
@Test public void testMatrixProject() throws Exception { | |||
MatrixProject project = j.createMatrixProject(); | |||
assertFalse(checker.executeCheck(project)); | |||
} | |||
@Test public void testControlComment() throws Exception { | |||
FreeStyleProject project = j.createFreeStyleProject(); | |||
assertFalse(checker.isIgnored(project.getDescription())); | |||
project.setDescription("#lint:ignore:" + checker.getClass().getSimpleName()); | |||
} | |||
} |