* For correct URL encoding we must encode parameters on lower level - in Query itself,
but not in concrete implementation of Connector, because in Query we can distinguish
concrete parts of URL.
* Moreover in this case any additional encoding routines in Connector are useless,
so were removed.
<scope>test</scope>
</dependency>
</dependencies>
-</project>
\ No newline at end of file
+</project>
*/
package org.sonar.wsclient.connectors;
-import org.apache.commons.httpclient.*;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.commons.httpclient.Credentials;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.HttpMethodBase;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
+import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.DeleteMethod;
+import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
-import org.apache.commons.httpclient.util.URIUtil;
import org.sonar.wsclient.Host;
+import org.sonar.wsclient.services.AbstractQuery;
import org.sonar.wsclient.services.CreateQuery;
import org.sonar.wsclient.services.DeleteQuery;
import org.sonar.wsclient.services.Query;
import org.sonar.wsclient.services.UpdateQuery;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
-
/**
* @since 2.1
*/
}
private HttpMethodBase newGetRequest(Query<?> query) {
- try {
- String url = server.getHost() + URIUtil.encodeQuery(query.getUrl());
- HttpMethodBase method = new GetMethod(url);
- method.setRequestHeader("Accept", "application/json");
- return method;
+ HttpMethodBase method = new GetMethod(server.getHost() + query.getUrl());
+ setJsonHeader(method);
+ return method;
+ }
- } catch (URIException e) {
- throw new ConnectionException("Query: " + query, e);
- }
+ private HttpMethodBase newDeleteRequest(DeleteQuery<?> query) {
+ HttpMethodBase method = new DeleteMethod(server.getHost() + query.getUrl());
+ setJsonHeader(method);
+ return method;
}
private HttpMethodBase newPostRequest(CreateQuery<?> query) {
- try {
- String url = server.getHost() + URIUtil.encodeQuery(query.getUrl());
- PostMethod method = new PostMethod(url);
- method.setRequestHeader("Accept", "application/json");
- if (query.getBody() != null) {
- method.setRequestEntity(new StringRequestEntity(query.getBody(), "text/plain; charset=UTF-8", "UTF-8"));
- }
- return method;
-
- } catch (URIException e) {
- throw new ConnectionException("Query: " + query, e);
- } catch (UnsupportedEncodingException e) {
- throw new ConnectionException("Unsupported encoding", e);
- }
+ PostMethod method = new PostMethod(server.getHost() + query.getUrl());
+ setJsonHeader(method);
+ setRequestEntity(method, query);
+ return method;
}
private HttpMethodBase newPutRequest(UpdateQuery<?> query) {
- try {
- String url = server.getHost() + URIUtil.encodeQuery(query.getUrl());
- PutMethod method = new PutMethod(url);
- method.setRequestHeader("Accept", "application/json");
- if (query.getBody() != null) {
- method.setRequestEntity(new StringRequestEntity(query.getBody(), "text/plain; charset=UTF-8", "UTF-8"));
- }
- return method;
+ PutMethod method = new PutMethod(server.getHost() + query.getUrl());
+ setJsonHeader(method);
+ setRequestEntity(method, query);
+ return method;
+ }
- } catch (URIException e) {
- throw new ConnectionException("Query: " + query, e);
- } catch (UnsupportedEncodingException e) {
- throw new ConnectionException("Unsupported encoding", e);
+ private void setRequestEntity(EntityEnclosingMethod request, AbstractQuery<?> query) {
+ if (query.getBody() != null) {
+ try {
+ request.setRequestEntity(new StringRequestEntity(query.getBody(), "text/plain; charset=UTF-8", "UTF-8"));
+ } catch (UnsupportedEncodingException e) {
+ throw new ConnectionException("Unsupported encoding", e);
+ }
}
}
- private HttpMethodBase newDeleteRequest(DeleteQuery<?> query) {
- try {
- String url = server.getHost() + URIUtil.encodeQuery(query.getUrl());
- HttpMethodBase method = new DeleteMethod(url);
- method.setRequestHeader("Accept", "application/json");
- return method;
-
- } catch (URIException e) {
- throw new ConnectionException("Query: " + query, e);
- }
+ private void setJsonHeader(HttpMethodBase request) {
+ request.setRequestHeader("Accept", "application/json");
}
private String getResponseBodyAsString(HttpMethod method) {
*/
package org.sonar.wsclient.connectors;
-import org.apache.http.*;
-import org.apache.http.auth.*;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpException;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpRequestInterceptor;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.auth.AuthScheme;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.AuthState;
+import org.apache.http.auth.Credentials;
+import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.sonar.wsclient.Host;
+import org.sonar.wsclient.services.AbstractQuery;
import org.sonar.wsclient.services.CreateQuery;
import org.sonar.wsclient.services.DeleteQuery;
import org.sonar.wsclient.services.Query;
import org.sonar.wsclient.services.UpdateQuery;
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-
/**
* @since 2.1
*/
json = EntityUtils.toString(entity);
} else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NOT_FOUND) {
- throw new ConnectionException("HTTP error: " + response.getStatusLine().getStatusCode() + ", msg: " + response.getStatusLine().getReasonPhrase() + ", query: " + request.toString());
+ throw new ConnectionException("HTTP error: " + response.getStatusLine().getStatusCode()
+ + ", msg: " + response.getStatusLine().getReasonPhrase()
+ + ", query: " + request.toString());
}
}
HttpConnectionParams.setConnectionTimeout(params, TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(params, TIMEOUT_MS);
if (server.getUsername() != null) {
- client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(server.getUsername(), server.getPassword()));
+ client.getCredentialsProvider()
+ .setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(server.getUsername(), server.getPassword()));
}
return client;
}
private HttpPost newPostMethod(CreateQuery<?> query) {
HttpPost post = new HttpPost(server.getHost() + query.getUrl());
- if (query.getBody() != null) {
- try {
- post.setEntity(new StringEntity(query.getBody(), "UTF-8"));
- } catch (UnsupportedEncodingException e) {
- throw new ConnectionException("Encoding is not supported", e);
- }
- }
setJsonHeader(post);
+ setRequestEntity(post, query);
return post;
}
private HttpPut newPutMethod(UpdateQuery<?> query) {
HttpPut put = new HttpPut(server.getHost() + query.getUrl());
+ setJsonHeader(put);
+ setRequestEntity(put, query);
+ return put;
+ }
+
+ private void setRequestEntity(HttpEntityEnclosingRequestBase request, AbstractQuery<?> query) {
if (query.getBody() != null) {
try {
- put.setEntity(new StringEntity(query.getBody(), "UTF-8"));
+ request.setEntity(new StringEntity(query.getBody(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new ConnectionException("Encoding is not supported", e);
}
}
- setJsonHeader(put);
- return put;
}
private void setJsonHeader(HttpRequestBase request) {
/**
* Must start with a slash, for example: /api/metrics
+ * <p>
+ * IMPORTANT: In implementations of this method we must use helper methods to construct URL.
+ * </p>
+ *
+ * @see #encode(String)
+ * @see #appendUrlParameter(StringBuilder, String, Object)
+ * @see #appendUrlParameter(StringBuilder, String, Object[])
+ * @see #appendUrlParameter(StringBuilder, String, Date, boolean)
*/
public abstract String getUrl();
/**
- * Request body. By defaut it is empty but it can be overriden.
+ * Request body. By default it is empty but it can be overridden.
*/
public String getBody() {
return null;
}
+ /**
+ * Encodes single parameter value.
+ */
+ protected static String encode(String value) {
+ return WSUtils.getINSTANCE().encodeUrl(value);
+ }
+
protected static void appendUrlParameter(StringBuilder url, String paramKey, Object paramValue) {
if (paramValue != null) {
url.append(paramKey)
.append('=')
- .append(paramValue)
+ .append(encode(paramValue.toString()))
.append('&');
}
}
url.append(',');
}
if (paramValues[index] != null) {
- url.append(paramValues[index]);
+ url.append(encode(paramValues[index].toString()));
}
}
url.append('&');
String format = (includeTime ? "yyyy-MM-dd'T'HH:mm:ssZ" : "yyyy-MM-dd");
url.append(paramKey)
.append('=')
- .append(WSUtils.getINSTANCE().encodeUrl(WSUtils.getINSTANCE().format(paramValue, format)))
+ .append(encode(WSUtils.getINSTANCE().format(paramValue, format)))
.append('&');
}
}
public String getUrl() {
StringBuilder url = new StringBuilder(BASE_URL);
url.append('?');
- if (resourceIdOrKey != null) {
- url.append("resource=").append(resourceIdOrKey).append('&');
- }
- if (direction != null) {
- url.append("dir=").append(direction).append('&');
- }
- if (parentId !=null) {
- url.append("parent=").append(parentId).append('&');
- }
- if (id !=null) {
- url.append("id=").append(id).append('&');
- }
+ appendUrlParameter(url, "resource", resourceIdOrKey);
+ appendUrlParameter(url, "dir", direction);
+ appendUrlParameter(url, "parent", parentId);
+ appendUrlParameter(url, "id", id);
return url.toString();
}
/**
* Resources that depend upon a resource
- *
+ *
* @param resourceIdOrKey the target resource. Can be the primary key (a number) or the logical key (String)
*/
public static DependencyQuery createForIncomingDependencies(String resourceIdOrKey) {
/**
* Resources that are depended upon a resource = all the resources that a resource depends upon
- *
+ *
* @param resourceIdOrKey the target resource. Can be the primary key (an integer) or the logical key (String)
*/
public static DependencyQuery createForOutgoingDependencies(String resourceIdOrKey) {
/**
* Resources that depend upon or are depended upon a resource. It equals the merge of createForIncomingDependencies(resourceIdOrKey)
* and createForOutgoingDependencies(resourceIdOrKey)
- *
+ *
* @param resourceIdOrKey the target resource. Can be the primary key (an integer) or the logical key (String)
*/
public static DependencyQuery createForResource(String resourceIdOrKey) {
@Override
public String getUrl() {
StringBuilder url = new StringBuilder(BASE_URL);
- url.append("?resource=")
- .append(resourceId)
- .append("&");
- if (scopes != null) {
- url.append("scopes=");
- if (scopes != null) {
- for (int index = 0; index < scopes.length; index++) {
- if (index > 0) {
- url.append(',');
- }
- if (scopes[index] != null) {
- url.append(scopes[index]);
- }
- }
- url.append('&');
- }
- }
+ url.append('?');
+ appendUrlParameter(url, "resource", resourceId);
+ appendUrlParameter(url, "scopes", scopes);
return url.toString();
}
@Override
public String getUrl() {
- return new StringBuilder().append(FavouriteQuery.BASE_URL).append("?key=").append(idOrKey).toString();
+ StringBuilder url = new StringBuilder(FavouriteQuery.BASE_URL).append('?');
+ appendUrlParameter(url, "key", idOrKey);
+ return url.toString();
}
@Override
@Override
public String getUrl() {
- return new StringBuilder().append(FavouriteQuery.BASE_URL).append('/').append(idOrKey).toString();
+ return new StringBuilder().append(FavouriteQuery.BASE_URL).append('/').append(encode(idOrKey)).toString();
}
}
StringBuilder sb = new StringBuilder(BASE_URL);
if (key != null && !"".equals(key)) {
sb.append("/");
- sb.append(key);
+ sb.append(encode(key));
}
sb.append("?");
return sb.toString();
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
-
package org.sonar.wsclient.services;
/**
public String getUrl() {
StringBuilder url = new StringBuilder();
url.append(PropertyQuery.BASE_URL);
- url.append("/").append(key);
+ url.append("/").append(encode(key));
url.append('?');
appendUrlParameter(url, "value", value);
appendUrlParameter(url, "resource", resourceKeyOrId);
public String getUrl() {
StringBuilder url = new StringBuilder();
url.append(PropertyQuery.BASE_URL);
- url.append("/").append(key);
+ url.append("/").append(encode(key));
url.append('?');
appendUrlParameter(url, "resource", resourceKeyOrId);
return url.toString();
public String getUrl() {
StringBuilder url = new StringBuilder(BASE_URL);
if (key != null) {
- url.append("/").append(key);
+ url.append("/").append(encode(key));
}
url.append('?');
appendUrlParameter(url, "resource", resourceKeyOrId);
public String getUrl() {
StringBuilder url = new StringBuilder();
url.append(PropertyQuery.BASE_URL);
- url.append("/").append(key);
+ url.append("/").append(encode(key));
url.append('?');
appendUrlParameter(url, "resource", resourceKeyOrId);
return url.toString();
/**
* Get only a few lines
- *
- * @param from Index of the first line, starts to 1
+ *
+ * @param from Index of the first line, starts to 1
* @param excludedTo Index of the last line (excluded).
*/
public SourceQuery setFromLineToLine(int from, int excludedTo) {
@Override
public String getUrl() {
StringBuilder url = new StringBuilder(BASE_URL);
- url.append("?resource=")
- .append(resourceKeyOrId)
- .append("&");
+ url.append('?');
+ appendUrlParameter(url, "resource", resourceKeyOrId);
if (from > 0 && to > 0) {
url.append("from=").append(from).append("&to=").append(to).append("&");
}
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
-
package org.sonar.wsclient.services;
/**
@Override
public String getUrl() {
- return new StringBuilder().append(UserPropertyQuery.BASE_URL).append('/').append(key).toString();
+ return new StringBuilder().append(UserPropertyQuery.BASE_URL).append('/').append(encode(key)).toString();
}
}
/**
* Get properties of the authenticated user.
- *
+ *
* @since 2.2
*/
public class UserPropertyQuery extends Query<Property> {
public String getUrl() {
String url = BASE_URL;
if (key != null) {
- url += "/" + key;
+ url += "/" + encode(key);
}
return url + "?";
}
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
-
package org.sonar.wsclient.unmarshallers;
import org.sonar.wsclient.services.Plugin;
*/
package org.sonar.wsclient;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.number.OrderingComparisons.greaterThan;
+import static org.junit.Assert.assertThat;
+
+import java.util.Arrays;
+import java.util.Collection;
+
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sonar.wsclient.connectors.ConnectionException;
import org.sonar.wsclient.connectors.HttpClient3Connector;
import org.sonar.wsclient.connectors.HttpClient4Connector;
-import org.sonar.wsclient.services.*;
-
-import java.util.Arrays;
-import java.util.Collection;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.nullValue;
-import static org.hamcrest.number.OrderingComparisons.greaterThan;
-import static org.junit.Assert.assertThat;
+import org.sonar.wsclient.services.Metric;
+import org.sonar.wsclient.services.MetricQuery;
+import org.sonar.wsclient.services.Query;
+import org.sonar.wsclient.services.Server;
+import org.sonar.wsclient.services.ServerQuery;
@RunWith(value = Parameterized.class)
public class SonarTest {
baseUrl = tester.createSocketConnector(true);
tester.start();
- return Arrays.asList(new Object[][]{
- {new Sonar(new HttpClient4Connector(new Host(baseUrl)))},
- {new Sonar(new HttpClient3Connector(new Host(baseUrl)))}
+ return Arrays.asList(new Object[][] {
+ { new Sonar(new HttpClient4Connector(new Host(baseUrl))) },
+ { new Sonar(new HttpClient3Connector(new Host(baseUrl))) }
});
}
-
@AfterClass
public static void stopServer() throws Exception {
tester.stop();
assertThat(metrics.size(), is(0));
}
- @Test
- public void urlWithCharactersToEncode() {
- sonar.find(new QueryWithInvalidCharacters());
- // no exception
- }
-
@Test
public void returnNullWhenSingleResultNotFound() {
Query<Metric> query = new EmptyQuery();
}
}
- static class QueryWithInvalidCharacters extends Query<Metric> {
- public String getUrl() {
- // [] must be encoded
- return "/api/violations?resource=myproject:[default]:Foo";
- }
-
- public Class<Metric> getModelClass() {
- return Metric.class;
- }
- }
}
-
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
-
package org.sonar.wsclient.services;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.sonar.wsclient.JdkUtils;
+import static junit.framework.Assert.assertEquals;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
-import static junit.framework.Assert.assertEquals;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.sonar.wsclient.JdkUtils;
public class AbstractQueryTest {
WSUtils.setInstance(null);
}
+ @Test
+ public void appendSpecialChars() {
+ StringBuilder url = new StringBuilder();
+ AbstractQuery.appendUrlParameter(url, "foo", "should escape []()");
+ assertEquals("foo=should+escape+%5B%5D%28%29&", url.toString());
+ }
+
+ @Test
+ public void appendSpecialCharsInArray() {
+ StringBuilder url = new StringBuilder();
+ AbstractQuery.appendUrlParameter(url, "foo", new String[] { "should escape", "[]()" });
+ assertEquals("foo=should+escape,%5B%5D%28%29&", url.toString());
+ }
+
@Test
public void appendUrlParameter() {
StringBuilder url = new StringBuilder();
@Test
public void appendUrlArrayParameter() {
StringBuilder url = new StringBuilder();
- AbstractQuery.appendUrlParameter(url, "foo", new String[]{"bar", "bar2"});
+ AbstractQuery.appendUrlParameter(url, "foo", new String[] { "bar", "bar2" });
assertEquals("foo=bar,bar2&", url.toString());
}
*/
package org.sonar.wsclient.services;
-import org.junit.Test;
-
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertThat;
-public class DependencyQueryTest {
+import org.junit.Test;
+
+public class DependencyQueryTest extends QueryTestCase {
@Test
public void createAllDependencies() {
*/
package org.sonar.wsclient.services;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.wsclient.JdkUtils;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
import java.util.Date;
-import static org.junit.Assert.*;
-
-public final class EventQueryTest {
+import org.junit.Test;
- @Before
- public void before() {
- // WSUtils is called during getUrl()
- // It has to be initialized.
- WSUtils.setInstance(new JdkUtils());
- }
+public final class EventQueryTest extends QueryTestCase {
@Test
public void from() {
@Test
public void urlWithTime() {
EventQuery query = new EventQuery("key");
- query.setCategories(new String[]{"category"});
+ query.setCategories(new String[] { "category" });
Date date = new Date();
query.setTo(date, true);
query.setFrom(date, true);
@Test
public void urlWithoutTime() {
EventQuery query = new EventQuery("key");
- query.setCategories(new String[]{"category"});
+ query.setCategories(new String[] { "category" });
Date date = new Date();
query.setTo(date, false);
query.setFrom(date, false);
@Test
public void urlWithoutDate() {
EventQuery query = new EventQuery("key");
- query.setCategories(new String[]{"category"});
+ query.setCategories(new String[] { "category" });
final String url = query.getUrl();
assertNotNull(url);
*/
package org.sonar.wsclient.services;
-import org.junit.Test;
-
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class MetricQueryTest {
+import org.junit.Test;
+
+public class MetricQueryTest extends QueryTestCase {
@Test
public void all() {
*/
package org.sonar.wsclient.services;
-import org.junit.Test;
-
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class PropertyCreateQueryTest {
+import org.junit.Test;
+
+public class PropertyCreateQueryTest extends QueryTestCase {
@Test
public void create() {
@Test
public void createForResource() {
PropertyCreateQuery query = new PropertyCreateQuery("foo", "bar", "my:resource");
- assertThat(query.getUrl(), is("/api/properties/foo?value=bar&resource=my:resource&"));
+ assertThat(query.getUrl(), is("/api/properties/foo?value=bar&resource=my%3Aresource&"));
assertThat(query.getModelClass().getName(), is(Property.class.getName()));
}
}
\ No newline at end of file
*/
package org.sonar.wsclient.services;
-import org.junit.Test;
-
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class PropertyDeleteQueryTest {
+import org.junit.Test;
+
+public class PropertyDeleteQueryTest extends QueryTestCase {
@Test
public void delete() {
@Test
public void deleteResourceProp() {
PropertyDeleteQuery query = new PropertyDeleteQuery("foo", "my:resource");
- assertThat(query.getUrl(), is("/api/properties/foo?resource=my:resource&"));
+ assertThat(query.getUrl(), is("/api/properties/foo?resource=my%3Aresource&"));
}
}
\ No newline at end of file
*/
package org.sonar.wsclient.services;
-import org.junit.Test;
-
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class PropertyQueryTest {
+import org.junit.Test;
+
+public class PropertyQueryTest extends QueryTestCase {
@Test
public void all() {
@Test
public void byKeyAndResource() {
- assertThat(PropertyQuery.createForResource("myprop", "my:resource").getUrl(), is("/api/properties/myprop?resource=my:resource&"));
+ assertThat(PropertyQuery.createForResource("myprop", "my:resource").getUrl(), is("/api/properties/myprop?resource=my%3Aresource&"));
assertThat(PropertyQuery.createForResource("myprop", "my:resource").getModelClass().getName(), is(Property.class.getName()));
}
}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.wsclient.services;
+
+import org.junit.BeforeClass;
+import org.sonar.wsclient.JdkUtils;
+
+public abstract class QueryTestCase {
+
+ @BeforeClass
+ public static void setupWsUtils() {
+ // WSUtils is called during getUrl()
+ // It has to be initialized.
+ WSUtils.setInstance(new JdkUtils());
+ }
+
+}
*/
package org.sonar.wsclient.services;
-import org.junit.Test;
-
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertThat;
-public class ResourceQueryTest {
+import org.junit.Test;
+
+public class ResourceQueryTest extends QueryTestCase {
@Test
public void resource() {
ResourceQuery query = new ResourceQuery("org.foo:bar");
- assertThat(query.getUrl(), is("/api/resources?resource=org.foo:bar&verbose=false&"));
+ assertThat(query.getUrl(), is("/api/resources?resource=org.foo%3Abar&verbose=false&"));
assertThat(query.getResourceKeyOrId(), is("org.foo:bar"));
assertThat(query.isVerbose(), is(false));
}
@Test
public void measuresOnRulePriorities() {
ResourceQuery query = new ResourceQuery().setMetrics("violations");
- query.setRuleSeverities("MAJOR,MINOR");
+ query.setRuleSeverities("MAJOR", "MINOR");
assertThat(query.getUrl(), is("/api/resources?metrics=violations&rule_priorities=MAJOR,MINOR&verbose=false&"));
}
*/
package org.sonar.wsclient.services;
-import org.junit.Test;
-
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-public class RuleQueryTest {
+import org.junit.Test;
+
+public class RuleQueryTest extends QueryTestCase {
@Test
public void languageRules() {
*/
package org.sonar.wsclient.services;
-import org.junit.Test;
-
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-/**
- * @author Evgeny Mandrikov
- */
-public class ServerQueryTest {
+import org.junit.Test;
+
+public class ServerQueryTest extends QueryTestCase {
@Test
public void index() {
ServerQuery query = new ServerQuery();
*/
package org.sonar.wsclient.services;
-import org.junit.Test;
-
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class SourceQueryTest {
+import org.junit.Test;
+
+public class SourceQueryTest extends QueryTestCase {
@Test
public void create() {
- assertThat(SourceQuery.create("myproject:org.foo.Bar").getUrl(), is("/api/sources?resource=myproject:org.foo.Bar&"));
+ assertThat(SourceQuery.create("myproject:org.foo.Bar").getUrl(), is("/api/sources?resource=myproject%3Aorg.foo.Bar&"));
assertThat(SourceQuery.create("myproject:org.foo.Bar").getModelClass().getName(), is(Source.class.getName()));
}
@Test
public void createWithHighlightedSyntax() {
- assertThat(SourceQuery.createWithHighlightedSyntax("myproject:org.foo.Bar").getUrl(), is("/api/sources?resource=myproject:org.foo.Bar&color=true&"));
+ assertThat(SourceQuery.createWithHighlightedSyntax("myproject:org.foo.Bar").getUrl(),
+ is("/api/sources?resource=myproject%3Aorg.foo.Bar&color=true&"));
assertThat(SourceQuery.createWithHighlightedSyntax("myproject:org.foo.Bar").getModelClass().getName(), is(Source.class.getName()));
}
@Test
public void getOnlyAFewLines() {
- assertThat(SourceQuery.create("myproject:org.foo.Bar").setFromLineToLine(10, 30).getUrl(), is("/api/sources?resource=myproject:org.foo.Bar&from=10&to=30&"));
- assertThat(SourceQuery.create("myproject:org.foo.Bar").setLinesFromLine(10, 20).getUrl(), is("/api/sources?resource=myproject:org.foo.Bar&from=10&to=30&"));
+ assertThat(SourceQuery.create("myproject:org.foo.Bar").setFromLineToLine(10, 30).getUrl(),
+ is("/api/sources?resource=myproject%3Aorg.foo.Bar&from=10&to=30&"));
+ assertThat(SourceQuery.create("myproject:org.foo.Bar").setLinesFromLine(10, 20).getUrl(),
+ is("/api/sources?resource=myproject%3Aorg.foo.Bar&from=10&to=30&"));
}
}
*/
package org.sonar.wsclient.services;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.wsclient.JdkUtils;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
-import java.util.Locale;
import java.util.TimeZone;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.wsclient.JdkUtils;
-public class TimeMachineQueryTest {
+public class TimeMachineQueryTest extends QueryTestCase {
private TimeZone systemTimeZone;
Date from = new SimpleDateFormat("yyyy-MM-dd").parse("2010-02-18");
Date to = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2010-03-25 14:59");
TimeMachineQuery query = TimeMachineQuery.createForMetrics("12345", "ncloc").setFrom(from).setTo(to);
- assertThat(query.getUrl(), is("/api/timemachine?resource=12345&metrics=ncloc&fromDateTime=2010-02-18T00%3A00%3A00%2B0000&toDateTime=2010-03-25T14%3A59%3A00%2B0000&"));
+ assertThat(
+ query.getUrl(),
+ is("/api/timemachine?resource=12345&metrics=ncloc&fromDateTime=2010-02-18T00%3A00%3A00%2B0000&toDateTime=2010-03-25T14%3A59%3A00%2B0000&"));
}
}
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
-
package org.sonar.wsclient.services;
-import org.junit.Test;
-
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-public class UpdateCenterQueryTest {
+import org.junit.Test;
+
+public class UpdateCenterQueryTest extends QueryTestCase {
@Test
public void index() {
*/
package org.sonar.wsclient.services;
-import org.junit.Test;
-
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class UserPropertyCreateQueryTest {
+import org.junit.Test;
+
+public class UserPropertyCreateQueryTest extends QueryTestCase {
@Test
public void create() {
*/
package org.sonar.wsclient.services;
-import org.junit.Test;
-
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class UserPropertyDeleteQueryTest {
+import org.junit.Test;
+
+public class UserPropertyDeleteQueryTest extends QueryTestCase {
@Test
public void delete() {
*/
package org.sonar.wsclient.services;
-import org.junit.Test;
-
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
-public class ViolationQueryTest {
+import org.junit.Test;
+
+public class ViolationQueryTest extends QueryTestCase {
@Test
public void resourceViolations() {
ViolationQuery query = ViolationQuery.createForResource("myproject:org.foo:bar");
- assertThat(query.getUrl(), is("/api/violations?resource=myproject:org.foo:bar&"));
+ assertThat(query.getUrl(), is("/api/violations?resource=myproject%3Aorg.foo%3Abar&"));
assertThat(query.getModelClass().getName(), is(Violation.class.getName()));
}
.setSeverities("MAJOR", "BLOCKER")
.setQualifiers("FIL")
.setRuleKeys("checkstyle:foo", "pmd:bar");
- assertThat(query.getUrl(), is("/api/violations?resource=myproject&depth=-1&limit=20&qualifiers=FIL&rules=checkstyle:foo,pmd:bar&priorities=MAJOR,BLOCKER&"));
+ assertThat(
+ query.getUrl(),
+ is("/api/violations?resource=myproject&depth=-1&limit=20&qualifiers=FIL&rules=checkstyle%3Afoo,pmd%3Abar&priorities=MAJOR,BLOCKER&"));
}
}
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
-
package org.sonar.wsclient.unmarshallers;
import org.junit.Test;
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
-
package org.sonar.wsclient.unmarshallers;
import org.apache.commons.io.IOUtils;
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
-
package org.sonar.wsclient.unmarshallers;
import org.junit.Test;