harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zhouke...@apache.org
Subject svn commit: r957416 - in /harmony/enhanced/java/trunk/classlib/modules/jndi/src: main/java/javax/naming/Reference.java test/java/org/apache/harmony/jndi/tests/javax/naming/ReferenceTest.java
Date Thu, 24 Jun 2010 04:56:54 GMT
Author: zhoukevin
Date: Thu Jun 24 04:56:54 2010
New Revision: 957416

URL: http://svn.apache.org/viewvc?rev=957416&view=rev
Log:
javax.naming.Reference.toString() should throw NullPointerException if it has a NULL element.
This patch fixes this behavior difference between Harmony and RI. In addition, it attaches
3 test cases to cover this change.

Modified:
    harmony/enhanced/java/trunk/classlib/modules/jndi/src/main/java/javax/naming/Reference.java
    harmony/enhanced/java/trunk/classlib/modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/ReferenceTest.java

Modified: harmony/enhanced/java/trunk/classlib/modules/jndi/src/main/java/javax/naming/Reference.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/jndi/src/main/java/javax/naming/Reference.java?rev=957416&r1=957415&r2=957416&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/jndi/src/main/java/javax/naming/Reference.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/jndi/src/main/java/javax/naming/Reference.java
Thu Jun 24 04:56:54 2010
@@ -282,9 +282,9 @@ public class Reference implements Clonea
     @Override
     public Object clone() {
         try {
-            Reference r = (Reference) super.clone();
-            r.addrs = (Vector<RefAddr>) this.addrs.clone();
-            return r;
+            Reference reference = (Reference) super.clone();
+            reference.addrs = (Vector<RefAddr>) this.addrs.clone();
+            return reference;
         } catch (CloneNotSupportedException e) {
             // jndi.03=Failed to clone object of Reference class.
             throw new AssertionError(Messages.getString("jndi.03")); //$NON-NLS-1$
@@ -305,12 +305,16 @@ public class Reference implements Clonea
      */
     @Override
     public boolean equals(Object o) {
-        if (o instanceof Reference) {
-            Reference r = (Reference) o;
-            return r.className.equals(this.className)
-                    && r.addrs.equals(this.addrs);
+        if (o == this) {
+            return true;
         }
-        return false;
+        if (!(o instanceof Reference)) {
+            return false;
+        }
+        Reference ref = (Reference) o;
+        return ref.className.equals(this.className)
+                && ref.addrs.size() == this.addrs.size()
+                && ref.addrs.equals(this.addrs);
     }
 
     /**
@@ -322,13 +326,12 @@ public class Reference implements Clonea
      */
     @Override
     public int hashCode() {
-        int i = this.className.hashCode();
+        int hashCode = this.className.hashCode();
         Enumeration<RefAddr> e = this.addrs.elements();
-
         while (e.hasMoreElements()) {
-            i += e.nextElement().hashCode();
+            hashCode += e.nextElement().hashCode();
         }
-        return i;
+        return hashCode;
     }
 
     /**
@@ -337,18 +340,16 @@ public class Reference implements Clonea
      * 
      * @return the string representation of this object
      */
-    @SuppressWarnings("nls")
     @Override
     public String toString() {
-        StringBuilder s = new StringBuilder("Reference Class Name: ");
-        s.append(className);
-        s.append("\n");
-
-        Enumeration<RefAddr> e = this.addrs.elements();
-        while (e.hasMoreElements()) {
-            s.append(e.nextElement());
+        StringBuilder sb = new StringBuilder("Reference Class Name: "); //$NON-NLS-1$
+        sb.append(this.className).append('\n');
+        if (addrs.size() > 0) {
+            Enumeration<RefAddr> elements = this.addrs.elements();
+            while (elements.hasMoreElements()) {
+                sb.append(elements.nextElement().toString());
+            }
         }
-        return s.toString();
+        return sb.toString();
     }
-
 }

Modified: harmony/enhanced/java/trunk/classlib/modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/ReferenceTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/ReferenceTest.java?rev=957416&r1=957415&r2=957416&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/ReferenceTest.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/ReferenceTest.java
Thu Jun 24 04:56:54 2010
@@ -83,15 +83,35 @@ public class ReferenceTest extends TestC
 		assertEquals(1, reference.size());
 	}
 
-	public void testConstructor_ByRefAddrNull() {
-		Reference reference = new Reference(null, null);
-
-		assertNull(reference.getClassName());
-		assertNull(reference.getFactoryClassName());
-		assertNull(reference.getFactoryClassLocation());
-		assertNull(reference.get(0));
-		assertEquals(1, reference.size());
-	}
+    public void testConstructor_ByRefAddrNull() {
+        Reference reference = new Reference(null, null);
+        assertNull(reference.getClassName());
+        assertNull(reference.getFactoryClassName());
+        assertNull(reference.getFactoryClassLocation());
+        assertNull(reference.get(0));
+        assertEquals(1, reference.size());
+
+        try {
+            reference.get("type");
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+
+        try {
+            reference.toString();
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+
+        try {
+            reference.hashCode();
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
 
 	public void testConstructor_ByFactory() {
 		String className = "java.util.Hashtable";
@@ -194,24 +214,25 @@ public class ReferenceTest extends TestC
 		assertEquals(refAddr2, ref.get(1));
 	}
 
-	public void testAdd_ByIndexInvalidGreat() {
-		String type = "Binary";
-		BinaryRefAddr refAddr = new BinaryRefAddr(type, buffer);
-		try {
-			ref.add(1, refAddr);
-			fail("This should throw a ArrayIndexOutOfBoundsException");
-		} catch (ArrayIndexOutOfBoundsException e) {
-		}
-	}
+    public void testAdd_ByIndexInvalidGreat() {
+        BinaryRefAddr refAddr = new BinaryRefAddr("Binary", buffer);
+        try {
+            ref.add(1, refAddr);
+            fail("should throw ArrayIndexOutOfBoundsException");
+        } catch (ArrayIndexOutOfBoundsException e) {
+            // Expected
+        }
+    }
 
 	public void testAdd_ByIndexInvalidLess() {
 		String type = "Binary";
 		BinaryRefAddr refAddr = new BinaryRefAddr(type, buffer);
 		try {
 			ref.add(-1, refAddr);
-			fail("This should throw a ArrayIndexOutOfBoundsException");
-		} catch (ArrayIndexOutOfBoundsException e) {
-		}
+			fail("should throw ArrayIndexOutOfBoundsException");
+        } catch (ArrayIndexOutOfBoundsException e) {
+            // Expected
+        }
 	}
 
 	public void testGet_SimpleInvalidGreat() {
@@ -221,9 +242,10 @@ public class ReferenceTest extends TestC
 
 		try {
 			ref.get(ref.size());
-			fail("This should throw a ArrayIndexOutOfBoundsException");
-		} catch (ArrayIndexOutOfBoundsException e) {
-		}
+			fail("should throw ArrayIndexOutOfBoundsException");
+        } catch (ArrayIndexOutOfBoundsException e) {
+            // Expected
+        }
 	}
 
 	public void testGet_SimpleInvalidLess() {
@@ -233,9 +255,10 @@ public class ReferenceTest extends TestC
 
 		try {
 			ref.get(-1);
-			fail("This should throw a ArrayIndexOutOfBoundsException");
-		} catch (ArrayIndexOutOfBoundsException e) {
-		}
+			fail("should throw ArrayIndexOutOfBoundsException");
+        } catch (ArrayIndexOutOfBoundsException e) {
+            // Expected
+        }
 	}
 
 	public void testGet_ByType() {
@@ -269,8 +292,9 @@ public class ReferenceTest extends TestC
 		ref.add(refAddr);
 		try {
 			ref.get(null);
-			fail("Should throw NullPointerException.");
+			fail("Should throw NullPointerException");
 		} catch (NullPointerException e) {
+		    // Expected
 		}
 	}
 
@@ -304,9 +328,10 @@ public class ReferenceTest extends TestC
 	public void testRemove_Invalid() {
 		try {
 			ref.remove(0);
-			fail("This should throw a ArrayIndexOutOfBoundsException");
-		} catch (ArrayIndexOutOfBoundsException e) {
-		}
+			fail("should throw ArrayIndexOutOfBoundsException");
+        } catch (ArrayIndexOutOfBoundsException e) {
+            // Expected
+        }
 	}
 
 	public void testClear_Simple() {
@@ -425,8 +450,9 @@ public class ReferenceTest extends TestC
 
 		try {
 			reference0.equals(reference1);
-			fail("Should throw NullPointerException.");
+			fail("Should throw NullPointerException");
 		} catch (NullPointerException e) {
+		    // Expected
 		}
 	}
 
@@ -437,12 +463,7 @@ public class ReferenceTest extends TestC
 
 		Reference reference0 = new Reference(null, classFactory, location);
 		Reference reference2 = new Reference(className, classFactory, location);
-
-		// try {
 		assertFalse(reference0.equals(reference2));
-		// fail("Should throw NullPointerException.");
-		// } catch (NullPointerException e) {
-		// }
 	}
 
 	public void testEquals_NullClassName3() {
@@ -455,8 +476,9 @@ public class ReferenceTest extends TestC
 
 		try {
 			reference2.equals(reference0);
-			fail("Should throw NullPointerException.");
+			fail("Should throw NullPointerException");
 		} catch (NullPointerException e) {
+		    // Expected
 		}
 	}
 



Mime
View raw message