您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

BootstrapperTest.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Sonar Runner - API
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  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
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner;
  21. import org.apache.commons.io.IOUtils;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.TemporaryFolder;
  25. import java.io.File;
  26. import java.io.IOException;
  27. import java.net.HttpURLConnection;
  28. import java.net.MalformedURLException;
  29. import java.net.URL;
  30. import java.util.HashMap;
  31. import java.util.Map;
  32. import static org.fest.assertions.Assertions.assertThat;
  33. import static org.mockito.Mockito.mock;
  34. import static org.mockito.Mockito.verify;
  35. import static org.mockito.Mockito.when;
  36. public class BootstrapperTest {
  37. @Rule
  38. public TemporaryFolder tempFolder = new TemporaryFolder();
  39. @Test
  40. public void shouldRemoveLastUrlSlash() {
  41. Bootstrapper bootstrapper = new Bootstrapper("", "http://test/", new File("target/tmp"), null);
  42. assertThat(bootstrapper.getServerUrl()).isEqualTo("http://test");
  43. }
  44. @Test(expected = Exception.class)
  45. public void shouldFailIfCanNotConnectServer() {
  46. Bootstrapper bootstrapper = new Bootstrapper("", "http://unknown.foo", new File("target/tmp"), null);
  47. bootstrapper.getServerVersion();
  48. }
  49. @Test
  50. public void shouldReturnUserAgent() {
  51. Bootstrapper bootstrapper = new Bootstrapper("test/0.1", "http://unknown.foo", new File("target/tmp"), null);
  52. String userAgent = bootstrapper.getUserAgent();
  53. assertThat(userAgent.length()).isGreaterThan(0);
  54. assertThat(userAgent).startsWith("sonar-bootstrapper/");
  55. assertThat(userAgent).endsWith(" test/0.1");
  56. }
  57. @Test
  58. public void shouldReturnValidVersion() {
  59. Bootstrapper bootstrapper = new Bootstrapper("", "http://test", new File("target/tmp"), null) {
  60. @Override
  61. String remoteContent(String path) throws IOException {
  62. return "2.6";
  63. }
  64. };
  65. assertThat(bootstrapper.getServerVersion()).isEqualTo("2.6");
  66. }
  67. @Test
  68. public void shouldParseEncodingFromContentType() {
  69. assertThat(Bootstrapper.getCharsetFromContentType("text/html; charset=EUC-JP")).isEqualTo("EUC-JP");
  70. assertThat(Bootstrapper.getCharsetFromContentType("text/html")).isNull();
  71. }
  72. @Test
  73. public void shouldCheckVersionForCache() {
  74. assertThat(Bootstrapper.isUnsupportedVersionForCache("1.0")).isTrue();
  75. assertThat(Bootstrapper.isUnsupportedVersionForCache("2.0")).isTrue();
  76. assertThat(Bootstrapper.isUnsupportedVersionForCache("2.1")).isTrue();
  77. assertThat(Bootstrapper.isUnsupportedVersionForCache("2.2")).isTrue();
  78. assertThat(Bootstrapper.isUnsupportedVersionForCache("2.3")).isTrue();
  79. assertThat(Bootstrapper.isUnsupportedVersionForCache("2.4")).isTrue();
  80. assertThat(Bootstrapper.isUnsupportedVersionForCache("2.4.1")).isTrue();
  81. assertThat(Bootstrapper.isUnsupportedVersionForCache("2.5")).isTrue();
  82. assertThat(Bootstrapper.isUnsupportedVersionForCache("2.11")).isTrue();
  83. assertThat(Bootstrapper.isUnsupportedVersionForCache("3.0")).isTrue();
  84. assertThat(Bootstrapper.isUnsupportedVersionForCache("3.1")).isTrue();
  85. assertThat(Bootstrapper.isUnsupportedVersionForCache("3.2")).isTrue();
  86. assertThat(Bootstrapper.isUnsupportedVersionForCache("3.3")).isTrue();
  87. assertThat(Bootstrapper.isUnsupportedVersionForCache("3.4")).isTrue();
  88. assertThat(Bootstrapper.isUnsupportedVersionForCache("3.5")).isFalse();
  89. }
  90. @Test
  91. public void shouldCacheWhenNecessary() throws Exception {
  92. File sonarUserHome = tempFolder.newFolder();
  93. SonarCache cache = SonarCache.create(sonarUserHome).build();
  94. final MockedConnectionFactory connections = new MockedConnectionFactory("http://test");
  95. connections.register("/api/server/version", "3.5");
  96. connections.register("/batch_bootstrap/index", "foo.jar|922afef30ca31573d7131347d01b76c4\nbar.jar|69155f65900fbabbf21e28abb33dd06a");
  97. connections.register("/batch/foo.jar", "fakecontent1");
  98. connections.register("/batch/bar.jar", "fakecontent2");
  99. Bootstrapper bootstrapper = new Bootstrapper("", "http://test", new File("target/tmp"), cache) {
  100. @Override
  101. HttpURLConnection newHttpConnection(URL url) throws IOException {
  102. return connections.get(url);
  103. }
  104. };
  105. bootstrapper.createClassLoader(new URL[] {}, this.getClass().getClassLoader());
  106. assertThat(new File(new File(cache.getCacheLocation(), "922afef30ca31573d7131347d01b76c4"), "foo.jar")).exists();
  107. assertThat(new File(new File(cache.getCacheLocation(), "69155f65900fbabbf21e28abb33dd06a"), "bar.jar")).exists();
  108. // Should not download during the second execution
  109. final MockedConnectionFactory connections2 = new MockedConnectionFactory("http://test");
  110. connections2.register("/api/server/version", "3.5");
  111. connections2.register("/batch_bootstrap/index", "foo.jar|922afef30ca31573d7131347d01b76c4\nbar.jar|69155f65900fbabbf21e28abb33dd06a");
  112. Bootstrapper bootstrapper2 = new Bootstrapper("", "http://test", new File("target/tmp"), cache) {
  113. @Override
  114. HttpURLConnection newHttpConnection(URL url) throws IOException {
  115. return connections2.get(url);
  116. }
  117. };
  118. bootstrapper2.createClassLoader(new URL[] {}, this.getClass().getClassLoader());
  119. }
  120. @Test
  121. public void shouldDownloadFromOldURL() throws Exception {
  122. File sonarUserHome = tempFolder.newFolder();
  123. final MockedConnectionFactory connections = new MockedConnectionFactory("http://test");
  124. connections.register("/api/server/version", "3.4");
  125. connections.register("/batch/", "foo.jar,bar.jar");
  126. connections.register("/batch/foo.jar", "fakecontent1");
  127. connections.register("/batch/bar.jar", "fakecontent2");
  128. Bootstrapper bootstrapper = new Bootstrapper("", "http://test", new File("target/tmp"), SonarCache.create(sonarUserHome).build()) {
  129. @Override
  130. HttpURLConnection newHttpConnection(URL url) throws IOException {
  131. return connections.get(url);
  132. }
  133. };
  134. bootstrapper.createClassLoader(new URL[] {}, this.getClass().getClassLoader());
  135. verify(connections.get("/batch/foo.jar")).getInputStream();
  136. verify(connections.get("/batch/bar.jar")).getInputStream();
  137. }
  138. private class MockedConnectionFactory {
  139. private Map<URL, HttpURLConnection> mockedConnections = new HashMap<URL, HttpURLConnection>();
  140. private String serverUrl;
  141. public MockedConnectionFactory(String serverUrl) {
  142. this.serverUrl = serverUrl;
  143. }
  144. public void register(String path, String content) throws Exception {
  145. HttpURLConnection mockConnection = mock(HttpURLConnection.class);
  146. when(mockConnection.getInputStream()).thenReturn(IOUtils.toInputStream(content));
  147. when(mockConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
  148. mockedConnections.put(new URL(serverUrl + path), mockConnection);
  149. }
  150. public HttpURLConnection get(URL url) {
  151. return mockedConnections.get(url);
  152. }
  153. public HttpURLConnection get(String path) throws MalformedURLException {
  154. return mockedConnections.get(new URL(serverUrl + path));
  155. }
  156. }
  157. }