Repository: geode-examples Updated Branches: refs/heads/feature/GEODE-2231 717eb02f9 -> b5bf7825f
GEODE-2231 Add BadEmployeeKey class and test of class Project: http://git-wip-us.apache.org/repos/asf/geode-examples/repo Commit: http://git-wip-us.apache.org/repos/asf/geode-examples/commit/b5bf7825 Tree: http://git-wip-us.apache.org/repos/asf/geode-examples/tree/b5bf7825 Diff: http://git-wip-us.apache.org/repos/asf/geode-examples/diff/b5bf7825 Branch: refs/heads/feature/GEODE-2231 Commit: b5bf7825f37a5cfc2434753ac32d7041cf7f03d7 Parents: 717eb02 Author: Karen Miller <kmil...@pivotal.io> Authored: Thu Feb 2 13:58:50 2017 -0800 Committer: Karen Miller <kmil...@pivotal.io> Committed: Thu Feb 2 13:58:50 2017 -0800 ---------------------------------------------------------------------- .../examples/partitioned/BadEmployeeKey.java | 28 ++++++--- .../partitioned/BadEmployeeKeyTest.java | 63 ++++++++++++++++++++ 2 files changed, 84 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode-examples/blob/b5bf7825/partitioned/src/main/java/org/apache/geode/examples/partitioned/BadEmployeeKey.java ---------------------------------------------------------------------- diff --git a/partitioned/src/main/java/org/apache/geode/examples/partitioned/BadEmployeeKey.java b/partitioned/src/main/java/org/apache/geode/examples/partitioned/BadEmployeeKey.java index 4aab20a..1174078 100644 --- a/partitioned/src/main/java/org/apache/geode/examples/partitioned/BadEmployeeKey.java +++ b/partitioned/src/main/java/org/apache/geode/examples/partitioned/BadEmployeeKey.java @@ -14,12 +14,15 @@ */ package org.apache.geode.examples.partitioned; +import java.util.Objects; import java.util.logging.Logger; import java.io.Serializable; import org.apache.geode.cache.client.ClientCache; public class BadEmployeeKey implements Serializable { + private static final long serialVersionUID = 1L; + static final Logger logger = Logger.getAnonymousLogger(); private String name; private int emplNumber; @@ -39,22 +42,33 @@ public class BadEmployeeKey implements Serializable { return (emplNumber); } - public boolean equals(EmployeeKey key) { - if (this.name.equals(key.getName()) && this.emplNumber == key.getEmplNumber()) { + @Override + public boolean equals(Object o) { + if (this == o) { return true; } - return false; + if (o == null || getClass() != o.getClass()) { + return false; + } + + BadEmployeeKey that = (BadEmployeeKey) o; + + if (emplNumber != that.emplNumber) { + return false; + } + return name.equals(that.name); } /* * This hashCode is what make this class a very poor implementation. It always returns the value - * 1, so that every key gets placed in the same bucket, and partitioning is useless. - * + * 1, so that every entry gets placed in the same bucket, and partitioning is useless. + * * Forgetting to define, or implementing an erroneous hashCode can result in rotten distribution - * of region entries across buckets. + * of region entries across buckets (and therefore, across partitions). */ + @Override public int hashCode() { - return (1); + return 1; } public String toString() { http://git-wip-us.apache.org/repos/asf/geode-examples/blob/b5bf7825/partitioned/src/test/java/org/apache/geode/examples/partitioned/BadEmployeeKeyTest.java ---------------------------------------------------------------------- diff --git a/partitioned/src/test/java/org/apache/geode/examples/partitioned/BadEmployeeKeyTest.java b/partitioned/src/test/java/org/apache/geode/examples/partitioned/BadEmployeeKeyTest.java new file mode 100644 index 0000000..1c16de8 --- /dev/null +++ b/partitioned/src/test/java/org/apache/geode/examples/partitioned/BadEmployeeKeyTest.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.examples.partitioned; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class BadEmployeeKeyTest { + + private BadEmployeeKey k; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Before + public void setup() { + k = new BadEmployeeKey("First Last", 3001); + } + + @Test + public void testGetName() { + assertEquals("First Last", k.getName()); + } + + @Test + public void testGetEmplNumber() { + assertEquals(3001, k.getEmplNumber()); + } + + @Test + public void testEquals() { + BadEmployeeKey otherKey = new BadEmployeeKey("First Last", 3001); + assertTrue(k.equals(otherKey)); + BadEmployeeKey nonMatchingKey = new BadEmployeeKey("Othername", 1); + assertFalse(k.equals(nonMatchingKey)); + } + + @Test + public void testHashCode() { + assertEquals(1, k.hashCode()); + } + + @Test + public void testToString() { + assertEquals("Name: First Last Employee Number: 3001", k.toString()); + } +}