Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JENKINS-16855: Add claim build action configuration slicer
- Loading branch information
Showing
3 changed files
with
162 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
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,81 @@ | ||
package configurationslicing.claim; | ||
|
||
import hudson.Extension; | ||
import hudson.model.AbstractProject; | ||
import hudson.model.Descriptor; | ||
import hudson.model.Hudson; | ||
import hudson.plugins.claim.ClaimPublisher; | ||
import hudson.tasks.Publisher; | ||
import hudson.util.DescribableList; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import configurationslicing.BooleanSlicer; | ||
|
||
@Extension | ||
public class ClaimSlicer extends BooleanSlicer<AbstractProject<?,?>> { | ||
public ClaimSlicer() { | ||
super(new ClaimSpec()); | ||
} | ||
|
||
public boolean isLoaded() { | ||
try { | ||
new ClaimPublisher(); | ||
return true; | ||
} catch (Throwable t) { | ||
return false; | ||
} | ||
} | ||
|
||
public static class ClaimSpec implements BooleanSlicer.BooleanSlicerSpec<AbstractProject<?,?>> | ||
{ | ||
public String getName() { | ||
return "Claim Slicer"; | ||
} | ||
|
||
public String getName(AbstractProject<?,?> item) { | ||
return item.getFullName(); | ||
} | ||
|
||
public String getUrl() { | ||
return "claim"; | ||
} | ||
|
||
public boolean getValue(AbstractProject<?,?> item) { | ||
|
||
DescribableList<Publisher, Descriptor<Publisher>> publishersList = item.getPublishersList(); | ||
ClaimPublisher claimPublisher = publishersList.get(ClaimPublisher.class); | ||
return claimPublisher != null; | ||
} | ||
|
||
@SuppressWarnings({ "unchecked", "rawtypes" }) | ||
public List<AbstractProject<?,?>> getWorkDomain() { | ||
return (List)Hudson.getInstance().getAllItems(AbstractProject.class); | ||
} | ||
|
||
public boolean setValue(AbstractProject<?,?> item, boolean value) { | ||
boolean oldval = getValue(item); | ||
if (value == oldval) { | ||
return true; | ||
} else if (value==false) { // request to remove the publisher | ||
DescribableList<Publisher, Descriptor<Publisher>> publishersList = item.getPublishersList(); | ||
ClaimPublisher claimPublisher = publishersList.get(ClaimPublisher.class); | ||
try { | ||
publishersList.remove(claimPublisher); | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
return true; | ||
} else { // request to add the publisher | ||
DescribableList<Publisher, Descriptor<Publisher>> publishersList = item.getPublishersList(); | ||
try { | ||
publishersList.add(new ClaimPublisher()); | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/test/java/configurationslicing/claim/ClaimSlicerTest.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,75 @@ | ||
package configurationslicing.claim; | ||
|
||
import hudson.model.AbstractProject; | ||
|
||
import org.junit.Test; | ||
import org.jvnet.hudson.test.HudsonTestCase; | ||
|
||
import configurationslicing.claim.ClaimSlicer.ClaimSpec; | ||
import configurationslicing.claim.ClaimSlicer; | ||
|
||
|
||
|
||
public class ClaimSlicerTest extends HudsonTestCase { | ||
|
||
/* | ||
* Test that we can interrogate and set values using the claim slicer on free style projects | ||
*/ | ||
@Test | ||
public void testFreeStyleValues() throws Exception { | ||
AbstractProject<?,?> item = createFreeStyleProject(); | ||
doTestValues(item); | ||
} | ||
|
||
/* | ||
* Test that we can interrogate and set values using the claim slicer on maven projects | ||
*/ | ||
|
||
@Test | ||
public void testMavenValues() throws Exception { | ||
AbstractProject<?,?> item = createMavenProject(); | ||
doTestValues(item); | ||
} | ||
|
||
/* | ||
* Test that the is loaded method returns true if the claim plug-in is loaded. | ||
* The test that this method returns false otherwise is problematic to test meaningfully in the unit test environment | ||
*/ | ||
@Test | ||
public void testIsLoaded() { | ||
ClaimSlicer slicer = new ClaimSlicer(); | ||
boolean isLoaded = slicer.isLoaded(); | ||
assertTrue("Expect claim slicer to be loaded when we have the claim plugin",isLoaded); | ||
} | ||
|
||
|
||
private void doTestValues(AbstractProject<?,?> item) { | ||
ClaimSpec spec = new ClaimSpec(); | ||
boolean claimsEnabled = spec.getValue(item); | ||
assertFalse("Claims should be disabled on a new project",claimsEnabled); | ||
|
||
boolean valueSet = spec.setValue(item, false); | ||
assertTrue("disabling a value when it is already disabled should work", valueSet); | ||
|
||
valueSet = spec.setValue(item,true); | ||
assertTrue("setting a value when it is disabled should work", valueSet); | ||
|
||
claimsEnabled = spec.getValue(item); | ||
assertTrue("Claims should be enabled after they have been set",claimsEnabled); | ||
|
||
valueSet = spec.setValue(item,true); | ||
assertTrue("setting a value when it is already enabled should work", valueSet); | ||
|
||
valueSet = spec.setValue(item,false); | ||
assertTrue("removing the publisher when it is enabled should work", valueSet); | ||
|
||
claimsEnabled = spec.getValue(item); | ||
assertFalse("Claims should be disabled after they have been unset",claimsEnabled); | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
} |