Skip to content
Permalink
Browse files

[JENKINS-42268] Added CyclomaticComplexity test cases (WiP)

  • Loading branch information
v1v committed Feb 27, 2017
1 parent 7eaac06 commit 96ed1451b6a5c02907c388b9bceb4dbd8368282b
14 pom.xml
@@ -195,6 +195,20 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>conditional-buildstep</artifactId>
<version>1.3.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>run-condition</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>

<!-- JENKINS-29427 -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
@@ -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());
}
}

0 comments on commit 96ed145

Please sign in to comment.
You can’t perform that action at this time.