From 45bb35971ea20d5d155e0b3161f19421fd56dc98 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sat, 12 Jan 2013 14:27:38 -0800 Subject: [PATCH] replaced usage of checkNotEmpty with checkNotNull(emptyToNull --- .../ec2/compute/EC2ComputeService.java | 7 +-- .../compute/options/EC2TemplateOptions.java | 8 ++-- .../ec2/domain/BlockDeviceMapping.java | 10 ++--- .../options/EC2TemplateOptionsTest.java | 6 +-- .../compute/options/NovaTemplateOptions.java | 7 +-- .../v2_0/options/CreateServerOptions.java | 4 +- .../options/NovaTemplateOptionsTest.java | 4 +- .../options/VCloudTemplateOptions.java | 7 +-- .../options/VCloudTemplateOptionsTest.java | 10 +---- .../TerremarkVCloudTemplateOptions.java | 6 +-- .../vcloud_0_8/domain/VAppConfiguration.java | 7 ++- .../TerremarkVCloudTemplateOptionsTest.java | 2 +- .../compute/predicates/NodePredicates.java | 6 +-- .../org/jclouds/compute/ComputeTestUtils.java | 4 +- .../java/org/jclouds/apis/ApiPredicates.java | 4 +- .../jclouds/providers/ProviderPredicates.java | 6 ++- .../java/org/jclouds/util/Preconditions2.java | 44 ------------------- .../aws/ec2/compute/AWSEC2ComputeService.java | 5 ++- .../ec2/compute/AWSEC2TemplateOptions.java | 11 ++--- .../options/AWSEC2TemplateOptionsTest.java | 10 ++--- 20 files changed, 57 insertions(+), 111 deletions(-) delete mode 100644 core/src/main/java/org/jclouds/util/Preconditions2.java diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/EC2ComputeService.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/EC2ComputeService.java index b3a5e72a6c..d2fa117e09 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/EC2ComputeService.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/EC2ComputeService.java @@ -17,6 +17,8 @@ * under the License. */ package org.jclouds.ec2.compute; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Strings.emptyToNull; import static com.google.common.collect.Iterables.concat; import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.transform; @@ -28,7 +30,6 @@ import static org.jclouds.compute.util.ComputeServiceUtils.addMetadataAndParseTa import static org.jclouds.compute.util.ComputeServiceUtils.metadataAndTagsAsValuesOfEmptyString; import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_GENERATE_INSTANCE_NAMES; import static org.jclouds.ec2.util.Tags.resourceToTagsAsMap; -import static org.jclouds.util.Preconditions2.checkNotEmpty; import java.util.Map; import java.util.Map.Entry; @@ -202,8 +203,8 @@ public class EC2ComputeService extends BaseComputeService { */ @VisibleForTesting void deleteSecurityGroup(String region, String group) { - checkNotEmpty(region, "region"); - checkNotEmpty(group, "group"); + checkNotNull(emptyToNull(region), "region must be defined"); + checkNotNull(emptyToNull(group), "group must be defined"); String groupName = namingConvention.create().sharedNameForGroup(group); if (client.getSecurityGroupServices().describeSecurityGroupsInRegion(region, groupName).size() > 0) { diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/options/EC2TemplateOptions.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/options/EC2TemplateOptions.java index 7aa429befc..0c7dbc6911 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/options/EC2TemplateOptions.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/options/EC2TemplateOptions.java @@ -22,6 +22,7 @@ import static com.google.common.base.Objects.equal; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; +import static com.google.common.base.Strings.emptyToNull; import java.util.List; import java.util.Map; @@ -36,7 +37,6 @@ import org.jclouds.ec2.domain.BlockDeviceMapping.MapNewVolumeToDevice; import org.jclouds.ec2.domain.BlockDeviceMapping.UnmapDeviceNamed; import org.jclouds.javax.annotation.Nullable; import org.jclouds.scriptbuilder.domain.Statement; -import org.jclouds.util.Preconditions2; import com.google.common.base.Objects; import com.google.common.base.Objects.ToStringHelper; @@ -144,7 +144,7 @@ public class EC2TemplateOptions extends TemplateOptions implements Cloneable { public EC2TemplateOptions securityGroups(Iterable groupNames) { checkArgument(Iterables.size(groupNames) > 0, "you must specify at least one security group"); for (String groupId : groupNames) - Preconditions2.checkNotEmpty(groupId, "all security groups must be non-empty"); + checkNotNull(emptyToNull(groupId), "all security groups must be non-empty"); this.groupNames = ImmutableSet.copyOf(groupNames); return this; } @@ -163,10 +163,8 @@ public class EC2TemplateOptions extends TemplateOptions implements Cloneable { * Specifies the keypair used to run instances with */ public EC2TemplateOptions keyPair(String keyPair) { - checkNotNull(keyPair, "use noKeyPair option to request boot without a keypair"); checkState(!noKeyPair, "you cannot specify both options keyPair and noKeyPair"); - Preconditions2.checkNotEmpty(keyPair, "keypair must be non-empty"); - this.keyPair = keyPair; + this.keyPair = checkNotNull(emptyToNull(keyPair), "use noKeyPair option to request boot without a keypair"); return this; } diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/domain/BlockDeviceMapping.java b/apis/ec2/src/main/java/org/jclouds/ec2/domain/BlockDeviceMapping.java index 51a9a35c97..ad499e2e8a 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/domain/BlockDeviceMapping.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/domain/BlockDeviceMapping.java @@ -20,9 +20,9 @@ package org.jclouds.ec2.domain; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Strings.emptyToNull; import org.jclouds.javax.annotation.Nullable; -import org.jclouds.util.Preconditions2; /** * @@ -101,7 +101,7 @@ public class BlockDeviceMapping implements Comparable{ @Nullable Integer sizeInGib, @Nullable Boolean noDevice, @Nullable Boolean deleteOnTermination) { checkNotNull(deviceName, "deviceName cannot be null"); - Preconditions2.checkNotEmpty(deviceName, "the deviceName must be non-empty"); + checkNotNull(emptyToNull(deviceName), "deviceName must be defined"); if (sizeInGib != null) { checkArgument(sizeInGib >= VOLUME_SIZE_MIN_VALUE && sizeInGib <= VOLUME_SIZE_MAX_VALUE, @@ -205,8 +205,7 @@ public class BlockDeviceMapping implements Comparable{ public MapEBSSnapshotToDevice(String deviceName, String snapshotId, @Nullable Integer sizeInGib, @Nullable Boolean deleteOnTermination) { super(deviceName, null, snapshotId, sizeInGib, null, deleteOnTermination); - checkNotNull(snapshotId, "snapshotId cannot be null"); - Preconditions2.checkNotEmpty(snapshotId, "the snapshotId must be non-empty"); + checkNotNull(emptyToNull(snapshotId), "snapshotId must be defined"); } } @@ -220,8 +219,7 @@ public class BlockDeviceMapping implements Comparable{ public static class MapEphemeralDeviceToDevice extends BlockDeviceMapping { public MapEphemeralDeviceToDevice(String deviceName, String virtualName) { super(deviceName, virtualName, null, null, null, null); - checkNotNull(virtualName, "virtualName cannot be null"); - Preconditions2.checkNotEmpty(virtualName, "the virtualName must be non-empty"); + checkNotNull(emptyToNull(virtualName), "virtualName must be defined"); } } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/compute/options/EC2TemplateOptionsTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/compute/options/EC2TemplateOptionsTest.java index d15fcbab2c..b2e9decabe 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/compute/options/EC2TemplateOptionsTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/compute/options/EC2TemplateOptionsTest.java @@ -46,7 +46,7 @@ public class EC2TemplateOptionsTest { assertEquals(options.as(EC2TemplateOptions.class), options); } - @Test(expectedExceptions = IllegalArgumentException.class) + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "all security groups must be non-empty") public void testsecurityGroupsIterableBadFormat() { EC2TemplateOptions options = new EC2TemplateOptions(); options.securityGroups(ImmutableSet.of("group1", "")); @@ -72,7 +72,7 @@ public class EC2TemplateOptionsTest { assertEquals(options.getGroups(), ImmutableSet.of("group1", "group2")); } - @Test(expectedExceptions = IllegalArgumentException.class) + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "all security groups must be non-empty") public void testsecurityGroupsVarArgsBadFormat() { EC2TemplateOptions options = new EC2TemplateOptions(); options.securityGroups("mygroup", ""); @@ -104,7 +104,7 @@ public class EC2TemplateOptionsTest { assertEquals(options.getGroups(), ImmutableSet.of("group1", "group2")); } - @Test(expectedExceptions = IllegalArgumentException.class) + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "use noKeyPair option to request boot without a keypair") public void testkeyPairBadFormat() { EC2TemplateOptions options = new EC2TemplateOptions(); options.keyPair(""); diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/options/NovaTemplateOptions.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/options/NovaTemplateOptions.java index 152d5ec993..21bdd3efe8 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/options/NovaTemplateOptions.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/options/NovaTemplateOptions.java @@ -21,6 +21,7 @@ package org.jclouds.openstack.nova.v2_0.compute.options; import static com.google.common.base.Objects.equal; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Strings.emptyToNull; import java.util.Arrays; import java.util.Map; @@ -28,12 +29,12 @@ import java.util.Set; import org.jclouds.compute.options.TemplateOptions; import org.jclouds.domain.LoginCredentials; +import org.jclouds.openstack.nova.v2_0.NovaApi; import org.jclouds.scriptbuilder.domain.Statement; -import org.jclouds.util.Preconditions2; import com.google.common.base.Objects; -import com.google.common.base.Optional; import com.google.common.base.Objects.ToStringHelper; +import com.google.common.base.Optional; import com.google.common.collect.ImmutableSet; /** @@ -154,7 +155,7 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable { */ public NovaTemplateOptions securityGroupNames(Iterable securityGroupNames) { for (String groupName : checkNotNull(securityGroupNames, "securityGroupNames")) - Preconditions2.checkNotEmpty(groupName, "all security groups must be non-empty"); + checkNotNull(emptyToNull(groupName), "all security groups must be non-empty"); this.securityGroupNames = Optional.> of(ImmutableSet.copyOf(securityGroupNames)); return this; } diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/options/CreateServerOptions.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/options/CreateServerOptions.java index 59f5e51f26..f9ef408ef2 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/options/CreateServerOptions.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/options/CreateServerOptions.java @@ -23,6 +23,7 @@ import static com.google.common.base.Objects.toStringHelper; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; +import static com.google.common.base.Strings.emptyToNull; import static com.google.common.io.BaseEncoding.base64; import java.util.List; @@ -37,7 +38,6 @@ import org.jclouds.http.HttpRequest; import org.jclouds.openstack.nova.v2_0.NovaApi; import org.jclouds.rest.MapBinder; import org.jclouds.rest.binders.BindToJsonPayload; -import org.jclouds.util.Preconditions2; import com.google.common.base.Objects; import com.google.common.base.Objects.ToStringHelper; @@ -318,7 +318,7 @@ public class CreateServerOptions implements MapBinder { */ public CreateServerOptions securityGroupNames(Iterable securityGroupNames) { for (String groupName : checkNotNull(securityGroupNames, "securityGroupNames")) - Preconditions2.checkNotEmpty(groupName, "all security groups must be non-empty"); + checkNotNull(emptyToNull(groupName), "all security groups must be non-empty"); this.securityGroupNames = ImmutableSet.copyOf(securityGroupNames); return this; } diff --git a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/options/NovaTemplateOptionsTest.java b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/options/NovaTemplateOptionsTest.java index 20f494cf50..f5d29205ee 100644 --- a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/options/NovaTemplateOptionsTest.java +++ b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/options/NovaTemplateOptionsTest.java @@ -48,7 +48,7 @@ public class NovaTemplateOptionsTest { assertEquals(options.as(NovaTemplateOptions.class), options); } - @Test(expectedExceptions = IllegalArgumentException.class) + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "all security groups must be non-empty") public void testsecurityGroupNamesIterableBadFormat() { NovaTemplateOptions options = new NovaTemplateOptions(); options.securityGroupNames(ImmutableSet.of("group1", "")); @@ -68,7 +68,7 @@ public class NovaTemplateOptionsTest { assertEquals(options.getSecurityGroupNames(), Optional.of(ImmutableSet.of("group1", "group2"))); } - @Test(expectedExceptions = IllegalArgumentException.class) + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "all security groups must be non-empty") public void testsecurityGroupNamesVarArgsBadFormat() { NovaTemplateOptions options = new NovaTemplateOptions(); options.securityGroupNames("mygroup", ""); diff --git a/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java b/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java index 93cff44ca7..3cc4a2a745 100644 --- a/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java +++ b/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java @@ -19,14 +19,16 @@ package org.jclouds.vcloud.compute.options; import static com.google.common.base.Objects.equal; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Strings.emptyToNull; import java.net.URI; import java.util.Map; import org.jclouds.compute.options.TemplateOptions; -import org.jclouds.util.Preconditions2; import org.jclouds.vcloud.domain.network.FenceMode; import org.jclouds.vcloud.domain.network.IpAddressAllocationMode; +import org.jclouds.vcloud.options.InstantiateVAppTemplateOptions; import com.google.common.base.Objects; import com.google.common.base.Objects.ToStringHelper; @@ -120,8 +122,7 @@ public class VCloudTemplateOptions extends TemplateOptions implements Cloneable * Specifies the customizationScript used to run instances with */ public VCloudTemplateOptions customizationScript(String customizationScript) { - Preconditions2.checkNotEmpty(customizationScript, "customizationScript must be non-empty"); - this.customizationScript = customizationScript; + this.customizationScript = checkNotNull(emptyToNull(customizationScript), "customizationScript must be defined"); return this; } diff --git a/apis/vcloud/src/test/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptionsTest.java b/apis/vcloud/src/test/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptionsTest.java index 4f47d80aee..ed8f38857a 100644 --- a/apis/vcloud/src/test/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptionsTest.java +++ b/apis/vcloud/src/test/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptionsTest.java @@ -58,18 +58,12 @@ public class VCloudTemplateOptionsTest { assertEquals(options.as(VCloudTemplateOptions.class), options); } - @Test(expectedExceptions = IllegalArgumentException.class) + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "customizationScript must be defined") public void testcustomizationScriptBadFormat() { VCloudTemplateOptions options = new VCloudTemplateOptions(); options.customizationScript(""); } - @Test - public void testNullcustomizationScript() { - VCloudTemplateOptions options = new VCloudTemplateOptions(); - assertEquals(options.getCustomizationScript(), null); - } - @Test public void testcustomizationScript() { VCloudTemplateOptions options = new VCloudTemplateOptions(); @@ -115,7 +109,7 @@ public class VCloudTemplateOptionsTest { assertEquals(options.getDescription(), "mykeypair"); } - @Test(expectedExceptions = IllegalArgumentException.class) + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "customizationScript must be defined") public void testcustomizationScriptNPE() { customizationScript(null); } diff --git a/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/compute/options/TerremarkVCloudTemplateOptions.java b/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/compute/options/TerremarkVCloudTemplateOptions.java index 334c740900..80174d74c3 100644 --- a/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/compute/options/TerremarkVCloudTemplateOptions.java +++ b/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/compute/options/TerremarkVCloudTemplateOptions.java @@ -20,11 +20,11 @@ package org.jclouds.trmk.vcloud_0_8.compute.options; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; +import static com.google.common.base.Strings.emptyToNull; import java.util.Map; import org.jclouds.compute.options.TemplateOptions; -import org.jclouds.util.Preconditions2; /** * Contains options supported in the {@code ComputeService#runNode} operation on @@ -73,10 +73,8 @@ public class TerremarkVCloudTemplateOptions extends TemplateOptions implements C * Specifies the keypair used to run instances with */ public TerremarkVCloudTemplateOptions sshKeyFingerprint(String keyPair) { - checkNotNull(keyPair, "use noKeyPair option to request boot without a keypair"); checkState(!noKeyPair, "you cannot specify both options keyPair and noKeyPair"); - Preconditions2.checkNotEmpty(keyPair, "keypair must be non-empty"); - this.keyPair = keyPair; + this.keyPair = checkNotNull(emptyToNull(keyPair), "use noKeyPair option to request boot without a keypair"); return this; } diff --git a/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/domain/VAppConfiguration.java b/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/domain/VAppConfiguration.java index 044d5c3efc..209ae44f5a 100644 --- a/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/domain/VAppConfiguration.java +++ b/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/domain/VAppConfiguration.java @@ -19,11 +19,11 @@ package org.jclouds.trmk.vcloud_0_8.domain; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Strings.emptyToNull; import java.util.List; -import org.jclouds.util.Preconditions2; - import com.google.common.collect.Lists; /** @@ -43,8 +43,7 @@ public class VAppConfiguration { * */ public VAppConfiguration changeNameTo(String name) { - Preconditions2.checkNotEmpty(name, "name must be specified"); - this.name = name; + this.name = checkNotNull(emptyToNull(name), "name must be defined"); return this; } diff --git a/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/compute/TerremarkVCloudTemplateOptionsTest.java b/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/compute/TerremarkVCloudTemplateOptionsTest.java index 230487eef4..434b0d7aaa 100644 --- a/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/compute/TerremarkVCloudTemplateOptionsTest.java +++ b/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/compute/TerremarkVCloudTemplateOptionsTest.java @@ -45,7 +45,7 @@ public class TerremarkVCloudTemplateOptionsTest { assertEquals(options.as(TerremarkVCloudTemplateOptions.class), options); } - @Test(expectedExceptions = IllegalArgumentException.class) + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "use noKeyPair option to request boot without a keypair") public void testkeyPairBadFormat() { TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions(); options.sshKeyFingerprint(""); diff --git a/compute/src/main/java/org/jclouds/compute/predicates/NodePredicates.java b/compute/src/main/java/org/jclouds/compute/predicates/NodePredicates.java index e14210b8b6..bf5df9d5cd 100644 --- a/compute/src/main/java/org/jclouds/compute/predicates/NodePredicates.java +++ b/compute/src/main/java/org/jclouds/compute/predicates/NodePredicates.java @@ -19,13 +19,13 @@ package org.jclouds.compute.predicates; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Strings.emptyToNull; import java.util.Set; import org.jclouds.compute.domain.ComputeMetadata; import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata.Status; -import org.jclouds.util.Preconditions2; import com.google.common.base.Predicate; import com.google.common.base.Predicates; @@ -191,7 +191,7 @@ public class NodePredicates { * @return predicate */ public static Predicate inGroup(final String group) { - Preconditions2.checkNotEmpty(group, "group must be defined"); + checkNotNull(emptyToNull(group), "group must be defined"); return new Predicate() { @Override public boolean apply(NodeMetadata nodeMetadata) { @@ -231,7 +231,7 @@ public class NodePredicates { * @return predicate */ public static Predicate runningInGroup(final String group) { - Preconditions2.checkNotEmpty(group, "group must be defined"); + checkNotNull(emptyToNull(group), "group must be defined"); return new Predicate() { @Override public boolean apply(NodeMetadata nodeMetadata) { diff --git a/compute/src/test/java/org/jclouds/compute/ComputeTestUtils.java b/compute/src/test/java/org/jclouds/compute/ComputeTestUtils.java index 8fe46dd526..8947b004df 100644 --- a/compute/src/test/java/org/jclouds/compute/ComputeTestUtils.java +++ b/compute/src/test/java/org/jclouds/compute/ComputeTestUtils.java @@ -19,6 +19,7 @@ package org.jclouds.compute; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Strings.emptyToNull; import static com.google.common.collect.Iterables.get; import static org.testng.Assert.assertEquals; @@ -31,7 +32,6 @@ import java.util.concurrent.TimeoutException; import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.rest.HttpClient; -import org.jclouds.util.Preconditions2; import com.google.common.base.Charsets; import com.google.common.base.Throwables; @@ -64,7 +64,7 @@ public class ComputeTestUtils { } public static void checkSecretKeyFile(String secretKeyFile) { - Preconditions2.checkNotEmpty(secretKeyFile, "System property: [test.ssh.keyfile] set to an empty string"); + checkNotNull(emptyToNull(secretKeyFile), "System property: [test.ssh.keyfile] set to an empty string"); if (!new File(secretKeyFile).exists()) { throw new IllegalStateException("secretKeyFile not found at: " + secretKeyFile); } diff --git a/core/src/main/java/org/jclouds/apis/ApiPredicates.java b/core/src/main/java/org/jclouds/apis/ApiPredicates.java index 3530bf839e..7e47255183 100644 --- a/core/src/main/java/org/jclouds/apis/ApiPredicates.java +++ b/core/src/main/java/org/jclouds/apis/ApiPredicates.java @@ -19,10 +19,10 @@ package org.jclouds.apis; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Strings.emptyToNull; import org.jclouds.View; import org.jclouds.rest.RestApiMetadata; -import org.jclouds.util.Preconditions2; import com.google.common.base.Predicate; import com.google.common.base.Predicates; @@ -53,7 +53,7 @@ public class ApiPredicates { * @return the apis with the given id */ public static Predicate id(final String id) { - Preconditions2.checkNotEmpty(id, "id must be defined"); + checkNotNull(emptyToNull(id), "id must be defined"); return new Predicate() { /** * {@inheritDoc} diff --git a/core/src/main/java/org/jclouds/providers/ProviderPredicates.java b/core/src/main/java/org/jclouds/providers/ProviderPredicates.java index 7c161f3f25..96d76e5fea 100644 --- a/core/src/main/java/org/jclouds/providers/ProviderPredicates.java +++ b/core/src/main/java/org/jclouds/providers/ProviderPredicates.java @@ -18,11 +18,13 @@ */ package org.jclouds.providers; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Strings.emptyToNull; + import org.jclouds.Context; import org.jclouds.View; import org.jclouds.apis.ApiMetadata; import org.jclouds.apis.ApiPredicates; -import org.jclouds.util.Preconditions2; import com.google.common.base.Preconditions; import com.google.common.base.Predicate; @@ -104,7 +106,7 @@ public class ProviderPredicates { * @return the providers with the given id */ public static Predicate id(final String id) { - Preconditions2.checkNotEmpty(id, "id must be defined"); + checkNotNull(emptyToNull(id), "id must be defined"); return new Predicate() { /** * {@inheritDoc} diff --git a/core/src/main/java/org/jclouds/util/Preconditions2.java b/core/src/main/java/org/jclouds/util/Preconditions2.java deleted file mode 100644 index b1c023ea42..0000000000 --- a/core/src/main/java/org/jclouds/util/Preconditions2.java +++ /dev/null @@ -1,44 +0,0 @@ -/** - * 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.util; - -import static com.google.common.base.Preconditions.checkArgument; - - -/** - * Preconditions not in guava. - * - * @author Adrian Cole - */ -public class Preconditions2 { - - - /** - * Will throw an exception if the argument is null or empty. Accepts a custom error message. - * - * @param nullableString - * string to verify. Can be null or empty. - * @param message - * message to show in case of exception - */ - public static void checkNotEmpty(String nullableString, String message) { - checkArgument(nullableString != null && nullableString.length() > 0, message); - } - -} diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2ComputeService.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2ComputeService.java index e593bb5da3..367d4086cf 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2ComputeService.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2ComputeService.java @@ -18,7 +18,9 @@ */ package org.jclouds.aws.ec2.compute; +import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; +import static com.google.common.base.Strings.emptyToNull; import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_RUNNING; import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_SUSPENDED; import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_TERMINATED; @@ -66,7 +68,6 @@ import org.jclouds.ec2.compute.EC2ComputeService; import org.jclouds.ec2.compute.domain.RegionAndName; import org.jclouds.ec2.domain.KeyPair; import org.jclouds.scriptbuilder.functions.InitAdminAccess; -import org.jclouds.util.Preconditions2; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; @@ -119,7 +120,7 @@ public class AWSEC2ComputeService extends EC2ComputeService { @VisibleForTesting void deletePlacementGroup(String region, String group) { - Preconditions2.checkNotEmpty(group, "group"); + checkNotNull(emptyToNull(group), "group must be defined"); // placementGroupName must be unique within an account per // http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?using_cluster_computing.html String placementGroup = String.format("jclouds#%s#%s", group, region); diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateOptions.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateOptions.java index 65c98c5910..3c11d7f15c 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateOptions.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateOptions.java @@ -22,6 +22,7 @@ import static com.google.common.base.Objects.equal; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; +import static com.google.common.base.Strings.emptyToNull; import java.util.Map; import java.util.Set; @@ -33,7 +34,6 @@ import org.jclouds.ec2.compute.options.EC2TemplateOptions; import org.jclouds.ec2.domain.BlockDeviceMapping; import org.jclouds.javax.annotation.Nullable; import org.jclouds.scriptbuilder.domain.Statement; -import org.jclouds.util.Preconditions2; import com.google.common.base.Objects; import com.google.common.base.Objects.ToStringHelper; @@ -148,9 +148,8 @@ public class AWSEC2TemplateOptions extends EC2TemplateOptions implements Cloneab * Specifies the keypair used to run instances with */ public AWSEC2TemplateOptions placementGroup(String placementGroup) { - checkNotNull(placementGroup, "use noPlacementGroup option to request boot without a keypair"); + checkNotNull(emptyToNull(placementGroup), "use noPlacementGroup option instead of passing null"); checkState(!noPlacementGroup, "you cannot specify both options placementGroup and noPlacementGroup"); - Preconditions2.checkNotEmpty(placementGroup, "placementGroup must be non-empty"); this.placementGroup = placementGroup; return this; } @@ -168,9 +167,7 @@ public class AWSEC2TemplateOptions extends EC2TemplateOptions implements Cloneab * Specifies the subnetId used to run instances in */ public AWSEC2TemplateOptions subnetId(String subnetId) { - checkNotNull(subnetId, "subnetId cannot be null"); - Preconditions2.checkNotEmpty(subnetId, "subnetId must be non-empty"); - this.subnetId = subnetId; + this.subnetId = checkNotNull(emptyToNull(subnetId), "subnetId must be defined"); return this; } @@ -204,7 +201,7 @@ public class AWSEC2TemplateOptions extends EC2TemplateOptions implements Cloneab public AWSEC2TemplateOptions securityGroupIds(Iterable groupIds) { checkArgument(Iterables.size(groupIds) > 0, "you must specify at least one security group"); for (String groupId : groupIds) - Preconditions2.checkNotEmpty(groupId, "all security groups must be non-empty"); + checkNotNull(emptyToNull(groupId), "all security groups must be non-empty"); this.groupIds = ImmutableSet.copyOf(groupIds); return this; } diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/options/AWSEC2TemplateOptionsTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/options/AWSEC2TemplateOptionsTest.java index 5fd03008b1..669f4eb316 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/options/AWSEC2TemplateOptionsTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/options/AWSEC2TemplateOptionsTest.java @@ -50,7 +50,7 @@ public class AWSEC2TemplateOptionsTest { assertEquals(options.as(AWSEC2TemplateOptions.class), options); } - @Test(expectedExceptions = IllegalArgumentException.class) + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "all security groups must be non-empty") public void testsecurityGroupIdsIterableBadFormat() { AWSEC2TemplateOptions options = new AWSEC2TemplateOptions(); options.securityGroupIds(ImmutableSet.of("groupId1", "")); @@ -76,7 +76,7 @@ public class AWSEC2TemplateOptionsTest { assertEquals(options.getGroupIds(), ImmutableSet.of("groupId1", "groupId2")); } - @Test(expectedExceptions = IllegalArgumentException.class) + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "all security groups must be non-empty") public void testsecurityGroupIdsVarArgsBadFormat() { AWSEC2TemplateOptions options = new AWSEC2TemplateOptions(); options.securityGroupIds("mygroupId", ""); @@ -107,7 +107,7 @@ public class AWSEC2TemplateOptionsTest { assertEquals(options.getGroupIds(), ImmutableSet.of("groupId1", "groupId2")); } - @Test(expectedExceptions = IllegalArgumentException.class) + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "all security groups must be non-empty") public void testsecurityGroupsIterableBadFormat() { AWSEC2TemplateOptions options = new AWSEC2TemplateOptions(); options.securityGroups(ImmutableSet.of("group1", "")); @@ -133,7 +133,7 @@ public class AWSEC2TemplateOptionsTest { assertEquals(options.getGroups(), ImmutableSet.of("group1", "group2")); } - @Test(expectedExceptions = IllegalArgumentException.class) + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "all security groups must be non-empty") public void testsecurityGroupsVarArgsBadFormat() { AWSEC2TemplateOptions options = new AWSEC2TemplateOptions(); options.securityGroups("mygroup", ""); @@ -164,7 +164,7 @@ public class AWSEC2TemplateOptionsTest { assertEquals(options.getGroups(), ImmutableSet.of("group1", "group2")); } - @Test(expectedExceptions = IllegalArgumentException.class) + @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "use noKeyPair option to request boot without a keypair") public void testkeyPairBadFormat() { AWSEC2TemplateOptions options = new AWSEC2TemplateOptions(); options.keyPair("");