You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2015/09/23 21:31:49 UTC

[2/2] wicket git commit: WICKET-5981 Fix the behavior of MarkupContainer#get(int) for non-empty MarkupContainer and non-existing index

WICKET-5981 Fix the behavior of MarkupContainer#get(int) for non-empty MarkupContainer and non-existing index


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/eb125865
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/eb125865
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/eb125865

Branch: refs/heads/master
Commit: eb125865624747fd2d216e16133a8d9181c19265
Parents: de5982b
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Wed Sep 23 21:30:20 2015 +0200
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Wed Sep 23 21:30:20 2015 +0200

----------------------------------------------------------------------
 .../java/org/apache/wicket/MarkupContainer.java |  8 +++--
 .../org/apache/wicket/MarkupContainerTest.java  | 33 ++++++++++++++++++--
 2 files changed, 35 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/eb125865/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java b/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
index 7941203..b739d47 100644
--- a/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
+++ b/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java
@@ -1016,15 +1016,17 @@ public abstract class MarkupContainer extends Component implements Iterable<Comp
 	@Deprecated
 	public final Component get(int index)
 	{
+		final int requestedIndex = index;
 		Component childAtIndex = null;
 		Iterator<Component> childIterator = iterator();
-		while (index-- >= 0 && childIterator.hasNext())
+		while (index >= 0 && childIterator.hasNext())
 		{
 			childAtIndex = childIterator.next();
+			index--;
 		}
-		if(childAtIndex == null) 
+		if(index >= 0 || childAtIndex == null)
 		{
-			throw new ArrayIndexOutOfBoundsException(Integer.toString(index));
+			throw new ArrayIndexOutOfBoundsException(Integer.toString(requestedIndex));
 		}
 		return childAtIndex;
 	}

http://git-wip-us.apache.org/repos/asf/wicket/blob/eb125865/wicket-core/src/test/java/org/apache/wicket/MarkupContainerTest.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/MarkupContainerTest.java b/wicket-core/src/test/java/org/apache/wicket/MarkupContainerTest.java
index b21757d..1a5698e 100644
--- a/wicket-core/src/test/java/org/apache/wicket/MarkupContainerTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/MarkupContainerTest.java
@@ -18,8 +18,6 @@ package org.apache.wicket;
 
 import static org.hamcrest.CoreMatchers.is;
 
-import java.util.NoSuchElementException;
-
 import org.apache.wicket.markup.IMarkupResourceStreamProvider;
 import org.apache.wicket.markup.html.WebComponent;
 import org.apache.wicket.markup.html.WebMarkupContainer;
@@ -27,7 +25,6 @@ import org.apache.wicket.markup.html.WebPage;
 import org.apache.wicket.util.resource.IResourceStream;
 import org.apache.wicket.util.resource.StringResourceStream;
 import org.apache.wicket.util.tester.WicketTestCase;
-import org.junit.Assert;
 import org.junit.Test;
 
 
@@ -131,6 +128,36 @@ public class MarkupContainerTest extends WicketTestCase
 	}
 
 	/**
+	 * Tests the get(int) method of MarkupContainer with non-existing index.
+	 */
+	@Test(expected = ArrayIndexOutOfBoundsException.class)
+	public void getIndexedOutOfBounds()
+	{
+		MarkupContainer c = new WebMarkupContainer("parent");
+		Component c1 = new WebComponent("c1");
+
+		c.add(c1);
+
+		assertThat(c.get(0), is(c1));
+		c.get(1);
+	}
+
+	/**
+	 * Tests the get(int) method of MarkupContainer with negative index.
+	 */
+	@Test(expected = ArrayIndexOutOfBoundsException.class)
+	public void getIndexedNegative()
+	{
+		MarkupContainer c = new WebMarkupContainer("parent");
+		Component c1 = new WebComponent("c1");
+
+		c.add(c1);
+
+		assertThat(c.get(0), is(c1));
+		c.get(-1);
+	}
+
+	/**
 	 * Tests the get(int) method of MarkupContainer when the index exceeds the number of children.
 	 */
 	@Test(expected = ArrayIndexOutOfBoundsException.class)