Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    DataTools.castValue() returns incorrect value for "false"

    1. SmartClient Version: v8.3p_2013-02-06/Enterprise Deployment (built 2013-02-06)
    The same result occurs with the 4.0d build from 2013-05-30

    2. N/A (Server-side), but IE 9 and Firefox 21.0

    3. Logs do not indicate any error

    4. no problem processing the response

    5. no error

    6. JUnit test cases:
    Code:
    /**
     * Test Functionality of the isomorphic castValue method.
     */
    public class DataToolsCastValue {
        @Test
        public void castFalseValue() throws Exception {
            final String falseString = "false";
            final Boolean output = (Boolean) DataTools.castValue(falseString, Boolean.class);
            assertFalse("'false' is not cast to false.", output);  // fails
        }
    
        @Test
        public void castTrueValue() throws Exception {
            final String trueString = "true";
            final Boolean output = (Boolean) DataTools.castValue(trueString, Boolean.class);
            assertTrue("'true' is not cast to true.", output);  // passes
        }
    }
    This incorrect behavior is causing some hibernate queries to fail, when searching for objects containing an attribute of type boolean that are set to false.

    #2
    This is exactly what the method is documented to do:

    If targetType is Boolean and passed value is instance of String then "true", "t", "yes" and "y" (ignoring case) converted to true; any other string converted to false
    From here.

    It's not clear how your code ends up relying on "false" -> false conversion in the first place; possibly that's what to correct. But if you need to rely on this conversion and prefer a different conversion behavior you'll need to add it yourself, because we won't be changing the documented behavior of this method.

    Comment


      #3
      The test case shows that the behavior is not as documented. The String, "false", is not in the set {"true", "t", "yes", "y"}, so it should be converted to false.
      The assertion in the test case fails: the value "false" is not converted to false; it is converted to true.

      Comment


        #4
        Looking at the bytecode, it seems like the code is not converting this set to true: {"t", "y", "true", "yes"}. Instead, it's converting *this* set to true: {"t", "y", "true", "false"}.
        I updated the test case to verify this:
        Code:
        /**
         * Test Functionality of the isomorphic castValue method.
         */
        public class DataToolsCastValue {
            @Test
            public void castFalseValue() throws Exception {
                final String falseString = "false";
                final Boolean output = (Boolean) DataTools.castValue(falseString, Boolean.class);
                assertFalse("'false' is not cast to false.", output);  // fails
            }
        
            @Test
            public void castTrueValue() throws Exception {
                final String trueString = "true";
                final Boolean output = (Boolean) DataTools.castValue(trueString, Boolean.class);
                assertTrue("'true' is not cast to true.", output);  // passes
            }
        
            @Test
            public void castYesValue() throws Exception {
                final String yesString = "yes";
                final Boolean output = (Boolean) DataTools.castValue(yesString, Boolean.class);
                assertTrue("'yes' is not cast to true.", output);  // fails
            }
        }

        Comment


          #5
          Hi,

          Thanks. Corrected.
          You can test it with next nightly build.

          Regards,
          Alius

          Comment


            #6
            Tests pass now. Thanks.

            Comment

            Working...
            X