Skip to content

Commit 358ba2d

Browse files
wolf.bubenikserialcool
authored andcommitted
Fix ArrayIndexOutOfBoundsException when accessing single value of empty multi value property.
1 parent e020701 commit 358ba2d

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

magkit-core/src/main/java/de/ibmix/magkit/core/utils/PropertyUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import info.magnolia.jcr.wrapper.DelegatePropertyWrapper;
2424
import info.magnolia.jcr.wrapper.HTMLEscapingContentDecorator;
2525
import info.magnolia.jcr.wrapper.HTMLEscapingPropertyWrapper;
26+
import org.apache.commons.lang3.ArrayUtils;
2627
import org.apache.commons.lang3.StringUtils;
2728
import org.slf4j.Logger;
2829
import org.slf4j.LoggerFactory;
@@ -191,7 +192,7 @@ private static Value getUnwrappedValue(@Nullable final Property input) {
191192
Value result = null;
192193
if (exists(input)) {
193194
try {
194-
result = input.isMultiple() ? input.getValues()[0] : input.getValue();
195+
result = input.isMultiple() ? ArrayUtils.get(input.getValues(), 0) : input.getValue();
195196
} catch (RepositoryException e) {
196197
// ignore and return empty result
197198
LOGGER.debug("Cannot access value of property ", e);

magkit-core/src/test/java/de/ibmix/magkit/core/utils/PropertyUtilsTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222

2323
import de.ibmix.magkit.test.jcr.NodeStubbingOperation;
24+
import info.magnolia.jcr.decoration.ContentDecoratorPropertyWrapper;
2425
import info.magnolia.jcr.wrapper.HTMLEscapingPropertyWrapper;
2526
import org.junit.After;
2627
import org.junit.Before;
@@ -30,6 +31,7 @@
3031
import javax.jcr.Node;
3132
import javax.jcr.Property;
3233
import javax.jcr.RepositoryException;
34+
import javax.jcr.Value;
3335
import java.util.Calendar;
3436
import java.util.Collection;
3537
import java.util.TimeZone;
@@ -55,12 +57,14 @@
5557
import static de.ibmix.magkit.test.jcr.NodeMockUtils.mockNode;
5658
import static de.ibmix.magkit.test.jcr.NodeStubbingOperation.stubProperty;
5759
import static de.ibmix.magkit.test.jcr.PropertyMockUtils.mockProperty;
60+
import static de.ibmix.magkit.test.jcr.PropertyStubbingOperation.stubValues;
5861
import static de.ibmix.magkit.test.jcr.ValueMockUtils.mockBinary;
5962
import static org.hamcrest.MatcherAssert.assertThat;
6063
import static org.hamcrest.core.Is.is;
6164
import static org.hamcrest.core.IsNull.notNullValue;
6265
import static org.hamcrest.core.IsNull.nullValue;
6366
import static org.mockito.ArgumentMatchers.anyString;
67+
import static org.mockito.Mockito.doReturn;
6468
import static org.mockito.Mockito.doThrow;
6569

6670
/**
@@ -464,4 +468,27 @@ public void getBinaryValuesTest() throws RepositoryException {
464468
assertThat(getBinaryValues(mockProperty("test", b, a)).get(0), is(b));
465469
assertThat(getBinaryValues(mockProperty("test", b, a)).get(1), is(a));
466470
}
471+
472+
@Test
473+
public void getValue() throws RepositoryException {
474+
assertThat(PropertyUtils.getValue(null), nullValue());
475+
476+
Property p = mockProperty("test");
477+
assertThat(PropertyUtils.getValue(p), nullValue());
478+
479+
// Test nor exception on empty multi value property:
480+
stubValues(new Value[0]).of(p);
481+
doReturn(true).when(p).isMultiple();
482+
assertThat(PropertyUtils.getValue(p), nullValue());
483+
484+
stubValues("first").of(p);
485+
assertThat(PropertyUtils.getValue(p).getString(), is("first"));
486+
487+
stubValues("first", "second", "third").of(p);
488+
assertThat(PropertyUtils.getValue(p).getString(), is("first"));
489+
490+
// Test no NullPointerException on empty PropertyWrapper:
491+
Property wrapper = new ContentDecoratorPropertyWrapper<>(null, null);
492+
assertThat(PropertyUtils.getValue(wrapper), nullValue());
493+
}
467494
}

0 commit comments

Comments
 (0)