aboutsummaryrefslogtreecommitdiffstats
path: root/server/process/sonar-process
diff options
context:
space:
mode:
authorStephane Gamard <stephane.gamard@searchbox.com>2014-08-13 17:30:17 +0200
committerStephane Gamard <stephane.gamard@searchbox.com>2014-08-13 17:30:17 +0200
commit4056a9ca7e06c148aef69b2768fc703ea2555921 (patch)
tree71dbc89a27895d36539e4073106590aeebef10d8 /server/process/sonar-process
parent80346c2d8aaff33baf91f94817d9f22d09d94c6d (diff)
downloadsonarqube-4056a9ca7e06c148aef69b2768fc703ea2555921.tar.gz
sonarqube-4056a9ca7e06c148aef69b2768fc703ea2555921.zip
added tests for sonar-process
Diffstat (limited to 'server/process/sonar-process')
-rw-r--r--server/process/sonar-process/pom.xml32
-rw-r--r--server/process/sonar-process/src/main/java/org/sonar/process/JmxUtils.java6
-rw-r--r--server/process/sonar-process/src/test/java/org/sonar/process/JmxUtilsTest.java89
-rw-r--r--server/process/sonar-process/src/test/java/org/sonar/process/ProcessUtilsTest.java30
-rw-r--r--server/process/sonar-process/src/test/java/org/sonar/process/ProcessWrapperTest.java149
-rw-r--r--server/process/sonar-process/src/test/resources/sonar-dummy-app.jarbin0 -> 854048 bytes
6 files changed, 305 insertions, 1 deletions
diff --git a/server/process/sonar-process/pom.xml b/server/process/sonar-process/pom.xml
index 42c05518fd5..4753b693998 100644
--- a/server/process/sonar-process/pom.xml
+++ b/server/process/sonar-process/pom.xml
@@ -69,4 +69,36 @@
<scope>test</scope>
</dependency>
</dependencies>
+ <!--
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <version>2.8</version>
+ <executions>
+ <execution>
+ <id>copy</id>
+ <phase>process-test-resources</phase>
+ <goals>
+ <goal>copy</goal>
+ </goals>
+ <configuration>
+ <artifactItems>
+ <artifactItem>
+ <groupId>org.codehaus.sonar</groupId>
+ <artifactId>sonar-dummy-app</artifactId>
+ <version>${project.version}</version>
+ <type>jar</type>
+ <outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
+ <destFileName>sonar-dummy-app.jar</destFileName>
+ </artifactItem>
+ </artifactItems>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ -->
</project>
diff --git a/server/process/sonar-process/src/main/java/org/sonar/process/JmxUtils.java b/server/process/sonar-process/src/main/java/org/sonar/process/JmxUtils.java
index c2486d8033b..0f35d65212b 100644
--- a/server/process/sonar-process/src/main/java/org/sonar/process/JmxUtils.java
+++ b/server/process/sonar-process/src/main/java/org/sonar/process/JmxUtils.java
@@ -25,16 +25,20 @@ import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
public class JmxUtils {
+
private JmxUtils() {
// only static stuff
}
+ public static final String DOMAIN = "org.sonar";
+ public static final String NAME_PROPERTY = "name";
+
public static final String WEB_SERVER_NAME = "web";
public static final String SEARCH_SERVER_NAME = "search";
public static ObjectName objectName(String name) {
try {
- return new ObjectName("org.sonar", "name", name);
+ return new ObjectName(DOMAIN, NAME_PROPERTY, name);
} catch (MalformedObjectNameException e) {
throw new IllegalStateException("Cannot create ObjectName for " + name, e);
}
diff --git a/server/process/sonar-process/src/test/java/org/sonar/process/JmxUtilsTest.java b/server/process/sonar-process/src/test/java/org/sonar/process/JmxUtilsTest.java
new file mode 100644
index 00000000000..7d9dcc54094
--- /dev/null
+++ b/server/process/sonar-process/src/test/java/org/sonar/process/JmxUtilsTest.java
@@ -0,0 +1,89 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 02110-1301, USA.
+ */
+package org.sonar.process;
+
+import org.junit.Test;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import java.lang.management.ManagementFactory;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
+
+public class JmxUtilsTest {
+
+ class MyBean implements ProcessMXBean {
+
+ @Override
+ public boolean isReady() {
+ return false;
+ }
+
+ @Override
+ public long ping() {
+ return 0;
+ }
+
+ @Override
+ public void terminate() {
+
+ }
+ }
+
+ @Test
+ public void construct_jmx_objectName() throws Exception {
+ MyBean mxBean = new MyBean();
+ ObjectName objectName = JmxUtils.objectName(mxBean.getClass().getSimpleName());
+ assertThat(objectName).isNotNull();
+ assertThat(objectName.getDomain()).isEqualTo(JmxUtils.DOMAIN);
+ assertThat(objectName.getKeyProperty(JmxUtils.NAME_PROPERTY)).isEqualTo(mxBean.getClass().getSimpleName());
+ }
+
+ @Test
+ public void fail_jmx_objectName() throws Exception {
+ try {
+ ObjectName objectName = JmxUtils.objectName(":");
+ fail();
+ } catch (Exception e) {
+ assertThat(e.getMessage()).isEqualTo("Cannot create ObjectName for :");
+ }
+ }
+
+ @Test
+ public void testRegisterMBean() throws Exception {
+
+ // 0 Get mbServer and create out test MXBean
+ MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
+ MyBean mxBean = new MyBean();
+ ObjectName objectName = JmxUtils.objectName(mxBean.getClass().getSimpleName());
+
+ // 1 assert that mxBean gets registered
+ assertThat(mbeanServer.isRegistered(objectName)).isFalse();
+ JmxUtils.registerMBean(mxBean, mxBean.getClass().getSimpleName());
+ assertThat(mbeanServer.isRegistered(objectName)).isTrue();
+
+ // 2 assert that we can over-register
+ assertThat(mbeanServer.isRegistered(objectName)).isTrue();
+ JmxUtils.registerMBean(mxBean, mxBean.getClass().getSimpleName());
+ assertThat(mbeanServer.isRegistered(objectName)).isTrue();
+ }
+} \ No newline at end of file
diff --git a/server/process/sonar-process/src/test/java/org/sonar/process/ProcessUtilsTest.java b/server/process/sonar-process/src/test/java/org/sonar/process/ProcessUtilsTest.java
new file mode 100644
index 00000000000..09ddf79ba5f
--- /dev/null
+++ b/server/process/sonar-process/src/test/java/org/sonar/process/ProcessUtilsTest.java
@@ -0,0 +1,30 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 02110-1301, USA.
+ */
+package org.sonar.process;
+
+import org.junit.Test;
+
+public class ProcessUtilsTest {
+
+ @Test
+ public void check_process_alive() {
+ ProcessBuilder processBuilder = new ProcessBuilder();
+ }
+} \ No newline at end of file
diff --git a/server/process/sonar-process/src/test/java/org/sonar/process/ProcessWrapperTest.java b/server/process/sonar-process/src/test/java/org/sonar/process/ProcessWrapperTest.java
new file mode 100644
index 00000000000..f7564f0200f
--- /dev/null
+++ b/server/process/sonar-process/src/test/java/org/sonar/process/ProcessWrapperTest.java
@@ -0,0 +1,149 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 02110-1301, USA.
+ */
+package org.sonar.process;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.FilenameUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.util.Properties;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
+
+
+public class ProcessWrapperTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ private static final String DUMMY_OK_APP = "org.sonar.application.DummyOkProcess";
+
+ int freePort;
+ File dummyAppJar;
+ Process proc;
+
+ @Before
+ public void setup() throws IOException {
+ ServerSocket socket = new ServerSocket(0);
+ freePort = socket.getLocalPort();
+ socket.close();
+
+ dummyAppJar = FileUtils.toFile(getClass().getResource("/sonar-dummy-app.jar"));
+ }
+
+ @After
+ public void tearDown() {
+ if (proc != null) {
+ proc.destroy();
+ }
+ }
+
+ @Test
+ public void has_dummy_app() {
+ assertThat(dummyAppJar).isFile();
+ assertThat(dummyAppJar).exists();
+ }
+
+ private void assertCanStart(ProcessWrapper process) {
+ assertThat(process.execute()).isTrue();
+ proc = process.process();
+ }
+
+ private void assertCanBeReady(ProcessWrapper process) throws InterruptedException {
+ int count = 0;
+ while (!process.isReady() && count < 5) {
+ Thread.sleep(500);
+ }
+ assertThat(process.getProcessMXBean().isReady()).isTrue();
+ }
+
+ private void assertPing(ProcessWrapper process) {
+ long now = System.currentTimeMillis();
+ long ping = process.getProcessMXBean().ping();
+ assertThat(ping - now).isLessThan(3000L);
+ }
+
+
+ @Test
+ public void execute_dummy_app() throws Exception {
+
+ ProcessWrapper process = new ProcessWrapper("DummyOkProcess")
+ .addProperty(MonitoredProcess.NAME_PROPERTY, "DummyOkProcess")
+ .addClasspath(dummyAppJar.getAbsolutePath())
+ .setWorkDir(temp.getRoot())
+ .setTempDirectory(temp.getRoot())
+ .setJmxPort(freePort)
+ .setClassName(DUMMY_OK_APP);
+
+ assertThat(process.isAlive()).isFalse();
+ assertCanStart(process);
+ process.start();
+ assertCanBeReady(process);
+ assertThat(process.isAlive()).isTrue();
+ assertPing(process);
+ process.terminate();
+ try {
+ assertPing(process);
+ fail();
+ } catch (Exception e) {
+
+ }
+ }
+
+
+ @Test
+ public void execute_dummy_in_space_folder_app() throws Exception {
+
+ // 0 create a home with space...
+ File home = temp.newFolder("t est");
+ assertThat(home.canWrite()).isTrue();
+ File lib = new File(home, "lib");
+ File tempdir = new File(home, "temp");
+ FileUtils.copyFileToDirectory(dummyAppJar, lib);
+
+ // 1 Create Properties
+ Props props = new Props(new Properties());
+ props.set("spaceHome", home.getAbsolutePath());
+
+ // 3 start dummy app
+ File effectiveHome = props.fileOf("spaceHome");
+
+ String cp = FilenameUtils.concat(new File(effectiveHome, "lib").getAbsolutePath(), "*");
+ System.out.println("cp = " + cp);
+ ProcessWrapper process = new ProcessWrapper("DummyOkProcess")
+ .addProperty(MonitoredProcess.NAME_PROPERTY, "DummyOkProcess")
+ .setTempDirectory(tempdir)
+ .addClasspath(cp)
+ .setWorkDir(home)
+ .setJmxPort(freePort)
+ .setClassName(DUMMY_OK_APP);
+
+ assertThat(process.isAlive()).isFalse();
+ assertCanStart(process);
+ }
+} \ No newline at end of file
diff --git a/server/process/sonar-process/src/test/resources/sonar-dummy-app.jar b/server/process/sonar-process/src/test/resources/sonar-dummy-app.jar
new file mode 100644
index 00000000000..6dfd458329a
--- /dev/null
+++ b/server/process/sonar-process/src/test/resources/sonar-dummy-app.jar
Binary files differ