harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r561285 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Stack.java test/api/common/tests/api/java/util/StackTest.java
Date Tue, 31 Jul 2007 09:39:45 GMT
Author: leoli
Date: Tue Jul 31 02:39:44 2007
New Revision: 561285

URL: http://svn.apache.org/viewvc?view=rev&rev=561285
Log:
Apply patch for HARMONY-4577( [classlib][luni] Stack.pop method of harmony throws EmptyStackException,
while RI throws ArrayIndexOutOfBoundsException).

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Stack.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/StackTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Stack.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Stack.java?view=diff&rev=561285&r1=561284&r2=561285
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Stack.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Stack.java Tue Jul
31 02:39:44 2007
@@ -39,7 +39,7 @@
 	 * 
 	 * @return true if the stack is empty, false otherwise
 	 */
-	public boolean empty() {
+	public synchronized boolean empty() {
 		return elementCount == 0;
 	}
 
@@ -71,14 +71,14 @@
 	 */
 	@SuppressWarnings("unchecked")
     public synchronized E pop() {
-		try {
-			int index = elementCount - 1;
-			E obj = (E)elementData[index];
-			removeElementAt(index);
-			return obj;
-		} catch (IndexOutOfBoundsException e) {
-			throw new EmptyStackException();
-		}
+		if(elementCount == 0){
+            throw new EmptyStackException();
+        }
+		final int index = --elementCount;
+		final E obj = (E)elementData[index];
+		elementData[index] = null;
+        modCount++;
+		return obj;
 	}
 
 	/**
@@ -92,7 +92,7 @@
 	 * @see #peek
 	 * @see #pop
 	 */
-	public synchronized E push(E object) {
+	public E push(E object) {
 		addElement(object);
 		return object;
 	}

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/StackTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/StackTest.java?view=diff&rev=561285&r1=561284&r2=561285
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/StackTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/StackTest.java
Tue Jul 31 02:39:44 2007
@@ -155,6 +155,45 @@
 		assertEquals("Search returned incorrect value for search for null--wanted -1",
 				-1, s.search(null));
 	}
+	
+	static class BugStack<E> extends Stack<E>{
+		/**
+		 * 
+		 */
+		private static final long serialVersionUID = -9133762075342926141L;
+
+		/**
+		 * 
+		 */
+		public void setLength(int elementCount)
+		{
+			this.elementCount = elementCount;
+		}
+		
+		public int getLength()
+		{
+			return elementCount;
+		}
+	}
+	
+	//test for wrong exception threw by pop method
+	public void test_pop_modify_elementCount(){
+		BugStack<String> testStack = new BugStack<String>();
+		testStack.push("A");
+		testStack.push("B");
+		testStack.setLength(20);
+		try{
+			testStack.pop();
+		}
+		catch(ArrayIndexOutOfBoundsException e)
+		{
+			//Expected to throw ArrayIndexOutOfBoundsException here
+		}
+		catch(EmptyStackException e)
+		{
+			fail("Should throw ArrayIndexOutOfBoundsException here");
+		}
+	}
 
 	/**
 	 * Sets up the fixture, for example, open a network connection. This method



Mime
View raw message