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.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.anyInt;
26 import static org.mockito.ArgumentMatchers.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.util.HashMap;
33 import java.util.Locale;
34 import java.util.Map;
35 import java.util.Vector;
36
37 import javax.servlet.ServletConfig;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40 import javax.servlet.http.HttpSession;
41
42 import org.apache.turbine.pipeline.PipelineData;
43 import org.apache.turbine.services.TurbineServices;
44 import org.apache.turbine.services.rundata.RunDataService;
45 import org.apache.turbine.util.RunData;
46 import org.junit.BeforeClass;
47
48
49
50
51
52
53
54
55
56
57 public abstract class BaseTestCase
58 {
59 static File log4j2File = new File("conf/test/log4j2.xml");
60
61 @BeforeClass
62 public static void baseInit()
63 throws Exception
64 {
65
66 }
67
68 protected RunData getRunData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception {
69 RunDataService rds =
70 (RunDataService) TurbineServices.getInstance().getService(
71 RunDataService.SERVICE_NAME);
72 RunData runData = rds.getRunData(request, response, config);
73 return runData;
74 }
75
76 protected PipelineData getPipelineData(HttpServletRequest request,HttpServletResponse response,ServletConfig config) throws Exception {
77 RunData runData = getRunData(request,response,config);
78 return runData;
79 }
80
81 protected Map<String,Object> attributes = new HashMap<>();
82 protected int maxInactiveInterval = 0;
83
84 @SuppressWarnings("boxing")
85 protected HttpServletRequest getMockRequest()
86 {
87 HttpServletRequest request = mock(HttpServletRequest.class);
88 HttpSession session = mock(HttpSession.class);
89
90 doAnswer(invocation -> {
91 String key = (String) invocation.getArguments()[0];
92 return attributes.get(key);
93 }).when(session).getAttribute(anyString());
94
95 doAnswer(invocation -> {
96 String key = (String) invocation.getArguments()[0];
97 Object value = invocation.getArguments()[1];
98 attributes.put(key, value);
99 return null;
100 }).when(session).setAttribute(anyString(), any());
101
102 when(session.getMaxInactiveInterval()).thenReturn(maxInactiveInterval);
103
104 doAnswer(invocation -> Integer.valueOf(maxInactiveInterval)).when(session).getMaxInactiveInterval();
105
106 doAnswer(invocation -> {
107 Integer value = (Integer) invocation.getArguments()[0];
108 maxInactiveInterval = value.intValue();
109 return null;
110 }).when(session).setMaxInactiveInterval(anyInt());
111
112 when(session.isNew()).thenReturn(true);
113 when(request.getSession()).thenReturn(session);
114
115 when(request.getServerName()).thenReturn("bob");
116 when(request.getProtocol()).thenReturn("http");
117 when(request.getScheme()).thenReturn("scheme");
118 when(request.getPathInfo()).thenReturn("damn");
119 when(request.getServletPath()).thenReturn("damn2");
120 when(request.getContextPath()).thenReturn("wow");
121 when(request.getContentType()).thenReturn("html/text");
122
123 when(request.getCharacterEncoding()).thenReturn("US-ASCII");
124 when(request.getServerPort()).thenReturn(8080);
125 when(request.getLocale()).thenReturn(Locale.US);
126
127 when(request.getHeader("Content-type")).thenReturn("html/text");
128 when(request.getHeader("Accept-Language")).thenReturn("en-US");
129
130 Vector<String> v = new Vector<>();
131 when(request.getParameterNames()).thenReturn(v.elements());
132 return request;
133 }
134 }
135