Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
MSONAR-129 sonar.host.url defined in settings.xml not taken into account
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmeneses committed Oct 26, 2015
1 parent 0974107 commit dadf53f
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 309 deletions.
288 changes: 0 additions & 288 deletions its/out.dump

This file was deleted.

35 changes: 35 additions & 0 deletions its/projects/maven/maven-global-properties/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sonarsource.it.samples</groupId>
<artifactId>sonar-host-url</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<sonar.host.url>http://dummy-url.org</sonar.host.url>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
49 changes: 48 additions & 1 deletion its/src/test/java/com/sonar/maven/it/ItUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,21 @@
*/
package com.sonar.maven.it;

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.io.File;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

public final class ItUtils {

Expand Down Expand Up @@ -49,4 +62,38 @@ public static File locateProjectPom(String projectName) {
return new File(locateProjectDir(projectName), "pom.xml");
}

/**
* Creates a settings xml with a sonar profile, containing all the given properties
*/
public static String createSettingsXml(Map<String, String> props) throws Exception {
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docBuilder.newDocument();

Element settings = doc.createElement("settings");
Element profiles = doc.createElement("profiles");
Element profile = doc.createElement("profile");

Element id = doc.createElement("id");
id.setTextContent("sonar");

Element properties = doc.createElement("properties");

for (Map.Entry<String, String> e : props.entrySet()) {
Element el = doc.createElement(e.getKey());
el.setTextContent(e.getValue());
properties.appendChild(el);
}

profile.appendChild(id);
profile.appendChild(properties);
profiles.appendChild(profile);
settings.appendChild(profiles);
doc.appendChild(settings);

Writer writer = new StringWriter();
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.transform(new DOMSource(doc), new StreamResult(writer));
return writer.toString();
}

}
45 changes: 43 additions & 2 deletions its/src/test/java/com/sonar/maven/it/suite/MavenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@
*/
package com.sonar.maven.it.suite;

import org.apache.commons.io.FileUtils;
import com.sonar.orchestrator.build.BuildRunner;
import com.sonar.maven.it.ItUtils;
import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.MavenBuild;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -36,7 +41,6 @@
import org.sonar.wsclient.Sonar;
import org.sonar.wsclient.services.Resource;
import org.sonar.wsclient.services.ResourceQuery;

import static org.fest.assertions.Assertions.assertThat;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;
Expand All @@ -51,6 +55,43 @@ public void deleteData() {
orchestrator.resetData();
}

@Test
/**
* See MSONAR-129
*/
public void useUserPropertiesGlobalConfig() throws Exception {
BuildRunner runner = new BuildRunner(orchestrator.getConfiguration(), orchestrator.getDatabase());
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/maven-only-test-dir"))
.setGoals(cleanSonarGoal());

File settingsXml = temp.newFile();
Map<String, String> props = orchestrator.getDatabase().getSonarProperties();
props.put("sonar.host.url", orchestrator.getServer().getUrl());
FileUtils.write(settingsXml, ItUtils.createSettingsXml(props));

build.addArgument("--settings=" + settingsXml.getAbsolutePath());
build.addArgument("-Psonar");
// we build without sonarqube server settings, it will need to fetch it from the profile defined in the settings xml file
BuildResult result = runner.run(null, build);

assertThat(result.getLogs()).contains(orchestrator.getServer().getUrl());
}

@Test
/**
* See MSONAR-129
*/
public void supportSonarHostURLParam() {
BuildRunner runner = new BuildRunner(orchestrator.getConfiguration(), orchestrator.getDatabase());
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/maven-global-properties"))
.setGoals(cleanSonarGoal());

BuildResult result = runner.runQuietly(null, build);

assertThat(result.isSuccess()).isFalse();
assertThat(result.getLogs()).contains("http://dummy-url.org");
}

@Test
public void shouldSupportJarWithoutSources() {
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/project-with-module-without-sources"))
Expand All @@ -68,7 +109,7 @@ public void shouldSupportJarWithoutSources() {
Resource subProject = orchestrator.getServer().getWsClient().find(ResourceQuery.create("com.sonarsource.it.samples.project-with-module-without-sources:without-sources"));
assertThat(subProject).isNotNull();
}

/**
* See SONAR-594
*/
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/org/codehaus/mojo/sonar/SonarMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ public class SonarMojo
@Parameter( defaultValue = "${session}", readonly = true )
private MavenSession session;

/**
* Sonar host URL.
*/
@Parameter( property = "sonar.host.url", defaultValue = "http://localhost:9000", alias = "sonar.host.url" )
private String sonarHostURL;

/**
* Set this to 'true' to skip analysis.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ public void execute()
{
try
{
Properties props = collectProperties();
checkDumpToFile( props );

applyMasks();
runner.start();
serverVersion = runner.serverVersion();
Expand All @@ -99,7 +96,7 @@ public void execute()
runner.setGlobalProperty( "sonar.verbose", "true" );
}

runner.runAnalysis( props );
runner.runAnalysis( collectProperties() );
runner.stop();
}
catch ( Exception e )
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/org/codehaus/mojo/sonar/bootstrap/RunnerFactory.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.codehaus.mojo.sonar.bootstrap;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.rtinfo.RuntimeInformation;

import org.sonar.runner.api.LogOutput;
import org.sonar.runner.api.EmbeddedRunner;
import org.apache.maven.execution.MavenSession;
import org.sonar.runner.api.LogOutput;

import java.util.Properties;

public class RunnerFactory
{
Expand All @@ -30,7 +31,9 @@ public EmbeddedRunner create()
{
EmbeddedRunner runner = EmbeddedRunner.create( logOutput );
runner.setApp( "Maven", runtimeInformation.getMavenVersion() );
runner.addGlobalProperties( session.getSystemProperties() );

runner.addGlobalProperties( createGlobalProperties() );

// Secret property to manage backward compatibility on SQ side (see ProjectScanContainer)
runner.setGlobalProperty( "sonar.mojoUseRunner", "true" );
if ( debugEnabled )
Expand All @@ -40,4 +43,13 @@ public EmbeddedRunner create()

return runner;
}

private Properties createGlobalProperties()
{
Properties p = new Properties();
p.putAll( session.getTopLevelProject().getProperties() );
p.putAll( session.getSystemProperties() );
p.putAll( session.getUserProperties() );
return p;
}
}
12 changes: 10 additions & 2 deletions src/test/java/org/codehaus/mojo/sonar/SonarMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import java.util.Properties;

import static org.mockito.Mockito.atLeastOnce;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -82,7 +81,10 @@ public void executeMojo()
throws Exception
{
File baseDir = executeProject( "sample-project", temp.newFile() );
assertPropsContains( entry( "sonar.projectKey", "org.codehaus.sonar:sample-project" ) );

// passed in the properties of the profile and project
assertGlobalPropsContains( entry( "sonar.host.url1", "http://myserver:9000" ) );
assertGlobalPropsContains( entry( "sonar.host.url2", "http://myserver:9000" ) );
}

@Test
Expand Down Expand Up @@ -209,6 +211,12 @@ private void assertPropsContains( MapAssert.Entry... entries )
assertThat( readProps( "target/dump.properties" ) ).includes( entries );
}

private void assertGlobalPropsContains( MapAssert.Entry... entries )
throws FileNotFoundException, IOException
{
assertThat( readProps( "target/dump.properties.global" ) ).includes( entries );
}

private Properties readProps( String filePath )
throws FileNotFoundException, IOException
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.codehaus.mojo.sonar.bootstrap;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.junit.Before;
import org.junit.Test;
import org.sonar.runner.api.EmbeddedRunner;
import org.sonar.runner.api.LogOutput;

import java.util.Properties;

import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.MapAssert.entry;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class RunnerFactoryTest
{
private LogOutput logOutput;

private RuntimeInformation runtimeInformation;

private MavenSession mavenSession;

private MavenProject rootProject;

@Before
public void setUp()
{
logOutput = mock( LogOutput.class );
runtimeInformation = mock( RuntimeInformation.class );
mavenSession = mock( MavenSession.class );
rootProject = mock( MavenProject.class );

Properties system = new Properties();
system.put( "system", "value" );
Properties user = new Properties();
user.put( "user", "value" );
Properties root = new Properties();
root.put( "root", "value" );

when( runtimeInformation.getMavenVersion() ).thenReturn( "1.0" );
when( mavenSession.getSystemProperties() ).thenReturn( system );
when( mavenSession.getUserProperties() ).thenReturn( user );
when( rootProject.getProperties() ).thenReturn( root );
when( mavenSession.getTopLevelProject() ).thenReturn( rootProject );
}

@Test
public void testProperties()
{
RunnerFactory factory = new RunnerFactory( logOutput, false, runtimeInformation, mavenSession );
EmbeddedRunner runner = factory.create();
verify( mavenSession ).getUserProperties();
verify( mavenSession ).getSystemProperties();
verify( rootProject ).getProperties();

assertThat( runner.globalProperties() ).includes( entry( "system", "value" ), entry( "user", "value" ),
entry( "root", "value" ) );
assertThat( runner.globalProperties() ).includes( entry( "sonar.mojoUseRunner", "true" ) );
}

@Test
public void testDebug()
{
RunnerFactory factoryDebug = new RunnerFactory( logOutput, true, runtimeInformation, mavenSession );
RunnerFactory factory = new RunnerFactory( logOutput, false, runtimeInformation, mavenSession );

EmbeddedRunner runnerDebug = factoryDebug.create();
EmbeddedRunner runner = factory.create();

assertThat( runnerDebug.globalProperties() ).includes( entry( "sonar.verbose", "true" ) );
assertThat( runner.globalProperties() ).excludes( entry( "sonar.verbose", "true" ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<properties>
<sonarRunner.dumpToFile>target/dump.properties</sonarRunner.dumpToFile>
<sonar.host.url2>http://myserver:9000</sonar.host.url2>
</properties>


Expand All @@ -31,9 +32,19 @@
<plugins>
<plugin>
<artifactId>sonar-maven-plugin</artifactId>
<configuration>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url1>http://myserver:9000</sonar.host.url1>
</properties>
</profile>
</profiles>
</project>

0 comments on commit dadf53f

Please sign in to comment.