Issue 695: Removed addAction method, and created Builder and added TestActions class

This commit is contained in:
Jason King 2011-11-21 13:44:23 +00:00 committed by danikov
parent 76cecee53a
commit d8156f6717
4 changed files with 120 additions and 27 deletions

View File

@ -22,7 +22,6 @@ import com.google.common.collect.Sets;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -34,37 +33,74 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/ */
public class Actions { public class Actions {
@XmlElement(name = "Action") @SuppressWarnings("unchecked")
private LinkedHashSet<Action> actions = Sets.newLinkedHashSet(); public static Builder builder() {
return new Builder();
}
void addAction(Action action) { public Builder toBuilder() {
checkNotNull(action,"action"); return new Builder().fromActions(this);
this.actions.add(action); }
}
public Set<Action> getActions() { public static class Builder {
return Collections.unmodifiableSet(actions);
}
@Override private Set<Action> actions = Sets.newLinkedHashSet();
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Actions actions1 = (Actions) o; /**
* @see Actions#getActions
*/
public Builder actions(Set<Action> actions) {
this.actions = Sets.newLinkedHashSet(checkNotNull(actions, "actions"));
return this;
}
if (!actions.equals(actions1.actions)) return false; public Builder addAction(Action action) {
actions.add(checkNotNull(action,"action"));
return this;
}
return true; public Actions build() {
} return new Actions(actions);
}
@Override public Builder fromActions(Actions in) {
public int hashCode() { return actions(in.getActions());
return actions.hashCode(); }
} }
public String toString() { public Actions() {
return "["+ actions.toString()+"]"; //For JAXB and builder use
} }
private Actions(Set<Action> actions) {
this.actions = Sets.newLinkedHashSet(actions);
}
@XmlElement(name = "Action")
private Set<Action> actions = Sets.newLinkedHashSet();
public Set<Action> getActions() {
return Collections.unmodifiableSet(actions);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Actions actions1 = (Actions) o;
if (!actions.equals(actions1.actions)) return false;
return true;
}
@Override
public int hashCode() {
return actions.hashCode();
}
public String toString() {
return "["+ actions.toString()+"]";
}
} }

View File

@ -162,7 +162,7 @@ public class HardwareConfiguration extends BaseResource<HardwareConfiguration> {
private Nics virtualNics = new Nics(); private Nics virtualNics = new Nics();
public HardwareConfiguration(Set<Action> actions, int processorCount, @Nullable Memory memory, Set<VirtualDisk> virtualDisks, Set<VirtualNic> virtualNics) { public HardwareConfiguration(Set<Action> actions, int processorCount, @Nullable Memory memory, Set<VirtualDisk> virtualDisks, Set<VirtualNic> virtualNics) {
for( Action action: checkNotNull(actions, "actions")) this.actions.addAction(action); this.actions = Actions.builder().actions(checkNotNull(actions, "actions")).build();
for( VirtualDisk disk: checkNotNull(virtualDisks, "virtualDisks")) this.virtualDisks.setVirtualDisk(disk); for( VirtualDisk disk: checkNotNull(virtualDisks, "virtualDisks")) this.virtualDisks.setVirtualDisk(disk);
for( VirtualNic virtualNic: checkNotNull(virtualNics, "virtualNics")) this.virtualNics.setVirtualNic(virtualNic); for( VirtualNic virtualNic: checkNotNull(virtualNics, "virtualNics")) this.virtualNics.setVirtualNic(virtualNic);

View File

@ -87,7 +87,7 @@ public class VirtualMachine extends BaseNamedResource<VirtualMachine> {
*/ */
public Builder actions(Set<Action> actions) { public Builder actions(Set<Action> actions) {
checkNotNull(actions,"actions"); checkNotNull(actions,"actions");
for(Action action:actions) this.actions.addAction(action); this.actions = Actions.builder().actions(actions).build();
return this; return this;
} }

View File

@ -0,0 +1,57 @@
/**
* 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.tmrk.enterprisecloud.domain;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Set;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
/**
* @author Jason King
*/
@Test(groups = "unit", testName = "ActionsTest")
public class ActionsTest {
private Action action;
private Actions actions;
@BeforeMethod()
public void setUp() throws URISyntaxException {
action = Action.builder().href(new URI("/1")).name("my action").type("test action").build();
actions = Actions.builder().addAction(action).build();
}
@Test
public void testAddAction() throws URISyntaxException {
Action action2 = Action.builder().href(new URI("/2")).name("my action 2").type("test action 2").build();
Actions twoActions = actions.toBuilder().addAction(action2).build();
Set<Action> actionSet = twoActions.getActions();
assertEquals(2,actionSet.size());
assertTrue(actionSet.contains(action));
assertTrue(actionSet.contains(action2));
}
}