From dd37757e86df02e2bc6a3de39eda02def96c845f Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Thu, 10 Jul 2014 10:15:38 +0200 Subject: [PATCH] SONAR-5409 - Added Props and Env to Process Ctor --- .../main/java/org/sonar/process/Process.java | 32 ++- .../java/org/sonar/process/ProcessTest.java | 22 +- .../process/ProcessTest/sonar.properties | 211 ++++++++++++++++++ 3 files changed, 250 insertions(+), 15 deletions(-) create mode 100644 sonar-process/src/test/resources/org/sonar/process/ProcessTest/sonar.properties diff --git a/sonar-process/src/main/java/org/sonar/process/Process.java b/sonar-process/src/main/java/org/sonar/process/Process.java index 0e3105092e5..704e33115b5 100644 --- a/sonar-process/src/main/java/org/sonar/process/Process.java +++ b/sonar-process/src/main/java/org/sonar/process/Process.java @@ -19,6 +19,7 @@ */ package org.sonar.process; +import com.google.common.annotations.VisibleForTesting; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,6 +27,8 @@ import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; +import java.net.URISyntaxException; +import java.net.URL; /** * @Since 4.5 @@ -40,20 +43,25 @@ public abstract class Process implements Runnable { final String name; final int port; -// final protected Env env; -// final protected Props props; + final protected Env env; + final protected Props props; - public Process(String name, int port) { + public Process(String name, int port) throws URISyntaxException { + this(new Env(), name, port); + } + + public Process(URL configFile, String name, int port) throws URISyntaxException { + this(new Env(configFile), name, port); + } + + @VisibleForTesting + Process(Env env, String name, int port) { + + // This is our environment + this.env = env; -// // Loading Env from sonar.properties file. -// try { -// this.env = new Env(); -// } catch (URISyntaxException e) { -// throw new IllegalStateException("Could not load Env", e); -// } -// -// // Loading all Properties from file -// this.props = Props.create(env); + // Loading all Properties from file + this.props = Props.create(env); this.name = name; this.port = port; diff --git a/sonar-process/src/test/java/org/sonar/process/ProcessTest.java b/sonar-process/src/test/java/org/sonar/process/ProcessTest.java index f17fa4efe76..3f2b27370aa 100644 --- a/sonar-process/src/test/java/org/sonar/process/ProcessTest.java +++ b/sonar-process/src/test/java/org/sonar/process/ProcessTest.java @@ -19,17 +19,30 @@ */ package org.sonar.process; +import com.google.common.io.Resources; import org.junit.Test; +import java.io.File; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; +import java.net.MalformedURLException; +import java.net.SocketException; +import java.net.URISyntaxException; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class ProcessTest { + @Test + public void process_loads() throws SocketException, URISyntaxException, MalformedURLException { + DatagramSocket socket = new DatagramSocket(0); + testProcess("test", socket); + } @Test(timeout = 5000L) - public void heart_beats() throws InterruptedException, IOException { + public void heart_beats() throws InterruptedException, IOException, URISyntaxException { DatagramSocket socket = new DatagramSocket(0); Process test = testProcess("test", socket); @@ -49,8 +62,11 @@ public class ProcessTest { socket.close(); } - private Process testProcess(final String name, final DatagramSocket socket) { - return new Process(name, socket.getLocalPort()) { + private Process testProcess(final String name, final DatagramSocket socket) throws URISyntaxException, MalformedURLException { + Env env = mock(Env.class); + File propsFile = new File(Resources.getResource(getClass(), "ProcessTest/sonar.properties").getFile()); + when(env.file("conf/sonar.properties")).thenReturn(propsFile); + return new Process(env, name, socket.getLocalPort()) { @Override public void execute() { try { diff --git a/sonar-process/src/test/resources/org/sonar/process/ProcessTest/sonar.properties b/sonar-process/src/test/resources/org/sonar/process/ProcessTest/sonar.properties new file mode 100644 index 00000000000..6c058aa3227 --- /dev/null +++ b/sonar-process/src/test/resources/org/sonar/process/ProcessTest/sonar.properties @@ -0,0 +1,211 @@ +# This file must contain only ISO 8859-1 characters +# see http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Properties.html#load(java.io.InputStream) +# +# To use an environment variable, use the following syntax : ${env:NAME_OF_ENV_VARIABLE} +# For example: +# sonar.jdbc.url= ${env:SONAR_JDBC_URL} +# +# +# See also the file conf/wrapper.conf for JVM advanced settings + + + +#-------------------------------------------------------------------------------------------------- +# DATABASE +# +# IMPORTANT: the embedded H2 database is used by default. It is recommended for tests only. +# Please use a production-ready database. Supported databases are MySQL, Oracle, PostgreSQL +# and Microsoft SQLServer. + +# Permissions to create tables, indices and triggers must be granted to JDBC user. +# The schema must be created first. +sonar.jdbc.username=sonar +sonar.jdbc.password=sonar + +#----- Embedded database H2 +# Note: it does not accept connections from remote hosts, so the +# SonarQube server and the maven plugin must be executed on the same host. + +# Comment the following line to deactivate the default embedded database. +sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar + +# directory containing H2 database files. By default it's the /data directory in the SonarQube installation. +#sonar.embeddedDatabase.dataDir= +# H2 embedded database server listening port, defaults to 9092 +#sonar.embeddedDatabase.port=9092 + + +#----- MySQL 5.x +# Comment the embedded database and uncomment the following line to use MySQL +#sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true + + +#----- Oracle 10g/11g +# To connect to Oracle database: +# +# - It's recommended to use the latest version of the JDBC driver (ojdbc6.jar). +# Download it in http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html +# - Copy the driver to the directory extensions/jdbc-driver/oracle/ +# - If you need to set the schema, please refer to http://jira.codehaus.org/browse/SONAR-5000 +# - Comment the embedded database and uncomment the following line: +#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE + + +#----- PostgreSQL 8.x/9.x +# Comment the embedded database and uncomment the following property to use PostgreSQL. +# If you don't use the schema named "public", please refer to http://jira.codehaus.org/browse/SONAR-5000 +#sonar.jdbc.url=jdbc:postgresql://localhost/sonar + + +#----- Microsoft SQLServer +# The Jtds open source driver is available in extensions/jdbc-driver/mssql. More details on http://jtds.sourceforge.net +#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor + + +#----- Connection pool settings +sonar.jdbc.maxActive=20 +sonar.jdbc.maxIdle=5 +sonar.jdbc.minIdle=2 +sonar.jdbc.maxWait=5000 +sonar.jdbc.minEvictableIdleTimeMillis=600000 +sonar.jdbc.timeBetweenEvictionRunsMillis=30000 + + + +#-------------------------------------------------------------------------------------------------- +# WEB SERVER + +# Binding IP address. For servers with more than one IP address, this property specifies which +# address will be used for listening on the specified ports. +# By default, ports will be used on all IP addresses associated with the server. +#sonar.web.host=0.0.0.0 + +# Web context. When set, it must start with forward slash (for example /sonarqube). +# The default value is root context (empty value). +#sonar.web.context= + +# TCP port for incoming HTTP connections. Disabled when value is -1. +#sonar.web.port=9000 + +# TCP port for incoming HTTPS connections. Disabled when value is -1 (default). +#sonar.web.https.port=-1 + +# HTTPS - the alias used to for the server certificate in the keystore. +# If not specified the first key read in the keystore is used. +#sonar.web.https.keyAlias= + +# HTTPS - the password used to access the server certificate from the +# specified keystore file. The default value is "changeit". +#sonar.web.https.keyPass=changeit + +# HTTPS - the pathname of the keystore file where is stored the server certificate. +# By default, the pathname is the file ".keystore" in the user home. +# If keystoreType doesn't need a file use empty value. +#sonar.web.https.keystoreFile= + +# HTTPS - the password used to access the specified keystore file. The default +# value is the value of sonar.web.https.keyPass. +#sonar.web.https.keystorePass= + +# HTTPS - the type of keystore file to be used for the server certificate. +# The default value is JKS (Java KeyStore). +#sonar.web.https.keystoreType=JKS + +# HTTPS - the name of the keystore provider to be used for the server certificate. +# If not specified, the list of registered providers is traversed in preference order +# and the first provider that supports the keystore type is used (see sonar.web.https.keystoreType). +#sonar.web.https.keystoreProvider= + +# HTTPS - the pathname of the truststore file which contains trusted certificate authorities. +# By default, this would be the cacerts file in your JRE. +# If truststoreFile doesn't need a file use empty value. +#sonar.web.https.truststoreFile= + +# HTTPS - the password used to access the specified truststore file. +#sonar.web.https.truststorePass= + +# HTTPS - the type of truststore file to be used. +# The default value is JKS (Java KeyStore). +#sonar.web.https.truststoreType=JKS + +# HTTPS - the name of the truststore provider to be used for the server certificate. +# If not specified, the list of registered providers is traversed in preference order +# and the first provider that supports the truststore type is used (see sonar.web.https.truststoreType). +#sonar.web.https.truststoreProvider= + +# HTTPS - whether to enable client certificate authentication. +# The default is false (client certificates disabled). +# Other possible values are 'want' (certificates will be requested, but not required), +# and 'true' (certificates are required). +#sonar.web.https.clientAuth=false + +# The maximum number of connections that the server will accept and process at any given time. +# When this number has been reached, the server will not accept any more connections until +# the number of connections falls below this value. The operating system may still accept connections +# based on the sonar.web.connections.acceptCount property. The default value is 50 for each +# enabled connector. +#sonar.web.http.maxThreads=50 +#sonar.web.https.maxThreads=50 + +# The minimum number of threads always kept running. The default value is 5 for each +# enabled connector. +#sonar.web.http.minThreads=5 +#sonar.web.https.minThreads=5 + +# The maximum queue length for incoming connection requests when all possible request processing +# threads are in use. Any requests received when the queue is full will be refused. +# The default value is 25 for each enabled connector. +#sonar.web.http.acceptCount=25 +#sonar.web.https.acceptCount=25 + +# Access logs are generated in the file logs/access.log. This file is rolled over when it's 5Mb. +# An archive of 3 files is kept in the same directory. +# Access logs are enabled by default. +#sonar.web.accessLogs.enable=true + +# TCP port for incoming AJP connections. Disabled when value is -1. +# sonar.ajp.port=9009 + + + +#-------------------------------------------------------------------------------------------------- +# UPDATE CENTER + +# The Update Center requires an internet connection to request http://update.sonarsource.org +# It is enabled by default. +#sonar.updatecenter.activate=true + +# HTTP proxy (default none) +#http.proxyHost= +#http.proxyPort= + +# NT domain name if NTLM proxy is used +#http.auth.ntlm.domain= + +# SOCKS proxy (default none) +#socksProxyHost= +#socksProxyPort= + +# proxy authentication. The 2 following properties are used for HTTP and SOCKS proxies. +#http.proxyUser= +#http.proxyPassword= + + +#-------------------------------------------------------------------------------------------------- +# NOTIFICATIONS + +# Delay (in seconds) between processing of notification queue +sonar.notifications.delay=60 + +#-------------------------------------------------------------------------------------------------- +# PROFILING +# Level of information displayed in the logs: NONE (default), BASIC (functional information) and FULL (functional and technical details) +#sonar.log.profilingLevel=NONE + + +#-------------------------------------------------------------------------------------------------- +# DEVELOPMENT MODE +# Only for debugging + +# Set to true to apply Ruby on Rails code changes on the fly +#sonar.rails.dev=false -- 2.39.5