1 18 package org.apache.struts.taglib.logic; 19 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 23 import javax.servlet.ServletException ; 24 import javax.servlet.jsp.JspException ; 25 import javax.servlet.jsp.PageContext ; 26 import junit.framework.Test; 27 import junit.framework.TestSuite; 28 29 import org.apache.cactus.JspTestCase; 30 import org.apache.struts.taglib.SimpleBeanForTesting; 31 import org.apache.struts.util.LabelValueBean; 32 33 38 public class TestEmptyTag extends JspTestCase { 39 40 45 public TestEmptyTag(String theName) { 46 super(theName); 47 } 48 49 54 public static void main(String [] theArgs) { 55 junit.awtui.TestRunner.main(new String [] {TestEmptyTag.class.getName()}); 56 } 57 58 62 public static Test suite() { 63 return new TestSuite(TestEmptyTag.class); 65 } 66 67 private void runNameTest(String testKey, EmptyTag et, 68 Object o, int scope, String whichScope, boolean condition, boolean useProperty, String property) 69 throws ServletException , JspException { 70 71 pageContext.setAttribute(testKey, o, scope); 72 et.setPageContext(pageContext); 73 et.setName(testKey); 74 if (useProperty){ 75 et.setProperty(property); 76 } 77 et.setScope(whichScope); 78 79 assertEquals( 80 "Testing " + testKey + " with EmtpyTag in " + whichScope + " scope", 81 condition, 82 et.condition()); 83 } 84 85 89 public void testEmptyTagUsingName() 90 throws ServletException , JspException { 91 92 String tst = ""; 93 runNameTest("testStringEmptyString", new EmptyTag(), tst, PageContext.APPLICATION_SCOPE, "application", true, false, null); 94 runNameTest("testStringEmptyString", new EmptyTag(), tst, PageContext.SESSION_SCOPE, "session", true, false, null); 95 runNameTest("testStringEmptyString", new EmptyTag(), tst, PageContext.REQUEST_SCOPE, "request", true, false, null); 96 97 tst = "hello"; 98 runNameTest("testStringNotEmpty", new EmptyTag(), tst, PageContext.APPLICATION_SCOPE, "application", false, false, null); 99 runNameTest("testStringNotEmpty", new EmptyTag(), tst, PageContext.SESSION_SCOPE, "session", false, false, null); 100 runNameTest("testStringNotEmpty", new EmptyTag(), tst, PageContext.REQUEST_SCOPE, "request", false, false, null); 101 102 ArrayList lst = new ArrayList (); 104 runNameTest("testArrayListEmpty", new EmptyTag(), lst, PageContext.APPLICATION_SCOPE, "application", true, false, null); 105 runNameTest("testArrayListEmpty", new EmptyTag(), lst, PageContext.SESSION_SCOPE, "session", true, false, null); 106 runNameTest("testArrayListEmpty", new EmptyTag(), lst, PageContext.REQUEST_SCOPE, "request", true, false, null); 107 108 lst.add(0, "test"); 109 runNameTest("testArrayListNotEmpty", new EmptyTag(), lst, PageContext.APPLICATION_SCOPE, "application", false, false, null); 110 runNameTest("testArrayListNotEmpty", new EmptyTag(), lst, PageContext.SESSION_SCOPE, "session", false, false, null); 111 runNameTest("testArrayListNotEmpty", new EmptyTag(), lst, PageContext.REQUEST_SCOPE, "request", false, false, null); 112 113 HashMap map = new HashMap (); 115 runNameTest("testMapEmpty", new EmptyTag(), map, PageContext.APPLICATION_SCOPE, "application", true, false, null); 116 runNameTest("testMapEmpty", new EmptyTag(), map, PageContext.SESSION_SCOPE, "session", true, false, null); 117 runNameTest("testMapEmpty", new EmptyTag(), map, PageContext.REQUEST_SCOPE, "request", true, false, null); 118 119 map.put("testKey", "test"); 120 runNameTest("testMapNotEmpty", new EmptyTag(), map, PageContext.APPLICATION_SCOPE, "application", false, false, null); 121 runNameTest("testMapNotEmpty", new EmptyTag(), map, PageContext.SESSION_SCOPE, "session", false, false, null); 122 runNameTest("testMapNotEmpty", new EmptyTag(), map, PageContext.REQUEST_SCOPE, "request", false, false, null); 123 124 } 125 126 130 public void testEmptyTagUsingProperty() 131 throws ServletException , JspException { 132 133 LabelValueBean lvb = new LabelValueBean(null, null); 134 runNameTest("testStringEmptyString", new EmptyTag(), lvb, PageContext.APPLICATION_SCOPE, "application", true, true, "value"); 135 runNameTest("testStringEmptyString", new EmptyTag(), lvb, PageContext.SESSION_SCOPE, "session", true, true, "value"); 136 runNameTest("testStringEmptyString", new EmptyTag(), lvb, PageContext.REQUEST_SCOPE, "request", true, true, "value"); 137 138 lvb.setValue(""); 139 runNameTest("testStringNotEmpty", new EmptyTag(), lvb, PageContext.APPLICATION_SCOPE, "application", true, true, "value"); 140 runNameTest("testStringNotEmpty", new EmptyTag(), lvb, PageContext.SESSION_SCOPE, "session", true, true, "value"); 141 runNameTest("testStringNotEmpty", new EmptyTag(), lvb, PageContext.REQUEST_SCOPE, "request", true, true, "value"); 142 143 lvb.setValue("hello"); 144 runNameTest("testStringNotEmpty", new EmptyTag(), lvb, PageContext.APPLICATION_SCOPE, "application", false, true, "value"); 145 runNameTest("testStringNotEmpty", new EmptyTag(), lvb, PageContext.SESSION_SCOPE, "session", false, true, "value"); 146 runNameTest("testStringNotEmpty", new EmptyTag(), lvb, PageContext.REQUEST_SCOPE, "request", false, true, "value"); 147 148 SimpleBeanForTesting sbft = new SimpleBeanForTesting(); 150 ArrayList lst = new ArrayList (); 151 sbft.setList(lst); 152 runNameTest("testArrayListEmpty", new EmptyTag(), sbft, PageContext.APPLICATION_SCOPE, "application", true, true, "list"); 153 runNameTest("testArrayListEmpty", new EmptyTag(), sbft, PageContext.SESSION_SCOPE, "session", true, true, "list"); 154 runNameTest("testArrayListEmpty", new EmptyTag(), sbft, PageContext.REQUEST_SCOPE, "request", true, true, "list"); 155 156 lst.add(0, "test"); 157 runNameTest("testArrayListNotEmpty", new EmptyTag(), sbft, PageContext.APPLICATION_SCOPE, "application", false, true, "list"); 158 runNameTest("testArrayListNotEmpty", new EmptyTag(), sbft, PageContext.SESSION_SCOPE, "session", false, true, "list"); 159 runNameTest("testArrayListNotEmpty", new EmptyTag(), sbft, PageContext.REQUEST_SCOPE, "request", false, true, "list"); 160 161 HashMap map = new HashMap (); 163 sbft.setMap(map); 164 runNameTest("testMapEmpty", new EmptyTag(), sbft, PageContext.APPLICATION_SCOPE, "application", true, true, "map"); 165 runNameTest("testMapEmpty", new EmptyTag(), sbft, PageContext.SESSION_SCOPE, "session", true, true, "map"); 166 runNameTest("testMapEmpty", new EmptyTag(), sbft, PageContext.REQUEST_SCOPE, "request", true, true, "map"); 167 168 map.put("testKey", "test"); 169 runNameTest("testMapNotEmpty", new EmptyTag(), sbft, PageContext.APPLICATION_SCOPE, "application", false, true, "map"); 170 runNameTest("testMapNotEmpty", new EmptyTag(), sbft, PageContext.SESSION_SCOPE, "session", false, true, "map"); 171 runNameTest("testMapNotEmpty", new EmptyTag(), sbft, PageContext.REQUEST_SCOPE, "request", false, true, "map"); 172 173 } 174 175 176 } 177 | Popular Tags |