1 package org.apache.turbine.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Matchers.anyInt;
26 import static org.mockito.Matchers.anyString;
27 import static org.mockito.Mockito.doAnswer;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
30
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.FileNotFoundException;
34 import java.util.HashMap;
35 import java.util.Locale;
36 import java.util.Map;
37 import java.util.Properties;
38 import java.util.Vector;
39
40 import javax.servlet.ServletConfig;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43 import javax.servlet.http.HttpSession;
44
45 import org.apache.log4j.PropertyConfigurator;
46 import org.apache.turbine.TurbineConstants;
47 import org.apache.turbine.pipeline.PipelineData;
48 import org.apache.turbine.services.TurbineServices;
49 import org.apache.turbine.services.rundata.RunDataService;
50 import org.apache.turbine.util.RunData;
51 import org.junit.BeforeClass;
52 import org.mockito.invocation.InvocationOnMock;
53 import org.mockito.stubbing.Answer;
54
55
56
57
58
59
60
61
62
63
64 public abstract class BaseTestCase
65 {
66 static File log4jFile = new File("conf/test/Log4j.properties");
67
68 @BeforeClass
69 public static void baseInit()
70 throws Exception
71 {
72
73 Properties p = new Properties();
74 try
75 {
76 p.load(new FileInputStream(log4jFile));
77 p.setProperty(TurbineConstants.APPLICATION_ROOT_KEY, new File(".").getAbsolutePath());
78 PropertyConfigurator.configure(p);
79
80 }
81 catch (FileNotFoundException fnf)
82 {
83 System.err.println("Could not open Log4J configuration file "
84 + log4jFile);
85 }
86 }
87
88 protected RunData getRunData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception {
89 RunDataService rds =
90 (RunDataService) TurbineServices.getInstance().getService(
91 RunDataService.SERVICE_NAME);
92 RunData runData = rds.getRunData(request, response, config);
93 return runData;
94 }
95
96 protected PipelineData getPipelineData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception {
97 RunData runData = getRunData(request,response,config);
98 return runData;
99 }
100
101 protected Map<String,Object> attributes = new HashMap<String,Object>();
102 protected int maxInactiveInterval = 0;
103
104 @SuppressWarnings("boxing")
105 protected HttpServletRequest getMockRequest()
106 {
107 HttpServletRequest request = mock(HttpServletRequest.class);
108 HttpSession session = mock(HttpSession.class);
109
110 doAnswer(new Answer<Object>()
111 {
112 @Override
113 public Object answer(InvocationOnMock invocation) throws Throwable
114 {
115 String key = (String) invocation.getArguments()[0];
116 return attributes.get(key);
117 }
118 }).when(session).getAttribute(anyString());
119
120 doAnswer(new Answer<Object>()
121 {
122 @Override
123 public Object answer(InvocationOnMock invocation) throws Throwable
124 {
125 String key = (String) invocation.getArguments()[0];
126 Object value = invocation.getArguments()[1];
127 attributes.put(key, value);
128 return null;
129 }
130 }).when(session).setAttribute(anyString(), any());
131
132 when(session.getMaxInactiveInterval()).thenReturn(maxInactiveInterval);
133
134 doAnswer(new Answer<Integer>()
135 {
136 @Override
137 public Integer answer(InvocationOnMock invocation) throws Throwable
138 {
139 return Integer.valueOf(maxInactiveInterval);
140 }
141 }).when(session).getMaxInactiveInterval();
142
143 doAnswer(new Answer<Object>()
144 {
145 @Override
146 public Object answer(InvocationOnMock invocation) throws Throwable
147 {
148 Integer value = (Integer) invocation.getArguments()[0];
149 maxInactiveInterval = value.intValue();
150 return null;
151 }
152 }).when(session).setMaxInactiveInterval(anyInt());
153
154 when(session.isNew()).thenReturn(true);
155 when(request.getSession()).thenReturn(session);
156
157 when(request.getServerName()).thenReturn("bob");
158 when(request.getProtocol()).thenReturn("http");
159 when(request.getScheme()).thenReturn("scheme");
160 when(request.getPathInfo()).thenReturn("damn");
161 when(request.getServletPath()).thenReturn("damn2");
162 when(request.getContextPath()).thenReturn("wow");
163 when(request.getContentType()).thenReturn("html/text");
164
165 when(request.getCharacterEncoding()).thenReturn("US-ASCII");
166 when(request.getServerPort()).thenReturn(8080);
167 when(request.getLocale()).thenReturn(Locale.US);
168
169 when(request.getHeader("Content-type")).thenReturn("html/text");
170 when(request.getHeader("Accept-Language")).thenReturn("en-US");
171
172 Vector<String> v = new Vector<String>();
173 when(request.getParameterNames()).thenReturn(v.elements());
174 return request;
175 }
176 }
177