Mercurial > hg > thermostat-ng > agent
changeset 1756:6e5f6b43160a
Tests for Id, VmId and AgentId
Tests for the Id class, which VmIdTest and AgentIdTest extend.
Update Id such that it cannot be created with a null "id."
Update the equals() method of Id to use the Objects class.
PR2388
Reviewed-by: omajid
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-June/014054.html
author | James Aziz <jaziz@redhat.com> |
---|---|
date | Tue, 16 Jun 2015 09:17:17 -0400 |
parents | e5dc352ea8be |
children | 19f6f5ec0910 |
files | killvm/command/src/test/java/com/redhat/thermostat/killvm/command/KillVmCommandTest.java storage/core/src/main/java/com/redhat/thermostat/storage/core/Id.java storage/core/src/test/java/com/redhat/thermostat/storage/core/AgentIdTest.java storage/core/src/test/java/com/redhat/thermostat/storage/core/IdTest.java storage/core/src/test/java/com/redhat/thermostat/storage/core/VmIdTest.java |
diffstat | 5 files changed, 173 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/killvm/command/src/test/java/com/redhat/thermostat/killvm/command/KillVmCommandTest.java Fri Jun 12 16:46:19 2015 -0400 +++ b/killvm/command/src/test/java/com/redhat/thermostat/killvm/command/KillVmCommandTest.java Tue Jun 16 09:17:17 2015 -0400 @@ -85,7 +85,8 @@ public void testKillLiveVM() throws CommandException, InterruptedException { String vmId = "liveVM"; - VmInfo vmInfo = mock(VmInfo.class); + VmInfo vmInfo = new VmInfo("myAgent", vmId, 123, 0, 0, null, null, null, null, null, null, null, null, null, + null, null,0, "myUsername"); when(vmInfoDAO.getVmInfo(any(VmId.class))).thenReturn(vmInfo); CommandContext ctx = createContext(vmId);
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/core/Id.java Fri Jun 12 16:46:19 2015 -0400 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/Id.java Tue Jun 16 09:17:17 2015 -0400 @@ -36,6 +36,7 @@ package com.redhat.thermostat.storage.core; +import java.util.Objects; import java.util.UUID; /** @@ -49,7 +50,7 @@ } public Id(String id) { - this.id = id; + this.id = Objects.requireNonNull(id); } @Override @@ -57,12 +58,8 @@ if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - Id sessionID = (Id) o; - - if (id != null ? !id.equals(sessionID.id) : sessionID.id != null) - return false; - - return true; + Id other = (Id) o; + return Objects.equals(this.id, other.get()); } public String get() {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/core/AgentIdTest.java Tue Jun 16 09:17:17 2015 -0400 @@ -0,0 +1,43 @@ +/* + * Copyright 2012-2015 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core; + +public class AgentIdTest extends IdTest { + + @Override + protected AgentId createId(String id) { return new AgentId(id); } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/core/IdTest.java Tue Jun 16 09:17:17 2015 -0400 @@ -0,0 +1,81 @@ +/* + * Copyright 2012-2015 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; + +public class IdTest { + + protected Id fooId1; + protected Id fooId2; + protected Id barId; + + protected Id createId(String id) { return new Id(id); } + + @Before + public void setup() { + fooId1 = createId("foo"); + fooId2 = createId("foo"); + barId = createId("bar"); + } + + @Test + public void testEquals() { + assertTrue(fooId1.equals(fooId1)); + assertTrue(fooId1.equals(fooId2)); + } + + @Test + public void testNotEquals() { + assertFalse(fooId1.equals(null)); + assertFalse(fooId1.equals(barId)); + } + + @Test + public void testEqualsHaveIdenticalHashcode() { + assertEquals(fooId1, fooId2); + + assertEquals(fooId1.hashCode(), fooId1.hashCode()); + assertEquals(fooId1.hashCode(), fooId2.hashCode()); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/core/VmIdTest.java Tue Jun 16 09:17:17 2015 -0400 @@ -0,0 +1,43 @@ +/* + * Copyright 2012-2015 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core; + +public class VmIdTest extends IdTest { + + @Override + protected VmId createId(String id) { return new VmId(id); } +}