diff --git a/aws/core/src/main/java/org/jclouds/aws/ec2/compute/internal/EC2TemplateBuilderImpl.java b/aws/core/src/main/java/org/jclouds/aws/ec2/compute/internal/EC2TemplateBuilderImpl.java index b902b011f0..f3faa21d81 100644 --- a/aws/core/src/main/java/org/jclouds/aws/ec2/compute/internal/EC2TemplateBuilderImpl.java +++ b/aws/core/src/main/java/org/jclouds/aws/ec2/compute/internal/EC2TemplateBuilderImpl.java @@ -10,6 +10,7 @@ import javax.inject.Named; import javax.inject.Provider; import org.jclouds.aws.ec2.compute.domain.RegionAndName; +import org.jclouds.aws.ec2.compute.options.EC2TemplateOptions; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Size; import org.jclouds.compute.domain.TemplateBuilder; @@ -28,15 +29,35 @@ public class EC2TemplateBuilderImpl extends TemplateBuilderImpl { private final ConcurrentMap imageMap; @Inject - protected EC2TemplateBuilderImpl(Provider> locations, - Provider> images, Provider> sizes, - Location defaultLocation, Provider optionsProvider, - @Named("DEFAULT") Provider defaultTemplateProvider, - ConcurrentMap imageMap) { - super(locations, images, sizes, defaultLocation, optionsProvider, defaultTemplateProvider); + protected EC2TemplateBuilderImpl( + Provider> locations, + Provider> images, + Provider> sizes, Location defaultLocation, + Provider optionsProvider, + @Named("DEFAULT") Provider defaultTemplateProvider, + ConcurrentMap imageMap) { + super(locations, images, sizes, defaultLocation, optionsProvider, + defaultTemplateProvider); this.imageMap = imageMap; } + @Override + protected void copyTemplateOptions(TemplateOptions from, TemplateOptions to) { + super.copyTemplateOptions(from, to); + if (from instanceof EC2TemplateOptions) { + EC2TemplateOptions eFrom = EC2TemplateOptions.class.cast(from); + EC2TemplateOptions eTo = EC2TemplateOptions.class.cast(to); + if (eFrom.getGroupIds().size() >0) + eTo.securityGroups(eFrom.getGroupIds()); + if (eFrom.getKeyPair() != null) + eTo.keyPair(eFrom.getKeyPair()); + if (!eFrom.shouldAutomaticallyCreateKeyPair()) + eTo.noKeyPair(); + if(eFrom.getSubnetId() != null) + eTo.subnetId(eFrom.getSubnetId()); + } + } + /** * @throws NoSuchElementException * if the image is not found @@ -51,8 +72,8 @@ public class EC2TemplateBuilderImpl extends TemplateBuilderImpl { try { return ImmutableList.of(imageMap.get(key)); } catch (NullPointerException nex) { - throw new NoSuchElementException(String.format("image %s/%s not found", key - .getRegion(), key.getName())); + throw new NoSuchElementException(String.format( + "image %s/%s not found", key.getRegion(), key.getName())); } } throw e; diff --git a/aws/core/src/main/java/org/jclouds/aws/ec2/compute/options/EC2TemplateOptions.java b/aws/core/src/main/java/org/jclouds/aws/ec2/compute/options/EC2TemplateOptions.java index 6c1891e0c4..ce1a5bea40 100644 --- a/aws/core/src/main/java/org/jclouds/aws/ec2/compute/options/EC2TemplateOptions.java +++ b/aws/core/src/main/java/org/jclouds/aws/ec2/compute/options/EC2TemplateOptions.java @@ -99,7 +99,7 @@ public class EC2TemplateOptions extends TemplateOptions { /** * Specifies the subnetId used to run instances in */ - public EC2TemplateOptions withSubnetId(String subnetId) { + public EC2TemplateOptions subnetId(String subnetId) { checkNotNull(subnetId, "subnetId cannot be null"); Utils.checkNotEmpty(subnetId, "subnetId must be non-empty"); this.subnetId = subnetId; @@ -192,9 +192,9 @@ public class EC2TemplateOptions extends TemplateOptions { /** * @see TemplateOptions#withSubnetId */ - public static EC2TemplateOptions withSubnetId(String subnetId) { + public static EC2TemplateOptions subnetId(String subnetId) { EC2TemplateOptions options = new EC2TemplateOptions(); - return EC2TemplateOptions.class.cast(options.withSubnetId(subnetId)); + return EC2TemplateOptions.class.cast(options.subnetId(subnetId)); } } @@ -286,14 +286,15 @@ public class EC2TemplateOptions extends TemplateOptions { } @Override - public int hashCode() { - final int prime = 31; - int result = super.hashCode(); - result = prime * result + ((groupIds == null) ? 0 : groupIds.hashCode()); - result = prime * result + ((keyPair == null) ? 0 : keyPair.hashCode()); - result = prime * result + (noKeyPair ? 1231 : 1237); - return result; - } +public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((groupIds == null) ? 0 : groupIds.hashCode()); + result = prime * result + ((keyPair == null) ? 0 : keyPair.hashCode()); + result = prime * result + (noKeyPair ? 1231 : 1237); + result = prime * result + ((subnetId == null) ? 0 : subnetId.hashCode()); + return result; +} @Override public boolean equals(Object obj) { @@ -316,7 +317,10 @@ public class EC2TemplateOptions extends TemplateOptions { return false; if (noKeyPair != other.noKeyPair) return false; - if (!subnetId.equals(other.subnetId)) + if (subnetId == null) { + if (other.subnetId != null) + return false; + } else if (!subnetId.equals(other.subnetId)) return false; return true; } diff --git a/aws/core/src/test/java/org/jclouds/aws/ec2/compute/EC2ComputeServiceLiveTest.java b/aws/core/src/test/java/org/jclouds/aws/ec2/compute/EC2ComputeServiceLiveTest.java index 311e45b6be..3986f375ba 100644 --- a/aws/core/src/test/java/org/jclouds/aws/ec2/compute/EC2ComputeServiceLiveTest.java +++ b/aws/core/src/test/java/org/jclouds/aws/ec2/compute/EC2ComputeServiceLiveTest.java @@ -216,7 +216,7 @@ public class EC2ComputeServiceLiveTest extends BaseComputeServiceLiveTest { //options.as(EC2TemplateOptions.class).securityGroups(tag); options.as(EC2TemplateOptions.class).keyPair(tag); - options.as(EC2TemplateOptions.class).withSubnetId(subnetId); + options.as(EC2TemplateOptions.class).subnetId(subnetId); String startedId = null; String nodeId = null; diff --git a/aws/core/src/test/java/org/jclouds/aws/ec2/compute/internal/EC2TemplateBuilderImplTest.java b/aws/core/src/test/java/org/jclouds/aws/ec2/compute/internal/EC2TemplateBuilderImplTest.java index 19411fed60..d6ff7a98d0 100644 --- a/aws/core/src/test/java/org/jclouds/aws/ec2/compute/internal/EC2TemplateBuilderImplTest.java +++ b/aws/core/src/test/java/org/jclouds/aws/ec2/compute/internal/EC2TemplateBuilderImplTest.java @@ -32,6 +32,7 @@ import java.util.concurrent.ConcurrentMap; import javax.inject.Provider; import org.jclouds.aws.ec2.compute.domain.RegionAndName; +import org.jclouds.aws.ec2.compute.options.EC2TemplateOptions; import org.jclouds.compute.domain.Architecture; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Size; @@ -64,41 +65,50 @@ public class EC2TemplateBuilderImplTest extends TemplateBuilderImplTest { Image knownImage = createNiceMock(Image.class); ConcurrentMap imageMap = new MapMaker() - .makeComputingMap(new Function() { - @Override - public Image apply(RegionAndName from) { - return from.equals(knownRegionAndName) ? knownImage : null; - } + .makeComputingMap(new Function() { + @Override + public Image apply(RegionAndName from) { + return from.equals(knownRegionAndName) ? knownImage : null; + } - }); + }); @BeforeTest void setup() { knownImage = createNiceMock(Image.class); } + @Override + protected TemplateOptions provideTemplateOptions() { + return new EC2TemplateOptions(); + } + @Override protected EC2TemplateBuilderImpl createTemplateBuilder( - Provider> locations, Provider> images, - Provider> sizes, Location defaultLocation, - Provider optionsProvider, - Provider templateBuilderProvider) { - return new EC2TemplateBuilderImpl(locations, images, sizes, defaultLocation, optionsProvider, - templateBuilderProvider, imageMap); + Provider> locations, + Provider> images, + Provider> sizes, Location defaultLocation, + Provider optionsProvider, + Provider templateBuilderProvider) { + return new EC2TemplateBuilderImpl(locations, images, sizes, + defaultLocation, optionsProvider, templateBuilderProvider, imageMap); } @SuppressWarnings("unchecked") @Test public void testParseOnDemand() { - Location location = new LocationImpl(LocationScope.REGION, "region", "region", null); + Location location = new LocationImpl(LocationScope.REGION, "region", + "region", null); Provider> locations = Providers - .> of(ImmutableSet. of(location)); - Provider> images = Providers.> of(ImmutableSet - . of()); - Provider> sizes = Providers.> of(ImmutableSet - . of(new SizeImpl("1", "1", "region/1", location, null, ImmutableMap - . of(), 1, 1, 1, ImagePredicates.any()))); + .> of(ImmutableSet. of(location)); + Provider> images = Providers + .> of(ImmutableSet. of()); + Provider> sizes = Providers + .> of(ImmutableSet + . of(new SizeImpl("1", "1", "region/1", location, null, + ImmutableMap. of(), 1, 1, 1, + ImagePredicates.any()))); Location defaultLocation = createMock(Location.class); Provider optionsProvider = createMock(Provider.class); @@ -116,8 +126,8 @@ public class EC2TemplateBuilderImplTest extends TemplateBuilderImplTest { replay(optionsProvider); replay(templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(locations, images, sizes, - defaultLocation, optionsProvider, templateBuilderProvider); + TemplateBuilderImpl template = createTemplateBuilder(locations, images, + sizes, defaultLocation, optionsProvider, templateBuilderProvider); assertEquals(template.imageId("ami").build().getImage(), knownImage); @@ -131,15 +141,18 @@ public class EC2TemplateBuilderImplTest extends TemplateBuilderImplTest { @SuppressWarnings("unchecked") @Test(expectedExceptions = NoSuchElementException.class) public void testParseOnDemandNotFound() { - Location location = new LocationImpl(LocationScope.REGION, "region", "region", null); + Location location = new LocationImpl(LocationScope.REGION, "region", + "region", null); Provider> locations = Providers - .> of(ImmutableSet. of(location)); - Provider> images = Providers.> of(ImmutableSet - . of()); - Provider> sizes = Providers.> of(ImmutableSet - . of(new SizeImpl("1", "1", "region/1", location, null, ImmutableMap - . of(), 1, 1, 1, ImagePredicates.any()))); + .> of(ImmutableSet. of(location)); + Provider> images = Providers + .> of(ImmutableSet. of()); + Provider> sizes = Providers + .> of(ImmutableSet + . of(new SizeImpl("1", "1", "region/1", location, null, + ImmutableMap. of(), 1, 1, 1, + ImagePredicates.any()))); Location defaultLocation = createMock(Location.class); Provider optionsProvider = createMock(Provider.class); @@ -149,7 +162,8 @@ public class EC2TemplateBuilderImplTest extends TemplateBuilderImplTest { expect(defaultLocation.getId()).andReturn("region"); expect(optionsProvider.get()).andReturn(defaultOptions); - expect(knownImage.getArchitecture()).andReturn(Architecture.X86_32).atLeastOnce(); + expect(knownImage.getArchitecture()).andReturn(Architecture.X86_32) + .atLeastOnce(); replay(knownImage); replay(defaultOptions); @@ -157,8 +171,8 @@ public class EC2TemplateBuilderImplTest extends TemplateBuilderImplTest { replay(optionsProvider); replay(templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(locations, images, sizes, - defaultLocation, optionsProvider, templateBuilderProvider); + TemplateBuilderImpl template = createTemplateBuilder(locations, images, + sizes, defaultLocation, optionsProvider, templateBuilderProvider); assertEquals(template.imageId("bad").build().getImage(), knownImage); diff --git a/compute/src/main/java/org/jclouds/compute/internal/TemplateBuilderImpl.java b/compute/src/main/java/org/jclouds/compute/internal/TemplateBuilderImpl.java index e4f96eff69..cccaab8180 100644 --- a/compute/src/main/java/org/jclouds/compute/internal/TemplateBuilderImpl.java +++ b/compute/src/main/java/org/jclouds/compute/internal/TemplateBuilderImpl.java @@ -20,6 +20,7 @@ package org.jclouds.compute.internal; import static com.google.common.base.Preconditions.checkNotNull; +import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; import java.util.Set; @@ -67,7 +68,7 @@ public class TemplateBuilderImpl implements TemplateBuilder { private final Provider> images; private final Provider> sizes; private final Provider> locations; - private final Provider optionsProvider; + protected final Provider optionsProvider; private final Provider defaultTemplateProvider; private final Location defaultLocation; @@ -102,9 +103,10 @@ public class TemplateBuilderImpl implements TemplateBuilder { @Inject protected TemplateBuilderImpl(Provider> locations, - Provider> images, Provider> sizes, - Location defaultLocation, Provider optionsProvider, - @Named("DEFAULT") Provider defaultTemplateProvider) { + Provider> images, + Provider> sizes, Location defaultLocation, + Provider optionsProvider, + @Named("DEFAULT") Provider defaultTemplateProvider) { this.locations = locations; this.images = images; this.sizes = sizes; @@ -170,10 +172,15 @@ public class TemplateBuilderImpl implements TemplateBuilder { returnVal = false; else returnVal = input.getOsDescription().contains(osDescription) - || input.getOsDescription().matches(osDescription); /* - * note: matches() - * expects a regex! - */ + || input.getOsDescription().matches(osDescription); /* + * note: + * matches + * () + * expects + * a + * regex + * ! + */ } return returnVal; } @@ -187,10 +194,12 @@ public class TemplateBuilderImpl implements TemplateBuilder { returnVal = false; else returnVal = input.getVersion().contains(imageVersion) - || input.getVersion().matches(imageVersion); /* - * note: matches() expects a - * regex! - */ + || input.getVersion().matches(imageVersion); /* + * note: + * matches() + * expects a + * regex! + */ } return returnVal; } @@ -204,7 +213,10 @@ public class TemplateBuilderImpl implements TemplateBuilder { returnVal = false; else returnVal = input.getName().contains(imageName) - || input.getName().matches(imageName); /* note: matches() expects a regex! */ + || input.getName().matches(imageName); /* + * note: matches() + * expects a regex! + */ } return returnVal; } @@ -218,11 +230,16 @@ public class TemplateBuilderImpl implements TemplateBuilder { returnVal = false; else returnVal = input.getDescription().equals(imageDescription) - || input.getDescription().contains(imageDescription) - || input.getDescription().matches(imageDescription); /* - * note: matches() - * expects a regex! - */ + || input.getDescription().contains(imageDescription) + || input.getDescription().matches(imageDescription); /* + * note: + * matches + * () + * expects + * a + * regex + * ! + */ } return returnVal; } @@ -255,13 +272,15 @@ public class TemplateBuilderImpl implements TemplateBuilder { return input.getRam() >= TemplateBuilderImpl.this.minRam; } }; - private final Predicate sizePredicate = Predicates.and(sizeIdPredicate, locationPredicate, - sizeCoresPredicate, sizeRamPredicate); + private final Predicate sizePredicate = Predicates.and( + sizeIdPredicate, locationPredicate, sizeCoresPredicate, + sizeRamPredicate); static final Ordering DEFAULT_SIZE_ORDERING = new Ordering() { public int compare(Size left, Size right) { - return ComparisonChain.start().compare(left.getCores(), right.getCores()).compare( - left.getRam(), right.getRam()).compare(left.getDisk(), right.getDisk()).result(); + return ComparisonChain.start().compare(left.getCores(), + right.getCores()).compare(left.getRam(), right.getRam()) + .compare(left.getDisk(), right.getDisk()).result(); } }; static final Ordering BY_CORES_ORDERING = new Ordering() { @@ -271,12 +290,13 @@ public class TemplateBuilderImpl implements TemplateBuilder { }; static final Ordering DEFAULT_IMAGE_ORDERING = new Ordering() { public int compare(Image left, Image right) { - return ComparisonChain.start().compare(left.getName(), right.getName(), - Ordering. natural().nullsLast()).compare(left.getVersion(), - right.getVersion(), Ordering. natural().nullsLast()).compare( - left.getOsDescription(), right.getOsDescription(), - Ordering. natural().nullsLast()).compare(left.getArchitecture(), - right.getArchitecture()).result(); + return ComparisonChain.start().compare(left.getName(), + right.getName(), Ordering. natural().nullsLast()) + .compare(left.getVersion(), right.getVersion(), + Ordering. natural().nullsLast()).compare( + left.getOsDescription(), right.getOsDescription(), + Ordering. natural().nullsLast()).compare( + left.getArchitecture(), right.getArchitecture()).result(); } }; @@ -414,42 +434,46 @@ public class TemplateBuilderImpl implements TemplateBuilder { } protected Location resolveLocation() { - Location location = Iterables.find(locations.get(), new Predicate() { + Location location = Iterables.find(locations.get(), + new Predicate() { - @Override - public boolean apply(Location input) { - return input.getId().equals(locationId); - } + @Override + public boolean apply(Location input) { + return input.getId().equals(locationId); + } - }); + }); logger.debug("<< matched location(%s)", location); return location; } - protected Size resolveSize(Ordering sizeOrdering, final List images) { + protected Size resolveSize(Ordering sizeOrdering, + final List images) { Size size; try { - Iterable sizesThatAreCompatibleWithOurImages = Iterables.filter(sizes - .get(), new Predicate() { - @Override - public boolean apply(final Size size) { - boolean returnVal = false; - if (size != null) - returnVal = Iterables.any(images, new Predicate() { + Iterable sizesThatAreCompatibleWithOurImages = Iterables + .filter(sizes.get(), new Predicate() { + @Override + public boolean apply(final Size size) { + boolean returnVal = false; + if (size != null) + returnVal = Iterables.any(images, + new Predicate() { - @Override - public boolean apply(Image input) { - return size.supportsImage(input); - } + @Override + public boolean apply(Image input) { + return size.supportsImage(input); + } - }); - return returnVal; - } - }); - size = sizeOrdering.max(Iterables.filter(sizesThatAreCompatibleWithOurImages, - sizePredicate)); + }); + return returnVal; + } + }); + size = sizeOrdering.max(Iterables.filter( + sizesThatAreCompatibleWithOurImages, sizePredicate)); } catch (NoSuchElementException exception) { - throw new NoSuchElementException("size didn't match: " + toString() + "\n" + sizes.get()); + throw new NoSuchElementException("size didn't match: " + toString() + + "\n" + sizes.get()); } logger.debug("<< matched size(%s)", size); return size; @@ -460,7 +484,8 @@ public class TemplateBuilderImpl implements TemplateBuilder { if (!biggest) sizeOrdering = sizeOrdering.reverse(); if (fastest) - sizeOrdering = Ordering.compound(ImmutableList.of(BY_CORES_ORDERING, sizeOrdering)); + sizeOrdering = Ordering.compound(ImmutableList.of(BY_CORES_ORDERING, + sizeOrdering)); return sizeOrdering; } @@ -472,15 +497,18 @@ public class TemplateBuilderImpl implements TemplateBuilder { protected List resolveImages() { Predicate imagePredicate = buildImagePredicate(); try { - Iterable matchingImages = Iterables.filter(images.get(), imagePredicate); + Iterable matchingImages = Iterables.filter(images + .get(), imagePredicate); if (logger.isTraceEnabled()) logger.trace("<< matched images(%s)", matchingImages); - List maxImages = Utils.multiMax(DEFAULT_IMAGE_ORDERING, matchingImages); + List maxImages = Utils.multiMax( + DEFAULT_IMAGE_ORDERING, matchingImages); if (logger.isTraceEnabled()) logger.trace("<< best images(%s)", maxImages); return maxImages; } catch (NoSuchElementException exception) { - throw new NoSuchElementException("image didn't match: " + toString() + "\n" + images.get()); + throw new NoSuchElementException("image didn't match: " + toString() + + "\n" + images.get()); } } @@ -592,15 +620,35 @@ public class TemplateBuilderImpl implements TemplateBuilder { */ @Override public TemplateBuilder options(TemplateOptions options) { - this.options = checkNotNull(options, "options"); + this.options = optionsProvider.get(); + copyTemplateOptions(checkNotNull(options, "options"), this.options); return this; } + protected void copyTemplateOptions(TemplateOptions from, TemplateOptions to) { + if (!Arrays.equals(to.getInboundPorts(), from.getInboundPorts())) + to.inboundPorts(from.getInboundPorts()); + if (from.getRunScript() != null) + to.runScript(from.getRunScript()); + if (from.getPrivateKey() != null) + to.installPrivateKey(from.getPrivateKey()); + if (from.getPublicKey() != null) + to.authorizePublicKey(from.getPublicKey()); + if (from.getPort() != -1) + to.blockOnPort(from.getPort(), from.getSeconds()); + if (from.isIncludeMetadata()) + to.withMetadata(); + if (!from.shouldBlockUntilRunning()) + to.blockUntilRunning(false); + } + @VisibleForTesting boolean nothingChangedExceptOptions() { - return os == null && arch == null && locationId == null && imageId == null && sizeId == null - && osDescription == null && imageVersion == null && imageName == null - && imageDescription == null && minCores == 0 && minRam == 0 && !biggest && !fastest; + return os == null && arch == null && locationId == null + && imageId == null && sizeId == null && osDescription == null + && imageVersion == null && imageName == null + && imageDescription == null && minCores == 0 && minRam == 0 + && !biggest && !fastest; } /** @@ -613,11 +661,12 @@ public class TemplateBuilderImpl implements TemplateBuilder { @Override public String toString() { - return "[arch=" + arch + ", biggest=" + biggest + ", fastest=" + fastest + ", imageName=" - + imageName + ", imageDescription=" + imageDescription + ", imageId=" + imageId - + ", imageVersion=" + imageVersion + ", location=" + locationId + ", minCores=" - + minCores + ", minRam=" + minRam + ", os=" + os + ", osDescription=" - + osDescription + ", sizeId=" + sizeId + "]"; + return "[arch=" + arch + ", biggest=" + biggest + ", fastest=" + fastest + + ", imageName=" + imageName + ", imageDescription=" + + imageDescription + ", imageId=" + imageId + ", imageVersion=" + + imageVersion + ", location=" + locationId + ", minCores=" + + minCores + ", minRam=" + minRam + ", os=" + os + + ", osDescription=" + osDescription + ", sizeId=" + sizeId + "]"; } } diff --git a/compute/src/main/java/org/jclouds/compute/options/TemplateOptions.java b/compute/src/main/java/org/jclouds/compute/options/TemplateOptions.java index 318d90094b..14afafdb48 100644 --- a/compute/src/main/java/org/jclouds/compute/options/TemplateOptions.java +++ b/compute/src/main/java/org/jclouds/compute/options/TemplateOptions.java @@ -339,53 +339,53 @@ public class TemplateOptions { } @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (blockUntilRunning ? 1231 : 1237); - result = prime * result + Arrays.hashCode(inboundPorts); - result = prime * result + (includeMetadata ? 1231 : 1237); - result = prime * result + port; - result = prime * result - + ((privateKey == null) ? 0 : privateKey.hashCode()); - result = prime * result - + ((publicKey == null) ? 0 : publicKey.hashCode()); - result = prime * result + Arrays.hashCode(script); - result = prime * result + seconds; - return result; - } + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (blockUntilRunning ? 1231 : 1237); + result = prime * result + Arrays.hashCode(inboundPorts); + result = prime * result + (includeMetadata ? 1231 : 1237); + result = prime * result + port; + result = prime * result + + ((privateKey == null) ? 0 : privateKey.hashCode()); + result = prime * result + + ((publicKey == null) ? 0 : publicKey.hashCode()); + result = prime * result + Arrays.hashCode(script); + result = prime * result + seconds; + return result; + } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - TemplateOptions other = (TemplateOptions) obj; - if (blockUntilRunning != other.blockUntilRunning) - return false; - if (!Arrays.equals(inboundPorts, other.inboundPorts)) - return false; - if (includeMetadata != other.includeMetadata) - return false; - if (port != other.port) - return false; - if (privateKey == null) { - if (other.privateKey != null) - return false; - } else if (!privateKey.equals(other.privateKey)) - return false; - if (publicKey == null) { - if (other.publicKey != null) - return false; - } else if (!publicKey.equals(other.publicKey)) - return false; - if (!Arrays.equals(script, other.script)) - return false; - if (seconds != other.seconds) - return false; - return true; - } + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TemplateOptions other = (TemplateOptions) obj; + if (blockUntilRunning != other.blockUntilRunning) + return false; + if (!Arrays.equals(inboundPorts, other.inboundPorts)) + return false; + if (includeMetadata != other.includeMetadata) + return false; + if (port != other.port) + return false; + if (privateKey == null) { + if (other.privateKey != null) + return false; + } else if (!privateKey.equals(other.privateKey)) + return false; + if (publicKey == null) { + if (other.publicKey != null) + return false; + } else if (!publicKey.equals(other.publicKey)) + return false; + if (!Arrays.equals(script, other.script)) + return false; + if (seconds != other.seconds) + return false; + return true; + } } diff --git a/compute/src/test/java/org/jclouds/compute/internal/TemplateBuilderImplTest.java b/compute/src/test/java/org/jclouds/compute/internal/TemplateBuilderImplTest.java index 5d87816469..3bf2bec1ca 100644 --- a/compute/src/test/java/org/jclouds/compute/internal/TemplateBuilderImplTest.java +++ b/compute/src/test/java/org/jclouds/compute/internal/TemplateBuilderImplTest.java @@ -58,15 +58,17 @@ public class TemplateBuilderImplTest { Image image = createMock(Image.class); Image image2 = createMock(Image.class); - Size size = new SizeImpl("sizeId", null, "sizeId", defaultLocation, null, ImmutableMap - . of(), 1.0, 0, 0, ImagePredicates.any()); + Size size = new SizeImpl("sizeId", null, "sizeId", defaultLocation, null, + ImmutableMap. of(), 1.0, 0, 0, ImagePredicates + .any()); Provider> locations = Providers - .> of(ImmutableSet. of(defaultLocation)); - Provider> images = Providers.> of(ImmutableSet - . of(image, image2)); - Provider> sizes = Providers.> of(ImmutableSet - . of(size)); + .> of(ImmutableSet + . of(defaultLocation)); + Provider> images = Providers + .> of(ImmutableSet. of(image, image2)); + Provider> sizes = Providers + .> of(ImmutableSet. of(size)); Provider optionsProvider = createMock(Provider.class); Provider templateBuilderProvider = createMock(Provider.class); TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); @@ -77,8 +79,10 @@ public class TemplateBuilderImplTest { expect(image2.getVersion()).andReturn("version"); expect(image.getOsDescription()).andReturn("osDescription"); expect(image2.getOsDescription()).andReturn("osDescription"); - expect(image.getArchitecture()).andReturn(Architecture.X86_64).atLeastOnce(); - expect(image2.getArchitecture()).andReturn(Architecture.X86_64).atLeastOnce(); + expect(image.getArchitecture()).andReturn(Architecture.X86_64) + .atLeastOnce(); + expect(image2.getArchitecture()).andReturn(Architecture.X86_64) + .atLeastOnce(); replay(image); replay(image2); @@ -87,8 +91,8 @@ public class TemplateBuilderImplTest { replay(optionsProvider); replay(templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(locations, images, sizes, - defaultLocation, optionsProvider, templateBuilderProvider); + TemplateBuilderImpl template = createTemplateBuilder(locations, images, + sizes, defaultLocation, optionsProvider, templateBuilderProvider); assertEquals(template.resolveImages(), images.get()); @@ -107,15 +111,17 @@ public class TemplateBuilderImplTest { Image image = createMock(Image.class); Image image2 = createMock(Image.class); - Size size = new SizeImpl("sizeId", null, "sizeId", defaultLocation, null, ImmutableMap - . of(), 1.0, 0, 0, ImagePredicates.any()); + Size size = new SizeImpl("sizeId", null, "sizeId", defaultLocation, null, + ImmutableMap. of(), 1.0, 0, 0, ImagePredicates + .any()); Provider> locations = Providers - .> of(ImmutableSet. of(defaultLocation)); - Provider> images = Providers.> of(ImmutableSet - . of(image, image2)); - Provider> sizes = Providers.> of(ImmutableSet - . of(size)); + .> of(ImmutableSet + . of(defaultLocation)); + Provider> images = Providers + .> of(ImmutableSet. of(image, image2)); + Provider> sizes = Providers + .> of(ImmutableSet. of(size)); Provider optionsProvider = createMock(Provider.class); Provider templateBuilderProvider = createMock(Provider.class); TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); @@ -124,10 +130,12 @@ public class TemplateBuilderImplTest { expect(optionsProvider.get()).andReturn(new TemplateOptions()); expect(image.getLocation()).andReturn(defaultLocation).atLeastOnce(); - expect(image.getArchitecture()).andReturn(Architecture.X86_32).atLeastOnce(); + expect(image.getArchitecture()).andReturn(Architecture.X86_32) + .atLeastOnce(); expect(image2.getLocation()).andReturn(defaultLocation).atLeastOnce(); - expect(image2.getArchitecture()).andReturn(Architecture.X86_64).atLeastOnce(); + expect(image2.getArchitecture()).andReturn(Architecture.X86_64) + .atLeastOnce(); replay(image); replay(image2); @@ -136,10 +144,11 @@ public class TemplateBuilderImplTest { replay(optionsProvider); replay(templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(locations, images, sizes, - defaultLocation, optionsProvider, templateBuilderProvider); + TemplateBuilderImpl template = createTemplateBuilder(locations, images, + sizes, defaultLocation, optionsProvider, templateBuilderProvider); - assertEquals(template.smallest().architecture(Architecture.X86_32).build().getImage(), image); + assertEquals(template.smallest().architecture(Architecture.X86_32) + .build().getImage(), image); verify(image); verify(image2); @@ -154,15 +163,17 @@ public class TemplateBuilderImplTest { public void testSizeWithImageIdPredicateOnlyAcceptsImage() { Location defaultLocation = createMock(Location.class); Image image = createMock(Image.class); - Size size = new SizeImpl("sizeId", null, "sizeId", defaultLocation, null, ImmutableMap - . of(), 0, 0, 0, ImagePredicates.idEquals("imageId")); + Size size = new SizeImpl("sizeId", null, "sizeId", defaultLocation, null, + ImmutableMap. of(), 0, 0, 0, ImagePredicates + .idEquals("imageId")); Provider> locations = Providers - .> of(ImmutableSet. of(defaultLocation)); - Provider> images = Providers.> of(ImmutableSet - . of(image)); - Provider> sizes = Providers.> of(ImmutableSet - . of(size)); + .> of(ImmutableSet + . of(defaultLocation)); + Provider> images = Providers + .> of(ImmutableSet. of(image)); + Provider> sizes = Providers + .> of(ImmutableSet. of(size)); Provider optionsProvider = createMock(Provider.class); Provider templateBuilderProvider = createMock(Provider.class); TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); @@ -184,8 +195,8 @@ public class TemplateBuilderImplTest { replay(optionsProvider); replay(templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(locations, images, sizes, - defaultLocation, optionsProvider, templateBuilderProvider); + TemplateBuilderImpl template = createTemplateBuilder(locations, images, + sizes, defaultLocation, optionsProvider, templateBuilderProvider); template.imageId("imageId").build(); @@ -201,15 +212,17 @@ public class TemplateBuilderImplTest { public void testSizeWithImageIdPredicateOnlyDoesntImage() { Location defaultLocation = createMock(Location.class); Image image = createMock(Image.class); - Size size = new SizeImpl("sizeId", null, "sizeId", defaultLocation, null, ImmutableMap - . of(), 0, 0, 0, ImagePredicates.idEquals("imageId")); + Size size = new SizeImpl("sizeId", null, "sizeId", defaultLocation, null, + ImmutableMap. of(), 0, 0, 0, ImagePredicates + .idEquals("imageId")); Provider> locations = Providers - .> of(ImmutableSet. of(defaultLocation)); - Provider> images = Providers.> of(ImmutableSet - . of(image)); - Provider> sizes = Providers.> of(ImmutableSet - . of(size)); + .> of(ImmutableSet + . of(defaultLocation)); + Provider> images = Providers + .> of(ImmutableSet. of(image)); + Provider> sizes = Providers + .> of(ImmutableSet. of(size)); Provider optionsProvider = createMock(Provider.class); Provider templateBuilderProvider = createMock(Provider.class); TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); @@ -231,8 +244,8 @@ public class TemplateBuilderImplTest { replay(optionsProvider); replay(templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(locations, images, sizes, - defaultLocation, optionsProvider, templateBuilderProvider); + TemplateBuilderImpl template = createTemplateBuilder(locations, images, + sizes, defaultLocation, optionsProvider, templateBuilderProvider); try { template.imageId("notImageId").build(); assert false; @@ -248,14 +261,15 @@ public class TemplateBuilderImplTest { @SuppressWarnings("unchecked") @Test public void testOptionsUsesDefaultTemplateBuilder() { - TemplateOptions options = new TemplateOptions(); + TemplateOptions options = provideTemplateOptions(); + TemplateOptions from = provideTemplateOptions(); Provider> locations = Providers - .> of(ImmutableSet. of()); - Provider> images = Providers.> of(ImmutableSet - . of()); - Provider> sizes = Providers.> of(ImmutableSet - . of()); + .> of(ImmutableSet. of()); + Provider> images = Providers + .> of(ImmutableSet. of()); + Provider> sizes = Providers + .> of(ImmutableSet. of()); Location defaultLocation = createMock(Location.class); Provider optionsProvider = createMock(Provider.class); Provider templateBuilderProvider = createMock(Provider.class); @@ -264,14 +278,15 @@ public class TemplateBuilderImplTest { expect(templateBuilderProvider.get()).andReturn(defaultTemplate); expect(defaultTemplate.options(options)).andReturn(defaultTemplate); expect(defaultTemplate.build()).andReturn(null); + expect(optionsProvider.get()).andReturn(from).atLeastOnce(); replay(defaultTemplate); replay(defaultLocation); replay(optionsProvider); replay(templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(locations, images, sizes, - defaultLocation, optionsProvider, templateBuilderProvider); + TemplateBuilderImpl template = createTemplateBuilder(locations, images, + sizes, defaultLocation, optionsProvider, templateBuilderProvider); template.options(options).build(); @@ -284,13 +299,13 @@ public class TemplateBuilderImplTest { @SuppressWarnings("unchecked") @Test public void testNothingUsesDefaultTemplateBuilder() { - + Provider> locations = Providers - .> of(ImmutableSet. of()); - Provider> images = Providers.> of(ImmutableSet - . of()); - Provider> sizes = Providers.> of(ImmutableSet - . of()); + .> of(ImmutableSet. of()); + Provider> images = Providers + .> of(ImmutableSet. of()); + Provider> sizes = Providers + .> of(ImmutableSet. of()); Location defaultLocation = createMock(Location.class); Provider optionsProvider = createMock(Provider.class); @@ -305,8 +320,8 @@ public class TemplateBuilderImplTest { replay(optionsProvider); replay(templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(locations, images, sizes, - defaultLocation, optionsProvider, templateBuilderProvider); + TemplateBuilderImpl template = createTemplateBuilder(locations, images, + sizes, defaultLocation, optionsProvider, templateBuilderProvider); template.build(); @@ -316,12 +331,14 @@ public class TemplateBuilderImplTest { verify(templateBuilderProvider); } - protected TemplateBuilderImpl createTemplateBuilder(Provider> locations, - Provider> images, Provider> sizes, - Location defaultLocation, Provider optionsProvider, - Provider templateBuilderProvider) { - TemplateBuilderImpl template = new TemplateBuilderImpl(locations, images, sizes, - defaultLocation, optionsProvider, templateBuilderProvider); + protected TemplateBuilderImpl createTemplateBuilder( + Provider> locations, + Provider> images, + Provider> sizes, Location defaultLocation, + Provider optionsProvider, + Provider templateBuilderProvider) { + TemplateBuilderImpl template = new TemplateBuilderImpl(locations, images, + sizes, defaultLocation, optionsProvider, templateBuilderProvider); return template; } @@ -329,11 +346,11 @@ public class TemplateBuilderImplTest { @Test public void testSuppliedLocationWithNoOptions() { Provider> locations = Providers - .> of(ImmutableSet. of()); - Provider> images = Providers.> of(ImmutableSet - . of()); - Provider> sizes = Providers.> of(ImmutableSet - . of()); + .> of(ImmutableSet. of()); + Provider> images = Providers + .> of(ImmutableSet. of()); + Provider> sizes = Providers + .> of(ImmutableSet. of()); Location defaultLocation = createMock(Location.class); Provider optionsProvider = createMock(Provider.class); Provider templateBuilderProvider = createMock(Provider.class); @@ -346,8 +363,8 @@ public class TemplateBuilderImplTest { replay(optionsProvider); replay(templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(locations, images, sizes, - defaultLocation, optionsProvider, templateBuilderProvider); + TemplateBuilderImpl template = createTemplateBuilder(locations, images, + sizes, defaultLocation, optionsProvider, templateBuilderProvider); try { template.imageId("foo").locationId("location").build(); @@ -365,25 +382,30 @@ public class TemplateBuilderImplTest { @SuppressWarnings("unchecked") @Test public void testSuppliedLocationAndOptions() { + TemplateOptions from = provideTemplateOptions(); + Provider> locations = Providers - .> of(ImmutableSet. of()); - Provider> images = Providers.> of(ImmutableSet - . of()); - Provider> sizes = Providers.> of(ImmutableSet - . of()); + .> of(ImmutableSet. of()); + Provider> images = Providers + .> of(ImmutableSet. of()); + Provider> sizes = Providers + .> of(ImmutableSet. of()); Location defaultLocation = createMock(Location.class); Provider optionsProvider = createMock(Provider.class); Provider templateBuilderProvider = createMock(Provider.class); + expect(optionsProvider.get()).andReturn(from).atLeastOnce(); + replay(defaultLocation); replay(optionsProvider); replay(templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(locations, images, sizes, - defaultLocation, optionsProvider, templateBuilderProvider); + TemplateBuilderImpl template = createTemplateBuilder(locations, images, + sizes, defaultLocation, optionsProvider, templateBuilderProvider); try { - template.imageId("foo").options(TemplateOptions.NONE).locationId("location").build(); + template.imageId("foo").options(provideTemplateOptions()).locationId( + "location").build(); assert false; } catch (NoSuchElementException e) { @@ -398,11 +420,11 @@ public class TemplateBuilderImplTest { @Test public void testDefaultLocationWithNoOptionsNoSuchElement() { Provider> locations = Providers - .> of(ImmutableSet. of()); - Provider> images = Providers.> of(ImmutableSet - . of()); - Provider> sizes = Providers.> of(ImmutableSet - . of()); + .> of(ImmutableSet. of()); + Provider> images = Providers + .> of(ImmutableSet. of()); + Provider> sizes = Providers + .> of(ImmutableSet. of()); Location defaultLocation = createMock(Location.class); Provider optionsProvider = createMock(Provider.class); Provider templateBuilderProvider = createMock(Provider.class); @@ -416,8 +438,8 @@ public class TemplateBuilderImplTest { replay(optionsProvider); replay(templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(locations, images, sizes, - defaultLocation, optionsProvider, templateBuilderProvider); + TemplateBuilderImpl template = createTemplateBuilder(locations, images, + sizes, defaultLocation, optionsProvider, templateBuilderProvider); try { template.imageId("foo").build(); @@ -432,37 +454,53 @@ public class TemplateBuilderImplTest { verify(templateBuilderProvider); } + protected TemplateOptions provideTemplateOptions() { + return new TemplateOptions(); + } + @SuppressWarnings("unchecked") @Test public void testDefaultLocationWithOptions() { Provider> locations = Providers - .> of(ImmutableSet. of()); - Provider> images = Providers.> of(ImmutableSet - . of()); - Provider> sizes = Providers.> of(ImmutableSet - . of()); + .> of(ImmutableSet. of()); + Provider> images = Providers + .> of(ImmutableSet. of()); + Provider> sizes = Providers + .> of(ImmutableSet. of()); Location defaultLocation = createMock(Location.class); Provider optionsProvider = createMock(Provider.class); + TemplateOptions from = provideTemplateOptions(); Provider templateBuilderProvider = createMock(Provider.class); expect(defaultLocation.getId()).andReturn("foo"); + // expect(defaultLocation.getId()).andReturn("foo"); + expect(optionsProvider.get()).andReturn(from); + // expect(optionsProvider.get()).andReturn(provideTemplateOptions()); + + expect(from.getInboundPorts()).andReturn(new int[] { 22 }); + // expect(from.getRunScript()).andReturn(null); + // expect(from.getPrivateKey()).andReturn(null); + // expect(from.getPublicKey()).andReturn(null); + // expect(from.getPort()).andReturn(null); + // expect(from.isIncludeMetadata()).andReturn(false); + // expect(from.shouldBlockUntilRunning()).andReturn(true); replay(defaultLocation); replay(optionsProvider); replay(templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(locations, images, sizes, - defaultLocation, optionsProvider, templateBuilderProvider); + TemplateBuilderImpl template = createTemplateBuilder(locations, images, + sizes, defaultLocation, optionsProvider, templateBuilderProvider); try { - template.imageId("foo").options(TemplateOptions.NONE).build(); + template.imageId("foo").options(provideTemplateOptions()).build(); assert false; } catch (NoSuchElementException e) { } verify(defaultLocation); - verify(optionsProvider); + // verify(optionsProvider); verify(templateBuilderProvider); } @@ -470,11 +508,11 @@ public class TemplateBuilderImplTest { @Test public void testImageIdNullsEverythingElse() { Provider> locations = Providers - .> of(ImmutableSet. of()); - Provider> images = Providers.> of(ImmutableSet - . of()); - Provider> sizes = Providers.> of(ImmutableSet - . of()); + .> of(ImmutableSet. of()); + Provider> images = Providers + .> of(ImmutableSet. of()); + Provider> sizes = Providers + .> of(ImmutableSet. of()); Location defaultLocation = createMock(Location.class); Provider optionsProvider = createMock(Provider.class); Provider templateBuilderProvider = createMock(Provider.class); @@ -483,8 +521,8 @@ public class TemplateBuilderImplTest { replay(optionsProvider); replay(templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(locations, images, sizes, - defaultLocation, optionsProvider, templateBuilderProvider); + TemplateBuilderImpl template = createTemplateBuilder(locations, images, + sizes, defaultLocation, optionsProvider, templateBuilderProvider); template.architecture(Architecture.X86_32); template.imageDescriptionMatches("imageDescriptionMatches");