Permalink
Browse files

Override equals/hashcode in Token

  • Loading branch information...
whaley authored and fernandezpablo85 committed Aug 20, 2012
1 parent 979c657 commit 57409b50ac3cdd6a0f71421d4d29fd4a78ec3031
Showing with 71 additions and 7 deletions.
  1. +27 −7 src/main/java/org/scribe/model/Token.java
  2. +44 −0 src/test/java/org/scribe/model/TokenTest.java
@@ -1,25 +1,26 @@
package org.scribe.model;
-import java.io.Serializable;
+import java.io.*;
+import org.scribe.utils.*;
/**
* Represents an OAuth token (either request or access token) and its secret
- *
+ *
* @author Pablo Fernandez
*/
public class Token implements Serializable
{
private static final long serialVersionUID = 715000866082812683L;
-
+
private final String token;
private final String secret;
private final String rawResponse;
/**
* Default constructor
- *
- * @param token token value
- * @param secret token secret
+ *
+ * @param token token value. Can't be null.
+ * @param secret token secret. Can't be null.
*/
public Token(String token, String secret)
{
@@ -28,6 +29,9 @@ public Token(String token, String secret)
public Token(String token, String secret, String rawResponse)
{
+ Preconditions.checkNotNull(token, "Token can't be null");
+ Preconditions.checkNotNull(secret, "Secret can't be null");
+
this.token = token;
this.secret = secret;
this.rawResponse = rawResponse;
@@ -73,6 +77,22 @@ public boolean isEmpty()
*/
public static Token empty()
{
- return new Token("","");
+ return new Token("", "");
+ }
+
+ @Override
+ public boolean equals(Object o)
+ {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ Token that = (Token) o;
+ return token.equals(that.token) && secret.equals(that.secret);
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return 31 * token.hashCode() + secret.hashCode();
}
}
@@ -0,0 +1,44 @@
+package org.scribe.model;
+
+import static junit.framework.Assert.*;
+import org.junit.*;
+
+public class TokenTest
+{
+ @Test
+ public void shouldTestEqualityBasedOnTokenAndSecret() throws Exception
+ {
+ Token expected = new Token("access","secret");
+ Token actual = new Token("access","secret");
+
+ assertEquals(expected, actual);
+ assertEquals(actual, actual);
+ }
+
+ @Test
+ public void shouldNotDependOnRawString() throws Exception
+ {
+ Token expected = new Token("access","secret", "raw_string");
+ Token actual = new Token("access","secret", "different_raw_string");
+
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void shouldReturnSameHashCodeForEqualObjects() throws Exception
+ {
+ Token expected = new Token("access","secret");
+ Token actual = new Token("access","secret");
+
+ assertEquals(expected.hashCode(), actual.hashCode());
+ }
+
+ @Test
+ public void shouldNotBeEqualToNullOrOtherObjects() throws Exception
+ {
+ Token expected = new Token("access","secret","response");
+
+ assertNotSame(expected, null);
+ assertNotSame(expected, new Object());
+ }
+}

0 comments on commit 57409b5

Please sign in to comment.