replaced usage of checkNotEmpty with checkNotNull(emptyToNull

This commit is contained in:
Adrian Cole 2013-01-12 14:27:38 -08:00
parent 0e4c10e29f
commit 45bb35971e
20 changed files with 57 additions and 111 deletions

View File

@ -17,6 +17,8 @@
* under the License. * under the License.
*/ */
package org.jclouds.ec2.compute; 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.concat;
import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform; 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.compute.util.ComputeServiceUtils.metadataAndTagsAsValuesOfEmptyString;
import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_GENERATE_INSTANCE_NAMES; import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_GENERATE_INSTANCE_NAMES;
import static org.jclouds.ec2.util.Tags.resourceToTagsAsMap; import static org.jclouds.ec2.util.Tags.resourceToTagsAsMap;
import static org.jclouds.util.Preconditions2.checkNotEmpty;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -202,8 +203,8 @@ public class EC2ComputeService extends BaseComputeService {
*/ */
@VisibleForTesting @VisibleForTesting
void deleteSecurityGroup(String region, String group) { void deleteSecurityGroup(String region, String group) {
checkNotEmpty(region, "region"); checkNotNull(emptyToNull(region), "region must be defined");
checkNotEmpty(group, "group"); checkNotNull(emptyToNull(group), "group must be defined");
String groupName = namingConvention.create().sharedNameForGroup(group); String groupName = namingConvention.create().sharedNameForGroup(group);
if (client.getSecurityGroupServices().describeSecurityGroupsInRegion(region, groupName).size() > 0) { if (client.getSecurityGroupServices().describeSecurityGroupsInRegion(region, groupName).size() > 0) {

View File

@ -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.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.emptyToNull;
import java.util.List; import java.util.List;
import java.util.Map; 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.ec2.domain.BlockDeviceMapping.UnmapDeviceNamed;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
import org.jclouds.scriptbuilder.domain.Statement; import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.util.Preconditions2;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper; import com.google.common.base.Objects.ToStringHelper;
@ -144,7 +144,7 @@ public class EC2TemplateOptions extends TemplateOptions implements Cloneable {
public EC2TemplateOptions securityGroups(Iterable<String> groupNames) { public EC2TemplateOptions securityGroups(Iterable<String> groupNames) {
checkArgument(Iterables.size(groupNames) > 0, "you must specify at least one security group"); checkArgument(Iterables.size(groupNames) > 0, "you must specify at least one security group");
for (String groupId : groupNames) 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); this.groupNames = ImmutableSet.copyOf(groupNames);
return this; return this;
} }
@ -163,10 +163,8 @@ public class EC2TemplateOptions extends TemplateOptions implements Cloneable {
* Specifies the keypair used to run instances with * Specifies the keypair used to run instances with
*/ */
public EC2TemplateOptions keyPair(String keyPair) { 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"); checkState(!noKeyPair, "you cannot specify both options keyPair and noKeyPair");
Preconditions2.checkNotEmpty(keyPair, "keypair must be non-empty"); this.keyPair = checkNotNull(emptyToNull(keyPair), "use noKeyPair option to request boot without a keypair");
this.keyPair = keyPair;
return this; return this;
} }

View File

@ -20,9 +20,9 @@ package org.jclouds.ec2.domain;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; 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.javax.annotation.Nullable;
import org.jclouds.util.Preconditions2;
/** /**
* *
@ -101,7 +101,7 @@ public class BlockDeviceMapping implements Comparable<BlockDeviceMapping>{
@Nullable Integer sizeInGib, @Nullable Boolean noDevice, @Nullable Boolean deleteOnTermination) { @Nullable Integer sizeInGib, @Nullable Boolean noDevice, @Nullable Boolean deleteOnTermination) {
checkNotNull(deviceName, "deviceName cannot be null"); 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) { if (sizeInGib != null) {
checkArgument(sizeInGib >= VOLUME_SIZE_MIN_VALUE && sizeInGib <= VOLUME_SIZE_MAX_VALUE, checkArgument(sizeInGib >= VOLUME_SIZE_MIN_VALUE && sizeInGib <= VOLUME_SIZE_MAX_VALUE,
@ -205,8 +205,7 @@ public class BlockDeviceMapping implements Comparable<BlockDeviceMapping>{
public MapEBSSnapshotToDevice(String deviceName, String snapshotId, @Nullable Integer sizeInGib, public MapEBSSnapshotToDevice(String deviceName, String snapshotId, @Nullable Integer sizeInGib,
@Nullable Boolean deleteOnTermination) { @Nullable Boolean deleteOnTermination) {
super(deviceName, null, snapshotId, sizeInGib, null, deleteOnTermination); super(deviceName, null, snapshotId, sizeInGib, null, deleteOnTermination);
checkNotNull(snapshotId, "snapshotId cannot be null"); checkNotNull(emptyToNull(snapshotId), "snapshotId must be defined");
Preconditions2.checkNotEmpty(snapshotId, "the snapshotId must be non-empty");
} }
} }
@ -220,8 +219,7 @@ public class BlockDeviceMapping implements Comparable<BlockDeviceMapping>{
public static class MapEphemeralDeviceToDevice extends BlockDeviceMapping { public static class MapEphemeralDeviceToDevice extends BlockDeviceMapping {
public MapEphemeralDeviceToDevice(String deviceName, String virtualName) { public MapEphemeralDeviceToDevice(String deviceName, String virtualName) {
super(deviceName, virtualName, null, null, null, null); super(deviceName, virtualName, null, null, null, null);
checkNotNull(virtualName, "virtualName cannot be null"); checkNotNull(emptyToNull(virtualName), "virtualName must be defined");
Preconditions2.checkNotEmpty(virtualName, "the virtualName must be non-empty");
} }
} }

View File

@ -46,7 +46,7 @@ public class EC2TemplateOptionsTest {
assertEquals(options.as(EC2TemplateOptions.class), options); 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() { public void testsecurityGroupsIterableBadFormat() {
EC2TemplateOptions options = new EC2TemplateOptions(); EC2TemplateOptions options = new EC2TemplateOptions();
options.securityGroups(ImmutableSet.of("group1", "")); options.securityGroups(ImmutableSet.of("group1", ""));
@ -72,7 +72,7 @@ public class EC2TemplateOptionsTest {
assertEquals(options.getGroups(), ImmutableSet.of("group1", "group2")); 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() { public void testsecurityGroupsVarArgsBadFormat() {
EC2TemplateOptions options = new EC2TemplateOptions(); EC2TemplateOptions options = new EC2TemplateOptions();
options.securityGroups("mygroup", ""); options.securityGroups("mygroup", "");
@ -104,7 +104,7 @@ public class EC2TemplateOptionsTest {
assertEquals(options.getGroups(), ImmutableSet.of("group1", "group2")); 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() { public void testkeyPairBadFormat() {
EC2TemplateOptions options = new EC2TemplateOptions(); EC2TemplateOptions options = new EC2TemplateOptions();
options.keyPair(""); options.keyPair("");

View File

@ -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.Objects.equal;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.emptyToNull;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map; import java.util.Map;
@ -28,12 +29,12 @@ import java.util.Set;
import org.jclouds.compute.options.TemplateOptions; import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.domain.LoginCredentials; import org.jclouds.domain.LoginCredentials;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.scriptbuilder.domain.Statement; import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.util.Preconditions2;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Objects.ToStringHelper; import com.google.common.base.Objects.ToStringHelper;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
/** /**
@ -154,7 +155,7 @@ public class NovaTemplateOptions extends TemplateOptions implements Cloneable {
*/ */
public NovaTemplateOptions securityGroupNames(Iterable<String> securityGroupNames) { public NovaTemplateOptions securityGroupNames(Iterable<String> securityGroupNames) {
for (String groupName : checkNotNull(securityGroupNames, "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.<Set<String>> of(ImmutableSet.copyOf(securityGroupNames)); this.securityGroupNames = Optional.<Set<String>> of(ImmutableSet.copyOf(securityGroupNames));
return this; return this;
} }

View File

@ -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.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState; 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 static com.google.common.io.BaseEncoding.base64;
import java.util.List; import java.util.List;
@ -37,7 +38,6 @@ import org.jclouds.http.HttpRequest;
import org.jclouds.openstack.nova.v2_0.NovaApi; import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.rest.MapBinder; import org.jclouds.rest.MapBinder;
import org.jclouds.rest.binders.BindToJsonPayload; import org.jclouds.rest.binders.BindToJsonPayload;
import org.jclouds.util.Preconditions2;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper; import com.google.common.base.Objects.ToStringHelper;
@ -318,7 +318,7 @@ public class CreateServerOptions implements MapBinder {
*/ */
public CreateServerOptions securityGroupNames(Iterable<String> securityGroupNames) { public CreateServerOptions securityGroupNames(Iterable<String> securityGroupNames) {
for (String groupName : checkNotNull(securityGroupNames, "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); this.securityGroupNames = ImmutableSet.copyOf(securityGroupNames);
return this; return this;
} }

View File

@ -48,7 +48,7 @@ public class NovaTemplateOptionsTest {
assertEquals(options.as(NovaTemplateOptions.class), options); 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() { public void testsecurityGroupNamesIterableBadFormat() {
NovaTemplateOptions options = new NovaTemplateOptions(); NovaTemplateOptions options = new NovaTemplateOptions();
options.securityGroupNames(ImmutableSet.of("group1", "")); options.securityGroupNames(ImmutableSet.of("group1", ""));
@ -68,7 +68,7 @@ public class NovaTemplateOptionsTest {
assertEquals(options.getSecurityGroupNames(), Optional.of(ImmutableSet.of("group1", "group2"))); 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() { public void testsecurityGroupNamesVarArgsBadFormat() {
NovaTemplateOptions options = new NovaTemplateOptions(); NovaTemplateOptions options = new NovaTemplateOptions();
options.securityGroupNames("mygroup", ""); options.securityGroupNames("mygroup", "");

View File

@ -19,14 +19,16 @@
package org.jclouds.vcloud.compute.options; package org.jclouds.vcloud.compute.options;
import static com.google.common.base.Objects.equal; 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.net.URI;
import java.util.Map; import java.util.Map;
import org.jclouds.compute.options.TemplateOptions; import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.util.Preconditions2;
import org.jclouds.vcloud.domain.network.FenceMode; import org.jclouds.vcloud.domain.network.FenceMode;
import org.jclouds.vcloud.domain.network.IpAddressAllocationMode; 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;
import com.google.common.base.Objects.ToStringHelper; 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 * Specifies the customizationScript used to run instances with
*/ */
public VCloudTemplateOptions customizationScript(String customizationScript) { public VCloudTemplateOptions customizationScript(String customizationScript) {
Preconditions2.checkNotEmpty(customizationScript, "customizationScript must be non-empty"); this.customizationScript = checkNotNull(emptyToNull(customizationScript), "customizationScript must be defined");
this.customizationScript = customizationScript;
return this; return this;
} }

View File

@ -58,18 +58,12 @@ public class VCloudTemplateOptionsTest {
assertEquals(options.as(VCloudTemplateOptions.class), options); assertEquals(options.as(VCloudTemplateOptions.class), options);
} }
@Test(expectedExceptions = IllegalArgumentException.class) @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "customizationScript must be defined")
public void testcustomizationScriptBadFormat() { public void testcustomizationScriptBadFormat() {
VCloudTemplateOptions options = new VCloudTemplateOptions(); VCloudTemplateOptions options = new VCloudTemplateOptions();
options.customizationScript(""); options.customizationScript("");
} }
@Test
public void testNullcustomizationScript() {
VCloudTemplateOptions options = new VCloudTemplateOptions();
assertEquals(options.getCustomizationScript(), null);
}
@Test @Test
public void testcustomizationScript() { public void testcustomizationScript() {
VCloudTemplateOptions options = new VCloudTemplateOptions(); VCloudTemplateOptions options = new VCloudTemplateOptions();
@ -115,7 +109,7 @@ public class VCloudTemplateOptionsTest {
assertEquals(options.getDescription(), "mykeypair"); assertEquals(options.getDescription(), "mykeypair");
} }
@Test(expectedExceptions = IllegalArgumentException.class) @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "customizationScript must be defined")
public void testcustomizationScriptNPE() { public void testcustomizationScriptNPE() {
customizationScript(null); customizationScript(null);
} }

View File

@ -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.checkNotNull;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.emptyToNull;
import java.util.Map; import java.util.Map;
import org.jclouds.compute.options.TemplateOptions; import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.util.Preconditions2;
/** /**
* Contains options supported in the {@code ComputeService#runNode} operation on * 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 * Specifies the keypair used to run instances with
*/ */
public TerremarkVCloudTemplateOptions sshKeyFingerprint(String keyPair) { 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"); checkState(!noKeyPair, "you cannot specify both options keyPair and noKeyPair");
Preconditions2.checkNotEmpty(keyPair, "keypair must be non-empty"); this.keyPair = checkNotNull(emptyToNull(keyPair), "use noKeyPair option to request boot without a keypair");
this.keyPair = keyPair;
return this; return this;
} }

View File

@ -19,11 +19,11 @@
package org.jclouds.trmk.vcloud_0_8.domain; package org.jclouds.trmk.vcloud_0_8.domain;
import static com.google.common.base.Preconditions.checkArgument; 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 java.util.List;
import org.jclouds.util.Preconditions2;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
/** /**
@ -43,8 +43,7 @@ public class VAppConfiguration {
* *
*/ */
public VAppConfiguration changeNameTo(String name) { public VAppConfiguration changeNameTo(String name) {
Preconditions2.checkNotEmpty(name, "name must be specified"); this.name = checkNotNull(emptyToNull(name), "name must be defined");
this.name = name;
return this; return this;
} }

View File

@ -45,7 +45,7 @@ public class TerremarkVCloudTemplateOptionsTest {
assertEquals(options.as(TerremarkVCloudTemplateOptions.class), options); 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() { public void testkeyPairBadFormat() {
TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions(); TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
options.sshKeyFingerprint(""); options.sshKeyFingerprint("");

View File

@ -19,13 +19,13 @@
package org.jclouds.compute.predicates; package org.jclouds.compute.predicates;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.emptyToNull;
import java.util.Set; import java.util.Set;
import org.jclouds.compute.domain.ComputeMetadata; import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadata.Status; import org.jclouds.compute.domain.NodeMetadata.Status;
import org.jclouds.util.Preconditions2;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
@ -191,7 +191,7 @@ public class NodePredicates {
* @return predicate * @return predicate
*/ */
public static Predicate<NodeMetadata> inGroup(final String group) { public static Predicate<NodeMetadata> inGroup(final String group) {
Preconditions2.checkNotEmpty(group, "group must be defined"); checkNotNull(emptyToNull(group), "group must be defined");
return new Predicate<NodeMetadata>() { return new Predicate<NodeMetadata>() {
@Override @Override
public boolean apply(NodeMetadata nodeMetadata) { public boolean apply(NodeMetadata nodeMetadata) {
@ -231,7 +231,7 @@ public class NodePredicates {
* @return predicate * @return predicate
*/ */
public static Predicate<NodeMetadata> runningInGroup(final String group) { public static Predicate<NodeMetadata> runningInGroup(final String group) {
Preconditions2.checkNotEmpty(group, "group must be defined"); checkNotNull(emptyToNull(group), "group must be defined");
return new Predicate<NodeMetadata>() { return new Predicate<NodeMetadata>() {
@Override @Override
public boolean apply(NodeMetadata nodeMetadata) { public boolean apply(NodeMetadata nodeMetadata) {

View File

@ -19,6 +19,7 @@
package org.jclouds.compute; package org.jclouds.compute;
import static com.google.common.base.Preconditions.checkNotNull; 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 com.google.common.collect.Iterables.get;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
@ -31,7 +32,6 @@ import java.util.concurrent.TimeoutException;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.rest.HttpClient; import org.jclouds.rest.HttpClient;
import org.jclouds.util.Preconditions2;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
@ -64,7 +64,7 @@ public class ComputeTestUtils {
} }
public static void checkSecretKeyFile(String secretKeyFile) { 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()) { if (!new File(secretKeyFile).exists()) {
throw new IllegalStateException("secretKeyFile not found at: " + secretKeyFile); throw new IllegalStateException("secretKeyFile not found at: " + secretKeyFile);
} }

View File

@ -19,10 +19,10 @@
package org.jclouds.apis; package org.jclouds.apis;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.emptyToNull;
import org.jclouds.View; import org.jclouds.View;
import org.jclouds.rest.RestApiMetadata; import org.jclouds.rest.RestApiMetadata;
import org.jclouds.util.Preconditions2;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
@ -53,7 +53,7 @@ public class ApiPredicates {
* @return the apis with the given id * @return the apis with the given id
*/ */
public static Predicate<ApiMetadata> id(final String id) { public static Predicate<ApiMetadata> id(final String id) {
Preconditions2.checkNotEmpty(id, "id must be defined"); checkNotNull(emptyToNull(id), "id must be defined");
return new Predicate<ApiMetadata>() { return new Predicate<ApiMetadata>() {
/** /**
* {@inheritDoc} * {@inheritDoc}

View File

@ -18,11 +18,13 @@
*/ */
package org.jclouds.providers; 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.Context;
import org.jclouds.View; import org.jclouds.View;
import org.jclouds.apis.ApiMetadata; import org.jclouds.apis.ApiMetadata;
import org.jclouds.apis.ApiPredicates; import org.jclouds.apis.ApiPredicates;
import org.jclouds.util.Preconditions2;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
@ -104,7 +106,7 @@ public class ProviderPredicates {
* @return the providers with the given id * @return the providers with the given id
*/ */
public static Predicate<ProviderMetadata> id(final String id) { public static Predicate<ProviderMetadata> id(final String id) {
Preconditions2.checkNotEmpty(id, "id must be defined"); checkNotNull(emptyToNull(id), "id must be defined");
return new Predicate<ProviderMetadata>() { return new Predicate<ProviderMetadata>() {
/** /**
* {@inheritDoc} * {@inheritDoc}

View File

@ -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);
}
}

View File

@ -18,7 +18,9 @@
*/ */
package org.jclouds.aws.ec2.compute; 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.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_RUNNING;
import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_SUSPENDED; import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_SUSPENDED;
import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_TERMINATED; 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.compute.domain.RegionAndName;
import org.jclouds.ec2.domain.KeyPair; import org.jclouds.ec2.domain.KeyPair;
import org.jclouds.scriptbuilder.functions.InitAdminAccess; import org.jclouds.scriptbuilder.functions.InitAdminAccess;
import org.jclouds.util.Preconditions2;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional; import com.google.common.base.Optional;
@ -119,7 +120,7 @@ public class AWSEC2ComputeService extends EC2ComputeService {
@VisibleForTesting @VisibleForTesting
void deletePlacementGroup(String region, String group) { 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 // placementGroupName must be unique within an account per
// http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?using_cluster_computing.html // http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?using_cluster_computing.html
String placementGroup = String.format("jclouds#%s#%s", group, region); String placementGroup = String.format("jclouds#%s#%s", group, region);

View File

@ -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.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.emptyToNull;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -33,7 +34,6 @@ import org.jclouds.ec2.compute.options.EC2TemplateOptions;
import org.jclouds.ec2.domain.BlockDeviceMapping; import org.jclouds.ec2.domain.BlockDeviceMapping;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
import org.jclouds.scriptbuilder.domain.Statement; import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.util.Preconditions2;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper; 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 * Specifies the keypair used to run instances with
*/ */
public AWSEC2TemplateOptions placementGroup(String placementGroup) { 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"); checkState(!noPlacementGroup, "you cannot specify both options placementGroup and noPlacementGroup");
Preconditions2.checkNotEmpty(placementGroup, "placementGroup must be non-empty");
this.placementGroup = placementGroup; this.placementGroup = placementGroup;
return this; return this;
} }
@ -168,9 +167,7 @@ public class AWSEC2TemplateOptions extends EC2TemplateOptions implements Cloneab
* Specifies the subnetId used to run instances in * Specifies the subnetId used to run instances in
*/ */
public AWSEC2TemplateOptions subnetId(String subnetId) { public AWSEC2TemplateOptions subnetId(String subnetId) {
checkNotNull(subnetId, "subnetId cannot be null"); this.subnetId = checkNotNull(emptyToNull(subnetId), "subnetId must be defined");
Preconditions2.checkNotEmpty(subnetId, "subnetId must be non-empty");
this.subnetId = subnetId;
return this; return this;
} }
@ -204,7 +201,7 @@ public class AWSEC2TemplateOptions extends EC2TemplateOptions implements Cloneab
public AWSEC2TemplateOptions securityGroupIds(Iterable<String> groupIds) { public AWSEC2TemplateOptions securityGroupIds(Iterable<String> groupIds) {
checkArgument(Iterables.size(groupIds) > 0, "you must specify at least one security group"); checkArgument(Iterables.size(groupIds) > 0, "you must specify at least one security group");
for (String groupId : groupIds) 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); this.groupIds = ImmutableSet.copyOf(groupIds);
return this; return this;
} }

View File

@ -50,7 +50,7 @@ public class AWSEC2TemplateOptionsTest {
assertEquals(options.as(AWSEC2TemplateOptions.class), options); 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() { public void testsecurityGroupIdsIterableBadFormat() {
AWSEC2TemplateOptions options = new AWSEC2TemplateOptions(); AWSEC2TemplateOptions options = new AWSEC2TemplateOptions();
options.securityGroupIds(ImmutableSet.of("groupId1", "")); options.securityGroupIds(ImmutableSet.of("groupId1", ""));
@ -76,7 +76,7 @@ public class AWSEC2TemplateOptionsTest {
assertEquals(options.getGroupIds(), ImmutableSet.of("groupId1", "groupId2")); 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() { public void testsecurityGroupIdsVarArgsBadFormat() {
AWSEC2TemplateOptions options = new AWSEC2TemplateOptions(); AWSEC2TemplateOptions options = new AWSEC2TemplateOptions();
options.securityGroupIds("mygroupId", ""); options.securityGroupIds("mygroupId", "");
@ -107,7 +107,7 @@ public class AWSEC2TemplateOptionsTest {
assertEquals(options.getGroupIds(), ImmutableSet.of("groupId1", "groupId2")); 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() { public void testsecurityGroupsIterableBadFormat() {
AWSEC2TemplateOptions options = new AWSEC2TemplateOptions(); AWSEC2TemplateOptions options = new AWSEC2TemplateOptions();
options.securityGroups(ImmutableSet.of("group1", "")); options.securityGroups(ImmutableSet.of("group1", ""));
@ -133,7 +133,7 @@ public class AWSEC2TemplateOptionsTest {
assertEquals(options.getGroups(), ImmutableSet.of("group1", "group2")); 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() { public void testsecurityGroupsVarArgsBadFormat() {
AWSEC2TemplateOptions options = new AWSEC2TemplateOptions(); AWSEC2TemplateOptions options = new AWSEC2TemplateOptions();
options.securityGroups("mygroup", ""); options.securityGroups("mygroup", "");
@ -164,7 +164,7 @@ public class AWSEC2TemplateOptionsTest {
assertEquals(options.getGroups(), ImmutableSet.of("group1", "group2")); 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() { public void testkeyPairBadFormat() {
AWSEC2TemplateOptions options = new AWSEC2TemplateOptions(); AWSEC2TemplateOptions options = new AWSEC2TemplateOptions();
options.keyPair(""); options.keyPair("");