Skip to content

Commit

Permalink
[FIXED JENKINS-10716]
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob_robertson committed Aug 16, 2011
1 parent e0639a2 commit 7c2ea9d
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 7 deletions.
21 changes: 18 additions & 3 deletions src/main/java/hudson/views/RegExJobFilter.java
Expand Up @@ -58,7 +58,8 @@ public List<String> getMatchValues(TopLevelItem item) {
List<String> values = new ArrayList<String>();
if (valueType == ValueType.DESCRIPTION) {
if (item instanceof AbstractItem) {
values.add(((AbstractItem) item).getDescription());
String desc = ((AbstractItem) item).getDescription();
addSplitValues(values, desc);
}
} else if (valueType == ValueType.SCM) {
if (item instanceof SCMedItem) {
Expand All @@ -76,11 +77,23 @@ public List<String> getMatchValues(TopLevelItem item) {
values.addAll(mavenValues);
} else if (valueType == ValueType.SCHEDULE) {
List<String> scheduleValues = TriggerFilterHelper.getValues(item);
values.addAll(scheduleValues);
for (String scheduleValue: scheduleValues) {
// we do this split, because the spec may have multiple lines - especially including the comment
addSplitValues(values, scheduleValue);
}
}
return values;
}

private void addSplitValues(List<String> values, String value) {
if (value != null) {
String[] split = value.split("\n");
for (String s: split) {
// trimming this is necessary to remove odd characters that cause problems
// the real example here is the description won't work without this trim
values.add(s.trim());
}
}
}
public boolean matches(TopLevelItem item) {
List<String> matchValues = getMatchValues(item);
boolean matched = false;
Expand All @@ -89,6 +102,8 @@ public boolean matches(TopLevelItem item) {
if (matchValue != null &&
// this doesn't use "find" because that would be too inclusive,
// and at this point it might break existing people's regexes
// - just to clarify this a bit more - if someone configures the regex of "Util.*"
// we cannot assume they want to match (find) a value of "SpecialUtil"
pattern.matcher(matchValue).matches()) {
matched = true;
break;
Expand Down
133 changes: 129 additions & 4 deletions src/test/java/hudson/views/RegExJobFilterTest.java
Expand Up @@ -2,7 +2,10 @@

import hudson.model.AbstractProject;
import hudson.model.Cause;
import hudson.model.DependencyGraph;
import hudson.model.Hudson;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.Job;
import hudson.model.Label;
import hudson.model.Node;
Expand All @@ -17,16 +20,21 @@
import hudson.scm.CVSSCM;
import hudson.scm.PollingResult;
import hudson.scm.SCM;
import hudson.security.Permission;
import hudson.triggers.TimerTrigger;
import hudson.util.DescribableList;
import hudson.views.AbstractIncludeExcludeJobFilter.IncludeExcludeType;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.SortedMap;

import junit.framework.TestCase;
import org.jvnet.hudson.test.HudsonTestCase;

public class RegExJobFilterTest extends TestCase {
public class RegExJobFilterTest extends HudsonTestCase {

/**
* Test all the helpers to see that no exceptions are thrown.
Expand Down Expand Up @@ -117,9 +125,43 @@ private void doTestScmRegEx(String root, String modules, String branch, boolean
boolean matched = filter.matches(item);
assertEquals(expectMatch, matched);
}
@SuppressWarnings({ "rawtypes", "unchecked" })

public void testDescription() throws IOException {
doTestDescription("", false);
doTestDescription(null, false);
doTestDescription("nothing", false);
doTestDescription("desc=test", true);
doTestDescription("mydesc=test2", true);
doTestDescription("thisis\nmydesc=testn2\nforyou", true);
doTestDescription("1&#xd;\ndesc=test&#xd;\n2", true);
doTestDescription("1 desc=test 2", true);
}
private void doTestDescription(String desc, boolean expectMatch) throws IOException {
RegExJobFilter filter = new RegExJobFilter(".*desc=test.*", IncludeExcludeType.includeMatched.toString(), RegExJobFilter.ValueType.DESCRIPTION.toString());
TestItem item = new TestItem("name");
item.setDescription(desc);
boolean matched = filter.matches(item);
assertEquals(expectMatch, matched);
}
public void testTrigger() throws Exception {
doTestTrigger("# monday", true);
doTestTrigger("# tuesday", false);
doTestTrigger("* * * * *", false);
doTestTrigger("* * * * *\n#monday", true);
doTestTrigger("#monday\n* * * * *", true);
}
@SuppressWarnings("unchecked")
private void doTestTrigger(String spec, boolean expectMatch) throws Exception {
RegExJobFilter filter = new RegExJobFilter(".*monday.*", IncludeExcludeType.includeMatched.toString(), RegExJobFilter.ValueType.SCHEDULE.toString());
TestProject proj = new TestProject("proj");
proj.addTrigger(new TimerTrigger(spec));
boolean matched = filter.matches(proj);
assertEquals(expectMatch, matched);
}
@SuppressWarnings({ "unchecked" })
private class TestItem extends Job implements SCMedItem, TopLevelItem {

private String description;
private SCM scm;

public TestItem(String name) {
Expand All @@ -130,7 +172,7 @@ public TestItem(String name, SCM scm) {
super(null, name);
this.scm = scm;
}

public AbstractProject<?, ?> asProject() {
return null;
}
Expand Down Expand Up @@ -226,6 +268,89 @@ public boolean isBuildBlocked() {
public boolean isConcurrentBuild() {
return false;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

}
@SuppressWarnings("unchecked")
static class TestProject extends AbstractProject implements TopLevelItem {

public TestProject(String name) {
super(new TestItemGroup(), name);
}
@Override
protected void buildDependencyGraph(DependencyGraph graph) {
}
@Override
protected Class getBuildClass() {
return null;
}
@Override
public DescribableList getPublishersList() {
return null;
}
@Override
public boolean isFingerprintConfigured() {
return false;
}
protected void removeRun(Run run) {
}
@Override
protected synchronized void saveNextBuildNumber() throws IOException {
}
@Override
public void checkPermission(Permission p) {
super.checkPermission(p);
}
public TopLevelItemDescriptor getDescriptor() {
return null;
}
@Override
public Hudson getParent() {
return null;
}
@Override
public synchronized void save() throws IOException {
// do nothing!
}
}
@SuppressWarnings("unchecked")
static class TestItemGroup implements ItemGroup {
public String getDisplayName() {
return null;
}
public void save() throws IOException {
}
public String getFullDisplayName() {
return null;
}
public String getFullName() {
return null;
}
public Item getItem(String name) {
return null;
}
public Collection getItems() {
return null;
}
public File getRootDirFor(Item child) {
return null;
}
public String getUrl() {
return null;
}
public String getUrlChildPrefix() {
return null;
}
public File getRootDir() {
return null;
}

}
}

0 comments on commit 7c2ea9d

Please sign in to comment.