Skip to content

Commit 3006d61

Browse files
author
Daniel Augusto Veronezi Salvador
committed
Use FileUtils constants intead of hardcode values
1 parent a143406 commit 3006d61

4 files changed

Lines changed: 14 additions & 13 deletions

File tree

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
import com.cloud.vm.VirtualMachine.PowerState;
191191
import com.cloud.vm.VirtualMachine.Type;
192192
import org.apache.cloudstack.utils.bytescale.ByteScaleUtils;
193+
import org.apache.commons.io.FileUtils;
193194
import org.libvirt.VcpuInfo;
194195

195196
@RunWith(PowerMockRunner.class)
@@ -5280,7 +5281,7 @@ public void validateCreateGuestResourceDefWithVcpuMaxLimitAsNull(){
52805281

52815282
@Test
52825283
public void validateGetDomainMemory() throws LibvirtException{
5283-
long valueExpected = ByteScaleUtils.KiB;
5284+
long valueExpected = FileUtils.ONE_KB;
52845285

52855286
Mockito.doReturn(valueExpected).when(domainMock).getMaxMemory();
52865287
Assert.assertEquals(valueExpected, LibvirtComputingResource.getDomainMemory(domainMock));

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtVmMemoryDeviceDefTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.cloud.hypervisor.kvm.resource;
1818

19-
import org.apache.cloudstack.utils.bytescale.ByteScaleUtils;
19+
import org.apache.commons.io.FileUtils;
2020
import org.junit.Assert;
2121
import org.junit.Test;
2222
import org.junit.runner.RunWith;
@@ -27,7 +27,7 @@ public class LibvirtVmMemoryDeviceDefTest {
2727

2828
@Test
2929
public void validateToString(){
30-
long memorySize = ByteScaleUtils.KiB;
30+
long memorySize = FileUtils.ONE_KB;
3131

3232
StringBuilder expectedToString = new StringBuilder();
3333
expectedToString.append("<memory model='dimm'>");

utils/src/main/java/org/apache/cloudstack/utils/bytescale/ByteScaleUtils.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616

1717
package org.apache.cloudstack.utils.bytescale;
1818

19+
import org.apache.commons.io.FileUtils;
20+
1921
/**
2022
* This class provides a facility to convert bytes through his scales (b, Kib, Kb, Mib, Mb...).
2123
*
2224
*/
2325
public class ByteScaleUtils {
2426

25-
public static final long KiB = 1024;
26-
public static final long MiB = KiB * 1024;
27-
2827
private ByteScaleUtils() {}
2928

3029
/**
@@ -34,7 +33,7 @@ private ByteScaleUtils() {}
3433
* @return The parameter multiplied by 1048576 (1024 * 1024, 1 MiB).
3534
*/
3635
public static long mibToBytes(long mib) {
37-
return mib * MiB;
36+
return mib * FileUtils.ONE_MB;
3837
}
3938

4039
/**
@@ -44,6 +43,6 @@ public static long mibToBytes(long mib) {
4443
* @return The parameter divided by 1024 (1 KiB).
4544
*/
4645
public static long bytesToKib(long b) {
47-
return b / KiB;
46+
return b / FileUtils.ONE_KB;
4847
}
4948
}

utils/src/test/java/org/apache/cloudstack/utils/bytescale/ByteScaleUtilsTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.cloudstack.utils.bytescale;
1818

1919
import junit.framework.TestCase;
20+
import org.apache.commons.io.FileUtils;
2021
import org.junit.Test;
2122
import org.junit.runner.RunWith;
2223
import org.mockito.junit.MockitoJUnitRunner;
@@ -27,27 +28,27 @@ public class ByteScaleUtilsTest extends TestCase {
2728
@Test
2829
public void validateMibToBytes() {
2930
long mib = 3000L;
30-
long b = 1024L * 1024L * mib;
31+
long b = FileUtils.ONE_MB * mib;
3132
assertEquals(b, ByteScaleUtils.mibToBytes(mib));
3233
}
3334

3435
@Test
3536
public void validateBytesToKib() {
36-
long kib = 1024L * 3000L;
37-
long b = 1024 * kib;
37+
long kib = FileUtils.ONE_KB * 3000L;
38+
long b = FileUtils.ONE_KB * kib;
3839
assertEquals(kib, ByteScaleUtils.bytesToKib(b));
3940
}
4041

4142
@Test
4243
public void validateMibToBytesIfIntTimesIntThenMustExtrapolateIntMaxValue() {
4344
int mib = 3000;
44-
long b = 1024L * 1024L * mib;
45+
long b = FileUtils.ONE_MB * mib;
4546
assertEquals(b, ByteScaleUtils.mibToBytes(mib));
4647
}
4748

4849
@Test
4950
public void validateBytesToKibIfIntByIntThenMustExtrapolateIntMaxValue(){
5051
int b = Integer.MAX_VALUE;
51-
assertEquals(b, ByteScaleUtils.bytesToKib(b * 1024L));
52+
assertEquals(b, ByteScaleUtils.bytesToKib(b * FileUtils.ONE_KB));
5253
}
5354
}

0 commit comments

Comments
 (0)