Skip to content

Commit 69df599

Browse files
committed
fix warnings
1 parent d42227c commit 69df599

File tree

7 files changed

+133
-114
lines changed

7 files changed

+133
-114
lines changed

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/ApiTestModule.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package me.chanjar.weixin.cp.api;
22

3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
36
import com.google.inject.Binder;
47
import com.google.inject.Module;
58
import com.thoughtworks.xstream.XStream;
69
import com.thoughtworks.xstream.annotations.XStreamAlias;
7-
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
810

9-
import java.io.InputStream;
11+
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
1012

1113
public class ApiTestModule implements Module {
1214

@@ -19,13 +21,18 @@ public static <T> T fromXml(Class<T> clazz, InputStream is) {
1921

2022
@Override
2123
public void configure(Binder binder) {
22-
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
23-
WxXmlCpInMemoryConfigStorage config = fromXml(WxXmlCpInMemoryConfigStorage.class, is1);
24-
WxCpServiceImpl wxService = new WxCpServiceImpl();
25-
wxService.setWxCpConfigStorage(config);
26-
27-
binder.bind(WxCpServiceImpl.class).toInstance(wxService);
28-
binder.bind(WxCpConfigStorage.class).toInstance(config);
24+
try (InputStream is1 = ClassLoader
25+
.getSystemResourceAsStream("test-config.xml")) {
26+
WxXmlCpInMemoryConfigStorage config = fromXml(
27+
WxXmlCpInMemoryConfigStorage.class, is1);
28+
WxCpServiceImpl wxService = new WxCpServiceImpl();
29+
wxService.setWxCpConfigStorage(config);
30+
31+
binder.bind(WxCpServiceImpl.class).toInstance(wxService);
32+
binder.bind(WxCpConfigStorage.class).toInstance(config);
33+
} catch (IOException e) {
34+
e.printStackTrace();
35+
}
2936
}
3037

3138
@XStreamAlias("xml")

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpBusyRetryTest.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package me.chanjar.weixin.cp.api;
22

3-
import me.chanjar.weixin.common.bean.result.WxError;
4-
import me.chanjar.weixin.common.exception.WxErrorException;
5-
import me.chanjar.weixin.common.util.http.RequestExecutor;
6-
import org.testng.annotations.DataProvider;
7-
import org.testng.annotations.Test;
8-
93
import java.util.concurrent.ExecutionException;
104
import java.util.concurrent.ExecutorService;
115
import java.util.concurrent.Executors;
126
import java.util.concurrent.Future;
137

8+
import org.testng.annotations.DataProvider;
9+
import org.testng.annotations.Test;
10+
11+
import me.chanjar.weixin.common.bean.result.WxError;
12+
import me.chanjar.weixin.common.exception.WxErrorException;
13+
import me.chanjar.weixin.common.util.http.RequestExecutor;
14+
1415
@Test
1516
public class WxCpBusyRetryTest {
1617

@@ -19,7 +20,9 @@ public Object[][] getService() {
1920
WxCpService service = new WxCpServiceImpl() {
2021

2122
@Override
22-
protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
23+
protected synchronized <T, E> T executeInternal(
24+
RequestExecutor<T, E> executor, String uri, E data)
25+
throws WxErrorException {
2326
WxError error = new WxError();
2427
error.setErrorCode(-1);
2528
throw new WxErrorException(error);

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpDepartAPITest.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package me.chanjar.weixin.cp.api;
22

3-
import com.google.inject.Inject;
4-
import me.chanjar.weixin.common.exception.WxErrorException;
5-
import me.chanjar.weixin.cp.bean.WxCpDepart;
3+
import java.util.List;
4+
65
import org.testng.Assert;
76
import org.testng.annotations.Guice;
87
import org.testng.annotations.Test;
98

10-
import java.util.List;
9+
import com.google.inject.Inject;
10+
11+
import me.chanjar.weixin.common.exception.WxErrorException;
12+
import me.chanjar.weixin.cp.bean.WxCpDepart;
1113

1214
/**
1315
* 测试部门接口
@@ -24,11 +26,12 @@ public class WxCpDepartAPITest {
2426
protected WxCpDepart depart;
2527

2628
public void testDepartCreate() throws WxErrorException {
27-
WxCpDepart depart = new WxCpDepart();
28-
depart.setName("子部门" + System.currentTimeMillis());
29-
depart.setParentId(1);
30-
depart.setOrder(1);
31-
Integer departId = this.wxCpService.departCreate(depart);
29+
WxCpDepart cpDepart = new WxCpDepart();
30+
cpDepart.setName("子部门" + System.currentTimeMillis());
31+
cpDepart.setParentId(1);
32+
cpDepart.setOrder(1);
33+
Integer departId = this.wxCpService.departCreate(cpDepart);
34+
System.out.println(departId);
3235
}
3336

3437
@Test(dependsOnMethods = "testDepartCreate")

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMediaAPITest.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package me.chanjar.weixin.cp.api;
22

3-
import com.google.inject.Inject;
4-
import me.chanjar.weixin.common.api.WxConsts;
5-
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
6-
import me.chanjar.weixin.common.exception.WxErrorException;
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
78
import org.testng.Assert;
89
import org.testng.annotations.DataProvider;
910
import org.testng.annotations.Guice;
1011
import org.testng.annotations.Test;
1112

12-
import java.io.IOException;
13-
import java.io.InputStream;
14-
import java.util.ArrayList;
15-
import java.util.List;
13+
import com.google.inject.Inject;
14+
15+
import me.chanjar.weixin.common.api.WxConsts;
16+
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
17+
import me.chanjar.weixin.common.exception.WxErrorException;
1618

1719
/**
1820
* 测试多媒体文件上传下载
@@ -31,17 +33,21 @@ public class WxCpMediaAPITest {
3133

3234
@Test(dataProvider = "uploadMedia")
3335
public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException {
34-
InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName);
35-
WxMediaUploadResult res = this.wxService.mediaUpload(mediaType, fileType, inputStream);
36-
Assert.assertNotNull(res.getType());
37-
Assert.assertNotNull(res.getCreatedAt());
38-
Assert.assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null);
36+
try (InputStream inputStream = ClassLoader
37+
.getSystemResourceAsStream(fileName);) {
38+
WxMediaUploadResult res = this.wxService.mediaUpload(mediaType, fileType,
39+
inputStream);
40+
Assert.assertNotNull(res.getType());
41+
Assert.assertNotNull(res.getCreatedAt());
42+
Assert.assertTrue(
43+
res.getMediaId() != null || res.getThumbMediaId() != null);
3944

40-
if (res.getMediaId() != null) {
41-
this.media_ids.add(res.getMediaId());
42-
}
43-
if (res.getThumbMediaId() != null) {
44-
this.media_ids.add(res.getThumbMediaId());
45+
if (res.getMediaId() != null) {
46+
this.media_ids.add(res.getMediaId());
47+
}
48+
if (res.getThumbMediaId() != null) {
49+
this.media_ids.add(res.getThumbMediaId());
50+
}
4551
}
4652
}
4753

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package me.chanjar.weixin.cp.demo;
22

3-
import me.chanjar.weixin.common.session.WxSessionManager;
4-
import me.chanjar.weixin.cp.api.*;
5-
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
6-
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
7-
import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage;
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.Map;
6+
87
import org.eclipse.jetty.server.Server;
98
import org.eclipse.jetty.servlet.ServletHandler;
109
import org.eclipse.jetty.servlet.ServletHolder;
1110

12-
import java.io.InputStream;
13-
import java.util.Map;
11+
import me.chanjar.weixin.common.session.WxSessionManager;
12+
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
13+
import me.chanjar.weixin.cp.api.WxCpMessageHandler;
14+
import me.chanjar.weixin.cp.api.WxCpMessageRouter;
15+
import me.chanjar.weixin.cp.api.WxCpService;
16+
import me.chanjar.weixin.cp.api.WxCpServiceImpl;
17+
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
18+
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
19+
import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage;
1420

1521
public class WxCpDemoServer {
1622

@@ -36,55 +42,48 @@ public static void main(String[] args) throws Exception {
3642
server.join();
3743
}
3844

39-
private static void initWeixin() {
40-
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
41-
WxCpDemoInMemoryConfigStorage config = WxCpDemoInMemoryConfigStorage.fromXml(is1);
42-
43-
wxCpConfigStorage = config;
44-
wxCpService = new WxCpServiceImpl();
45-
wxCpService.setWxCpConfigStorage(config);
46-
47-
WxCpMessageHandler handler = new WxCpMessageHandler() {
48-
@Override
49-
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context,
50-
WxCpService wxCpService, WxSessionManager sessionManager) {
51-
WxCpXmlOutTextMessage m = WxCpXmlOutMessage
52-
.TEXT()
53-
.content("测试加密消息")
54-
.fromUser(wxMessage.getToUserName())
55-
.toUser(wxMessage.getFromUserName())
56-
.build();
57-
return m;
58-
}
59-
};
60-
61-
WxCpMessageHandler oauth2handler = new WxCpMessageHandler() {
62-
@Override
63-
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context,
64-
WxCpService wxCpService, WxSessionManager sessionManager) {
65-
String href = "<a href=\"" + wxCpService.oauth2buildAuthorizationUrl(wxCpConfigStorage.getOauth2redirectUri(), null)
66-
+ "\">测试oauth2</a>";
67-
return WxCpXmlOutMessage
68-
.TEXT()
69-
.content(href)
70-
.fromUser(wxMessage.getToUserName())
71-
.toUser(wxMessage.getFromUserName()).build();
72-
}
73-
};
74-
75-
wxCpMessageRouter = new WxCpMessageRouter(wxCpService);
76-
wxCpMessageRouter
77-
.rule()
78-
.async(false)
79-
.content("哈哈") // 拦截内容为“哈哈”的消息
80-
.handler(handler)
81-
.end()
82-
.rule()
83-
.async(false)
84-
.content("oauth")
85-
.handler(oauth2handler)
86-
.end()
87-
;
88-
45+
private static void initWeixin() throws IOException {
46+
try (InputStream is1 = ClassLoader
47+
.getSystemResourceAsStream("test-config.xml")) {
48+
WxCpDemoInMemoryConfigStorage config = WxCpDemoInMemoryConfigStorage
49+
.fromXml(is1);
50+
51+
wxCpConfigStorage = config;
52+
wxCpService = new WxCpServiceImpl();
53+
wxCpService.setWxCpConfigStorage(config);
54+
55+
WxCpMessageHandler handler = new WxCpMessageHandler() {
56+
@Override
57+
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage,
58+
Map<String, Object> context, WxCpService wxService,
59+
WxSessionManager sessionManager) {
60+
WxCpXmlOutTextMessage m = WxCpXmlOutMessage.TEXT().content("测试加密消息")
61+
.fromUser(wxMessage.getToUserName())
62+
.toUser(wxMessage.getFromUserName()).build();
63+
return m;
64+
}
65+
};
66+
67+
WxCpMessageHandler oauth2handler = new WxCpMessageHandler() {
68+
@Override
69+
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage,
70+
Map<String, Object> context, WxCpService wxService,
71+
WxSessionManager sessionManager) {
72+
String href = "<a href=\""
73+
+ wxService.oauth2buildAuthorizationUrl(
74+
wxCpConfigStorage.getOauth2redirectUri(), null)
75+
+ "\">测试oauth2</a>";
76+
return WxCpXmlOutMessage.TEXT().content(href)
77+
.fromUser(wxMessage.getToUserName())
78+
.toUser(wxMessage.getFromUserName()).build();
79+
}
80+
};
81+
82+
wxCpMessageRouter = new WxCpMessageRouter(wxCpService);
83+
wxCpMessageRouter.rule().async(false).content("哈哈") // 拦截内容为“哈哈”的消息
84+
.handler(handler).end().rule().async(false).content("oauth")
85+
.handler(oauth2handler).end();
86+
87+
}
8988
}
9089
}

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpEndpointServlet.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package me.chanjar.weixin.cp.demo;
22

3+
import java.io.IOException;
4+
5+
import javax.servlet.http.HttpServlet;
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
39
import me.chanjar.weixin.common.util.StringUtils;
410
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
511
import me.chanjar.weixin.cp.api.WxCpMessageRouter;
@@ -8,17 +14,11 @@
814
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
915
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil;
1016

11-
import javax.servlet.ServletException;
12-
import javax.servlet.http.HttpServlet;
13-
import javax.servlet.http.HttpServletRequest;
14-
import javax.servlet.http.HttpServletResponse;
15-
import java.io.IOException;
16-
1717
/**
1818
* @author Daniel Qian
1919
*/
2020
public class WxCpEndpointServlet extends HttpServlet {
21-
21+
private static final long serialVersionUID = 1L;
2222
protected WxCpConfigStorage wxCpConfigStorage;
2323
protected WxCpService wxCpService;
2424
protected WxCpMessageRouter wxCpMessageRouter;
@@ -32,7 +32,7 @@ public WxCpEndpointServlet(WxCpConfigStorage wxCpConfigStorage, WxCpService wxCp
3232

3333
@Override
3434
protected void service(HttpServletRequest request, HttpServletResponse response)
35-
throws ServletException, IOException {
35+
throws IOException {
3636

3737
response.setContentType("text/html;charset=utf-8");
3838
response.setStatus(HttpServletResponse.SC_OK);

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpOAuth2Servlet.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package me.chanjar.weixin.cp.demo;
22

3-
import me.chanjar.weixin.common.exception.WxErrorException;
4-
import me.chanjar.weixin.cp.api.WxCpService;
3+
import java.io.IOException;
4+
import java.util.Arrays;
55

6-
import javax.servlet.ServletException;
76
import javax.servlet.http.HttpServlet;
87
import javax.servlet.http.HttpServletRequest;
98
import javax.servlet.http.HttpServletResponse;
10-
import java.io.IOException;
11-
import java.util.Arrays;
9+
10+
import me.chanjar.weixin.common.exception.WxErrorException;
11+
import me.chanjar.weixin.cp.api.WxCpService;
1212

1313
public class WxCpOAuth2Servlet extends HttpServlet {
14+
private static final long serialVersionUID = 1L;
1415

1516
protected WxCpService wxCpService;
1617

@@ -20,7 +21,7 @@ public WxCpOAuth2Servlet(WxCpService wxCpService) {
2021

2122
@Override
2223
protected void service(HttpServletRequest request, HttpServletResponse response)
23-
throws ServletException, IOException {
24+
throws IOException {
2425

2526
response.setContentType("text/html;charset=utf-8");
2627
response.setStatus(HttpServletResponse.SC_OK);

0 commit comments

Comments
 (0)