Added an abstract ProviderMetadata object to allow for overriding the equals and

hashCode methods.

[in core/src/main/java]

* org/jclouds/providers/BaseProviderMetadata.java: Added.

* org/jclouds/providers/ProviderMetadata.java: Fixed indentation to be three
   space based instead of two spaces.

[in core/src/test/java]

* org/jclouds/providers/JcloudsTestBlobStoreProviderMetadata.java,
  org/jclouds/providers/JcloudsTestComputeProviderMetadata.java: Updated to
   extend the new BaseProviderMetadata class instead of just implementing the
   ProviderMetadata interface.

* org/jclouds/providers/ProvidersTest.java
  (test*): Updated tests to check for ProviderMetadata object equality instead
   of testing each method individually.

Issue: http://code.google.com/p/jclouds/issues/detail?id=550
This commit is contained in:
Jeremy Whitlock 2011-05-11 20:20:40 -06:00
parent 197284471e
commit 3870f0d2bd
5 changed files with 148 additions and 53 deletions

View File

@ -0,0 +1,107 @@
/**
*
* Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.providers;
import java.net.URI;
/**
* The BaseProviderMetadata class is an abstraction of {@link ProviderMetadata} to be extended
* by those implementing ProviderMetadata.
*
* @author Jeremy Whitlock <jwhitlock@apache.org>
*/
public abstract class BaseProviderMetadata implements ProviderMetadata {
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
URI console = getConsole();
URI homepage = getHomepage();
String id = getId();
String name = getName();
String type = getType();
result = prime * result + ((console == null) ? 0 : console.hashCode());
result = prime * result + ((homepage == null) ? 0 : homepage.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
URI tConsole = getConsole();
URI tHomepage = getHomepage();
String tId = getId();
String tName = getName();
String tType = getType();
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ProviderMetadata other = (ProviderMetadata) obj;
URI oConsole = other.getConsole();
URI oHomepage = other.getHomepage();
String oId = other.getId();
String oName = other.getName();
String oType = other.getType();
if (tConsole == null) {
if (oConsole != null)
return false;
} else if (!tConsole.equals(oConsole))
return false;
if (tHomepage == null) {
if (oHomepage != null)
return false;
} else if (!tHomepage.equals(oHomepage))
return false;
if (tId == null) {
if (oId != null)
return false;
} else if (!tId.equals(oId))
return false;
if (tName == null) {
if (oName != null)
return false;
} else if (!tName.equals(oName))
return false;
if (tType == null) {
if (oType != null)
return false;
} else if (!tType.equals(oType))
return false;
return true;
}
}

View File

@ -31,39 +31,39 @@ public interface ProviderMetadata {
public static final String BLOBSTORE_TYPE = "blobstore"; public static final String BLOBSTORE_TYPE = "blobstore";
public static final String COMPUTE_TYPE = "compute"; public static final String COMPUTE_TYPE = "compute";
/** /**
* Returns an identifier unique to the provider. * Returns an identifier unique to the provider.
* *
* @return the provider's unique identifier * @return the provider's unique identifier
*/ */
public String getId(); public String getId();
/** /**
* Returns the provider type. * Returns the provider type.
* *
* @return the provider's type * @return the provider's type
*/ */
public String getType(); public String getType();
/** /**
* Returns the name of the provider. * Returns the name of the provider.
* *
* @return the name (display name) of the provider * @return the name (display name) of the provider
*/ */
public String getName(); public String getName();
/** /**
* Returns the URI to the provider's homepage. * Returns the URI to the provider's homepage.
* *
* @return the url for the provider's homepage * @return the url for the provider's homepage
*/ */
public URI getHomepage(); public URI getHomepage();
/** /**
* Returns the URI to the provider's console. * Returns the URI to the provider's console.
* *
* @return the url for the provider's console * @return the url for the provider's console
*/ */
public URI getConsole(); public URI getConsole();
} }

View File

@ -25,7 +25,7 @@ import java.net.URI;
* *
* @author Jeremy Whitlock <jwhitlock@apache.org> * @author Jeremy Whitlock <jwhitlock@apache.org>
*/ */
public class JcloudsTestBlobStoreProviderMetadata implements ProviderMetadata { public class JcloudsTestBlobStoreProviderMetadata extends BaseProviderMetadata {
/** /**
* {@ see org.jclouds.types.ProviderMetadata#getId()} * {@ see org.jclouds.types.ProviderMetadata#getId()}

View File

@ -25,7 +25,7 @@ import java.net.URI;
* *
* @author Jeremy Whitlock <jwhitlock@apache.org> * @author Jeremy Whitlock <jwhitlock@apache.org>
*/ */
public class JcloudsTestComputeProviderMetadata implements ProviderMetadata { public class JcloudsTestComputeProviderMetadata extends BaseProviderMetadata {
/** /**
* {@ see org.jclouds.types.ProviderMetadata#getId()} * {@ see org.jclouds.types.ProviderMetadata#getId()}

View File

@ -33,6 +33,9 @@ import org.testng.annotations.Test;
@Test( groups = "unit" ) @Test( groups = "unit" )
public class ProvidersTest { public class ProvidersTest {
private final ProviderMetadata testBlobstoreProvider = new JcloudsTestBlobStoreProviderMetadata();
private final ProviderMetadata testComputeProvider = new JcloudsTestComputeProviderMetadata();
@Test @Test
public void testWithId() { public void testWithId() {
ProviderMetadata providerMetadata; ProviderMetadata providerMetadata;
@ -45,9 +48,9 @@ public class ProvidersTest {
; // Expected ; // Expected
} }
providerMetadata = Providers.withId("test-blobstore-provider"); providerMetadata = Providers.withId(testBlobstoreProvider.getId());
assertEquals("Test Blobstore Provider", providerMetadata.getName()); assertEquals(testBlobstoreProvider, providerMetadata);
} }
@Test @Test
@ -55,21 +58,13 @@ public class ProvidersTest {
Iterable<ProviderMetadata> providersMetadata = Providers.ofType(ProviderMetadata.BLOBSTORE_TYPE); Iterable<ProviderMetadata> providersMetadata = Providers.ofType(ProviderMetadata.BLOBSTORE_TYPE);
for (ProviderMetadata providerMetadata : providersMetadata) { for (ProviderMetadata providerMetadata : providersMetadata) {
assertEquals("Test Blobstore Provider", providerMetadata.getName()); assertEquals(testBlobstoreProvider, providerMetadata);
assertEquals("test-blobstore-provider", providerMetadata.getId());
assertEquals(ProviderMetadata.BLOBSTORE_TYPE, providerMetadata.getType());
assertEquals("http://jclouds.org", providerMetadata.getHomepage().toString());
assertEquals("http://jclouds.org/console", providerMetadata.getConsole().toString());
} }
providersMetadata = Providers.ofType(ProviderMetadata.COMPUTE_TYPE); providersMetadata = Providers.ofType(ProviderMetadata.COMPUTE_TYPE);
for (ProviderMetadata providerMetadata : providersMetadata) { for (ProviderMetadata providerMetadata : providersMetadata) {
assertEquals("Test Compute Provider", providerMetadata.getName()); assertEquals(testComputeProvider, providerMetadata);
assertEquals("test-compute-provider", providerMetadata.getId());
assertEquals(ProviderMetadata.COMPUTE_TYPE, providerMetadata.getType());
assertEquals("http://jclouds.org", providerMetadata.getHomepage().toString());
assertEquals("http://jclouds.org/console", providerMetadata.getConsole().toString());
} }
providersMetadata = Providers.ofType("fake-type"); providersMetadata = Providers.ofType("fake-type");
@ -83,16 +78,9 @@ public class ProvidersTest {
for (ProviderMetadata providerMetadata : providersMetadata) { for (ProviderMetadata providerMetadata : providersMetadata) {
if (providerMetadata.getName().equals("Test Blobstore Provider")) { if (providerMetadata.getName().equals("Test Blobstore Provider")) {
assertEquals("test-blobstore-provider", providerMetadata.getId()); assertEquals(testBlobstoreProvider, providerMetadata);
assertEquals(ProviderMetadata.BLOBSTORE_TYPE, providerMetadata.getType());
assertEquals("http://jclouds.org", providerMetadata.getHomepage().toString());
assertEquals("http://jclouds.org/console", providerMetadata.getConsole().toString());
} else { } else {
assertEquals("Test Compute Provider", providerMetadata.getName()); assertEquals(testComputeProvider, providerMetadata);
assertEquals("test-compute-provider", providerMetadata.getId());
assertEquals(ProviderMetadata.COMPUTE_TYPE, providerMetadata.getType());
assertEquals("http://jclouds.org", providerMetadata.getHomepage().toString());
assertEquals("http://jclouds.org/console", providerMetadata.getConsole().toString());
} }
} }
} }