Unverified Commit f8643538 authored by Manoel Campos's avatar Manoel Campos
Browse files

Includes some more tests



- Uses reflection to check if the constants (tags) used by CloudSim Plus
  to represent messages have duplicated values.
  The value of each constant into a given class needs to be unique.
Signed-off-by: default avatarManoel Campos <manoelcampos@gmail.com>
parent fa062219
......@@ -462,6 +462,18 @@
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>1.7.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<reporting>
......
......@@ -283,7 +283,7 @@ public final class CloudSimTags {
* Defines the base tag to be used for failure events such as
* failure of hosts or VMs.
*/
public static final int FAILURE = BASE + 47;
public static final int FAILURE = BASE + 48;
/**
* Defines the tag that represents a request to generate a host failure.
......
......@@ -208,7 +208,7 @@ public class NetworkHost extends HostSimple {
private void sendPacketsToExternalVms() {
final double availableBwByPacket = getBandwidthByPacket(pktsToSendForExternalVms.size());
for (final HostPacket hostPkt : pktsToSendForExternalVms) {
final double delay = Conversion.bytesToMegaBites(hostPkt.getVmPacket().getSize()) / availableBwByPacket;
final double delay = Conversion.bytesToMegaBits(hostPkt.getVmPacket().getSize()) / availableBwByPacket;
totalDataTransferBytes += hostPkt.getVmPacket().getSize();
// send to Datacenter with delay
......
......@@ -273,7 +273,7 @@ public abstract class AbstractSwitch extends CloudSimEntity implements Switch {
* @return the expected time to transfer the packet through the network (in seconds)
*/
protected double networkDelayForPacketTransmission(HostPacket netPkt, double bwCapacity, List<HostPacket> netPktList) {
return Conversion.bytesToMegaBites(netPkt.getVmPacket().getSize()) / getAvailableBwForEachPacket(bwCapacity, netPktList);
return Conversion.bytesToMegaBits(netPkt.getVmPacket().getSize()) / getAvailableBwForEachPacket(bwCapacity, netPktList);
}
/**
......
......@@ -109,7 +109,7 @@ public class EdgeSwitch extends AbstractSwitch {
}
// otherwise, packet is to be sent to upper switch
/**
/*
* ASSUMPTION: Each Edge is connected to one Aggregate Switch.
* If there are more than one Aggregate Switch, the following code has to be modified.
*/
......
package org.cloudbus.cloudsim.util;
/**
* A class that provides a set of methods for unit conversion.
* Provides a set of methods for unit conversion.
*
* @author Manoel Campos da Silva Filho
*/
......@@ -56,8 +56,8 @@ public final class Conversion {
* @param bytes the value in bytes
* @return the value in Megabites (Mb)
*/
public static double bytesToMegaBites(final double bytes){
return bytesToBites(bytesToMegaBytes(bytes));
public static double bytesToMegaBits(final double bytes){
return bytesToBits(bytesToMegaBytes(bytes));
}
/**
......@@ -67,7 +67,7 @@ public final class Conversion {
* @param bytes the value in bytes, KB, MB, GB, etc
* @return the value in bites, Kbits, Mbits, Gbits and so on, according to the given value
*/
public static double bytesToBites(final double bytes){
public static double bytesToBits(final double bytes){
return bytes * 8;
}
......
......@@ -41,8 +41,8 @@ public final class ExecutionTimeMeasurer {
* @param name the name of the method/process being measured.
* @see #getExecutionStartTimes()
*/
public static void start(String name) {
getExecutionStartTimes().put(name, System.currentTimeMillis());
public static void start(final String name) {
executionStartTimes.put(name, System.currentTimeMillis());
}
/**
......@@ -52,10 +52,8 @@ public final class ExecutionTimeMeasurer {
* @return the time the method/process spent in execution (in seconds)
* @see #getExecutionStartTimes()
*/
public static double end(String name) {
final double executionTime = (System.currentTimeMillis() - getExecutionStartTimes().get(name)) / 1000.0;
getExecutionStartTimes().remove(name);
return executionTime;
public static double end(final String name) {
return (System.currentTimeMillis() - executionStartTimes.remove(name)) / 1000.0;
}
/**
......@@ -64,8 +62,12 @@ public final class ExecutionTimeMeasurer {
* @return the execution times map
* @see #executionStartTimes
*/
public static Map<String, Long> getExecutionStartTimes() {
protected static Map<String, Long> getExecutionStartTimes() {
return executionStartTimes;
}
protected static Long getExecutionStartTime(final String name){
return executionStartTimes.get(name);
}
}
package org.cloudbus.cloudsim.core;
import org.junit.BeforeClass;
import org.junit.Test;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.*;
/**
* @author Manoel Campos da Silva Filho
*/
public class CloudSimTagsTest {
/**
* List of constants from the class under test.
*/
private static List<Field> constants;
@BeforeClass
public static void setUp(){
constants = getDeclaredConstants(CloudSimTags.class);
}
/**
* Checks if there are different constants defined with the same value.
*/
@Test
public void testConstantsWithSameValue(){
for (final Field field : constants) {
Field anotherField = getAnotherConstWithSameValue(constants, field);
assertFalse(
getFieldsWithDuplicatedValueMsg(field, anotherField),
!field.equals(anotherField));
}
}
public static List<Field> getDeclaredConstants(Class aClass) {
final List<Field> list = Stream.of(aClass.getDeclaredFields())
.filter(CloudSimTagsTest::isFieldConstant)
.collect(Collectors.toList());
list.forEach(CloudSimTagsTest::setAccessible);
return list;
}
public static String getFieldsWithDuplicatedValueMsg(final Field field, final Field anotherField) {
return "The constant " + field.getName() + " has the same value of the constant " + anotherField.getName();
}
/**
* Sets a field to be accessible, enabling to get its value using Reflection.
* @param field the field to make accessible via Reflection
*/
private static void setAccessible(final Field field) {
field.setAccessible(true);
}
/**
* Checks if a given field is a constant or not
* @param field the field to check
* @return true if the field is a constant, false otherwise
*/
private static boolean isFieldConstant(final Field field){
return Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers());
}
/**
* Try to get a constant with the same value of the given constant.
*
* @param fields The list of Fields referring to the constants belonging to a class
* @param field the {@link Field} object that gives access to the
* constant.
* @return another {@link Field} referring to a constant with the same
* value of the given one; or the given field if there is no other constant with
* the same value of the given field.
*/
public static Field getAnotherConstWithSameValue(final List<Field> fields, final Field field){
Object fValue = null, fieldValue = null;
for (final Field f : fields) {
try {
fValue = f.get(fValue);
fieldValue = field.get(fieldValue);
if(!f.equals(field) && fValue.equals(fieldValue)){
return f;
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
return field;
}
}
package org.cloudbus.cloudsim.util;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Manoel Campos da Silva Filho
*/
public class ConversionTest {
private static final double ONE_KILOBIT_IN_BITS = 1024;
private static final double ONE_KILOBYTE_IN_BYTES = 1024;
private static final double ONE_MEGABYTE_IN_BYTES = 1024*1024;
@Test
public void bytesToMegaBytes(){
final float expectedMegaBytes = 1;
assertEquals(expectedMegaBytes, Conversion.bytesToMegaBytes(ONE_MEGABYTE_IN_BYTES), 0);
}
@Test
public void bytesToMegaBites(){
final double expectedMegaBits = 8;
assertEquals(expectedMegaBits, Conversion.bytesToMegaBits(ONE_MEGABYTE_IN_BYTES), 0);
}
@Test
public void bytesToBites(){
final double expectedBits = 8192;
assertEquals(expectedBits, Conversion.bytesToBits(ONE_KILOBYTE_IN_BYTES),0);
}
@Test
public void bitesToBytes(){
final double expectedBytes = 128;
assertEquals(expectedBytes, Conversion.bitesToBytes(ONE_KILOBIT_IN_BITS),0);
}
}
package org.cloudbus.cloudsim.util;
import org.cloudbus.cloudsim.core.CloudSimTags;
import org.cloudbus.cloudsim.core.CloudSimTagsTest;
import org.junit.BeforeClass;
import org.junit.Test;
import java.lang.reflect.Field;
import java.util.List;
import static org.junit.Assert.*;
/**
* @author Manoel Campos da Silva Filho
*/
public class DataCloudTagsTest {
/**
* List of constants from the class under test.
*/
private static List<Field> constants;
@BeforeClass
public static void setUp(){
constants = CloudSimTagsTest.getDeclaredConstants(CloudSimTags.class);
}
/**
* Checks if there are different constants defined with the same value.
*/
@Test
public void testConstantsWithSameValue(){
for (final Field field : constants) {
Field anotherField = CloudSimTagsTest.getAnotherConstWithSameValue(constants, field);
assertFalse(
CloudSimTagsTest.getFieldsWithDuplicatedValueMsg(field, anotherField),
!field.equals(anotherField));
}
}
}
package org.cloudbus.cloudsim.util;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.*;
import org.powermock.api.easymock.PowerMock;
import java.util.HashMap;
import java.util.Map;
/**
* @author Manoel Campos da Silva Filho
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ExecutionTimeMeasurer.class, ExecutionTimeMeasurerTest.class})
public class ExecutionTimeMeasurerTest {
private static final String ENTRY_NAME = "testStart";
private static final long START_TIME = 1000;
private static final long FINISH_TIME = 3000;
@Before
public void setUp(){
PowerMock.mockStatic(System.class);
expectCurrentTimeMillis(START_TIME);
}
private void expectCurrentTimeMillis(final long timeMillis) {
EasyMock.expect(System.currentTimeMillis()).andReturn(timeMillis);
}
@Test
public void testGetExecutionStartTimes_HasOneEntry(){
start(true);
final int entries = 1;
assertEquals(entries, ExecutionTimeMeasurer.getExecutionStartTimes().size());
}
@Test
public void testGetExecutionStartTime(){
start(true);
assertEquals(START_TIME, ExecutionTimeMeasurer.getExecutionStartTime(ENTRY_NAME), 0);
}
private void start(boolean replyAndVerifyAll) {
if(replyAndVerifyAll){
PowerMock.replayAll();
}
ExecutionTimeMeasurer.start(ENTRY_NAME);
if(replyAndVerifyAll) {
PowerMock.verifyAll();
}
}
@Test
public void testEnd(){
expectCurrentTimeMillis(FINISH_TIME);
PowerMock.replayAll();
start(false);
final double expectedExecutionTime = 2;
assertEquals(expectedExecutionTime, ExecutionTimeMeasurer.end(ENTRY_NAME), 0);
PowerMock.verifyAll();
}
@Test
public void testGetExecutionStartTimes(){
Map<String, Long> map = new HashMap<>(1);
map.put(ENTRY_NAME, START_TIME);
start(true);
assertEquals(map, ExecutionTimeMeasurer.getExecutionStartTimes());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment