|
| 1 | +package hudson.plugins.release; |
| 2 | + |
| 3 | +import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput; |
| 4 | +import com.gargoylesoftware.htmlunit.html.HtmlForm; |
| 5 | +import com.gargoylesoftware.htmlunit.html.HtmlPage; |
| 6 | + |
| 7 | +import org.jvnet.hudson.test.JenkinsRule; |
| 8 | + |
| 9 | +import hudson.model.*; |
| 10 | +import hudson.plugins.release.ReleaseWrapper; |
| 11 | +import hudson.plugins.release.ReleaseWrapper.ReleaseAction; |
| 12 | +import hudson.tasks.BuildWrapper; |
| 13 | +import hudson.util.DescribableList; |
| 14 | + |
| 15 | +import java.util.Collection; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import static org.hamcrest.CoreMatchers.not; |
| 19 | +import static org.hamcrest.Matchers.is; |
| 20 | +import static org.hamcrest.Matchers.notNullValue; |
| 21 | + |
| 22 | +import static org.junit.Assert.assertNotNull; |
| 23 | +import static org.junit.Assert.assertThat; |
| 24 | +import static org.junit.Assert.assertTrue; |
| 25 | + |
| 26 | +import org.junit.After; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.Rule; |
| 30 | + |
| 31 | +/** |
| 32 | + * Test the general integration of the plugin with Jenkins/Hudson and ensure it gets added properly and correctly add it's links/buttons etc to the |
| 33 | + * required pages. |
| 34 | + */ |
| 35 | +public class TestReleasePluginJob |
| 36 | +{ |
| 37 | + @Rule |
| 38 | + public JenkinsRule j = new JenkinsRule(); |
| 39 | + |
| 40 | + FreeStyleProject job; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void createJob() throws Exception |
| 44 | + { |
| 45 | + job = j.createFreeStyleProject("test"); |
| 46 | + } |
| 47 | + |
| 48 | + @After |
| 49 | + public void deleteJob() throws Exception |
| 50 | + { |
| 51 | + job.delete(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testPermission() throws Exception |
| 56 | + { |
| 57 | + // Check if the user has permission to release |
| 58 | + assertTrue("No permission", ReleaseWrapper.hasReleasePermission(job)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testInBuilders() throws Exception |
| 63 | + { |
| 64 | + // Check the job has the ReleaseWrapper listed in it's builders, AND that it it listed in the project actions |
| 65 | + enableReleaseBuilder(); |
| 66 | + DescribableList<BuildWrapper, Descriptor<BuildWrapper>> buildWrappersList = job.getBuildWrappersList(); |
| 67 | + Descriptor<BuildWrapper> descriptor = Descriptor.find(ReleaseWrapper.DescriptorImpl.class.getName()); |
| 68 | + BuildWrapper buildWrapper = buildWrappersList.get(descriptor); |
| 69 | + assertNotNull(buildWrapper); |
| 70 | + Collection<? extends Action> actions = buildWrapper.getProjectActions(job); |
| 71 | + assertNotNull("Actions can't be null", actions); |
| 72 | + assertThat("Must have actions", actions.size(), not(0)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testInActions() throws Exception |
| 77 | + { |
| 78 | + // Check that the job has an action listed when release is enabled |
| 79 | + enableReleaseBuilder(); |
| 80 | + assertNotNull("Must have action", job.getAction(ReleaseAction.class)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testEnableReleaseUsingWebPage() throws Exception |
| 85 | + { |
| 86 | + enableReleaseBuilder(); |
| 87 | + |
| 88 | + /** Check if the Release links exist */ |
| 89 | + HtmlPage page = j.createWebClient().goTo("job/test/"); |
| 90 | + // Internationalisation not needed because the URL doesn't change between languages |
| 91 | + // see getUrlName() in ReleaseAction |
| 92 | + List<Object> nodes = page.selectNodes("//a[contains(@href, '/test/release')]"); |
| 93 | + assertThat("Require 2 href links", nodes.size(), is(2)); |
| 94 | + } |
| 95 | + |
| 96 | + /** Enable the release builder by adding it to the job */ |
| 97 | + private void enableReleaseBuilder() throws Exception |
| 98 | + { |
| 99 | + /** Enable the release plugin for the job */ |
| 100 | + HtmlForm configForm = j.createWebClient().getPage(job, "configure").getFormByName("config"); |
| 101 | + assertThat(configForm, notNullValue()); |
| 102 | + |
| 103 | + // not sure if internationalisation required for this? |
| 104 | + HtmlCheckBoxInput releaseCheckButton = configForm.getInputByName("hudson-plugins-release-ReleaseWrapper"); |
| 105 | + assertThat("No Release Check Button found", releaseCheckButton, notNullValue()); |
| 106 | + releaseCheckButton.setChecked(true); |
| 107 | + j.submit(configForm); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments