Skip to content

Commit

Permalink
SONARJNKNS-238 Make dependency on Maven optional
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmeneses committed Mar 8, 2016
1 parent 9a43779 commit 2c572f5
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
109 changes: 109 additions & 0 deletions its/src/test/java/com/sonar/it/jenkins/JenkinsWithoutMaven.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Jenkins :: Integration Tests
* Copyright (C) 2013 ${owner}
* [email protected]
*
* 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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");
}
Expand All @@ -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) {
Expand Down Expand Up @@ -551,6 +564,14 @@ public WebElement findElement(By by) {
}
}

public void assertNoElement(By by) {
List<WebElement> 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<WebElement> elms = driver.findElements(by);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@

<dependencies>
<dependency>
<!-- needed for SonarPublisher -->
<groupId>org.jenkins-ci.main</groupId>
<artifactId>maven-plugin</artifactId>
<version>2.7.1</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/hudson/plugins/sonar/SonarInstallation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/sonar/SonarPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Publisher> {

@CopyOnWrite
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/sonar/client/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2c572f5

Please sign in to comment.