ensured credentials compare equivalent in subclasses

This commit is contained in:
Adrian Cole 2010-10-26 20:16:24 -07:00
parent 4b38c49000
commit a9fe466614
2 changed files with 22 additions and 24 deletions

View File

@ -61,8 +61,8 @@ public class Credentials {
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + ((identity == null) ? 0 : identity.hashCode());
result = prime * result + ((credential == null) ? 0 : credential.hashCode()); result = prime * result + ((credential == null) ? 0 : credential.hashCode());
result = prime * result + ((identity == null) ? 0 : identity.hashCode());
return result; return result;
} }
@ -72,20 +72,19 @@ public class Credentials {
return true; return true;
if (obj == null) if (obj == null)
return false; return false;
if (getClass() != obj.getClass()) if (!(obj instanceof Credentials))
return false; return false;
Credentials other = (Credentials) obj; Credentials other = (Credentials) obj;
if (identity == null) {
if (other.identity != null)
return false;
} else if (!identity.equals(other.identity))
return false;
if (credential == null) { if (credential == null) {
if (other.credential != null) if (other.credential != null)
return false; return false;
} else if (!credential.equals(other.credential)) } else if (!credential.equals(other.credential))
return false; return false;
if (identity == null) {
if (other.identity != null)
return false;
} else if (!identity.equals(other.identity))
return false;
return true; return true;
} }

View File

@ -32,8 +32,8 @@ import org.testng.annotations.Test;
public class CredentialsTest { public class CredentialsTest {
public void testAzure() { public void testAzure() {
Credentials creds = Credentials.parse(URI Credentials creds = Credentials
.create("compute://identity:Base64==@azureblob/container-hyphen/prefix")); .parse(URI.create("compute://identity:Base64==@azureblob/container-hyphen/prefix"));
assertEquals(creds.identity, "identity"); assertEquals(creds.identity, "identity");
assertEquals(creds.credential, "Base64=="); assertEquals(creds.credential, "Base64==");
} }
@ -46,36 +46,31 @@ public class CredentialsTest {
} }
public void testDollar() { public void testDollar() {
Credentials creds = Credentials.parse(URI Credentials creds = Credentials.parse(URI.create("compute://user%40domain:pa%24sword@hostingdotcom"));
.create("compute://user%40domain:pa%24sword@hostingdotcom"));
assertEquals(creds.identity, "user@domain"); assertEquals(creds.identity, "user@domain");
assertEquals(creds.credential, "pa$sword"); assertEquals(creds.credential, "pa$sword");
} }
public void testTerremark() { public void testTerremark() {
Credentials creds = Credentials.parse(URI Credentials creds = Credentials.parse(URI.create("compute://user%40domain:password@terremark"));
.create("compute://user%40domain:password@terremark"));
assertEquals(creds.identity, "user@domain"); assertEquals(creds.identity, "user@domain");
assertEquals(creds.credential, "password"); assertEquals(creds.credential, "password");
} }
public void testTerremark2() { public void testTerremark2() {
Credentials creds = Credentials.parse(URI Credentials creds = Credentials.parse(URI.create("compute://user%40domain:passw%40rd@terremark"));
.create("compute://user%40domain:passw%40rd@terremark"));
assertEquals(creds.identity, "user@domain"); assertEquals(creds.identity, "user@domain");
assertEquals(creds.credential, "passw@rd"); assertEquals(creds.credential, "passw@rd");
} }
public void testTerremark3() { public void testTerremark3() {
Credentials creds = Credentials.parse(URI Credentials creds = Credentials.parse(URI.create("compute://user%40domain:AbC%21%40943%21@terremark"));
.create("compute://user%40domain:AbC%21%40943%21@terremark"));
assertEquals(creds.identity, "user@domain"); assertEquals(creds.identity, "user@domain");
assertEquals(creds.credential, "AbC!@943!"); assertEquals(creds.credential, "AbC!@943!");
} }
public void testCloudFiles() { public void testCloudFiles() {
Credentials creds = Credentials.parse(URI Credentials creds = Credentials.parse(URI.create("compute://identity:h3c@cloudfiles/container-hyphen/prefix"));
.create("compute://identity:h3c@cloudfiles/container-hyphen/prefix"));
assertEquals(creds.identity, "identity"); assertEquals(creds.identity, "identity");
assertEquals(creds.credential, "h3c"); assertEquals(creds.credential, "h3c");
@ -83,18 +78,22 @@ public class CredentialsTest {
public void testS3() { public void testS3() {
Credentials creds = Credentials Credentials creds = Credentials.parse(URI.create("compute://0AB:aA%2B%2F0@s3/buck-et/prefix"));
.parse(URI.create("compute://0AB:aA%2B%2F0@s3/buck-et/prefix"));
assertEquals(creds.identity, "0AB"); assertEquals(creds.identity, "0AB");
assertEquals(creds.credential, "aA+/0"); assertEquals(creds.credential, "aA+/0");
} }
public void testS3Space() { public void testS3Space() {
Credentials creds = Credentials.parse(URI Credentials creds = Credentials.parse(URI.create("compute://0AB:aA%2B%2F0@s3/buck-et/pre%20fix"));
.create("compute://0AB:aA%2B%2F0@s3/buck-et/pre%20fix"));
assertEquals(creds.identity, "0AB"); assertEquals(creds.identity, "0AB");
assertEquals(creds.credential, "aA+/0"); assertEquals(creds.credential, "aA+/0");
} }
public void testSubClassEquals() {
Credentials creds = new Credentials("user", "pass");
assertEquals(creds, new Credentials("user", "pass") {
});
}
} }