Merge pull request #907 from pandriani/master

[CloudStack] allow "," as decimal separator in cpuUsed regular expression
This commit is contained in:
Adrian Cole 2012-10-24 20:08:03 -07:00
commit 08516ff5d4
2 changed files with 6 additions and 2 deletions

View File

@ -588,7 +588,7 @@ public class VirtualMachine {
@Nullable VirtualMachine.State state, @Nullable String templateDisplayText, @Nullable String templateId, @Nullable VirtualMachine.State state, @Nullable String templateDisplayText, @Nullable String templateId,
@Nullable String templateName, @Nullable String zoneId, @Nullable String zoneName, @Nullable Set<NIC> nics, @Nullable String templateName, @Nullable String zoneId, @Nullable String zoneName, @Nullable Set<NIC> nics,
@Nullable String hypervisor, @Nullable Set<SecurityGroup> securityGroups) { @Nullable String hypervisor, @Nullable Set<SecurityGroup> securityGroups) {
Preconditions.checkArgument(Strings.isNullOrEmpty(cpuUsed) || cpuUsed.matches("^[0-9\\.\\-]+%$"), "cpuUsed value should be a decimal number followed by %"); Preconditions.checkArgument(Strings.isNullOrEmpty(cpuUsed) || cpuUsed.matches("^[0-9\\.|,\\-]+%$"), "cpuUsed value should be a decimal number followed by %");
this.id = checkNotNull(id, "id"); this.id = checkNotNull(id, "id");
this.account = account; this.account = account;
this.cpuCount = cpuCount; this.cpuCount = cpuCount;
@ -667,7 +667,7 @@ public class VirtualMachine {
* @return the amount of the vm's CPU currently used * @return the amount of the vm's CPU currently used
*/ */
public float getCpuUsed() { public float getCpuUsed() {
return cpuUsed != null ? Float.parseFloat(cpuUsed.substring(0, cpuUsed.length() - 1)) : 0.0f; return cpuUsed != null ? Float.parseFloat(cpuUsed.substring(0, cpuUsed.length() - 1).replace(',', '.')) : 0.0f;
} }
private String getCpuUsedAsString() { private String getCpuUsedAsString() {

View File

@ -47,6 +47,10 @@ public class VirtualMachineTest {
// Retrieving CpuUsed should just give us a straightforward float // Retrieving CpuUsed should just give us a straightforward float
vm = VirtualMachine.builder().id("3").cpuUsed("23.4%").build(); vm = VirtualMachine.builder().id("3").cpuUsed("23.4%").build();
assertEquals(vm.getCpuUsed(), 23.4, 0.01); assertEquals(vm.getCpuUsed(), 23.4, 0.01);
//Allow ',' as decimal separator
vm = VirtualMachine.builder().id("4").cpuUsed("23,4%").build();
assertEquals(vm.getCpuUsed(), 23.4, 0.01);
} }
} }