[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/loader ...

Manik Surtani manik at jboss.org
Mon Jul 16 19:43:37 EDT 2007


  User: msurtani
  Date: 07/07/16 19:43:37

  Modified:    tests/functional/org/jboss/cache/loader 
                        UnnecessaryLoadingTest.java
  Log:
  JBCACHE-1133
  
  Revision  Changes    Path
  1.6       +82 -24    JBossCache/tests/functional/org/jboss/cache/loader/UnnecessaryLoadingTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: UnnecessaryLoadingTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/loader/UnnecessaryLoadingTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -b -r1.5 -r1.6
  --- UnnecessaryLoadingTest.java	13 Jul 2007 12:37:02 -0000	1.5
  +++ UnnecessaryLoadingTest.java	16 Jul 2007 23:43:37 -0000	1.6
  @@ -54,6 +54,17 @@
         cache.stop();
      }
   
  +   protected void assertDataLoaded(Fqn f)
  +   {
  +      assertTrue("Data should be loaded for node " + f, cache.peek(f, false).isDataLoaded());
  +   }
  +
  +   protected void assertDataNotLoaded(Fqn f)
  +   {
  +      assertFalse("Data should not be loaded for node " + f, cache.peek(f, false).isDataLoaded());
  +   }
  +
  +
      public void testNoLoading()
      {
         // we expect these nodes to be stored.
  @@ -129,15 +140,15 @@
         cache.put(child, k, v);
   
         // nodes should be marked as having data loaded.
  -      assertTrue(cache.peek(parent, false).isDataLoaded());
  -      assertTrue(cache.peek(child, false).isDataLoaded());
  +      assertDataLoaded(parent);
  +      assertDataLoaded(child);
   
         // now evict the parent
         cache.evict(parent, false);
   
         // only the evicted parent should have isDataLoaded() set to false.  Not the child.
  -      assertFalse(cache.peek(parent, false).isDataLoaded());
  -      assertTrue(cache.peek(child, false).isDataLoaded());
  +      assertDataNotLoaded(parent);
  +      assertDataLoaded(child);
         assertNotNull(cache.peek(child, false));
   
         // there is no REAL cache loader so this return value should not be tested
  @@ -159,27 +170,34 @@
         m.put("foo", "bar");
   
         // expecting a put on parent and child
  -      mockCacheLoader.expects(once()).method("get").with(eq(parent));
  +//      mockCacheLoader.expects(once()).method("get").with(eq(parent));
         mockCacheLoader.expects(once()).method("put").with(eq(child), eq(m));
  -      mockCacheLoader.expects(once()).method("get").with(eq(child));
  +
         cache.put(child, m);
  -      assertTrue(cache.peek(child, false).isDataLoaded());
  -      // should not load anything
  +      assertDataNotLoaded(child);
  +
  +      // should load child data
  +      mockCacheLoader.expects(once()).method("get").with(eq(child));
         cache.get(child, "foo");
  -      assertTrue(cache.peek(child, false).isDataLoaded());
  +      assertDataNotLoaded(child);
         cache.get(child, "foo");
  -      assertTrue(cache.peek(child, false).isDataLoaded());
  +      assertDataNotLoaded(child);
  +      cache.get(child, "foo2"); // does not exist, will trigger a load
  +      assertDataLoaded(child);
  +
  +      // should not load
         Node node = cache.getRoot().getChild(parent);
  -      assertTrue(cache.peek(child, false).isDataLoaded());
  +      assertDataLoaded(child);
  +      assertDataNotLoaded(parent);
   
         // needs to load children at this stage in case there are other children that have been evicted.
         mockCacheLoader.expects(once()).method("getChildrenNames").with(eq(parent)).will(returnValue(Collections.singleton(child.getLastElementAsString())));
   
         Set children = node.getChildren(); //getchildrennames /parent
         assertEquals(1, children.size());
  -      assertTrue(cache.peek(child, false).isDataLoaded());
  +      assertDataLoaded(child);
         cache.get(child, "foo"); //get /parent/child
  -      assertTrue(cache.peek(child, false).isDataLoaded());
  +      assertDataLoaded(child);
      }
   
      public void testDontLoadDataWhenGettingNode()
  @@ -198,7 +216,7 @@
         mockCacheLoader.expects(once()).method("get").with(eq(parent));
   
         cache.put(parent, k, v);
  -      assertTrue(cache.peek(parent, false).isDataLoaded());
  +      assertDataLoaded(parent);
         // evict the parent
         cache.evict(parent, false);
         assertNull(cache.peek(parent, false));
  @@ -207,7 +225,7 @@
         // now get node.
         Node n = cache.getRoot().getChild(parent);
         assertNotNull(n);
  -      assertFalse(cache.peek(parent, false).isDataLoaded());
  +      assertDataNotLoaded(parent);
      }
   
      public void testDontLoadDataWhenClearingNode()
  @@ -226,7 +244,7 @@
         mockCacheLoader.expects(once()).method("get").with(eq(parent));
   
         cache.put(parent, k, v);
  -      assertTrue(cache.peek(parent, false).isDataLoaded());
  +      assertDataLoaded(parent);
         // evict the parent
         cache.evict(parent, false);
         assertNull(cache.peek(parent, false));
  @@ -235,14 +253,14 @@
         // now get node.
         Node n = cache.getRoot().getChild(parent);
         assertNotNull(n);
  -      assertFalse(cache.peek(parent, false).isDataLoaded());
  +      assertDataNotLoaded(parent);
   
         // should not load node but should change isDataLoaded to true
         // will trigger a removedata() though
         mockCacheLoader.expects(once()).method("removeData").with(eq(parent));
         n.clearData();
   
  -      assertTrue(cache.peek(parent, false).isDataLoaded());
  +      assertDataLoaded(parent);
      }
   
      public void testDontLoadDataWhenReplacingNode()
  @@ -270,18 +288,17 @@
         // now get node.
         Node n = cache.getRoot().getChild(parent);
         assertNotNull(n);
  -      assertFalse(cache.peek(parent, false).isDataLoaded());
  +      assertDataNotLoaded(parent);
   
         // should not load node but should change isDataLoaded to true
         // will trigger a put() though
         // for the moment this does a get as well - which while unnecessary is the best we can do for now until we bring in
         // an AOP framework to work on nodes directly.
  -      mockCacheLoader.expects(once()).method("get").with(eq(parent)).will(returnValue(Collections.singletonMap(k, v)));
         mockCacheLoader.expects(once()).method("removeData").with(eq(parent));
         mockCacheLoader.expects(once()).method("put").with(eq(parent), eq(Collections.singletonMap("hello", "world")));
         n.replaceAll(Collections.singletonMap("hello", "world"));
   
  -      assertTrue(cache.peek(parent, false).isDataLoaded());
  +      assertDataLoaded(parent);
      }
   
      public void testLazyLoadDataWhenWorkingWithNode()
  @@ -300,7 +317,7 @@
         mockCacheLoader.expects(once()).method("get").with(eq(parent));
   
         cache.put(parent, k, v);
  -      assertTrue(cache.peek(parent, false).isDataLoaded());
  +      assertDataLoaded(parent);
         // evict the parent
         cache.evict(parent, false);
         assertNull(cache.peek(parent, false));
  @@ -309,12 +326,53 @@
         // now get node.
         Node n = cache.getRoot().getChild(parent);
         assertNotNull(n);
  -      assertFalse(cache.peek(parent, false).isDataLoaded());
  +      assertDataNotLoaded(parent);
   
         // will trigger a load
         mockCacheLoader.expects(once()).method("get").with(eq(parent)).will(returnValue(Collections.singletonMap(k, v)));
         assertEquals(v, n.get(k));
         // should change isDataLoaded to true
  -      assertTrue(cache.peek(parent, false).isDataLoaded());
  +      assertDataLoaded(parent);
  +   }
  +
  +   public void testDontLoadWhenKeyInMemory()
  +   {
  +      mockCacheLoader.expects(never()).method("get").withAnyArguments();
  +      mockCacheLoader.expects(never()).method("getChildrenNames").withAnyArguments();
  +      mockCacheLoader.expects(never()).method("exists").withAnyArguments();
  +      mockCacheLoader.expects(never()).method("remove").withAnyArguments();
  +      mockCacheLoader.expects(never()).method("removeData").withAnyArguments();
  +      mockCacheLoader.expects(never()).method("put").withAnyArguments();
  +
  +      mockCacheLoader.expects(once()).method("get").with(eq(parent));
  +      mockCacheLoader.expects(once()).method("put").with(eq(parent), eq(k), eq(v));
  +
  +      cache.put(parent, k, v);
  +      assertDataLoaded(parent);
  +
  +      // now evict
  +      cache.evict(parent, false);
  +
  +      // now to a put(Map)
  +      Map m = new HashMap();
  +      m.put("k2", "v2");
  +
  +      // should not load
  +      mockCacheLoader.expects(once()).method("put").with(eq(parent), eq(m));
  +      cache.put(parent, m);
  +
  +      assertDataNotLoaded(parent);
  +
  +      // now a get for an existing key should not trigger a load!
  +      assertEquals("v2", cache.get(parent, "k2"));
  +
  +      assertDataNotLoaded(parent);
  +
  +      // but going a get for a nonexistent key should!
  +      mockCacheLoader.expects(once()).method("get").with(eq(parent)).will(returnValue(Collections.singletonMap(k, v)));
  +      assertEquals(v, cache.get(parent, k));
  +
  +      // should not have overwritten in-memory data
  +      assertEquals("v2", cache.get(parent, "k2"));
      }
   }
  \ No newline at end of file
  
  
  



More information about the jboss-cvs-commits mailing list