You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EmbeddedTomcatTest.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.app;
  21. import java.io.File;
  22. import java.net.ConnectException;
  23. import java.net.InetAddress;
  24. import java.net.URL;
  25. import java.util.Properties;
  26. import org.apache.commons.io.FileUtils;
  27. import org.junit.Ignore;
  28. import org.junit.Rule;
  29. import org.junit.Test;
  30. import org.junit.rules.TemporaryFolder;
  31. import org.sonar.process.NetworkUtilsImpl;
  32. import org.sonar.process.Props;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.junit.Assert.fail;
  35. @Ignore
  36. public class EmbeddedTomcatTest {
  37. @Rule
  38. public TemporaryFolder temp = new TemporaryFolder();
  39. @Test
  40. public void start() throws Exception {
  41. Props props = new Props(new Properties());
  42. // prepare file system
  43. File home = temp.newFolder();
  44. File data = temp.newFolder();
  45. File webDir = new File(home, "web");
  46. FileUtils.write(new File(home, "web/WEB-INF/web.xml"), "<web-app/>");
  47. props.set("sonar.path.home", home.getAbsolutePath());
  48. props.set("sonar.path.data", data.getAbsolutePath());
  49. props.set("sonar.path.web", webDir.getAbsolutePath());
  50. props.set("sonar.path.logs", temp.newFolder().getAbsolutePath());
  51. // start server on a random port
  52. InetAddress address = InetAddress.getLoopbackAddress();
  53. int httpPort = NetworkUtilsImpl.INSTANCE.getNextAvailablePort(address);
  54. props.set("sonar.web.host", address.getHostAddress());
  55. props.set("sonar.web.port", String.valueOf(httpPort));
  56. EmbeddedTomcat tomcat = new EmbeddedTomcat(props);
  57. assertThat(tomcat.getStatus()).isEqualTo(EmbeddedTomcat.Status.DOWN);
  58. tomcat.start();
  59. assertThat(tomcat.getStatus()).isEqualTo(EmbeddedTomcat.Status.UP);
  60. // check that http connector accepts requests
  61. URL url = new URL("http://" + address.getHostAddress() + ":" + httpPort);
  62. url.openConnection().connect();
  63. // stop server
  64. tomcat.terminate();
  65. // tomcat.isUp() must not be called. It is used to wait for server startup, not shutdown.
  66. try {
  67. url.openConnection().connect();
  68. fail();
  69. } catch (ConnectException e) {
  70. // expected
  71. }
  72. }
  73. }