1 16 package org.apache.commons.chain.web.servlet; 17 18 19 import junit.framework.Test; 20 import junit.framework.TestCase; 21 import junit.framework.TestSuite; 22 import org.apache.commons.chain.Context; 23 24 import javax.servlet.ServletContext ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 import javax.servlet.http.HttpSession ; 28 import java.util.Locale ; 29 30 31 33 public class ServletGetLocaleCommandTestCase extends TestCase { 34 35 36 38 43 public ServletGetLocaleCommandTestCase(String name) { 44 super(name); 45 } 46 47 48 50 51 protected Locale locale = null; 52 53 protected ServletContext scontext = null; 55 protected HttpServletRequest request = null; 56 protected HttpServletResponse response = null; 57 protected HttpSession session = null; 58 59 protected Context context = null; 61 protected ServletGetLocaleCommand command = null; 62 63 64 66 67 70 public void setUp() { 71 72 locale = new Locale ("en", "US"); 73 74 scontext = new MockServletContext(); 76 session = new MockHttpSession(scontext); 77 request = new MockHttpServletRequest("/context", "/servlet", 78 "/path/info", "a=b&c=d", 79 session); 80 ((MockHttpServletRequest) request).setLocale(locale); 81 response = new MockHttpServletResponse(); 82 83 context = new ServletWebContext(scontext, request, response); 85 command = new ServletGetLocaleCommand(); 86 87 } 88 89 90 93 public static Test suite() { 94 95 return (new TestSuite(ServletGetLocaleCommandTestCase.class)); 96 97 } 98 99 100 103 public void tearDown() { 104 105 scontext = null; 106 session = null; 107 request = null; 108 response = null; 109 110 context = null; 111 command = null; 112 113 } 114 115 116 118 119 public void testConfigured() throws Exception { 121 122 command.setLocaleKey("special"); 123 assertEquals("special", command.getLocaleKey()); 124 check(context, command); 125 126 } 127 128 129 public void testDefaut() throws Exception { 131 132 assertEquals("locale", command.getLocaleKey()); 133 check(context, command); 134 135 } 136 137 138 140 141 protected void check(Context context, ServletGetLocaleCommand command) 142 throws Exception { 143 144 String localeKey = command.getLocaleKey(); 145 assertNotNull(localeKey); 146 Object value = context.get(localeKey); 147 assertNull(value); 148 boolean result = command.execute(context); 149 assertFalse(result); 150 value = context.get(localeKey); 151 assertNotNull(value); 152 assertTrue(value instanceof Locale ); 153 assertEquals(locale, (Locale ) value); 154 155 } 156 157 158 } 159 | Popular Tags |