Uploaded image for project: 'Spring Modules'
  1. Spring Modules
  2. MOD-241

HashCodeCacheKeyGenerator doesn't always produce unique keys for simple methods

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 0.6
    • Fix Version/s: 0.7
    • Component/s: CACHE
    • Labels:
      None

      Description

      HashCodeCacheKeyGenerator doesn't generate unique keys for methods that take two integers as demonstrated by the following. It quickly reports duplicate keys.

      import junit.framework.TestCase;
      import org.aopalliance.intercept.MethodInvocation;
      import org.easymock.MockControl;
      import org.springmodules.cache.key.HashCodeCacheKeyGenerator;
      import org.springmodules.cache.key.CacheKeyGenerator;

      import java.lang.reflect.Method;
      import java.util.HashSet;
      import java.util.Set;

      /**

      • Created by IntelliJ IDEA.
      • User: william.poitras
      • Date: Oct 16, 2006
      • Time: 3:02:25 PM
      • To change this template use File | Settings | File Templates.
        */
        public class CacheKeyTest extends TestCase {
        CacheKeyGenerator fixture;
        MockControl mc;
        MethodInvocation mi;
        protected void setUp() throws Exception { super.setUp(); //To change body of overridden methods use File | Settings | File Templates. fixture = new HashCodeCacheKeyGenerator(true); mc = MockControl.createControl(MethodInvocation.class); mi = (MethodInvocation) mc.getMock(); }

      public void testHashKey() throws NoSuchMethodException {
      Method m = AddressCacheDao.class.getMethod("checkSolsListMember", new Class[]

      {int.class, int.class}

      );
      Set s = new HashSet();
      mc.expectAndDefaultReturn(mi.getMethod(), m);
      for (int i = 1; i <= 4000; i++) {
      for (int j = 100; j < 107; j++) {
      mc.expectAndReturn(mi.getArguments(), new Object[]

      {new Integer(i),new Integer(j)}

      );
      }
      }
      mc.replay();
      for (int i = 1; i <= 4000; i++) {
      for (int j = 100; j < 107; j++)

      { Object hashKey = fixture.generateKey(mi); System.out.println("insert hashKey("+i+","+j+") = " + hashKey); assertFalse(i+","+j+" already inserted", s.contains(hashKey)); s.add(hashKey); }

      }
      mc.verify();
      }
      }

        Attachments

          Activity

            People

            Assignee:
            irbouho Omar Irbouh
            Reporter:
            wpoitras Bill Poitras
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: