From 2c572f56d951a8f7781ee45c217aa786213056f8 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 4 Mar 2016 16:59:16 +0100 Subject: [PATCH] SONARJNKNS-238 Make dependency on Maven optional --- .../sonar/it/jenkins/JenkinsTestSuite.java | 2 +- .../sonar/it/jenkins/JenkinsWithoutMaven.java | 109 ++++++++++++++++++ .../orchestrator/JenkinsOrchestrator.java | 36 +++++- pom.xml | 2 + .../sonar/SonarGlobalConfiguration.java | 4 + .../plugins/sonar/SonarInstallation.java | 9 +- .../hudson/plugins/sonar/SonarPublisher.java | 2 +- .../plugins/sonar/client/HttpClient.java | 2 +- 8 files changed, 158 insertions(+), 8 deletions(-) create mode 100644 its/src/test/java/com/sonar/it/jenkins/JenkinsWithoutMaven.java diff --git a/its/src/test/java/com/sonar/it/jenkins/JenkinsTestSuite.java b/its/src/test/java/com/sonar/it/jenkins/JenkinsTestSuite.java index bc7af74a..3d09146e 100644 --- a/its/src/test/java/com/sonar/it/jenkins/JenkinsTestSuite.java +++ b/its/src/test/java/com/sonar/it/jenkins/JenkinsTestSuite.java @@ -27,7 +27,7 @@ import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) -@SuiteClasses({JenkinsTest.class}) +@SuiteClasses({JenkinsTest.class, JenkinsWithoutMaven.class}) public class JenkinsTestSuite { @ClassRule diff --git a/its/src/test/java/com/sonar/it/jenkins/JenkinsWithoutMaven.java b/its/src/test/java/com/sonar/it/jenkins/JenkinsWithoutMaven.java new file mode 100644 index 00000000..e97e5a31 --- /dev/null +++ b/its/src/test/java/com/sonar/it/jenkins/JenkinsWithoutMaven.java @@ -0,0 +1,109 @@ +/* + * Jenkins :: Integration Tests + * Copyright (C) 2013 ${owner} + * sonarqube@googlegroups.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package com.sonar.it.jenkins; + +import com.sonar.it.jenkins.orchestrator.JenkinsOrchestrator; +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.SynchronousAnalyzer; +import com.sonar.orchestrator.locator.FileLocation; +import com.sonar.orchestrator.locator.Location; +import com.sonar.orchestrator.locator.URLLocation; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.sonar.wsclient.services.PropertyUpdateQuery; +import org.sonar.wsclient.services.ResourceQuery; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +import static org.fest.assertions.Assertions.assertThat; + +public class JenkinsWithoutMaven { + @ClassRule + public static Orchestrator orchestrator = JenkinsTestSuite.ORCHESTRATOR; + + @ClassRule + public static JenkinsOrchestrator jenkins = JenkinsOrchestrator.builderEnv().build(); + + @BeforeClass + public static void setUpSonar() throws MalformedURLException { + // Workaround for SONAR-4257 + orchestrator.getServer().getAdminWsClient().update(new PropertyUpdateQuery("sonar.core.serverBaseURL", orchestrator.getServer().getUrl())); + } + + @BeforeClass + public static void setUpJenkins() throws IOException { + orchestrator.resetData(); + Location sqJenkinsPluginLocation = FileLocation.of("../target/sonar.hpi"); + jenkins + .disablePlugin("maven-plugin") + .installPlugin(URLLocation.create(new URL("http://mirrors.jenkins-ci.org/plugins/jquery/1.11.2-0/jquery.hpi"))) + .installPlugin(URLLocation.create(new URL("http://mirrors.jenkins-ci.org/plugins/filesystem_scm/1.20/filesystem_scm.hpi"))) + .installPlugin(sqJenkinsPluginLocation) + .configureMavenInstallation() + .configureSonarRunner2_4Installation() + .configureMsBuildSQScanner_installation() + .configureSonarInstallation(orchestrator); + jenkins.checkSavedSonarInstallation(orchestrator); + jenkins.configureDefaultQG(orchestrator); + } + + @Before + public void resetData() throws Exception { + orchestrator.resetData(); + } + + @Test + public void testFreestyleJobWithSonarRunner() throws Exception { + String jobName = "abacus-runner"; + String projectKey = "abacus-runner"; + assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(projectKey))).isNull(); + jenkins + .newFreestyleJobWithSonarRunner(jobName, "-X", new File("projects", "abacus"), + "sonar.projectKey", projectKey, + "sonar.projectVersion", "1.0", + "sonar.projectName", "Abacus", + "sonar.sources", "src/main/java") + .executeJob(jobName); + waitForComputationOnSQServer(); + assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(projectKey))).isNotNull(); + assertSonarUrlOnJob(jobName, projectKey); + jenkins.assertQGOnProjectPage(jobName); + } + + @Test + public void testNoSonarPublisher() { + String jobName = "no Sonar Publisher"; + jenkins.assertNoSonarPublisher(jobName, new File("projects", "noPublisher")); + } + + private void assertSonarUrlOnJob(String jobName, String projectKey) { + assertThat(jenkins.getSonarUrlOnJob(jobName)).startsWith(orchestrator.getServer().getUrl()); + assertThat(jenkins.getSonarUrlOnJob(jobName)).endsWith(projectKey); + } + + private void waitForComputationOnSQServer() { + new SynchronousAnalyzer(orchestrator.getServer()).waitForDone(); + } +} diff --git a/its/src/test/java/com/sonar/it/jenkins/orchestrator/JenkinsOrchestrator.java b/its/src/test/java/com/sonar/it/jenkins/orchestrator/JenkinsOrchestrator.java index 8336da54..d026d14e 100644 --- a/its/src/test/java/com/sonar/it/jenkins/orchestrator/JenkinsOrchestrator.java +++ b/its/src/test/java/com/sonar/it/jenkins/orchestrator/JenkinsOrchestrator.java @@ -81,6 +81,7 @@ public class JenkinsOrchestrator extends SingleStartExternalResource { private JenkinsServer server; private WebDriver driver; private CLI cli; + private static int tokenCounter = 0; JenkinsOrchestrator(Configuration config, JenkinsDistribution distribution) { this.config = config; @@ -449,7 +450,7 @@ public void checkSavedSonarInstallation(Orchestrator orchestrator) { driver.get(server.getUrl() + "/configure"); try { - Thread.sleep(1000); + Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } @@ -484,7 +485,7 @@ public void checkSavedSonarInstallation(Orchestrator orchestrator) { } public String generateToken(Orchestrator orchestrator) { - String json = orchestrator.getServer().adminWsClient().post("api/user_tokens/generate", "name", "token"); + String json = orchestrator.getServer().adminWsClient().post("api/user_tokens/generate", "name", "token" + tokenCounter++); Map response = (Map) JSONValue.parse(json); return (String) response.get("token"); } @@ -498,6 +499,18 @@ public JenkinsOrchestrator installPlugin(Location hpi) { return this; } + public JenkinsOrchestrator disablePlugin(String name) throws IOException { + File f = new File(new File(server.getHome(), "plugins"), name + ".hpi.disabled"); + f.createNewFile(); + return this; + } + + public JenkinsOrchestrator enablePlugin(String name) { + File f = new File(new File(server.getHome(), "plugins"), name + ".hpi.disabled"); + FileUtils.deleteQuietly(f); + return this; + } + public BuildResult executeJob(String jobName) { BuildResult result = executeJobQuietly(jobName); if (result.getStatus() != 0) { @@ -551,6 +564,14 @@ public WebElement findElement(By by) { } } + public void assertNoElement(By by) { + List elements = driver.findElements(by); + if (!elements.isEmpty()) { + System.err.println("Not expecting finding element, but found " + elements.size() + ". Save screenshot to: target/no_such_element.png"); + takeScreenshot(new File("target/no_such_element.png")); + } + } + public WebElement findElement(By by, int index) { try { List elms = driver.findElements(by); @@ -587,6 +608,17 @@ public Boolean apply(WebDriver input) { }); } + public void assertNoSonarPublisher(String jobName, File projectPath) { + newFreestyleJobConfig(jobName, projectPath); + + WebElement addPostBuildButton = findElement(buttonByText("Add post-build action")); + scrollToElement(addPostBuildButton); + addPostBuildButton.click(); + findElement(By.linkText("SonarQube analysis with Maven")).click(); + assertNoElement(By.xpath("//div[@descriptorid='hudson.plugins.sonar.SonarPublisher']")); + findElement(buttonByText("Save")).click(); + } + public void select(WebElement element, String optionValue) { Select select = new Select(element); select.selectByValue(optionValue); diff --git a/pom.xml b/pom.xml index 954abf22..ce7a54e6 100644 --- a/pom.xml +++ b/pom.xml @@ -118,9 +118,11 @@ + org.jenkins-ci.main maven-plugin 2.7.1 + true org.jenkins-ci.plugins diff --git a/src/main/java/hudson/plugins/sonar/SonarGlobalConfiguration.java b/src/main/java/hudson/plugins/sonar/SonarGlobalConfiguration.java index c891138b..140dc2ae 100644 --- a/src/main/java/hudson/plugins/sonar/SonarGlobalConfiguration.java +++ b/src/main/java/hudson/plugins/sonar/SonarGlobalConfiguration.java @@ -36,6 +36,10 @@ import java.util.List; +/** + * Since 2.4 + * The global configuration was migrated from SonarPublisher to this component. + */ @Extension(ordinal = 100) public class SonarGlobalConfiguration extends GlobalConfiguration { @CopyOnWrite diff --git a/src/main/java/hudson/plugins/sonar/SonarInstallation.java b/src/main/java/hudson/plugins/sonar/SonarInstallation.java index 4babd3ee..4ce86732 100644 --- a/src/main/java/hudson/plugins/sonar/SonarInstallation.java +++ b/src/main/java/hudson/plugins/sonar/SonarInstallation.java @@ -48,12 +48,12 @@ public class SonarInstallation { private final String serverUrl; /** - * @since 2.5 + * @since 2.4 */ private String serverVersion; /** - * @since 2.5 + * @since 2.4 */ private String serverAuthenticationToken; @@ -190,12 +190,15 @@ public String getServerUrl() { return serverUrl; } + /** + * @since 2.4 + */ public String getServerAuthenticationToken() { return serverAuthenticationToken; } /** - * serverVersion might be null when upgrading to 2.5. + * serverVersion might be null when upgrading to 2.4. * Automatically figures out a value in that case. */ public String getServerVersion() { diff --git a/src/main/java/hudson/plugins/sonar/SonarPublisher.java b/src/main/java/hudson/plugins/sonar/SonarPublisher.java index 441c7a75..526b3c86 100644 --- a/src/main/java/hudson/plugins/sonar/SonarPublisher.java +++ b/src/main/java/hudson/plugins/sonar/SonarPublisher.java @@ -417,7 +417,7 @@ public boolean usesPrivateRepository() { /** * Optional because Maven plugin might not be available. */ - @Extension(optional = true, ordinal = 1000) + @Extension(ordinal = 1000, optional = true) public static final class DescriptorImpl extends BuildStepDescriptor { @CopyOnWrite diff --git a/src/main/java/hudson/plugins/sonar/client/HttpClient.java b/src/main/java/hudson/plugins/sonar/client/HttpClient.java index 185e6dc2..d37c4a6e 100644 --- a/src/main/java/hudson/plugins/sonar/client/HttpClient.java +++ b/src/main/java/hudson/plugins/sonar/client/HttpClient.java @@ -19,7 +19,7 @@ package hudson.plugins.sonar.client; import org.apache.commons.io.IOUtils; -import org.eclipse.aether.util.StringUtils; +import org.apache.commons.lang.StringUtils; import java.io.InputStream; import java.net.HttpURLConnection;