Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-45879] Fixing false positive (#23)
* [JENKINS-45879] Fixing false positive with empty lines and return carries

Change-Id: I0604d71b59c5866c69e7ff2185f3e1c821daad00

* Using String Utils and fixed Hash validation with Case Insensitive and also Return Carries with blank spaces

* Added missing conercases such as NPE, since this is a String Library
  • Loading branch information
v1v committed Aug 1, 2017
1 parent 7fa2a63 commit 5f1409c
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 26 deletions.
Expand Up @@ -4,7 +4,7 @@
import hudson.model.AbstractProject;
import hudson.triggers.TimerTrigger;
import org.jenkins.ci.plugins.jenkinslint.model.AbstractCheck;

import org.jenkins.ci.plugins.jenkinslint.utils.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -25,7 +25,8 @@ public boolean executeCheck(Item item) {
String spec = ((AbstractProject) item).getTrigger(TimerTrigger.class).getSpec().toLowerCase();
String[] myData = spec.split("\n");
for (String line: myData) {
if (!isH(line) && !isComment(line) && !isAt(line)) {
if (!StringUtils.isH(line) && !StringUtils.isComment(line) &&
!StringUtils.isAt(line) && !StringUtils.isEmptyOrBlank(line)) {
found = true;
}
}
Expand All @@ -34,28 +35,4 @@ public boolean executeCheck(Item item) {
}
return found;
}

private boolean isComment (String line) {
boolean found = false;
Pattern p = Pattern.compile("^\\s*#\\s*.*");
Matcher m = p.matcher(line);
found = m.matches();
return found;
}

private boolean isH (String line) {
boolean found = false;
Pattern p = Pattern.compile("^\\s*h.*");
Matcher m = p.matcher(line);
found = m.matches();
return found;
}

private boolean isAt (String line) {
boolean found = false;
Pattern p = Pattern.compile("^\\s*@.*");
Matcher m = p.matcher(line);
found = m.matches();
return found;
}
}
@@ -0,0 +1,42 @@
package org.jenkins.ci.plugins.jenkinslint.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author Victor Martinez
*/
public class StringUtils {

public static boolean isEmptyOrBlank (String line) {
boolean found = false;
Pattern p = Pattern.compile("^\\s+");
Matcher m = p.matcher(line);
found = m.matches();
return found || line.isEmpty();
}

public static boolean isComment (String line) {
boolean found = false;
Pattern p = Pattern.compile("^\\s*#\\s*.*");
Matcher m = p.matcher(line);
found = m.matches();
return found;
}

public static boolean isH (String line) {
boolean found = false;
Pattern p = Pattern.compile("^\\s*h.*", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(line);
found = m.matches();
return found;
}

public static boolean isAt (String line) {
boolean found = false;
Pattern p = Pattern.compile("^\\s*@.*");
Matcher m = p.matcher(line);
found = m.matches();
return found;
}
}
Expand Up @@ -7,6 +7,7 @@
import org.jenkins.ci.plugins.jenkinslint.AbstractTestCase;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.WithoutJenkins;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -44,6 +45,19 @@ public class TimerTriggerCheckerTestCase extends AbstractTestCase {
project.save();
assertTrue(checker.executeCheck(project));
}
@Issue("JENKINS-45879")
@Test public void testWithAtTimerTriggerWithReturnCarries() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
TimerTrigger newTrigger = new TimerTrigger(TIMER_WITH_H + "\n\n" + TIMER_WITH_H);
project.addTrigger(newTrigger);
project.save();
assertFalse(checker.executeCheck(project));
project.delete();
newTrigger = new TimerTrigger(TIMER_WITH_H + "\n\n \n" + TIMER_WITH_H);
project.addTrigger(newTrigger);
project.save();
assertFalse(checker.executeCheck(project));
}
@Test public void testWithTimerTrigger() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
TimerTrigger newTrigger = new TimerTrigger(TIMER_WITHOUT_H);
Expand Down
@@ -0,0 +1,65 @@
package org.jenkins.ci.plugins.jenkinslint.utils;

import org.junit.Test;
import org.jvnet.hudson.test.WithoutJenkins;
import org.jenkins.ci.plugins.jenkinslint.utils.StringUtils;
import java.lang.NullPointerException;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* StringUtils Test Case.
*
* @author Victor Martinez
*/
public class StringUtilsTestCase {

@WithoutJenkins
@Test public void testIsAt() {
assertTrue(StringUtils.isAt("@bob"));
assertTrue(StringUtils.isAt(" @bob"));
assertFalse(StringUtils.isAt("bob@"));
}
@WithoutJenkins
@Test(expected = NullPointerException.class)
public void testIsAtNull() throws Exception {
StringUtils.isAt(null);
}
@WithoutJenkins
@Test public void testIsH() {
assertTrue(StringUtils.isH("H bob"));
assertTrue(StringUtils.isH("h bob"));
assertTrue(StringUtils.isH(" h bob"));
assertTrue(StringUtils.isH(" H bob"));
assertFalse(StringUtils.isH("bobH"));
}
@WithoutJenkins
@Test(expected = NullPointerException.class)
public void testIsHNull() throws Exception {
StringUtils.isH(null);
}
@WithoutJenkins
@Test public void testIsComment() {
assertTrue(StringUtils.isComment("# bob"));
assertTrue(StringUtils.isComment(" # bob"));
assertFalse(StringUtils.isComment("bob # bob"));
}
@WithoutJenkins
@Test(expected = NullPointerException.class)
public void testIsCommentNull() throws Exception {
StringUtils.isComment(null);
}
@WithoutJenkins
@Test public void testIsEmptyOrBlank() {
assertTrue(StringUtils.isEmptyOrBlank(""));
assertTrue(StringUtils.isEmptyOrBlank(" "));
assertTrue(StringUtils.isEmptyOrBlank("\n\n"));
assertTrue(StringUtils.isEmptyOrBlank("\n \n"));
assertFalse(StringUtils.isEmptyOrBlank("bob"));
}
@WithoutJenkins
@Test(expected = NullPointerException.class)
public void testIsEmptyOrBlankNull() throws Exception {
StringUtils.isEmptyOrBlank(null);
}
}

0 comments on commit 5f1409c

Please sign in to comment.