fix warnings

This commit is contained in:
BinaryWang
2016-09-08 15:46:22 +08:00
parent d42227c78e
commit 69df599afe
7 changed files with 130 additions and 111 deletions

View File

@@ -1,12 +1,14 @@
package me.chanjar.weixin.cp.api; package me.chanjar.weixin.cp.api;
import java.io.IOException;
import java.io.InputStream;
import com.google.inject.Binder; import com.google.inject.Binder;
import com.google.inject.Module; import com.google.inject.Module;
import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import java.io.InputStream; import me.chanjar.weixin.common.util.xml.XStreamInitializer;
public class ApiTestModule implements Module { public class ApiTestModule implements Module {
@@ -19,13 +21,18 @@ public class ApiTestModule implements Module {
@Override @Override
public void configure(Binder binder) { public void configure(Binder binder) {
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml"); try (InputStream is1 = ClassLoader
WxXmlCpInMemoryConfigStorage config = fromXml(WxXmlCpInMemoryConfigStorage.class, is1); .getSystemResourceAsStream("test-config.xml")) {
WxCpServiceImpl wxService = new WxCpServiceImpl(); WxXmlCpInMemoryConfigStorage config = fromXml(
wxService.setWxCpConfigStorage(config); WxXmlCpInMemoryConfigStorage.class, is1);
WxCpServiceImpl wxService = new WxCpServiceImpl();
wxService.setWxCpConfigStorage(config);
binder.bind(WxCpServiceImpl.class).toInstance(wxService); binder.bind(WxCpServiceImpl.class).toInstance(wxService);
binder.bind(WxCpConfigStorage.class).toInstance(config); binder.bind(WxCpConfigStorage.class).toInstance(config);
} catch (IOException e) {
e.printStackTrace();
}
} }
@XStreamAlias("xml") @XStreamAlias("xml")

View File

@@ -1,16 +1,17 @@
package me.chanjar.weixin.cp.api; package me.chanjar.weixin.cp.api;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
@Test @Test
public class WxCpBusyRetryTest { public class WxCpBusyRetryTest {
@@ -19,7 +20,9 @@ public class WxCpBusyRetryTest {
WxCpService service = new WxCpServiceImpl() { WxCpService service = new WxCpServiceImpl() {
@Override @Override
protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { protected synchronized <T, E> T executeInternal(
RequestExecutor<T, E> executor, String uri, E data)
throws WxErrorException {
WxError error = new WxError(); WxError error = new WxError();
error.setErrorCode(-1); error.setErrorCode(-1);
throw new WxErrorException(error); throw new WxErrorException(error);

View File

@@ -1,13 +1,15 @@
package me.chanjar.weixin.cp.api; package me.chanjar.weixin.cp.api;
import com.google.inject.Inject; import java.util.List;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.Guice; import org.testng.annotations.Guice;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.util.List; import com.google.inject.Inject;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpDepart;
/** /**
* 测试部门接口 * 测试部门接口
@@ -24,11 +26,12 @@ public class WxCpDepartAPITest {
protected WxCpDepart depart; protected WxCpDepart depart;
public void testDepartCreate() throws WxErrorException { public void testDepartCreate() throws WxErrorException {
WxCpDepart depart = new WxCpDepart(); WxCpDepart cpDepart = new WxCpDepart();
depart.setName("子部门" + System.currentTimeMillis()); cpDepart.setName("子部门" + System.currentTimeMillis());
depart.setParentId(1); cpDepart.setParentId(1);
depart.setOrder(1); cpDepart.setOrder(1);
Integer departId = this.wxCpService.departCreate(depart); Integer departId = this.wxCpService.departCreate(cpDepart);
System.out.println(departId);
} }
@Test(dependsOnMethods = "testDepartCreate") @Test(dependsOnMethods = "testDepartCreate")

View File

@@ -1,19 +1,21 @@
package me.chanjar.weixin.cp.api; package me.chanjar.weixin.cp.api;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
/** /**
* 测试多媒体文件上传下载 * 测试多媒体文件上传下载
* *
@@ -31,17 +33,21 @@ public class WxCpMediaAPITest {
@Test(dataProvider = "uploadMedia") @Test(dataProvider = "uploadMedia")
public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException { public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException {
InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName); try (InputStream inputStream = ClassLoader
WxMediaUploadResult res = this.wxService.mediaUpload(mediaType, fileType, inputStream); .getSystemResourceAsStream(fileName);) {
Assert.assertNotNull(res.getType()); WxMediaUploadResult res = this.wxService.mediaUpload(mediaType, fileType,
Assert.assertNotNull(res.getCreatedAt()); inputStream);
Assert.assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null); Assert.assertNotNull(res.getType());
Assert.assertNotNull(res.getCreatedAt());
Assert.assertTrue(
res.getMediaId() != null || res.getThumbMediaId() != null);
if (res.getMediaId() != null) { if (res.getMediaId() != null) {
this.media_ids.add(res.getMediaId()); this.media_ids.add(res.getMediaId());
} }
if (res.getThumbMediaId() != null) { if (res.getThumbMediaId() != null) {
this.media_ids.add(res.getThumbMediaId()); this.media_ids.add(res.getThumbMediaId());
}
} }
} }

View File

@@ -1,16 +1,22 @@
package me.chanjar.weixin.cp.demo; package me.chanjar.weixin.cp.demo;
import me.chanjar.weixin.common.session.WxSessionManager; import java.io.IOException;
import me.chanjar.weixin.cp.api.*; import java.io.InputStream;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; import java.util.Map;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler; import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import java.io.InputStream; import me.chanjar.weixin.common.session.WxSessionManager;
import java.util.Map; import me.chanjar.weixin.cp.api.WxCpConfigStorage;
import me.chanjar.weixin.cp.api.WxCpMessageHandler;
import me.chanjar.weixin.cp.api.WxCpMessageRouter;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage;
public class WxCpDemoServer { public class WxCpDemoServer {
@@ -36,55 +42,48 @@ public class WxCpDemoServer {
server.join(); server.join();
} }
private static void initWeixin() { private static void initWeixin() throws IOException {
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml"); try (InputStream is1 = ClassLoader
WxCpDemoInMemoryConfigStorage config = WxCpDemoInMemoryConfigStorage.fromXml(is1); .getSystemResourceAsStream("test-config.xml")) {
WxCpDemoInMemoryConfigStorage config = WxCpDemoInMemoryConfigStorage
.fromXml(is1);
wxCpConfigStorage = config; wxCpConfigStorage = config;
wxCpService = new WxCpServiceImpl(); wxCpService = new WxCpServiceImpl();
wxCpService.setWxCpConfigStorage(config); wxCpService.setWxCpConfigStorage(config);
WxCpMessageHandler handler = new WxCpMessageHandler() { WxCpMessageHandler handler = new WxCpMessageHandler() {
@Override @Override
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage,
WxCpService wxCpService, WxSessionManager sessionManager) { Map<String, Object> context, WxCpService wxService,
WxCpXmlOutTextMessage m = WxCpXmlOutMessage WxSessionManager sessionManager) {
.TEXT() WxCpXmlOutTextMessage m = WxCpXmlOutMessage.TEXT().content("测试加密消息")
.content("测试加密消息") .fromUser(wxMessage.getToUserName())
.fromUser(wxMessage.getToUserName()) .toUser(wxMessage.getFromUserName()).build();
.toUser(wxMessage.getFromUserName()) return m;
.build(); }
return m; };
}
};
WxCpMessageHandler oauth2handler = new WxCpMessageHandler() { WxCpMessageHandler oauth2handler = new WxCpMessageHandler() {
@Override @Override
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage,
WxCpService wxCpService, WxSessionManager sessionManager) { Map<String, Object> context, WxCpService wxService,
String href = "<a href=\"" + wxCpService.oauth2buildAuthorizationUrl(wxCpConfigStorage.getOauth2redirectUri(), null) WxSessionManager sessionManager) {
+ "\">测试oauth2</a>"; String href = "<a href=\""
return WxCpXmlOutMessage + wxService.oauth2buildAuthorizationUrl(
.TEXT() wxCpConfigStorage.getOauth2redirectUri(), null)
.content(href) + "\">测试oauth2</a>";
.fromUser(wxMessage.getToUserName()) return WxCpXmlOutMessage.TEXT().content(href)
.toUser(wxMessage.getFromUserName()).build(); .fromUser(wxMessage.getToUserName())
} .toUser(wxMessage.getFromUserName()).build();
}; }
};
wxCpMessageRouter = new WxCpMessageRouter(wxCpService); wxCpMessageRouter = new WxCpMessageRouter(wxCpService);
wxCpMessageRouter wxCpMessageRouter.rule().async(false).content("哈哈") // 拦截内容为“哈哈”的消息
.rule() .handler(handler).end().rule().async(false).content("oauth")
.async(false) .handler(oauth2handler).end();
.content("哈哈") // 拦截内容为“哈哈”的消息
.handler(handler)
.end()
.rule()
.async(false)
.content("oauth")
.handler(oauth2handler)
.end()
;
}
} }
} }

View File

@@ -1,5 +1,11 @@
package me.chanjar.weixin.cp.demo; package me.chanjar.weixin.cp.demo;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import me.chanjar.weixin.common.util.StringUtils; import me.chanjar.weixin.common.util.StringUtils;
import me.chanjar.weixin.cp.api.WxCpConfigStorage; import me.chanjar.weixin.cp.api.WxCpConfigStorage;
import me.chanjar.weixin.cp.api.WxCpMessageRouter; import me.chanjar.weixin.cp.api.WxCpMessageRouter;
@@ -8,17 +14,11 @@ import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil; import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/** /**
* @author Daniel Qian * @author Daniel Qian
*/ */
public class WxCpEndpointServlet extends HttpServlet { public class WxCpEndpointServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected WxCpConfigStorage wxCpConfigStorage; protected WxCpConfigStorage wxCpConfigStorage;
protected WxCpService wxCpService; protected WxCpService wxCpService;
protected WxCpMessageRouter wxCpMessageRouter; protected WxCpMessageRouter wxCpMessageRouter;
@@ -32,7 +32,7 @@ public class WxCpEndpointServlet extends HttpServlet {
@Override @Override
protected void service(HttpServletRequest request, HttpServletResponse response) protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws IOException {
response.setContentType("text/html;charset=utf-8"); response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK); response.setStatus(HttpServletResponse.SC_OK);

View File

@@ -1,16 +1,17 @@
package me.chanjar.weixin.cp.demo; package me.chanjar.weixin.cp.demo;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.WxCpService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
public class WxCpOAuth2Servlet extends HttpServlet { public class WxCpOAuth2Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected WxCpService wxCpService; protected WxCpService wxCpService;
@@ -20,7 +21,7 @@ public class WxCpOAuth2Servlet extends HttpServlet {
@Override @Override
protected void service(HttpServletRequest request, HttpServletResponse response) protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws IOException {
response.setContentType("text/html;charset=utf-8"); response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK); response.setStatus(HttpServletResponse.SC_OK);