<scope>test</scope>
</dependency>
</dependencies>
+
+ <build>
+ <resources>
+ <resource>
+ <directory>src/main/resources</directory>
+ <filtering>true</filtering>
+ </resource>
+ </resources>
+ </build>
</project>
private File bootDir;
private String serverUrl;
+ private String productToken;
private String serverVersion;
- public Bootstrapper(String serverUrl, File workDir) {
+ /**
+ * @param productToken part of User-Agent request-header field - see http://tools.ietf.org/html/rfc1945#section-10.15
+ */
+ public Bootstrapper(String productToken, String serverUrl, File workDir) {
+ this.productToken = productToken;
bootDir = new File(workDir, "batch");
bootDir.mkdirs();
if (serverUrl.endsWith("/")) {
}
}
- static HttpURLConnection newHttpConnection(URL url) throws IOException {
+ /**
+ * By convention, the product tokens are listed in order of their significance for identifying the application.
+ */
+ String getUserAgent() {
+ return "sonar-bootstrapper/" + BootstrapperVersion.getVersion() + " " + productToken;
+ }
+
+ HttpURLConnection newHttpConnection(URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(CONNECT_TIMEOUT_MILLISECONDS);
connection.setReadTimeout(READ_TIMEOUT_MILLISECONDS);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("GET");
- // TODO connection.setRequestProperty("User-Agent", userAgent);
+ connection.setRequestProperty("User-Agent", getUserAgent());
return connection;
}
--- /dev/null
+package org.sonar.batch.bootstrapper;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+public enum BootstrapperVersion {
+
+ INSTANCE;
+
+ private static final String PROPERTIES_PATH = "/org/sonar/batch/bootstrapper/version.txt";
+ private String version;
+
+ public static String getVersion() {
+ return INSTANCE.version;
+ }
+
+ private BootstrapperVersion() {
+ InputStream input = getClass().getResourceAsStream(PROPERTIES_PATH);
+ try {
+ Properties properties = new Properties();
+ properties.load(input);
+ this.version = properties.getProperty("version");
+
+ } catch (IOException e) {
+ // Can not load the version
+ this.version = "";
+
+ } finally {
+ BootstrapperIOUtils.closeQuietly(input);
+ }
+ }
+}
--- /dev/null
+version=${project.version}
*/
package org.sonar.batch.bootstrapper;
-import org.junit.Test;
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.assertThat;
import java.io.File;
import java.io.IOException;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
+import org.junit.Test;
public class BootstrapperTest {
@Test
public void shouldRemoveLastUrlSlash() {
- Bootstrapper bootstrapper = new Bootstrapper("http://test/", new File("target"));
+ Bootstrapper bootstrapper = new Bootstrapper("", "http://test/", new File("target"));
assertThat(bootstrapper.getServerUrl(), is("http://test"));
}
@Test(expected = Exception.class)
public void shouldFailIfCanNotConnectServer() {
- Bootstrapper bootstrapper = new Bootstrapper("http://unknown.foo", new File("target"));
+ Bootstrapper bootstrapper = new Bootstrapper("", "http://unknown.foo", new File("target"));
bootstrapper.getServerVersion();
}
+ @Test
+ public void shouldReturnUserAgent() {
+ Bootstrapper bootstrapper = new Bootstrapper("test/0.1", "http://unknown.foo", new File("target"));
+ String userAgent = bootstrapper.getUserAgent();
+ assertThat(userAgent.length(), greaterThan(0));
+ assertThat(userAgent, allOf(startsWith("sonar-bootstrapper/"), endsWith(" test/0.1")));
+ }
+
@Test
public void shouldReturnValidVersion() {
- Bootstrapper bootstrapper = new Bootstrapper("http://test", new File("target")) {
+ Bootstrapper bootstrapper = new Bootstrapper("", "http://test", new File("target")) {
@Override
String remoteContent(String path) throws IOException {
return "2.6";
--- /dev/null
+package org.sonar.batch.bootstrapper;
+
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.not;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
+public class BootstrapperVersionTest {
+
+ @Test
+ public void shouldLoadVersion() {
+ String version = BootstrapperVersion.getVersion();
+ assertThat(version, containsString("."));
+ assertThat(version, not(containsString("$")));
+ }
+
+}