[ 
https://issues.apache.org/jira/browse/SLING-5257?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Guillaume Douillet updated SLING-5257:
--------------------------------------
    Description: 
Hello,

When the underlying property value is an empty array, calling 
{{MockProperty.getType}} generates an 
{{java.lang.ArrayIndexOutOfBoundsException}}.

Here a small test case exhibiting this behavior (to put in 
{{org.apache.sling.testing.mock.jcr.MockPropertyTest}}) :
{code}
   @Test
    public void testEmptyArrayGetType() throws RepositoryException {
        this.node1.setProperty("prop1", new Value[] {});
        Property prop1 = this.node1.getProperty("prop1");
        assertTrue(prop1.isMultiple());
        assertEquals(PropertyType.UNDEFINED, prop1.getType());
    }
{code}

A fix could be to return {{PropertyType.UNDEFINED}} when the underlying array 
value is empty :
 {code}
    @Override
    public int getType() throws RepositoryException {
        if (this.itemData.getValues().length>0)
        {
                return this.itemData.getValues()[0].getType();
        }
        else
        {
                return PropertyType.UNDEFINED;
        }
    }
{code}

  was:
Hello,

According to JCR 2.0 API, {{javax.jcr.Property.getValues()}} should throw 
{{ValueFormatException}} when the underlying property is single-valued :
{code}
 /**
     * Returns an array of all the values of this property. Used to access
     * multi-value properties. The array returned is a copy of the stored
     * values, so changes to it are not reflected in internal storage.
     *
     * @return a <code>Value</code> array.
     * @throws ValueFormatException if the property is single-valued.
     * @throws RepositoryException  if another error occurs.
     */
    public Value[] getValues() throws ValueFormatException, RepositoryException;
{code}

However, {{org.apache.sling.testing.mock.jcr.MockProperty}} implements a 
different behavior. {{ValueFormatException}} is not thrown but an array 
containing a single value is returned instead.

Here a small test case exhibiting this behavior (to put in 
{{org.apache.sling.testing.mock.jcr.MockPropertyTest}}) :
{code}
    @Test(expected=ValueFormatException.class)
    public void testSingleValueAsValueArray() throws RepositoryException {
        this.node1.setProperty("prop1", 
this.session.getValueFactory().createValue("value1"));
        Property prop1 = this.node1.getProperty("prop1");
        assertFalse(prop1.isMultiple());
        assertEquals("value1", prop1.getValues()[0].getString());
    }
{code}


> [TESTING][JCR MOCK] MockProperty getType generates 
> java.lang.ArrayIndexOutOfBoundsException when the underlying array value is 
> empty
> ------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: SLING-5257
>                 URL: https://issues.apache.org/jira/browse/SLING-5257
>             Project: Sling
>          Issue Type: Bug
>          Components: Testing
>    Affects Versions: Testing JCR Mock 1.1.10
>            Reporter: Guillaume Douillet
>
> Hello,
> When the underlying property value is an empty array, calling 
> {{MockProperty.getType}} generates an 
> {{java.lang.ArrayIndexOutOfBoundsException}}.
> Here a small test case exhibiting this behavior (to put in 
> {{org.apache.sling.testing.mock.jcr.MockPropertyTest}}) :
> {code}
>    @Test
>     public void testEmptyArrayGetType() throws RepositoryException {
>         this.node1.setProperty("prop1", new Value[] {});
>         Property prop1 = this.node1.getProperty("prop1");
>         assertTrue(prop1.isMultiple());
>         assertEquals(PropertyType.UNDEFINED, prop1.getType());
>     }
> {code}
> A fix could be to return {{PropertyType.UNDEFINED}} when the underlying array 
> value is empty :
>  {code}
>     @Override
>     public int getType() throws RepositoryException {
>       if (this.itemData.getValues().length>0)
>       {
>               return this.itemData.getValues()[0].getType();
>       }
>       else
>       {
>               return PropertyType.UNDEFINED;
>       }
>     }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to