harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mloe...@apache.org
Subject svn commit: r465190 - in /incubator/harmony/enhanced/classlib/trunk/modules/beans/src: main/java/java/beans/ main/java/org/apache/harmony/beans/ main/java/org/apache/harmony/beans/internal/nls/ test/java/org/apache/harmony/beans/tests/java/beans/
Date Wed, 18 Oct 2006 09:51:17 GMT
Author: mloenko
Date: Wed Oct 18 02:51:16 2006
New Revision: 465190

URL: http://svn.apache.org/viewvc?view=rev&rev=465190
Log:
applied patch from HARMONY-1611
[classlib][beans] refactoring of PersistenceDelegateTest

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PersistenceDelegate.java
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/ObjectNode.java
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PersistenceDelegate.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PersistenceDelegate.java?view=diff&rev=465190&r1=465189&r2=465190
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PersistenceDelegate.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PersistenceDelegate.java
Wed Oct 18 02:51:16 2006
@@ -17,14 +17,18 @@
 
 package java.beans;
 
-public abstract class PersistenceDelegate {
+import org.apache.harmony.beans.internal.nls.Messages;
 
-    public PersistenceDelegate() {
-    }
+public abstract class PersistenceDelegate {
 
     protected void initialize(Class<?> type, Object oldInstance,
             Object newInstance, Encoder out) {
-        if ((out != null) && (type != null)) {
+
+        if (type == null) {
+            throw new NullPointerException(Messages.getString("beans.4B")); //$NON-NLS-1$
+        }
+
+        if (out != null) {
             PersistenceDelegate pd = out.getPersistenceDelegate(type
                     .getSuperclass());
 
@@ -61,8 +65,6 @@
             }
 
             out.writeExpression(instantiate(oldInstance, out));
-            newInstance = out.get(oldInstance);
-            initialize(oldInstance.getClass(), oldInstance, newInstance, out);
         }
     }
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/ObjectNode.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/ObjectNode.java?view=diff&rev=465190&r1=465189&r2=465190
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/ObjectNode.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/ObjectNode.java
Wed Oct 18 02:51:16 2006
@@ -59,29 +59,30 @@
     }
 
     public Object getObjectValue() throws Exception {
-        if (objectValue != null) {
-            return objectValue;
-        }
-
-        if (nodes != null) {
-            Object[] oldArgs = initializer.getArguments();
-            Object[] newArgs = new Object[oldArgs.length];
-
-            for (int i = 0; i < oldArgs.length; ++i) {
-                if (oldArgs[i] != null) {
-                    ObjectNode node = nodes.get(oldArgs[i]);
-
-                    newArgs[i] = node.getObjectValue();
-                } else {
-                    newArgs[i] = null;
-                }
-            }
-
-            objectValue = (new Expression(initializer.getTarget(), initializer
-                    .getMethodName(), newArgs)).getValue();
-        } else {
+        if (objectValue == null) {
             objectValue = initializer.getValue();
         }
+
+        // XXX We probably don't need this. Investigate.
+//        if (nodes != null) {
+//            Object[] oldArgs = initializer.getArguments();
+//            Object[] newArgs = new Object[oldArgs.length];
+//
+//            for (int i = 0; i < oldArgs.length; ++i) {
+//                if (oldArgs[i] != null) {
+//                    ObjectNode node = nodes.get(oldArgs[i]);
+//
+//                    newArgs[i] = node.getObjectValue();
+//                } else {
+//                    newArgs[i] = null;
+//                }
+//            }
+//
+//            objectValue = (new Expression(initializer.getTarget(), initializer
+//                    .getMethodName(), newArgs)).getValue();
+//        } else {
+//            objectValue = initializer.getValue();
+//        }
 
         return objectValue;
     }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties?view=diff&rev=465190&r1=465189&r2=465190
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties
Wed Oct 18 02:51:16 2006
@@ -89,3 +89,6 @@
 beans.47=Unable to instantiate property editor
 beans.48=Property editor is not assignable from the PropertyEditor interface
 beans.49=Child cannot implement both BeanContextChild and BeanContextProxy
+beans.4A=newInstance is null
+beans.4B=type is null
+

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java?view=diff&rev=465190&r1=465189&r2=465190
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java
Wed Oct 18 02:51:16 2006
@@ -23,6 +23,9 @@
 import java.beans.PersistenceDelegate;
 import java.beans.Statement;
 
+import java.util.Stack;
+import java.util.EmptyStackException;
+
 import junit.framework.TestCase;
 
 import org.apache.harmony.beans.tests.support.mock.MockFoo;
@@ -52,42 +55,14 @@
      * Tests writeObject() under normal condition when mutatesTo() returns True.
      */
     public void testWriteObject_NormalMutatesToTrue() {
-        MockPersistenceDelegate pd = new MockPersistenceDelegate(true, true);
-        MockEncoder enc = new MockEncoder();
-        MockFoo oldInstance = new MockFoo();
-        enc.setPersistenceDelegate(MockFooStop.class, pd);
-
-        pd.writeObject(oldInstance, enc);
-
-        // should have called initialize()
-        assertSame(enc, CallVerificationStack.getInstance().pop());
-        assertSame(null, CallVerificationStack.getInstance().pop());
-        assertSame(oldInstance, CallVerificationStack.getInstance().pop());
-        assertEquals(
-                "org.apache.harmony.beans.tests.java.beans.PersistenceDelegateTest$MockPersistenceDelegate",
-                CallVerificationStack.getInstance().getCurrentSourceClass());
-        assertEquals("initialize", CallVerificationStack.getInstance()
-                .getCurrentSourceMethod());
-        assertSame(MockFoo.class, CallVerificationStack.getInstance().pop());
-
-        // should have called mutatesTo()
-        assertSame(null, CallVerificationStack.getInstance().pop());
-        assertEquals(
-                "org.apache.harmony.beans.tests.java.beans.PersistenceDelegateTest$MockPersistenceDelegate",
-                CallVerificationStack.getInstance().getCurrentSourceClass());
-        assertEquals("mutatesTo", CallVerificationStack.getInstance()
-                .getCurrentSourceMethod());
-        assertSame(oldInstance, CallVerificationStack.getInstance().pop());
-
-        // should have called Encoder.get()
-        assertEquals(
-                "org.apache.harmony.beans.tests.java.beans.PersistenceDelegateTest$MockEncoder",
-                CallVerificationStack.getInstance().getCurrentSourceClass());
-        assertEquals("get", CallVerificationStack.getInstance()
-                .getCurrentSourceMethod());
-        assertSame(oldInstance, CallVerificationStack.getInstance().pop());
-
-        assertTrue(CallVerificationStack.getInstance().empty());
+        MockPersistenceDelegate2 pd = new MockPersistenceDelegate2(true);
+        MockEncoder2 enc = new MockEncoder2();
+        MockFoo foo = new MockFoo();
+
+        pd.writeObject(foo, enc);
+        
+        assertEquals("initialize", pd.popMethod());
+        assertEquals("mutatesTo", pd.popMethod());
     }
 
     /*
@@ -95,65 +70,23 @@
      * false.
      */
     public void testWriteObject_NormalMutatesToFalse() {
-        MockPersistenceDelegate pd = new MockPersistenceDelegate(false, true);
-        MockEncoder enc = new MockEncoder();
-        MockFoo oldInstance = new MockFoo();
-        enc.setPersistenceDelegate(MockFooStop.class, pd);
-
-        pd.writeObject(oldInstance, enc);
-
-        // should have called writeExpression()
-        assertEquals(
-                "org.apache.harmony.beans.tests.java.beans.PersistenceDelegateTest$MockEncoder",
-                CallVerificationStack.getInstance().getCurrentSourceClass());
-        assertEquals("writeExpression", CallVerificationStack.getInstance()
-                .getCurrentSourceMethod());
-        assertTrue(CallVerificationStack.getInstance().pop() instanceof Expression);
-
-        // should have called instantiate()
-        assertSame(enc, CallVerificationStack.getInstance().pop());
-        assertEquals(
-                "org.apache.harmony.beans.tests.java.beans.PersistenceDelegateTest$MockPersistenceDelegate",
-                CallVerificationStack.getInstance().getCurrentSourceClass());
-        assertEquals("instantiate", CallVerificationStack.getInstance()
-                .getCurrentSourceMethod());
-        assertSame(oldInstance, CallVerificationStack.getInstance().pop());
-
-        // should have called Encoder.remove()
-        assertEquals(
-                "org.apache.harmony.beans.tests.java.beans.PersistenceDelegateTest$MockEncoder",
-                CallVerificationStack.getInstance().getCurrentSourceClass());
-        assertEquals("remove", CallVerificationStack.getInstance()
-                .getCurrentSourceMethod());
-        assertSame(oldInstance, CallVerificationStack.getInstance().pop());
-
-        // should have called mutatesTo()
-        assertSame(null, CallVerificationStack.getInstance().pop());
-        assertEquals(
-                "org.apache.harmony.beans.tests.java.beans.PersistenceDelegateTest$MockPersistenceDelegate",
-                CallVerificationStack.getInstance().getCurrentSourceClass());
-        assertEquals("mutatesTo", CallVerificationStack.getInstance()
-                .getCurrentSourceMethod());
-        assertSame(oldInstance, CallVerificationStack.getInstance().pop());
-
-        // should have called Encoder.get()
-        assertEquals(
-                "org.apache.harmony.beans.tests.java.beans.PersistenceDelegateTest$MockEncoder",
-                CallVerificationStack.getInstance().getCurrentSourceClass());
-        assertEquals("get", CallVerificationStack.getInstance()
-                .getCurrentSourceMethod());
-        assertSame(oldInstance, CallVerificationStack.getInstance().pop());
-
-        assertTrue(CallVerificationStack.getInstance().empty());
+        MockPersistenceDelegate2 pd = new MockPersistenceDelegate2(false);
+        MockEncoder2 enc = new MockEncoder2();
+        MockFoo foo = new MockFoo();
+
+        pd.writeObject(foo, enc);
+        
+        assertEquals("instantiate", pd.popMethod());
+        assertEquals("mutatesTo", pd.popMethod());
+        assertWasAdded(MockFoo.class.getClass(), "new", null, enc);
     }
 
     /*
      * Tests writeObject() when object is null.
      */
     public void testWriteObject_NullObject() {
-        MockPersistenceDelegate pd = new MockPersistenceDelegate();
-        MockEncoder enc = new MockEncoder();
-        enc.setPersistenceDelegate(MockFooStop.class, pd);
+		MockPersistenceDelegate2 pd = new MockPersistenceDelegate2();
+		Encoder enc = new Encoder();
 
         try {
             pd.writeObject(null, enc);
@@ -167,9 +100,7 @@
      * Tests writeObject() when encoder is null.
      */
     public void testWriteObject_NullEncoder() {
-        MockPersistenceDelegate pd = new MockPersistenceDelegate();
-        MockEncoder enc = new MockEncoder();
-        enc.setPersistenceDelegate(MockFooStop.class, pd);
+		MockPersistenceDelegate2 pd = new MockPersistenceDelegate2();
 
         try {
             pd.writeObject(new MockFoo(), null);
@@ -284,14 +215,73 @@
      */
     public void testMutatesTo_Null() {
         DummyPersistenceDelegate pd = new DummyPersistenceDelegate();
+
         assertFalse(pd.mutatesTo("test", null));
         assertFalse(pd.mutatesTo(null, null));
+        assertFalse(pd.mutatesTo(null, "test"));
+    }
+
+    
+    // <--
+
+    private void assertWasAdded(Class targetClass, String methodName,
+                                 Object[] args, MockEncoder2 enc) {
         try {
-            pd.mutatesTo(null, "test");
-            fail("Should throw NullPointerException!");
-        } catch (NullPointerException ex) {
-            // expected
+            while (true) {
+                Statement stmt = enc.pop();
+
+                if (equals(stmt, targetClass, methodName, args)) {
+                    break;
+                }
+            }
+        } catch (EmptyStackException e) {
+            fail("Required statement was not found");
+        }
+    }
+
+    private boolean equals(Statement stmt, Class targetClass,
+                              String methodName, Object[] args) {
+           
+        if (stmt == null || !methodName.equals(stmt.getMethodName())) {
+            return false;
+        }
+
+        if (targetClass != null) {
+            // check if we have the object of the same class at least
+            if (targetClass != stmt.getTarget().getClass()) {
+                return false;
+            }
+        } else {
+            if (stmt.getTarget() != null) {
+                return false;
+            }
+        }
+        
+        if (args != null) {
+            if (stmt.getArguments() == null ||
+                args.length != stmt.getArguments().length) {
+                return false;
+            }
+
+            for (int i = 0; i < args.length; i++) {
+
+                if (args[i] != null) {
+                    if (!args[i].equals(stmt.getArguments()[i])) {
+                        return false;
+                    }
+                } else {
+                    if (stmt.getArguments()[i] != null) {
+                        return false;
+                    }
+                }
+            }
+        } else {
+            if (stmt.getArguments() != null && stmt.getArguments().length > 0)
{
+                return false;
+            }
         }
+        
+        return true;
     }
 
     /*
@@ -329,7 +319,7 @@
         }
 
         @Override
-        public PersistenceDelegate getPersistenceDelegate(Class type) {
+        public PersistenceDelegate getPersistenceDelegate(Class<?> type) {
             return super.getPersistenceDelegate(type);
         }
 
@@ -349,7 +339,7 @@
         }
 
         @Override
-        public void setPersistenceDelegate(Class type,
+        public void setPersistenceDelegate(Class<?> type,
                 PersistenceDelegate persistenceDelegate) {
             super.setPersistenceDelegate(type, persistenceDelegate);
         }
@@ -413,7 +403,7 @@
         }
 
         @Override
-        public void initialize(Class type, Object oldInstance,
+        public void initialize(Class<?> type, Object oldInstance,
                 Object newInstance, Encoder enc) {
             StackTraceElement[] eles = (new Throwable()).getStackTrace();
             if (!this.filter
@@ -445,6 +435,65 @@
 
     }
 
+    static class MockEncoder2 extends Encoder {
+        Stack<Statement> stmts = new Stack<Statement>();
+        
+        public void writeExpression(Expression expr) {
+            stmts.push(expr);
+            super.writeExpression(expr);
+        }
+
+        public void writeStatement(Statement stmt) {
+            stmts.push(stmt);
+            super.writeStatement(stmt);
+        }
+        
+        public void writeObject(Object obj) {
+            super.writeObject(obj);
+        }
+
+        public Statement pop() {
+            return stmts.pop();
+        }
+
+    }
+
+    static class MockPersistenceDelegate2 extends PersistenceDelegate {
+        private Boolean mutatesToFlag = null;
+        Stack<String> methods = new Stack<String>();
+
+        public MockPersistenceDelegate2() {}
+        
+        public MockPersistenceDelegate2(boolean mutatesToFlag) {
+            this.mutatesToFlag = mutatesToFlag;
+        }
+
+        public void initialize(Class<?> type, Object oldInstance,
+                Object newInstance, Encoder enc) {
+            methods.push("initialize");
+            super.initialize(type, oldInstance, newInstance, enc);
+        }
+        
+        public Expression instantiate(Object oldInstance, Encoder out) {
+            methods.push("instantiate");
+            return new Expression(oldInstance.getClass(), "new", null);
+        }
+        
+        public boolean mutatesTo(Object oldInstance, Object newInstance) {
+            methods.push("mutatesTo");
+
+            if (mutatesToFlag != null) {
+                return mutatesToFlag;
+            } else {
+                return super.mutatesTo(oldInstance, newInstance);
+            }
+        }
+        
+        String popMethod() {
+            return methods.pop();
+        }
+    }
+
     /*
      * Dummy PersistenceDelegate subclass.
      */
@@ -455,7 +504,7 @@
         }
 
         @Override
-        public void initialize(Class type, Object oldInstance,
+        public void initialize(Class<?> type, Object oldInstance,
                 Object newInstance, Encoder enc) {
             super.initialize(type, oldInstance, newInstance, enc);
         }



Mime
View raw message