RamProvisionerSimple extends RamProvisioner { @Override public boolean allocateRamForVm(Vm vm, int ram) { int maxRam = vm.getRam(); if (ram >= maxRam) { ram = maxRam; } deallocateRamForVm(vm); if (getAvailableRam() >= ram) { setAvailableRam(getAvailableRam() - ram); getRamTable().put(vm.getUid(), ram); vm.setCurrentAllocatedRam(getAllocatedRamForVm(vm)); return true; } vm.setCurrentAllocatedRam(getAllocatedRamForVm(vm)); return false; } RamProvisionerSimple(int availableRam); @Override boolean allocateRamForVm(Vm vm, int ram); @Override int getAllocatedRamForVm(Vm vm); @Override void deallocateRamForVm(Vm vm); @Override void deallocateRamForAllVms(); @Override boolean isSuitableForVm(Vm vm, int ram);  }
@Test public void testAllocateRamForVm() { Vm vm1 = new Vm(0, 0, 0, 0, RAM / 2, 0, 0, "", null); Vm vm2 = new Vm(1, 0, 0, 0, RAM, 0, 0, "", null); assertTrue(ramProvisioner.isSuitableForVm(vm1, RAM / 2)); assertTrue(ramProvisioner.allocateRamForVm(vm1, RAM / 2)); assertEquals(RAM / 2, ramProvisioner.getAvailableRam()); assertFalse(ramProvisioner.isSuitableForVm(vm2, RAM)); assertFalse(ramProvisioner.allocateRamForVm(vm2, RAM)); assertEquals(RAM / 2, ramProvisioner.getAvailableRam()); assertTrue(ramProvisioner.isSuitableForVm(vm2, RAM / 4)); assertTrue(ramProvisioner.allocateRamForVm(vm2, RAM / 4)); assertEquals(RAM * 1 / 4, ramProvisioner.getAvailableRam()); assertTrue(ramProvisioner.isSuitableForVm(vm2, RAM / 2)); assertTrue(ramProvisioner.allocateRamForVm(vm2, RAM / 2)); assertEquals(0, ramProvisioner.getAvailableRam()); }