1
0
mirror of synced 2025-12-26 04:27:56 +08:00

polish code

This commit is contained in:
Daniel Qian
2014-10-22 13:38:16 +08:00
parent f76f0e6773
commit 2c96d542d6
31 changed files with 145 additions and 220 deletions

View File

@@ -1,5 +1,6 @@
package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
@@ -23,7 +24,7 @@ public class WxMpCustomMessageAPITest {
public void testSendCustomMessage() throws WxErrorException {
ApiTestModule.WxXmlMpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlMpInMemoryConfigStorage) wxService.wxMpConfigStorage;
WxMpCustomMessage message = new WxMpCustomMessage();
message.setMsgType(WxMpConsts.CUSTOM_MSG_TEXT);
message.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
message.setToUser(configStorage.getOpenId());
message.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");

View File

@@ -3,6 +3,7 @@ package me.chanjar.weixin.mp.api;
import java.io.IOException;
import java.io.InputStream;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.mp.bean.WxMpMassGroupMessage;
import me.chanjar.weixin.mp.bean.WxMpMassNews;
@@ -15,7 +16,6 @@ import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.mp.bean.WxMpMassNews.WxMassNewsArticle;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.google.inject.Inject;
@@ -37,7 +37,7 @@ public class WxMpMassMessageAPITest {
// 发送群发消息
ApiTestModule.WxXmlMpInMemoryConfigStorage configProvider = (ApiTestModule.WxXmlMpInMemoryConfigStorage) wxService.wxMpConfigStorage;
WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage();
massMessage.setMsgType(WxMpConsts.MASS_MSG_TEXT);
massMessage.setMsgType(WxConsts.MASS_MSG_TEXT);
massMessage.setContent("测试群发消息\n欢迎欢迎热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
massMessage.getToUsers().add(configProvider.getOpenId());
@@ -63,7 +63,7 @@ public class WxMpMassMessageAPITest {
@Test
public void testTextMassGroupMessageSend() throws WxErrorException {
WxMpMassGroupMessage massMessage = new WxMpMassGroupMessage();
massMessage.setMsgtype(WxMpConsts.MASS_MSG_TEXT);
massMessage.setMsgtype(WxConsts.MASS_MSG_TEXT);
massMessage.setContent("测试群发消息\n欢迎欢迎热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
massMessage.setGroupId(wxService.groupGet().get(0).getId());
@@ -93,7 +93,7 @@ public class WxMpMassMessageAPITest {
{
// 上传视频到媒体库
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.mp4");
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxMpConsts.MEDIA_VIDEO, WxMpConsts.FILE_MP4, inputStream);
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxConsts.MEDIA_VIDEO, WxConsts.FILE_MP4, inputStream);
Assert.assertNotNull(uploadMediaRes);
Assert.assertNotNull(uploadMediaRes.getMediaId());
@@ -105,27 +105,27 @@ public class WxMpMassMessageAPITest {
WxMpMassUploadResult uploadResult = wxService.massVideoUpload(video);
Assert.assertNotNull(uploadResult);
Assert.assertNotNull(uploadResult.getMediaId());
messages[0] = new Object[] { WxMpConsts.MASS_MSG_VIDEO, uploadResult.getMediaId() };
messages[0] = new Object[] { WxConsts.MASS_MSG_VIDEO, uploadResult.getMediaId() };
}
/**
* 图片素材
*/
{
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxMpConsts.MEDIA_IMAGE, WxMpConsts.FILE_JPG, inputStream);
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxConsts.MEDIA_IMAGE, WxConsts.FILE_JPG, inputStream);
Assert.assertNotNull(uploadMediaRes);
Assert.assertNotNull(uploadMediaRes.getMediaId());
messages[1] = new Object[] { WxMpConsts.MASS_MSG_IMAGE, uploadMediaRes.getMediaId() };
messages[1] = new Object[] { WxConsts.MASS_MSG_IMAGE, uploadMediaRes.getMediaId() };
}
/**
* 语音素材
*/
{
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.mp3");
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxMpConsts.MEDIA_VOICE, WxMpConsts.FILE_MP3, inputStream);
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxConsts.MEDIA_VOICE, WxConsts.FILE_MP3, inputStream);
Assert.assertNotNull(uploadMediaRes);
Assert.assertNotNull(uploadMediaRes.getMediaId());
messages[2] = new Object[] { WxMpConsts.MASS_MSG_VOICE, uploadMediaRes.getMediaId() };
messages[2] = new Object[] { WxConsts.MASS_MSG_VOICE, uploadMediaRes.getMediaId() };
}
/**
* 图文素材
@@ -133,19 +133,19 @@ public class WxMpMassMessageAPITest {
{
// 上传照片到媒体库
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxMpConsts.MEDIA_IMAGE, WxMpConsts.FILE_JPG, inputStream);
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxConsts.MEDIA_IMAGE, WxConsts.FILE_JPG, inputStream);
Assert.assertNotNull(uploadMediaRes);
Assert.assertNotNull(uploadMediaRes.getMediaId());
// 上传图文消息
WxMpMassNews news = new WxMpMassNews();
WxMassNewsArticle article1 = new WxMassNewsArticle();
WxMpMassNews.WxMpMassNewsArticle article1 = new WxMpMassNews.WxMpMassNewsArticle();
article1.setTitle("标题1");
article1.setContent("内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1");
article1.setThumbMediaId(uploadMediaRes.getMediaId());
news.addArticle(article1);
WxMassNewsArticle article2 = new WxMassNewsArticle();
WxMpMassNews.WxMpMassNewsArticle article2 = new WxMpMassNews.WxMpMassNewsArticle();
article2.setTitle("标题2");
article2.setContent("内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2");
article2.setThumbMediaId(uploadMediaRes.getMediaId());
@@ -158,7 +158,7 @@ public class WxMpMassMessageAPITest {
WxMpMassUploadResult massUploadResult = wxService.massNewsUpload(news);
Assert.assertNotNull(massUploadResult);
Assert.assertNotNull(uploadMediaRes.getMediaId());
messages[3] = new Object[] { WxMpConsts.MASS_MSG_NEWS, massUploadResult.getMediaId() };
messages[3] = new Object[] { WxConsts.MASS_MSG_NEWS, massUploadResult.getMediaId() };
}
return messages;
}

View File

@@ -5,6 +5,7 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
@@ -48,10 +49,10 @@ public class WxMpMediaAPITest {
@DataProvider
public Object[][] uploadMedia() {
return new Object[][] {
new Object[] { WxMpConsts.MEDIA_IMAGE, WxMpConsts.FILE_JPG, "mm.jpeg" },
new Object[] { WxMpConsts.MEDIA_VOICE, WxMpConsts.FILE_MP3, "mm.mp3" },
new Object[] { WxMpConsts.MEDIA_VIDEO, WxMpConsts.FILE_MP4, "mm.mp4" },
new Object[] { WxMpConsts.MEDIA_THUMB, WxMpConsts.FILE_JPG, "mm.jpeg" }
new Object[] { WxConsts.MEDIA_IMAGE, WxConsts.FILE_JPG, "mm.jpeg" },
new Object[] { WxConsts.MEDIA_VOICE, WxConsts.FILE_MP3, "mm.mp3" },
new Object[] { WxConsts.MEDIA_VIDEO, WxConsts.FILE_MP4, "mm.mp4" },
new Object[] { WxConsts.MEDIA_THUMB, WxConsts.FILE_JPG, "mm.jpeg" }
};
}

View File

@@ -2,6 +2,7 @@ package me.chanjar.weixin.mp.api;
import javax.xml.bind.JAXBException;
import me.chanjar.weixin.common.api.WxConsts;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
@@ -44,12 +45,12 @@ public class WxMpMenuAPITest {
public Object[][] getMenu() throws JAXBException {
WxMenu menu = new WxMenu();
WxMenuButton button1 = new WxMenuButton();
button1.setType(WxMpConsts.BUTTON_CLICK);
button1.setType(WxConsts.BUTTON_CLICK);
button1.setName("今日歌曲");
button1.setKey("V1001_TODAY_MUSIC");
WxMenuButton button2 = new WxMenuButton();
button2.setType(WxMpConsts.BUTTON_CLICK);
button2.setType(WxConsts.BUTTON_CLICK);
button2.setName("歌手简介");
button2.setKey("V1001_TODAY_SINGER");
@@ -61,17 +62,17 @@ public class WxMpMenuAPITest {
menu.getButtons().add(button3);
WxMenuButton button31 = new WxMenuButton();
button31.setType(WxMpConsts.BUTTON_VIEW);
button31.setType(WxConsts.BUTTON_VIEW);
button31.setName("搜索");
button31.setUrl("http://www.soso.com/");
WxMenuButton button32 = new WxMenuButton();
button32.setType(WxMpConsts.BUTTON_VIEW);
button32.setType(WxConsts.BUTTON_VIEW);
button32.setName("视频");
button32.setUrl("http://v.qq.com/");
WxMenuButton button33 = new WxMenuButton();
button33.setType(WxMpConsts.BUTTON_CLICK);
button33.setType(WxConsts.BUTTON_CLICK);
button33.setName("赞一下我们");
button33.setKey("V1001_GOOD");

View File

@@ -2,6 +2,7 @@ package me.chanjar.weixin.mp.api;
import java.util.Map;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
import org.testng.Assert;
@@ -21,21 +22,21 @@ public class WxMpMessageRouterTest {
router
.rule()
.async(async)
.msgType(WxMpConsts.XML_MSG_TEXT).event(WxMpConsts.EVT_CLICK).eventKey("KEY_1").content("CONTENT_1")
.msgType(WxConsts.XML_MSG_TEXT).event(WxConsts.EVT_CLICK).eventKey("KEY_1").content("CONTENT_1")
.handler(new WxEchoMpMessageHandler(sb, "COMBINE_4"))
.end()
.rule()
.async(async)
.msgType(WxMpConsts.XML_MSG_TEXT).event(WxMpConsts.EVT_CLICK).eventKey("KEY_1")
.msgType(WxConsts.XML_MSG_TEXT).event(WxConsts.EVT_CLICK).eventKey("KEY_1")
.handler(new WxEchoMpMessageHandler(sb, "COMBINE_3"))
.end()
.rule()
.async(async)
.msgType(WxMpConsts.XML_MSG_TEXT).event(WxMpConsts.EVT_CLICK)
.msgType(WxConsts.XML_MSG_TEXT).event(WxConsts.EVT_CLICK)
.handler(new WxEchoMpMessageHandler(sb, "COMBINE_2"))
.end()
.rule().async(async).msgType(WxMpConsts.XML_MSG_TEXT).handler(new WxEchoMpMessageHandler(sb, WxMpConsts.XML_MSG_TEXT)).end()
.rule().async(async).event(WxMpConsts.EVT_CLICK).handler(new WxEchoMpMessageHandler(sb, WxMpConsts.EVT_CLICK)).end()
.rule().async(async).msgType(WxConsts.XML_MSG_TEXT).handler(new WxEchoMpMessageHandler(sb, WxConsts.XML_MSG_TEXT)).end()
.rule().async(async).event(WxConsts.EVT_CLICK).handler(new WxEchoMpMessageHandler(sb, WxConsts.EVT_CLICK)).end()
.rule().async(async).eventKey("KEY_1").handler(new WxEchoMpMessageHandler(sb, "KEY_1")).end()
.rule().async(async).content("CONTENT_1").handler(new WxEchoMpMessageHandler(sb, "CONTENT_1")).end()
.rule().async(async).rContent(".*bc.*").handler(new WxEchoMpMessageHandler(sb, "abcd")).end()
@@ -91,10 +92,10 @@ public class WxMpMessageRouterTest {
@DataProvider(name="messages-1")
public Object[][] messages2() {
WxMpXmlMessage message1 = new WxMpXmlMessage();
message1.setMsgType(WxMpConsts.XML_MSG_TEXT);
message1.setMsgType(WxConsts.XML_MSG_TEXT);
WxMpXmlMessage message2 = new WxMpXmlMessage();
message2.setEvent(WxMpConsts.EVT_CLICK);
message2.setEvent(WxConsts.EVT_CLICK);
WxMpXmlMessage message3 = new WxMpXmlMessage();
message3.setEventKey("KEY_1");
@@ -109,23 +110,23 @@ public class WxMpMessageRouterTest {
message6.setContent("abcd");
WxMpXmlMessage c2 = new WxMpXmlMessage();
c2.setMsgType(WxMpConsts.XML_MSG_TEXT);
c2.setEvent(WxMpConsts.EVT_CLICK);
c2.setMsgType(WxConsts.XML_MSG_TEXT);
c2.setEvent(WxConsts.EVT_CLICK);
WxMpXmlMessage c3 = new WxMpXmlMessage();
c3.setMsgType(WxMpConsts.XML_MSG_TEXT);
c3.setEvent(WxMpConsts.EVT_CLICK);
c3.setMsgType(WxConsts.XML_MSG_TEXT);
c3.setEvent(WxConsts.EVT_CLICK);
c3.setEventKey("KEY_1");
WxMpXmlMessage c4 = new WxMpXmlMessage();
c4.setMsgType(WxMpConsts.XML_MSG_TEXT);
c4.setEvent(WxMpConsts.EVT_CLICK);
c4.setMsgType(WxConsts.XML_MSG_TEXT);
c4.setEvent(WxConsts.EVT_CLICK);
c4.setEventKey("KEY_1");
c4.setContent("CONTENT_1");
return new Object[][] {
new Object[] { message1, WxMpConsts.XML_MSG_TEXT + "," },
new Object[] { message2, WxMpConsts.EVT_CLICK + "," },
new Object[] { message1, WxConsts.XML_MSG_TEXT + "," },
new Object[] { message2, WxConsts.EVT_CLICK + "," },
new Object[] { message3, "KEY_1," },
new Object[] { message4, "CONTENT_1," },
new Object[] { message5, "ALL," },

View File

@@ -1,6 +1,6 @@
package me.chanjar.weixin.mp.bean;
import me.chanjar.weixin.mp.api.WxMpConsts;
import me.chanjar.weixin.common.api.WxConsts;
import org.testng.Assert;
import org.testng.annotations.Test;
@@ -12,7 +12,7 @@ public class WxMpCustomMessageTest {
public void testTextReply() {
WxMpCustomMessage reply = new WxMpCustomMessage();
reply.setToUser("OPENID");
reply.setMsgType(WxMpConsts.CUSTOM_MSG_TEXT);
reply.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
reply.setContent("sfsfdsdf");
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
}
@@ -25,7 +25,7 @@ public class WxMpCustomMessageTest {
public void testImageReply() {
WxMpCustomMessage reply = new WxMpCustomMessage();
reply.setToUser("OPENID");
reply.setMsgType(WxMpConsts.CUSTOM_MSG_IMAGE);
reply.setMsgType(WxConsts.CUSTOM_MSG_IMAGE);
reply.setMediaId("MEDIA_ID");
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
}
@@ -38,7 +38,7 @@ public class WxMpCustomMessageTest {
public void testVoiceReply() {
WxMpCustomMessage reply = new WxMpCustomMessage();
reply.setToUser("OPENID");
reply.setMsgType(WxMpConsts.CUSTOM_MSG_VOICE);
reply.setMsgType(WxConsts.CUSTOM_MSG_VOICE);
reply.setMediaId("MEDIA_ID");
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
}
@@ -51,7 +51,7 @@ public class WxMpCustomMessageTest {
public void testVideoReply() {
WxMpCustomMessage reply = new WxMpCustomMessage();
reply.setToUser("OPENID");
reply.setMsgType(WxMpConsts.CUSTOM_MSG_VIDEO);
reply.setMsgType(WxConsts.CUSTOM_MSG_VIDEO);
reply.setMediaId("MEDIA_ID");
reply.setThumbMediaId("MEDIA_ID");
reply.setTitle("TITLE");
@@ -67,7 +67,7 @@ public class WxMpCustomMessageTest {
public void testMusicReply() {
WxMpCustomMessage reply = new WxMpCustomMessage();
reply.setToUser("OPENID");
reply.setMsgType(WxMpConsts.CUSTOM_MSG_MUSIC);
reply.setMsgType(WxConsts.CUSTOM_MSG_MUSIC);
reply.setThumbMediaId("MEDIA_ID");
reply.setDescription("DESCRIPTION");
reply.setTitle("TITLE");
@@ -91,7 +91,7 @@ public class WxMpCustomMessageTest {
public void testNewsReply() {
WxMpCustomMessage reply = new WxMpCustomMessage();
reply.setToUser("OPENID");
reply.setMsgType(WxMpConsts.CUSTOM_MSG_NEWS);
reply.setMsgType(WxConsts.CUSTOM_MSG_NEWS);
WxArticle article1 = new WxArticle();
article1.setUrl("URL");

View File

@@ -1,6 +1,6 @@
package me.chanjar.weixin.mp.bean;
import me.chanjar.weixin.mp.api.WxMpConsts;
import me.chanjar.weixin.common.api.WxConsts;
import org.testng.Assert;
import org.testng.annotations.Test;
@@ -57,7 +57,7 @@ public class WxMpXmlMessageTest {
Assert.assertEquals(wxMessage.getToUserName(), "toUser");
Assert.assertEquals(wxMessage.getFromUserName(), "fromUser");
Assert.assertEquals(wxMessage.getCreateTime(), new Long(1348831860l));
Assert.assertEquals(wxMessage.getMsgType(), WxMpConsts.XML_MSG_TEXT);
Assert.assertEquals(wxMessage.getMsgType(), WxConsts.XML_MSG_TEXT);
Assert.assertEquals(wxMessage.getContent(), "this is a test");
Assert.assertEquals(wxMessage.getMsgId(), new Long(1234567890123456l));
Assert.assertEquals(wxMessage.getPicUrl(), "this is a url");