Skip to content

Commit

Permalink
Merge pull request #9 from ninian/master
Browse files Browse the repository at this point in the history
JENKINS-16855: Add claim build action configuration slicer
  • Loading branch information
ninian committed Oct 3, 2014
2 parents ded8666 + a150eb8 commit 6cec5bb
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pom.xml
@@ -1,10 +1,9 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jvnet.hudson.plugins</groupId>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.395</version>
<relativePath>../pom.xml</relativePath>
<version>1.424.6</version>
</parent>
<artifactId>configurationslicing</artifactId>
<packaging>hpi</packaging>
Expand All @@ -23,7 +22,7 @@

<dependencies>
<dependency>
<groupId>org.jvnet.hudson.main</groupId>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>maven-plugin</artifactId>
<optional>true</optional>
</dependency>
Expand Down Expand Up @@ -93,6 +92,12 @@
<version>1.0</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>claim</artifactId>
<version>2.3</version>
<optional>true</optional>
</dependency>
</dependencies>

<scm>
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/configurationslicing/claim/ClaimSlicer.java
@@ -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 src/test/java/configurationslicing/claim/ClaimSlicerTest.java
@@ -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);

}





}

0 comments on commit 6cec5bb

Please sign in to comment.