Skip to content

Commit

Permalink
update unitils-core to 3.4.2, increase unit test coverage with Equals…
Browse files Browse the repository at this point in the history
…Verifier, make OAuthParameters immutable

git-svn-id: http://josm.openstreetmap.de/svn/trunk@9220 0c6e7542-c601-0410-84e7-c038aed88b3b
  • Loading branch information
don-vip committed Dec 30, 2015
1 parent c0bcf7c commit 60b4829
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 93 deletions.
3 changes: 2 additions & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
<classpathentry kind="lib" path="test/lib/fest/jcip-annotations-1.0.jar"/>
<classpathentry kind="lib" path="test/lib/fest/MRJToolkitStubs-1.0.jar"/>
<classpathentry kind="lib" path="test/lib/jfcunit.jar"/>
<classpathentry kind="lib" path="test/lib/equalsverifier-1.7.6.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.7.0_80"/>
<classpathentry exported="true" kind="con" path="GROOVY_SUPPORT"/>
<classpathentry kind="lib" path="test/lib/unitils-core/commons-collections-3.2.jar"/>
<classpathentry kind="lib" path="test/lib/unitils-core/commons-lang-2.3.jar"/>
<classpathentry kind="lib" path="test/lib/unitils-core/commons-logging-1.1.jar"/>
<classpathentry kind="lib" path="test/lib/unitils-core/ognl-2.6.9.jar"/>
<classpathentry kind="lib" path="test/lib/unitils-core/unitils-core-3.3.jar"/>
<classpathentry kind="lib" path="test/lib/unitils-core/unitils-core-3.4.2.jar"/>
<classpathentry kind="lib" path="test/lib/fest/debug-1.0.jar"/>
<classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/>
<classpathentry kind="lib" path="tools/findbugs/annotations.jar"/>
Expand Down
113 changes: 40 additions & 73 deletions src/org/openstreetmap/josm/data/oauth/OAuthParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.openstreetmap.josm.tools.CheckParameterUtil;

/**
* This class manages a set of OAuth parameters.
* This class manages an immutable set of OAuth parameters.
* @since 2747
*/
public class OAuthParameters {
Expand All @@ -39,7 +39,6 @@ public class OAuthParameters {
*/
public static final String DEFAULT_AUTHORISE_URL = Main.getOSMWebsite() + "/oauth/authorize";


/**
* Replies a set of default parameters for a consumer accessing the standard OSM server
* at {@link OsmApi#DEFAULT_API_URL}.
Expand All @@ -60,28 +59,24 @@ public static OAuthParameters createDefault() {
* @since 5422
*/
public static OAuthParameters createDefault(String apiUrl) {
OAuthParameters parameters = new OAuthParameters();
parameters.setConsumerKey(DEFAULT_JOSM_CONSUMER_KEY);
parameters.setConsumerSecret(DEFAULT_JOSM_CONSUMER_SECRET);
parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL);
parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL);
parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL);
String host = "";
if (!OsmApi.DEFAULT_API_URL.equals(apiUrl)) {
try {
String host = new URL(apiUrl).getHost();
if (host.endsWith("dev.openstreetmap.org")) {
parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL.replace("www.openstreetmap.org", host));
parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL.replace("www.openstreetmap.org", host));
parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL.replace("www.openstreetmap.org", host));
}
host = new URL(apiUrl).getHost();
} catch (MalformedURLException e) {
// Ignored
if (Main.isTraceEnabled()) {
Main.trace(e.getMessage());
}
}
}
return parameters;
boolean osmDevServer = host.endsWith("dev.openstreetmap.org");
return new OAuthParameters(
DEFAULT_JOSM_CONSUMER_KEY,
DEFAULT_JOSM_CONSUMER_SECRET,
osmDevServer ? DEFAULT_REQUEST_TOKEN_URL.replace("www.openstreetmap.org", host) : DEFAULT_REQUEST_TOKEN_URL,
osmDevServer ? DEFAULT_ACCESS_TOKEN_URL.replace("www.openstreetmap.org", host) : DEFAULT_ACCESS_TOKEN_URL,
osmDevServer ? DEFAULT_AUTHORISE_URL.replace("www.openstreetmap.org", host) : DEFAULT_AUTHORISE_URL);
}

/**
Expand All @@ -92,28 +87,40 @@ public static OAuthParameters createDefault(String apiUrl) {
*/
public static OAuthParameters createFromPreferences(Preferences pref) {
OAuthParameters parameters = createDefault(pref.get("osm-server.url"));
parameters.setConsumerKey(pref.get("oauth.settings.consumer-key", parameters.getConsumerKey()));
parameters.setConsumerSecret(pref.get("oauth.settings.consumer-secret", parameters.getConsumerSecret()));
parameters.setRequestTokenUrl(pref.get("oauth.settings.request-token-url", parameters.getRequestTokenUrl()));
parameters.setAccessTokenUrl(pref.get("oauth.settings.access-token-url", parameters.getAccessTokenUrl()));
parameters.setAuthoriseUrl(pref.get("oauth.settings.authorise-url", parameters.getAuthoriseUrl()));
return parameters;
}

private String consumerKey;
private String consumerSecret;
private String requestTokenUrl;
private String accessTokenUrl;
private String authoriseUrl;

/**
* Constructs a new, unitialized, {@code OAuthParameters}.
return new OAuthParameters(
pref.get("oauth.settings.consumer-key", parameters.getConsumerKey()),
pref.get("oauth.settings.consumer-secret", parameters.getConsumerSecret()),
pref.get("oauth.settings.request-token-url", parameters.getRequestTokenUrl()),
pref.get("oauth.settings.access-token-url", parameters.getAccessTokenUrl()),
pref.get("oauth.settings.authorise-url", parameters.getAuthoriseUrl())
);
}

private final String consumerKey;
private final String consumerSecret;
private final String requestTokenUrl;
private final String accessTokenUrl;
private final String authoriseUrl;

/**
* Constructs a new {@code OAuthParameters}.
* @param consumerKey consumer key
* @param consumerSecret consumer secret
* @param requestTokenUrl request token URL
* @param accessTokenUrl access token URL
* @param authoriseUrl authorise URL
*
* @see #createDefault
* @see #createFromPreferences
* @since 9220
*/
public OAuthParameters() {
// contents can be set later with setters
public OAuthParameters(String consumerKey, String consumerSecret,
String requestTokenUrl, String accessTokenUrl, String authoriseUrl) {
this.consumerKey = consumerKey;
this.consumerSecret = consumerSecret;
this.requestTokenUrl = requestTokenUrl;
this.accessTokenUrl = accessTokenUrl;
this.authoriseUrl = authoriseUrl;
}

/**
Expand All @@ -139,14 +146,6 @@ public String getConsumerKey() {
return consumerKey;
}

/**
* Sets the consumer key.
* @param consumerKey The consumer key
*/
public void setConsumerKey(String consumerKey) {
this.consumerKey = consumerKey;
}

/**
* Gets the consumer secret.
* @return The consumer secret
Expand All @@ -155,14 +154,6 @@ public String getConsumerSecret() {
return consumerSecret;
}

/**
* Sets the consumer secret.
* @param consumerSecret The consumer secret
*/
public void setConsumerSecret(String consumerSecret) {
this.consumerSecret = consumerSecret;
}

/**
* Gets the request token URL.
* @return The request token URL
Expand All @@ -171,14 +162,6 @@ public String getRequestTokenUrl() {
return requestTokenUrl;
}

/**
* Sets the request token URL.
* @param requestTokenUrl the request token URL
*/
public void setRequestTokenUrl(String requestTokenUrl) {
this.requestTokenUrl = requestTokenUrl;
}

/**
* Gets the access token URL.
* @return The access token URL
Expand All @@ -187,14 +170,6 @@ public String getAccessTokenUrl() {
return accessTokenUrl;
}

/**
* Sets the access token URL.
* @param accessTokenUrl The access token URL
*/
public void setAccessTokenUrl(String accessTokenUrl) {
this.accessTokenUrl = accessTokenUrl;
}

/**
* Gets the authorise URL.
* @return The authorise URL
Expand All @@ -203,14 +178,6 @@ public String getAuthoriseUrl() {
return authoriseUrl;
}

/**
* Sets the authorise URL.
* @param authoriseUrl The authorise URL
*/
public void setAuthoriseUrl(String authoriseUrl) {
this.authoriseUrl = authoriseUrl;
}

/**
* Builds an {@link OAuthConsumer} based on these parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,12 @@ protected void setChildComponentsEnabled(boolean enabled) {
public OAuthParameters getAdvancedParameters() {
if (cbUseDefaults.isSelected())
return OAuthParameters.createDefault(apiUrl);
OAuthParameters parameters = new OAuthParameters();
parameters.setConsumerKey(tfConsumerKey.getText());
parameters.setConsumerSecret(tfConsumerSecret.getText());
parameters.setRequestTokenUrl(tfRequestTokenURL.getText());
parameters.setAccessTokenUrl(tfAccessTokenURL.getText());
parameters.setAuthoriseUrl(tfAuthoriseURL.getText());
return parameters;
return new OAuthParameters(
tfConsumerKey.getText(),
tfConsumerSecret.getText(),
tfRequestTokenURL.getText(),
tfAccessTokenURL.getText(),
tfAuthoriseURL.getText());
}

/**
Expand Down
Binary file added test/lib/equalsverifier-1.7.6.jar
Binary file not shown.
Binary file removed test/lib/unitils-core/unitils-core-3.3.jar
Binary file not shown.
Binary file added test/lib/unitils-core/unitils-core-3.4.2.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import nl.jqno.equalsverifier.EqualsVerifier;

import org.junit.Test;
import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.io.OsmApi;

/**
Expand All @@ -24,16 +26,15 @@ public void testCreateDefault() {
OAuthParameters dev = OAuthParameters.createDefault("http://api06.dev.openstreetmap.org/api");
assertNotNull(dev);
assertNotEquals(def, dev);
Main.logLevel = 5; // enable trace for line coverage
assertEquals(def, OAuthParameters.createDefault("wrong_url"));
}

/**
* Unit test of method {@link OAuthParameters#equals}.
* Unit test of methods {@link OAuthParameters#equals} and {@link OAuthParameters#hashCode}.
*/
@Test
public void testEquals() {
OAuthParameters dev = OAuthParameters.createDefault("http://master.apis.dev.openstreetmap.org/api");
OAuthParameters dev2 = new OAuthParameters(dev);
assertEquals(dev, dev2);
public void equalsContract() {
EqualsVerifier.forClass(OAuthParameters.class).usingGetClass().verify();
}
}
11 changes: 4 additions & 7 deletions test/unit/org/openstreetmap/josm/data/oauth/OAuthTokenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import nl.jqno.equalsverifier.EqualsVerifier;
import oauth.signpost.OAuthConsumer;

import org.junit.Test;
Expand All @@ -26,14 +27,10 @@ public void testCreateToken() {
}

/**
* Unit test of method {@link OAuthToken#equals}.
* Unit test of methods {@link OAuthToken#equals} and {@link OAuthToken#hashCode}.
*/
@Test
public void testEquals() {
OAuthToken tok = new OAuthToken(
OAuthParameters.DEFAULT_JOSM_CONSUMER_KEY,
OAuthParameters.DEFAULT_JOSM_CONSUMER_SECRET);
OAuthToken tok2 = new OAuthToken(tok);
assertEquals(tok, tok2);
public void equalsContract() {
EqualsVerifier.forClass(OAuthToken.class).usingGetClass().verify();
}
}

0 comments on commit 60b4829

Please sign in to comment.