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.
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
Showing
4 changed files
with
124 additions
and
26 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
42 changes: 42 additions & 0 deletions
42
src/main/java/org/jenkins/ci/plugins/jenkinslint/utils/StringUtils.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 | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
src/test/java/org/jenkins/ci/plugins/jenkinslint/utils/StringUtilsTestCase.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 | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |