From 89de5bf4184e04af2cc73f97e1fcd190410e927b Mon Sep 17 00:00:00 2001 From: leticiamaia Date: Tue, 8 Apr 2014 15:33:01 -0300 Subject: [PATCH] Refactoring code and increasing Test coverage. --- .../manager/xmpp/core/ManagerItem.java | 11 +-- .../manager/xmpp/core/ManagerModel.java | 5 +- .../manager/xmpp/core/ResourcesInfo.java | 4 +- .../manager/xmpp/TestManagerItem.java | 75 +++++++++++++++++++ .../manager/xmpp/TestManagerModel.java | 18 +++-- .../xmpp/model/RendezvousItemCopy.java | 1 + 6 files changed, 96 insertions(+), 18 deletions(-) create mode 100644 src/test/java/org/fogbowcloud/manager/xmpp/TestManagerItem.java diff --git a/src/main/java/org/fogbowcloud/manager/xmpp/core/ManagerItem.java b/src/main/java/org/fogbowcloud/manager/xmpp/core/ManagerItem.java index 806e3902..54b4db7f 100644 --- a/src/main/java/org/fogbowcloud/manager/xmpp/core/ManagerItem.java +++ b/src/main/java/org/fogbowcloud/manager/xmpp/core/ManagerItem.java @@ -33,10 +33,7 @@ public String getFormattedTime() { SimpleDateFormat dateFormatISO8601 = new SimpleDateFormat( ISO_8601_DATE_FORMAT, Locale.ROOT); dateFormatISO8601.setTimeZone(TimeZone.getTimeZone("GMT")); - - Date date = new Date(lastTime); - - return dateFormatISO8601.format(date); + return dateFormatISO8601.format(new Date(lastTime)); } /** @@ -44,9 +41,7 @@ public String getFormattedTime() { * * @param lastTime */ - protected void setLastTime(long lastTime){ + public void setLastTime(long lastTime){ this.lastTime = lastTime; } - - -} +} \ No newline at end of file diff --git a/src/main/java/org/fogbowcloud/manager/xmpp/core/ManagerModel.java b/src/main/java/org/fogbowcloud/manager/xmpp/core/ManagerModel.java index ae1df933..b02b2f82 100644 --- a/src/main/java/org/fogbowcloud/manager/xmpp/core/ManagerModel.java +++ b/src/main/java/org/fogbowcloud/manager/xmpp/core/ManagerModel.java @@ -1,11 +1,11 @@ package org.fogbowcloud.manager.xmpp.core; -import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; public class ManagerModel { - private List managers = new ArrayList(); + private List managers = new LinkedList(); public void update(List managers) { if (managers == null) throw new IllegalArgumentException(); @@ -15,5 +15,4 @@ public void update(List managers) { public List getMembers() { return managers; } - } diff --git a/src/main/java/org/fogbowcloud/manager/xmpp/core/ResourcesInfo.java b/src/main/java/org/fogbowcloud/manager/xmpp/core/ResourcesInfo.java index 8eaec3e6..c435764e 100644 --- a/src/main/java/org/fogbowcloud/manager/xmpp/core/ResourcesInfo.java +++ b/src/main/java/org/fogbowcloud/manager/xmpp/core/ResourcesInfo.java @@ -1,6 +1,7 @@ package org.fogbowcloud.manager.xmpp.core; public class ResourcesInfo { + private String id; private String cpuIdle; private String cpuInUse; @@ -9,7 +10,6 @@ public class ResourcesInfo { public ResourcesInfo(String id, String cpuIdle, String cpuInUse, String memIdle, String memInUse) { - setId(id); setCpuIdle(cpuIdle); setCpuInUse(cpuInUse); @@ -22,7 +22,7 @@ public String getId() { } public void setId(String id) { - if (id == null || "".equals(id)) { + if (id == null || id.isEmpty()) { throw new IllegalArgumentException("ResourceInfo id is invalid."); } this.id = id; diff --git a/src/test/java/org/fogbowcloud/manager/xmpp/TestManagerItem.java b/src/test/java/org/fogbowcloud/manager/xmpp/TestManagerItem.java new file mode 100644 index 00000000..a62cf702 --- /dev/null +++ b/src/test/java/org/fogbowcloud/manager/xmpp/TestManagerItem.java @@ -0,0 +1,75 @@ +package org.fogbowcloud.manager.xmpp; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.fogbowcloud.manager.xmpp.core.ManagerItem; +import org.fogbowcloud.manager.xmpp.core.ResourcesInfo; +import org.fogbowcloud.manager.xmpp.model.ManagerTestHelper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestManagerItem { + + private ResourcesInfo resources; + private String DATE = "2000-10-31T01:30:00.000+0000"; + + @Before + public void setUp() { + resources = ManagerTestHelper.getResources(); + } + + @Test(expected = IllegalArgumentException.class) + public void testInvalidResources() { + new ManagerItem(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullResourceId() { + new ResourcesInfo(null, "cpuIdle", + "cpuInUse", "memIdle", "memInUse"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullResourcesCpuIdle() { + new ResourcesInfo("id", null, + "cpuInUse", "memIdle", "memInUse"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullResourcesCpuInUse() { + new ResourcesInfo("id", "CpuIdle", + null, "memIdle", "memInUse"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullResourcesMemIdle() { + new ResourcesInfo("id", "CpuIdle", + "cpuInUse", null, "memInUse"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullResourcesMemInUse() { + new ResourcesInfo("id", "CpuIdle", + "cpuInUse", "memIdle", null); + } + + @Test + public void testConstructor() { + ManagerItem managerItem = new ManagerItem(resources); + Assert.assertEquals(resources, managerItem.getResourcesInfo()); + } + + @Test + public void getFormattedTime() throws ParseException { + ManagerItem managerItem = new ManagerItem(resources); + SimpleDateFormat dateFormat = new SimpleDateFormat( + ManagerItem.ISO_8601_DATE_FORMAT); + Date date = dateFormat.parse(DATE); + Long lastTime = date.getTime(); + managerItem.setLastTime(lastTime); + Assert.assertEquals(DATE, managerItem.getFormattedTime()); + } +} diff --git a/src/test/java/org/fogbowcloud/manager/xmpp/TestManagerModel.java b/src/test/java/org/fogbowcloud/manager/xmpp/TestManagerModel.java index 5629211c..90758bb6 100644 --- a/src/test/java/org/fogbowcloud/manager/xmpp/TestManagerModel.java +++ b/src/test/java/org/fogbowcloud/manager/xmpp/TestManagerModel.java @@ -1,6 +1,6 @@ package org.fogbowcloud.manager.xmpp; -import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; import org.fogbowcloud.manager.xmpp.core.ManagerItem; @@ -29,23 +29,31 @@ public void testUpdateWithException() { @Test public void testUpdateEmptyList() { - List resources = new ArrayList(); + List resources = new LinkedList(); managerModel.update(resources); Assert.assertEquals(resources, managerModel.getMembers()); } @Test public void testUpdate1Member() { - List members = new ArrayList(); + List members = new LinkedList(); members.add(new ManagerItem(resources)); managerModel.update(members); Assert.assertEquals(1, members.size()); Assert.assertEquals(members, managerModel.getMembers()); } - + + + @Test(expected = IllegalArgumentException.class) + public void testUpdateWithIvalidMember() { + List members = new LinkedList(); + members.add(new ManagerItem(null)); + managerModel.update(members); + } + @Test public void testUpdateManyMembers() { - List members = new ArrayList(); + List members = new LinkedList(); for (int i = 0; i < 10; i++) { resources.setId("" + i); members.add(new ManagerItem(resources)); diff --git a/src/test/java/org/fogbowcloud/manager/xmpp/model/RendezvousItemCopy.java b/src/test/java/org/fogbowcloud/manager/xmpp/model/RendezvousItemCopy.java index 0678a717..5c56545a 100644 --- a/src/test/java/org/fogbowcloud/manager/xmpp/model/RendezvousItemCopy.java +++ b/src/test/java/org/fogbowcloud/manager/xmpp/model/RendezvousItemCopy.java @@ -9,6 +9,7 @@ import org.fogbowcloud.manager.xmpp.core.model.DateUtils; public class RendezvousItemCopy { + public static final String ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; private long lastTime; private ResourcesInfo resourcesInfo;