Author: trader Date: 2010-02-25 10:18:18 -0800 (Thu, 25 Feb 2010) New Revision: 14322 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=14322 Added: trunk/unittest/src/org/hyperic/hq/appdef/server/ trunk/unittest/src/org/hyperic/hq/appdef/server/session/ trunk/unittest/src/org/hyperic/hq/appdef/server/session/ServerTests.java Modified: trunk/src/org/hyperic/hq/appdef/server/session/Server.java Log: [HHQ-3697] [HPD-223] NPE in Server.matchesValueObject() -- install path did not have a null check. Re-factored for readability. Added unittests. Modified: trunk/src/org/hyperic/hq/appdef/server/session/Server.java =================================================================== --- trunk/src/org/hyperic/hq/appdef/server/session/Server.java 2010-02-25 01:13:21 UTC (rev 14321) +++ trunk/src/org/hyperic/hq/appdef/server/session/Server.java 2010-02-25 18:18:18 UTC (rev 14322) @@ -240,25 +240,64 @@ public boolean matchesValueObject(ServerValue obj) { - boolean matches = true; - matches = super.matchesValueObject(obj) && - (getName() != null ? this.getName().equals(obj.getName()) - : (obj.getName() == null)) && - (getDescription() != null ? - this.getDescription().equals(obj.getDescription()) - : (obj.getDescription() == null)) && - (getLocation() != null ? - this.getLocation().equals(obj.getLocation()) - : (obj.getLocation() == null)) && - (isRuntimeAutodiscovery() == obj.getRuntimeAutodiscovery()) && - (getInstallPath().equals(obj.getInstallPath())) && - (getAutoinventoryIdentifier() != null ? - getAutoinventoryIdentifier().equals( - obj.getAutoinventoryIdentifier()) - : (obj.getAutoinventoryIdentifier() == null)); - - return matches; + return super.matchesValueObject(obj) && + nameMatches(obj) && + descriptionMatches(obj) && + locationMatches(obj) && + runtimeAutoDiscoveryMatches(obj) && + installPathMatches(obj) && + autoInventoryIdentifierMatches(obj); } + + private boolean nameMatches(ServerValue obj) + { + if (getName() == null) { + return obj.getName() == null; + } else { + return getName().equals(obj.getName()); + } + } + + private boolean descriptionMatches(ServerValue obj) + { + if (getDescription() == null) { + return obj.getDescription() == null; + } else { + return getDescription().equals(obj.getDescription()); + } + } + + private boolean locationMatches(ServerValue obj) + { + if (getLocation() == null) { + return obj.getLocation() == null; + } else { + return getLocation().equals(obj.getLocation()); + } + } + + private boolean runtimeAutoDiscoveryMatches(ServerValue obj) + { + return isRuntimeAutodiscovery() == obj.getRuntimeAutodiscovery(); + } + + private boolean installPathMatches(ServerValue obj) + { + if (getInstallPath() == null) { + return obj.getInstallPath() == null; + } else { + return getInstallPath().equals(obj.getInstallPath()); + } + } + + private boolean autoInventoryIdentifierMatches(ServerValue obj) + { + if (getAutoinventoryIdentifier() == null) { + return obj.getAutoinventoryIdentifier() == null; + } else { + return getAutoinventoryIdentifier().equals(obj.getAutoinventoryIdentifier()); + } + } /** * Validate a new service value object to be hosted by this server Added: trunk/unittest/src/org/hyperic/hq/appdef/server/session/ServerTests.java =================================================================== --- trunk/unittest/src/org/hyperic/hq/appdef/server/session/ServerTests.java (rev 0) +++ trunk/unittest/src/org/hyperic/hq/appdef/server/session/ServerTests.java 2010-02-25 18:18:18 UTC (rev 14322) @@ -0,0 +1,151 @@ +package org.hyperic.hq.appdef.server.session; + +import junit.framework.TestCase; + +import org.hyperic.hq.appdef.shared.ServerValue; + +public class ServerTests extends TestCase { + + private static final String NAME = "Fred"; + private static final String DESC = "Caveman"; + private static final String LOC = "Bedrock"; + private static final String INST_PATH = "/down/the/street"; + private static final String AID = "Flintstone"; + + public void testNullServerAndNullServerValue() throws Exception { + + Server s = new Server(new Integer(1)); + ServerValue sv = new ServerValue(); + + assertTrue(s.matchesValueObject(sv)); + } + + public void testNameMatch() throws Exception { + Server s = new Server(new Integer(1)); + s.setName(NAME); + ServerValue sv = new ServerValue(); + assertFalse(s.matchesValueObject(sv)); + + sv.setName(NAME); + assertTrue(s.matchesValueObject(sv)); + + s.setName(null); + assertFalse(s.matchesValueObject(sv)); + + sv.setName(null); + assertTrue(s.matchesValueObject(sv)); + } + + public void testDescriptionMatch() throws Exception { + Server s = new Server(new Integer(1)); + s.setDescription(DESC); + ServerValue sv = new ServerValue(); + assertFalse(s.matchesValueObject(sv)); + + sv.setDescription(DESC); + assertTrue(s.matchesValueObject(sv)); + + s.setDescription(null); + assertFalse(s.matchesValueObject(sv)); + + sv.setDescription(null); + assertTrue(s.matchesValueObject(sv)); + } + + public void testLocationMatch() throws Exception { + Server s = new Server(new Integer(1)); + s.setLocation(LOC); + ServerValue sv = new ServerValue(); + assertFalse(s.matchesValueObject(sv)); + + sv.setLocation(LOC); + assertTrue(s.matchesValueObject(sv)); + + s.setLocation(null); + assertFalse(s.matchesValueObject(sv)); + + sv.setLocation(null); + assertTrue(s.matchesValueObject(sv)); + } + + public void testRuntimeDiscoveryMatch() throws Exception { + Server s = new Server(new Integer(1)); + s.setRuntimeAutodiscovery(false); + ServerValue sv = new ServerValue(); + sv.setRuntimeAutodiscovery(false); + assertTrue(s.matchesValueObject(sv)); + + s.setRuntimeAutodiscovery(true); + assertFalse(s.matchesValueObject(sv)); + + sv.setRuntimeAutodiscovery(true); + assertTrue(s.matchesValueObject(sv)); + + s.setRuntimeAutodiscovery(false); + assertFalse(s.matchesValueObject(sv)); + } + + public void testInstallPathMatch() throws Exception { + Server s = new Server(new Integer(1)); + s.setInstallPath(INST_PATH); + ServerValue sv = new ServerValue(); + assertFalse(s.matchesValueObject(sv)); + + sv.setInstallPath(INST_PATH); + assertTrue(s.matchesValueObject(sv)); + + s.setInstallPath(null); + assertFalse(s.matchesValueObject(sv)); + + sv.setInstallPath(null); + assertTrue(s.matchesValueObject(sv)); + } + + public void testAutoInventoryIdentifierMatch() throws Exception { + Server s = new Server(new Integer(1)); + s.setAutoinventoryIdentifier(AID); + ServerValue sv = new ServerValue(); + assertFalse(s.matchesValueObject(sv)); + + sv.setAutoinventoryIdentifier(AID); + assertTrue(s.matchesValueObject(sv)); + + s.setAutoinventoryIdentifier(null); + assertFalse(s.matchesValueObject(sv)); + + sv.setAutoinventoryIdentifier(null); + assertTrue(s.matchesValueObject(sv)); + } + + public void testAllMatching() throws Exception { + Server s = new Server(new Integer(1)); + s.setName(NAME); + ServerValue sv = new ServerValue(); + sv.setName(NAME); + assertTrue(s.matchesValueObject(sv)); + + s.setDescription(DESC); + sv.setDescription(DESC); + assertTrue(s.matchesValueObject(sv)); + + s.setLocation(LOC); + sv.setLocation(LOC); + assertTrue(s.matchesValueObject(sv)); + + s.setRuntimeAutodiscovery(false); + sv.setRuntimeAutodiscovery(false); + assertTrue(s.matchesValueObject(sv)); + + s.setRuntimeAutodiscovery(true); + sv.setRuntimeAutodiscovery(true); + assertTrue(s.matchesValueObject(sv)); + + s.setInstallPath(INST_PATH); + sv.setInstallPath(INST_PATH); + assertTrue(s.matchesValueObject(sv)); + + s.setAutoinventoryIdentifier(AID); + sv.setAutoinventoryIdentifier(AID); + assertTrue(s.matchesValueObject(sv)); + } +} |