Commits

Anonymous committed f5b9b78

Comments (0)

Files changed (1)

src/test/com/opensymphony/xwork/util/InstantiatingNullHandlerTest.java

  */
 package com.opensymphony.xwork.util;
 
+import java.util.Map;
+
 import junit.framework.TestCase;
+import ognl.OgnlContext;
+
+import com.opensymphony.xwork.test.TestBean2;
 
 
 /**
  * DOCUMENT ME!
  *
- * @author $author$
+ * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
  * @version $Revision$
  */
 public class InstantiatingNullHandlerTest extends TestCase {
 
-    public void testBlank() {
-
-    }
     /*public void testInheritance() {
         Tiger t = new Tiger();
         CompoundRoot root = new CompoundRoot();
         assertNotNull(kittenList);
         assertTrue(kittenList instanceof List);
     }*/
+    
+    public void testNullPropertyValueWithoutCreateNullObjects() {
+        Map context = new OgnlContext();
+        context.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.FALSE);
+        
+        CompoundRoot root = new CompoundRoot();
+        TestBean2 testBean =  new TestBean2();
+        root.add(testBean);
+        
+        InstantiatingNullHandler nh = new InstantiatingNullHandler();
+        
+        Object cat = nh.nullPropertyValue(context, root, "cat");
+        
+        assertNull(cat); 
+    }
+    
+    public void testNullPropertyValue() {
+        Map context = new OgnlContext();
+        context.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.TRUE);
+        
+        CompoundRoot root = new CompoundRoot();
+        TestBean2 testBean =  new TestBean2();
+        root.add(testBean);
+        
+        InstantiatingNullHandler nh = new InstantiatingNullHandler();
+        
+        Object cat = nh.nullPropertyValue(context, root, "cat");
+        
+        assertNotNull(cat);
+        assertEquals(Cat.class, cat.getClass()); 
+    }
+ 
+    public void testNullPropertyValueWithWrongInnerBean() {
+        Map context = new OgnlContext();
+        context.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.TRUE);
+        
+        CompoundRoot root = new CompoundRoot();
+        TestBean3 testBean =  new TestBean3();
+        root.add(testBean);
+        
+        InstantiatingNullHandler nh = new InstantiatingNullHandler();
+        
+        Object cat = nh.nullPropertyValue(context, root, "cat");
+        
+        assertNotNull(cat);
+        assertEquals(Cat.class, cat.getClass()); 
+        
+        Object anotherBean = nh.nullPropertyValue(context, root, "anotherBean");
+
+        assertNull(anotherBean);
+    }
+    
+    private static class TestBean3 extends TestBean2 {
+        private AnotherBean anotherBean;
+        
+        public void setAnotherBean(AnotherBean bean) {
+            this.anotherBean = bean;
+        }
+        
+        public AnotherBean getAnotherBean() {
+            return this.anotherBean;
+        }
+    }
+    
+    private static class AnotherBean {
+        public AnotherBean(int dummyCtorParam) {
+        }
+    }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.