0 Replies Latest reply on Jul 6, 2010 7:05 AM by Nicolas Filotto

    Is-it standard to ignore null values?

    Nicolas Filotto Novice

      Hi All,

       

      During my investigation, I noticed that Infinispan ignores null values. Maybe I misunderstand the contract of a Map but I thought that if a map implementation doesn't support a null value, it is supposed to throw a NullPointerException otherwise it is supposed to take into account the null value. In case of inifinispan it seems to ignore it which cans be dangerous when we first put a non null value then we try to put a null value, so we believe that the current value is null but in fact it is still the non null value. If it is forbidden we expect a  NullPointerException otherwise cache.get should return null, do you agree with my assumption?

       

      I tried the following code with the most common implementations of a Map such as HashMap, ConcurrentHashMap, LinkedHashMap, Properties and WeakHashMap and for all of them the test case described below pass:

       

      {code}

       

         private void testNullValue(Map<String, String> cache) throws Exception
         {
            System.out.println("Test Null Value with " + cache.getClass());
            boolean supported = false;
            try
            {
               cache.put("a", null);
               // Null value supported
               supported = true;
            }
            catch (NullPointerException e)
            {
               // Null Value not supported : OK
            }
            catch (Exception e)
            {
               fail("Only NullPointerException is allowed");
            }
            System.out.println("The map of type " + cache.getClass() + " " + (supported ? "supports" : "doesn't support")
               + " null values");
            cache.put("a", "b");
            try
            {
               cache.put("a", null);
               // Null value supported
               if (supported)
               {
                  assertNull("As " + cache.getClass()+ " supports null value we expect 'null' as value", cache.get("a"));
               }
               else
               {
                  fail("'null values' are not supposed to be supported by " + cache.getClass());
               }
            }
            catch (NullPointerException e)
            {
               // Null Value not supported : OK
               if (supported)
               {
                  fail("'null values' are supposed to be supported by " + cache.getClass());
               }
               else
               {
                  assertEquals("As " + cache.getClass()+ " doesn't support null value we expect 'b' as value", "b", cache.get("a"));
               }
            }
            catch (Exception e)
            {
               fail("Only NullPointerException is allowed");
            }
         }

       

      {code}

       

      The same test case fails with infinispan since null values are ignored. I attached the full Test Case.

       

      Thank you for your answers,

      BR,

      Nicolas