今天主要处理测试用例属性的相关功能以及抄了一份在过滤器中打印请求参数和响应结果的方法。
今天主要学习使用了比较常用的功能:1:更新数据时从其他表读取数据(这里有个问题,有个更好的办法是inner join,但是我这个更新涉及到了修改关联项apiId
,不能使用这个方法)2:处理list属性批量插入
下面是一个更新用例的SQL
配置:
<update id="updateCase" parameterType="com.okay.family.common.bean.testcase.request.CaseAttributeBean">
UPDATE
<include refid="table"/>
c
SET
editor=#{uid},name=#{name},envId=#{envId},serviceId=#{serviceId},moduleId=#{moduleId},projectList=#{projectList,jdbcType=OTHER,typeHandler=com.okay.family.common.typehandler.ListIntegerHandler},apiId=#{apiId},headermoco=(SELECT
header_para FROM family_server_api_info WHERE id = #{apiId}),paramsmoco=(SELECT request_par FROM
family_server_api_info WHERE id = #{apiId})
WHERE
id = #{id}
</update>
<update id="updateCase" parameterType="com.okay.family.common.bean.testcase.request.CaseAttributeBean">
UPDATE
<include refid="table"/>
c
INNER JOIN
<include refid="api_info"/> a ON a.id = #{apiId}
SET
editor=#{uid},c.name=#{name},c.envId=#{envId},c.serviceId=#{serviceId},c.moduleId=#{moduleId},c.projectList=#{projectList,jdbcType=OTHER,typeHandler=com.okay.family.common.typehandler.ListIntegerHandler},c.apiId=#{apiId},c.headermoco=a.header_para,c.paramsmoco=a.request_par
WHERE
c.id = #{id}
</update>
批量插入的SQL配置:
<insert id="addCaseProjectRelation" parameterType="com.okay.family.common.bean.testcase.request.CaseAttributeBean">
insert into
<include refid="case_project_relation"/>
(caseId,projectId)
values
<foreach collection="projectList" item="projectId" index="index" separator=",">
(
#{id}, #{projectId}
)
</foreach>
</insert>
CaseAttributeBean
代码:
class CaseAttributeBean extends AbstractBean {
private static final long serialVersionUID = -629232822729332L;
int id
int uid
int envId
/**
* 关联服务id
*/
int serviceId
/**
* 关联服务模块id
*/
int moduleId
@NotNull
String name
List<Integer> projectList
int apiId
@NotNull
@Pattern(regexp = "add|update|copy",message = "类型错误,应该传:add|update|copy")
String type
}
package com.okay.family.common.wapper;
import com.okay.family.fun.utils.Time;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
@Component
@WebFilter(urlPatterns = "/*", filterName = "wrappingFilter")
public class WrappingFilter implements Filter {
private static Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public void init(FilterConfig config) throws ServletException {
}
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
ResponseWrapper responseWrapper = new ResponseWrapper(resp);
RequestWrapper requestWrapper = new RequestWrapper(req);
String url = requestWrapper.getRequestURL().toString();
String queryArgs = requestWrapper.getQueryString();
queryArgs = queryArgs == null ? requestWrapper.getBody() : queryArgs;
long start = Time.getTimeStamp();
chain.doFilter(requestWrapper == null ? request : requestWrapper, responseWrapper);
long end = Time.getTimeStamp();
byte[] bytes = responseWrapper.getContent();
String respContent = new String(bytes);
logger.info("请求:{},耗时:{}ms,参数:{},响应:{}", url, end - start,queryArgs,respContent);
response.getOutputStream().write(respContent.getBytes());
}
}
RequestWrapper
代码package com.okay.family.common.wapper;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
public class RequestWrapper extends HttpServletRequestWrapper {
private final String body;
public RequestWrapper(HttpServletRequest request) {
super(request);
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
InputStream inputStream = null;
try {
inputStream = request.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
} else {
stringBuilder.append("");
}
} catch (IOException ex) {
} finally {
if (inputStream != null) {
try {
inputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedReader != null) {
try {
bufferedReader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
body = stringBuilder.toString();
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
ServletInputStream servletInputStream = new ServletInputStream() {
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
@Override
public int read() throws IOException {
return byteArrayInputStream.read();
}
};
return servletInputStream;
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
public String getBody() {
return this.body;
}
}
RequestWrapper
代码这个跟上面很相似
package com.okay.family.common.wapper;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
public class ResponseWrapper extends HttpServletResponseWrapper {
private ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
private PrintWriter printWriter = new PrintWriter(outputStream);
public ResponseWrapper(HttpServletResponse response) {
super(response);
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
return new ServletOutputStream() {
@Override
public boolean isReady() {
return false;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
@Override
public void write(int b) throws IOException {
outputStream.write(b);
}
@Override
public void write(byte[] b) throws IOException {
outputStream.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
outputStream.write(b, off, len);
}
@Override
public void flush() throws IOException {
outputStream.flush();
}
};
}
@Override
public PrintWriter getWriter() throws IOException {
return printWriter;
}
public void flush(){
try {
printWriter.flush();
printWriter.close();
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public byte[] getContent() {
flush();
return outputStream.toByteArray();
}
}
Fhaohaizi@163.com
。