1 20 package org.apache.cactus.sample.servlet.unit; 21 22 import java.util.Enumeration ; 23 import java.util.Vector ; 24 25 import javax.servlet.ServletContext ; 26 27 import org.apache.cactus.ServletTestCase; 28 import org.apache.cactus.server.ServletContextWrapper; 29 30 35 public class TestServletConfig extends ServletTestCase 36 { 37 41 public void testSetConfigParameter() 42 { 43 config.setInitParameter("testparam", "test value"); 44 45 assertEquals("test value", config.getInitParameter("testparam")); 46 47 boolean found = false; 48 Enumeration en = config.getInitParameterNames(); 49 50 while (en.hasMoreElements()) 51 { 52 String name = (String ) en.nextElement(); 53 54 if (name.equals("testparam")) 55 { 56 found = true; 57 58 break; 59 } 60 } 61 62 assertTrue("[testparam] not found in parameter names", found); 63 } 64 65 67 71 public void testSetConfigParameterOverrideWebXmlParameter() 72 { 73 assertTrue( 76 config.getOriginalConfig().getInitParameter("param1") != null); 77 assertTrue( 78 !config.getOriginalConfig().getInitParameter("param1").equals( 79 "testoverrideparam1")); 80 81 config.setInitParameter("param1", "testoverrideparam1"); 82 83 Enumeration en = config.getInitParameterNames(); 84 int count = 0; 85 86 while (en.hasMoreElements()) 87 { 88 String name = (String ) en.nextElement(); 89 90 if (name.equals("param1")) 91 { 92 assertEquals("testoverrideparam1", 93 config.getInitParameter(name)); 94 count++; 95 } 96 } 97 98 assertTrue("[param1] was found " + count + " times. Should have " 99 + "been found once.", count == 1); 100 } 101 102 104 108 public void testGetServletNameOverriden() 109 { 110 config.setServletName("MyServlet"); 111 assertEquals("MyServlet", config.getServletName()); 112 assertTrue(!config.getOriginalConfig().getServletName().equals( 113 config.getServletName())); 114 } 115 116 118 122 public void testGetServletNameNoOverride() 123 { 124 assertEquals(config.getOriginalConfig().getServletName(), 125 config.getServletName()); 126 } 127 128 130 134 public void testGetLogs() 135 { 136 String message = "some test log"; 137 ServletContext context = config.getServletContext(); 138 139 context.log(message); 140 141 Vector logs = ((ServletContextWrapper) context).getLogs(); 142 143 assertEquals("Found more than one log message", logs.size(), 1); 144 assertTrue("Cannot find expected log message : [" + message + "]", 145 logs.contains("some test log")); 146 } 147 148 } 149 | Popular Tags |