1 7 package com.inversoft.verge.util.test; 8 9 10 import com.inversoft.beans.BeanException; 11 import com.inversoft.junit.WebTestCase; 12 import com.inversoft.verge.util.ScopeConstants; 13 import com.inversoft.verge.util.WebBean; 14 import com.inversoft.verge.util.WebBeanProperty; 15 16 17 24 public class WebBeanPropertyTest extends WebTestCase { 25 26 29 public WebBeanPropertyTest(String name) { 30 super(name); 31 setLocal(true); 32 } 33 34 35 38 public void testBean() { 39 try { 40 WebBeanProperty wbp = new WebBeanProperty("test.string1", ScopeConstants.PAGE_INT, Bean1.class); 41 assertTrue("should have id of test", wbp.getWebBean().getID().equals("test")); 42 assertTrue("should have property of string1", wbp.getPropertyName().equals("string1")); 43 assertTrue("should have propertyType of Stringt", wbp.getPropertyType() == String .class); 44 45 wbp = new WebBeanProperty("test.string1", ScopeConstants.REQUEST_INT, Bean1.class); 46 wbp = new WebBeanProperty("test.string1", ScopeConstants.SESSION_INT, Bean1.class); 47 wbp = new WebBeanProperty("test.string1", ScopeConstants.APPLICATION_INT, Bean1.class); 48 } catch (BeanException e) { 49 fail(e.toString()); 50 } 51 52 try { 53 new WebBeanProperty("test.string1", 10, Bean1.class); 54 fail("Should have failed"); 56 } catch (BeanException be) { 57 } 59 try { 60 new WebBeanProperty("test", 10, Bean1.class); 61 fail("Should have failed"); 62 } catch (BeanException be) { 63 } 65 try { 66 new WebBeanProperty("test", null); 67 fail("Should have failed"); 68 } catch (BeanException be) { 69 fail(be.toString()); 70 } catch (NullPointerException npe) { 71 } 73 74 try { 75 Bean1 bean1_request = new Bean1(); 76 request.setAttribute("test-request", bean1_request); 77 Bean1 bean1_session = new Bean1(); 78 request.getSession().setAttribute("test-session", bean1_session); 79 Bean1 bean1_context = new Bean1(); 80 context.setAttribute("test-context", bean1_context); 81 82 WebBeanProperty wbp = new WebBeanProperty("test-request.string1", ScopeConstants.REQUEST_INT, Bean1.class); 83 assertTrue("Should be same instance from request", 84 wbp.getWebBean().getInstance(request) == bean1_request); 85 wbp = new WebBeanProperty("test-session.string1", ScopeConstants.SESSION_INT, Bean1.class); 86 assertTrue("Should be same instance from session", 87 wbp.getWebBean().getInstance(request) == bean1_session); 88 wbp = new WebBeanProperty("test-context.string1", ScopeConstants.APPLICATION_INT, Bean1.class); 89 assertTrue("Should be same instance from context", 90 wbp.getWebBean().getInstance(request) == bean1_context); 91 } catch (BeanException be) { 92 fail(be.toString()); 93 } 94 95 try { 96 WebBean webBean = new WebBean("test", ScopeConstants.REQUEST_INT, Bean1.class); 97 98 WebBeanProperty wbp = new WebBeanProperty("string1", webBean); 99 assertTrue("Should have an id of test", wbp.getWebBean().getID().equals("test")); 100 assertTrue("Should be request scoped", wbp.getWebBean().getScope() == ScopeConstants.REQUEST_INT); 101 assertTrue("Should have beanClass of Bean1", wbp.getWebBean().getBeanClass() == Bean1.class); 102 assertTrue("Should have an propertyName of string1", wbp.getPropertyName().equals("string1")); 103 } catch (BeanException be) { 104 fail(be.toString()); 105 } 106 } 107 108 111 public void testGetSimpleProperty() { 112 113 try { 114 Bean1 bean1_request = new Bean1(); 115 request.setAttribute("test", bean1_request); 116 bean1_request.setString1("Bob"); 117 118 WebBeanProperty wbp = new WebBeanProperty("test.string1", ScopeConstants.REQUEST_INT, Bean1.class); 119 assertTrue("Should be Bob", wbp.getPropertyValue(request).equals("Bob")); 120 121 wbp = new WebBeanProperty("testCreate.string1", ScopeConstants.REQUEST_INT, 122 Bean1.class); 123 assertTrue("Should be Bob", wbp.getPropertyValue(request) == null); 124 } catch (BeanException be) { 125 fail(be.toString()); 126 } 127 } 128 129 132 public void testGetProperty() { 133 134 try { 135 Bean1 bean1_request = new Bean1(); 136 Bean2 bean2_request = new Bean2(); 137 request.setAttribute("test", bean1_request); 138 bean1_request.setProperty1(bean2_request); 139 bean2_request.setName("Bob"); 140 141 WebBeanProperty wbp = new WebBeanProperty("test.property1.name", ScopeConstants.REQUEST_INT, Bean1.class); 142 assertTrue("Should be Bob", wbp.getPropertyValue(request).equals("Bob")); 143 144 wbp = new WebBeanProperty("testCreate.string1", ScopeConstants.REQUEST_INT, 145 Bean1.class); 146 assertTrue("Should be Bob", wbp.getPropertyValue(request) == null); 147 } catch (BeanException be) { 148 fail(be.toString()); 149 } 150 } 151 } | Popular Tags |