Issue 695: Override methods in builders, equals, hashcode and toString methods. Fix test

This commit is contained in:
Jason King 2011-11-10 15:19:41 +00:00
parent 9e9fface25
commit 508973b7ab
8 changed files with 520 additions and 64 deletions

View File

@ -27,6 +27,7 @@ import java.util.Map;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
import org.jclouds.trmk.enterprisecloud.domain.internal.BaseNamedResource; import org.jclouds.trmk.enterprisecloud.domain.internal.BaseNamedResource;
import org.jclouds.trmk.enterprisecloud.domain.internal.BaseResource;
/** /**
* *
@ -108,6 +109,14 @@ public class Action extends BaseNamedResource<Action> {
return fromNamedResource(in).actionDisabled(in.getActionDisabled()); return fromNamedResource(in).actionDisabled(in.getActionDisabled());
} }
/**
* {@inheritDoc}
*/
@Override
public Builder fromResource(BaseResource<Action> in) {
return Builder.class.cast(super.fromResource(in));
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -116,6 +125,30 @@ public class Action extends BaseNamedResource<Action> {
return Builder.class.cast(super.fromNamedResource(in)); return Builder.class.cast(super.fromNamedResource(in));
} }
/**
* {@inheritDoc}
*/
@Override
public Builder name(String name) {
return Builder.class.cast(super.name(name));
}
/**
* {@inheritDoc}
*/
@Override
public Builder href(URI href) {
return Builder.class.cast(super.href(href));
}
/**
* {@inheritDoc}
*/
@Override
public Builder type(String type) {
return Builder.class.cast(super.type(type));
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -147,4 +180,28 @@ public class Action extends BaseNamedResource<Action> {
return actionDisabled; return actionDisabled;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Action action = (Action) o;
if (actionDisabled != action.actionDisabled) return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (actionDisabled != null ? actionDisabled.hashCode() : 0);
return result;
}
@Override
public String string() {
return super.string()+", actionDisabled="+actionDisabled;
}
} }

View File

@ -24,6 +24,7 @@ import java.net.URI;
import java.util.Map; import java.util.Map;
import org.jclouds.trmk.enterprisecloud.domain.internal.BaseNamedResource; import org.jclouds.trmk.enterprisecloud.domain.internal.BaseNamedResource;
import org.jclouds.trmk.enterprisecloud.domain.internal.BaseResource;
/** /**
* *
@ -118,6 +119,14 @@ public class Link extends BaseNamedResource<Link> {
return fromNamedResource(in).rel(in.getRelationship()); return fromNamedResource(in).rel(in.getRelationship());
} }
/**
* {@inheritDoc}
*/
@Override
public Builder fromResource(BaseResource<Link> in) {
return Builder.class.cast(super.fromResource(in));
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -126,6 +135,30 @@ public class Link extends BaseNamedResource<Link> {
return Builder.class.cast(super.fromNamedResource(in)); return Builder.class.cast(super.fromNamedResource(in));
} }
/**
* {@inheritDoc}
*/
@Override
public Builder name(String name) {
return Builder.class.cast(super.name(name));
}
/**
* {@inheritDoc}
*/
@Override
public Builder href(URI href) {
return Builder.class.cast(super.href(href));
}
/**
* {@inheritDoc}
*/
@Override
public Builder type(String type) {
return Builder.class.cast(super.type(type));
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -153,4 +186,28 @@ public class Link extends BaseNamedResource<Link> {
return rel; return rel;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Link link = (Link) o;
if (rel != link.rel) return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + rel.hashCode();
return result;
}
@Override
public String string() {
return super.string()+", rel="+rel;
}
} }

View File

@ -22,6 +22,7 @@ import java.net.URI;
import java.util.Map; import java.util.Map;
import org.jclouds.trmk.enterprisecloud.domain.internal.BaseNamedResource; import org.jclouds.trmk.enterprisecloud.domain.internal.BaseNamedResource;
import org.jclouds.trmk.enterprisecloud.domain.internal.BaseResource;
/** /**
* *
@ -57,22 +58,30 @@ public class NamedResource extends BaseNamedResource<NamedResource> {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public Builder name(String name) { public Builder fromResource(BaseResource<NamedResource> in) {
return Builder.class.cast(super.name(name)); return Builder.class.cast(super.fromResource(in));
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public Builder type(String type) { public Builder fromNamedResource(BaseNamedResource<NamedResource> in) {
return Builder.class.cast(super.type(type)); return Builder.class.cast(super.fromNamedResource(in));
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public Builder name(String name) {
return Builder.class.cast(super.name(name));
}
/**
* {@inheritDoc}
*/
@Override
public Builder href(URI href) { public Builder href(URI href) {
return Builder.class.cast(super.href(href)); return Builder.class.cast(super.href(href));
} }
@ -81,8 +90,8 @@ public class NamedResource extends BaseNamedResource<NamedResource> {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public Builder fromNamedResource(BaseNamedResource<NamedResource> in) { public Builder type(String type) {
return Builder.class.cast(super.fromNamedResource(in)); return Builder.class.cast(super.type(type));
} }
/** /**

View File

@ -299,11 +299,48 @@ public class Task extends BaseResource<Task> {
return initiatedBy; return initiatedBy;
} }
@Override @Override
public String toString() { public boolean equals(Object o) {
return "[type=" + type + ", href=" + href + ", operation=" + operation + ", status=" + status + ", impactedItem=" if (this == o) return true;
+ impactedItem + ", startTime=" + startTime + ", completedTime=" + completedTime + ", notes=" + notes if (o == null || getClass() != o.getClass()) return false;
+ ", errorMessage=" + errorMessage + ", initiatedBy=" + initiatedBy + "]"; if (!super.equals(o)) return false;
}
Task task = (Task) o;
if (completedTime != null ? !completedTime.equals(task.completedTime) : task.completedTime != null)
return false;
if (errorMessage != null ? !errorMessage.equals(task.errorMessage) : task.errorMessage != null)
return false;
if (!impactedItem.equals(task.impactedItem)) return false;
if (!initiatedBy.equals(task.initiatedBy)) return false;
if (notes != null ? !notes.equals(task.notes) : task.notes != null)
return false;
if (!operation.equals(task.operation)) return false;
if (!startTime.equals(task.startTime)) return false;
if (status != task.status) return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + operation.hashCode();
result = 31 * result + status.hashCode();
result = 31 * result + impactedItem.hashCode();
result = 31 * result + startTime.hashCode();
result = 31 * result + (completedTime != null ? completedTime.hashCode() : 0);
result = 31 * result + (notes != null ? notes.hashCode() : 0);
result = 31 * result + (errorMessage != null ? errorMessage.hashCode() : 0);
result = 31 * result + initiatedBy.hashCode();
return result;
}
@Override
public String string() {
return super.string()+", operation="+operation+", status="+status+
", impactedItem="+impactedItem+", startTime="+startTime+", completedTime="+completedTime+
", notes="+notes+", errorMessage="+errorMessage+", initiatedBy="+initiatedBy;
}
} }

View File

@ -0,0 +1,309 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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.trmk.enterprisecloud.domain;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
import static com.google.common.base.Preconditions.checkNotNull;
import java.net.URI;
import java.util.Date;
import java.util.Map;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.trmk.enterprisecloud.domain.internal.BaseResource;
/**
*
* @author Adrian Cole
*
*/
public class VirtualMachine extends BaseResource<VirtualMachine> {
public static enum Status {
/**
* the task is queued for execution.
*/
QUEUED,
/**
* the task is running.
*/
RUNNING,
/**
* the task failed.
*/
FAILED,
/**
* the task completed successfully.
*/
SUCCESS,
/**
* the task failed with an error.
*/
ERROR,
/**
* Status was not parsed by jclouds.
*/
UNRECOGNIZED;
public String value() {
return UPPER_UNDERSCORE.to(UPPER_CAMEL, name());
}
@Override
public String toString() {
return value();
}
public static Status fromValue(String status) {
try {
return valueOf(UPPER_CAMEL.to(UPPER_UNDERSCORE, checkNotNull(status, "status")));
} catch (IllegalArgumentException e) {
return UNRECOGNIZED;
}
}
}
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
/**
* {@inheritDoc}
*/
@Override
public Builder toBuilder() {
return new Builder().fromTask(this);
}
public static class Builder extends BaseResource.Builder<VirtualMachine> {
protected String operation;
protected Status status;
protected NamedResource impactedItem;
protected Date startTime;
protected Date completedTime;
protected String notes;
protected String errorMessage;
protected NamedResource initiatedBy;
/**
* @see org.jclouds.trmk.enterprisecloud.domain.VirtualMachine#getOperation
*/
public Builder operation(String operation) {
this.operation = operation;
return this;
}
/**
* @see org.jclouds.trmk.enterprisecloud.domain.VirtualMachine#getStatus
*/
public Builder status(Status status) {
this.status = status;
return this;
}
/**
* @see org.jclouds.trmk.enterprisecloud.domain.VirtualMachine#getImpactedItem
*/
public Builder impactedItem(NamedResource impactedItem) {
this.impactedItem = impactedItem;
return this;
}
/**
* @see org.jclouds.trmk.enterprisecloud.domain.VirtualMachine#getStartTime
*/
public Builder startTime(Date startTime) {
this.startTime = startTime;
return this;
}
/**
* @see org.jclouds.trmk.enterprisecloud.domain.VirtualMachine#getCompletedTime
*/
public Builder completedTime(Date completedTime) {
this.completedTime = completedTime;
return this;
}
/**
* @see org.jclouds.trmk.enterprisecloud.domain.VirtualMachine#getNotes
*/
public Builder notes(String notes) {
this.notes = notes;
return this;
}
/**
* @see org.jclouds.trmk.enterprisecloud.domain.VirtualMachine#getErrorMessage
*/
public Builder errorMessage(String errorMessage) {
this.errorMessage = errorMessage;
return this;
}
/**
* @see org.jclouds.trmk.enterprisecloud.domain.VirtualMachine#getInitiatedBy
*/
public Builder initiatedBy(NamedResource initiatedBy) {
this.initiatedBy = initiatedBy;
return this;
}
@Override
public VirtualMachine build() {
return new VirtualMachine(href, type, operation, status, impactedItem, startTime, completedTime, notes, errorMessage,
initiatedBy);
}
public Builder fromTask(VirtualMachine in) {
return fromResource(in).operation(in.getOperation()).status(in.getStatus()).impactedItem(in.getImpactedItem())
.startTime(in.getStartTime()).completedTime(in.getCompletedTime()).notes(in.getNotes())
.errorMessage(in.getErrorMessage()).initiatedBy(in.getInitiatedBy());
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromResource(BaseResource<VirtualMachine> in) {
return Builder.class.cast(super.fromResource(in));
}
/**
* {@inheritDoc}
*/
@Override
public Builder type(String type) {
return Builder.class.cast(super.type(type));
}
/**
* {@inheritDoc}
*/
@Override
public Builder href(URI href) {
return Builder.class.cast(super.href(href));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromAttributes(Map<String, String> attributes) {
return Builder.class.cast(super.fromAttributes(attributes));
}
}
protected final String operation;
protected final Status status;
protected final NamedResource impactedItem;
protected final Date startTime;
protected final Date completedTime;
protected final String notes;
protected final String errorMessage;
protected final NamedResource initiatedBy;
public VirtualMachine(URI href, String type, String operation, Status status, NamedResource impactedItem, Date startTime,
@Nullable Date completedTime, @Nullable String notes, @Nullable String errorMessage, NamedResource initiatedBy) {
super(href, type);
this.operation = checkNotNull(operation, "operation");
this.status = checkNotNull(status, "status");
this.impactedItem = checkNotNull(impactedItem, "impactedItem");
this.startTime = checkNotNull(startTime, "startTime");
this.completedTime = completedTime;// null if Queued or Running
this.notes = notes;
this.errorMessage = errorMessage;
this.initiatedBy = checkNotNull(initiatedBy, "initiatedBy");
}
/**
*
*
* @return name of action performed
*/
public String getOperation() {
return operation;
}
/**
*
*
* @return the status of the task
*/
public Status getStatus() {
return status;
}
/**
*
* @return the item acted upon
*/
public NamedResource getImpactedItem() {
return impactedItem;
}
/**
*
* @return time action started
*/
public Date getStartTime() {
return startTime;
}
/**
*
* @return time action completed, or null if Queued or Running
*/
@Nullable
public Date getCompletedTime() {
return completedTime;
}
/**
* @return notes on action
*/
public String getNotes() {
return notes;
}
/**
* @return error message
*/
public String getErrorMessage() {
return errorMessage;
}
/**
*
* @return the item acted upon
*/
public NamedResource getInitiatedBy() {
return initiatedBy;
}
@Override
public String toString() {
return "[type=" + type + ", href=" + href + ", operation=" + operation + ", status=" + status + ", impactedItem="
+ impactedItem + ", startTime=" + startTime + ", completedTime=" + completedTime + ", notes=" + notes
+ ", errorMessage=" + errorMessage + ", initiatedBy=" + initiatedBy + "]";
}
}

View File

@ -55,6 +55,15 @@ public class BaseNamedResource<T extends BaseNamedResource<T>> extends BaseResou
return new BaseNamedResource<T>(href, type, name); return new BaseNamedResource<T>(href, type, name);
} }
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public Builder<T> fromResource(BaseResource<T> in) {
return Builder.class.cast(super.fromResource(in));
}
public Builder<T> fromNamedResource(BaseNamedResource<T> in) { public Builder<T> fromNamedResource(BaseNamedResource<T> in) {
return fromResource(in).name(in.getName()); return fromResource(in).name(in.getName());
} }
@ -66,15 +75,6 @@ public class BaseNamedResource<T extends BaseNamedResource<T>> extends BaseResou
public Builder<T> fromAttributes(Map<String, String> attributes) { public Builder<T> fromAttributes(Map<String, String> attributes) {
return Builder.class.cast(super.fromAttributes(attributes)).name(attributes.get("name")); return Builder.class.cast(super.fromAttributes(attributes)).name(attributes.get("name"));
} }
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public Builder<T> fromResource(BaseResource<T> in) {
return Builder.class.cast(super.fromResource(in));
}
} }
protected final String name; protected final String name;
@ -88,45 +88,28 @@ public class BaseNamedResource<T extends BaseNamedResource<T>> extends BaseResou
return name; return name;
} }
@Override @Override
public int hashCode() { public boolean equals(Object o) {
final int prime = 31; if (this == o) return true;
int result = 1; if (o == null || getClass() != o.getClass()) return false;
result = prime * result + ((href == null) ? 0 : href.hashCode()); if (!super.equals(o)) return false;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override BaseNamedResource that = (BaseNamedResource) o;
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BaseNamedResource<?> other = (BaseNamedResource<?>) obj;
if (href == null) {
if (other.href != null)
return false;
} else if (!href.equals(other.href))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
@Override if (!name.equals(that.name)) return false;
public String toString() {
return "[href=" + href + ", name=" + name + ", type=" + type + "]"; return true;
} }
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + name.hashCode();
return result;
}
@Override
public String string() {
return super.string()+", name="+name;
}
} }

View File

@ -131,6 +131,10 @@ public class BaseResource<T extends BaseResource<T>> {
@Override @Override
public String toString() { public String toString() {
return "[href=" + href + ", type=" + type + "]"; return String.format("[%s]",string());
}
protected String string() {
return "href="+href+", type="+type;
} }
} }

View File

@ -45,7 +45,7 @@ public class TaskAsyncClientTest extends BaseTerremarkEnterpriseCloudAsyncClient
Method method = TaskAsyncClient.class.getMethod("getTasksInEnvironment", long.class); Method method = TaskAsyncClient.class.getMethod("getTasksInEnvironment", long.class);
HttpRequest httpRequest = processor.createRequest(method, 1l); HttpRequest httpRequest = processor.createRequest(method, 1l);
assertRequestLineEquals(httpRequest, "GET http://209.251.187.125/livespec/tasks/environments/1 HTTP/1.1"); assertRequestLineEquals(httpRequest, "GET https://services-beta.enterprisecloud.terremark.com/cloudapi/tasks/environments/1 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, assertNonPayloadHeadersEqual(httpRequest,
"Accept: application/vnd.tmrk.cloud.task; type=collection\nx-trmk-version: 2011-07-01\n"); "Accept: application/vnd.tmrk.cloud.task; type=collection\nx-trmk-version: 2011-07-01\n");
assertPayloadEquals(httpRequest, null, null, false); assertPayloadEquals(httpRequest, null, null, false);
@ -60,9 +60,9 @@ public class TaskAsyncClientTest extends BaseTerremarkEnterpriseCloudAsyncClient
public void testGetTask() throws SecurityException, NoSuchMethodException, IOException { public void testGetTask() throws SecurityException, NoSuchMethodException, IOException {
Method method = TaskAsyncClient.class.getMethod("getTask", URI.class); Method method = TaskAsyncClient.class.getMethod("getTask", URI.class);
HttpRequest httpRequest = processor.createRequest(method, URI.create("http://209.251.187.125/livespec/tasks/1")); HttpRequest httpRequest = processor.createRequest(method, URI.create("https://services-beta.enterprisecloud.terremark.com/cloudapi/tasks/1"));
assertRequestLineEquals(httpRequest, "GET http://209.251.187.125/livespec/tasks/1 HTTP/1.1"); assertRequestLineEquals(httpRequest, "GET https://services-beta.enterprisecloud.terremark.com/cloudapi/tasks/1 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/vnd.tmrk.cloud.task\nx-trmk-version: 2011-07-01\n"); assertNonPayloadHeadersEqual(httpRequest, "Accept: application/vnd.tmrk.cloud.task\nx-trmk-version: 2011-07-01\n");
assertPayloadEquals(httpRequest, null, null, false); assertPayloadEquals(httpRequest, null, null, false);