From 771ddef37e057a58d44ac0279333a71dbb721520 Mon Sep 17 00:00:00 2001 From: Daniel Qian Date: Mon, 25 Aug 2014 14:10:18 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 33 ++- .../chanjarster/weixin/api/ApiTestModule.java | 29 +++ .../chanjarster/weixin/api/WxBaseAPITest.java | 113 ++++++++++ .../weixin/api/WxCustomMessageAPITest.java | 30 +++ .../chanjarster/weixin/api/WxMenuAPITest.java | 86 ++++++++ .../weixin/api/WxMessageRouterTest.java | 5 - .../chanjarster/weixin/api/WxServiceTest.java | 195 ------------------ .../chanjarster/weixin/bean/WxMenuTest.java | 2 +- src/test/resources/testng.xml | 22 ++ 9 files changed, 309 insertions(+), 206 deletions(-) create mode 100644 src/test/java/chanjarster/weixin/api/ApiTestModule.java create mode 100644 src/test/java/chanjarster/weixin/api/WxBaseAPITest.java create mode 100644 src/test/java/chanjarster/weixin/api/WxCustomMessageAPITest.java create mode 100644 src/test/java/chanjarster/weixin/api/WxMenuAPITest.java delete mode 100644 src/test/java/chanjarster/weixin/api/WxServiceTest.java create mode 100644 src/test/resources/testng.xml diff --git a/pom.xml b/pom.xml index 471624ff..b2ea6748 100644 --- a/pom.xml +++ b/pom.xml @@ -26,11 +26,11 @@ fluent-hc ${httpclient.version} - - org.apache.httpcomponents - httpmime - ${httpclient.version} - + + org.apache.httpcomponents + httpmime + ${httpclient.version} + javax.xml.bind jaxb-api @@ -63,10 +63,33 @@ 1.9.5 test + + com.google.inject + guice + 3.0 + test + org.apache.commons commons-lang3 3.1 + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.17 + + + src/test/resources/testng.xml + + + + + + + diff --git a/src/test/java/chanjarster/weixin/api/ApiTestModule.java b/src/test/java/chanjarster/weixin/api/ApiTestModule.java new file mode 100644 index 00000000..19f255a3 --- /dev/null +++ b/src/test/java/chanjarster/weixin/api/ApiTestModule.java @@ -0,0 +1,29 @@ +package chanjarster.weixin.api; + +import java.io.InputStream; + +import javax.xml.bind.JAXBException; + +import chanjarster.weixin.api.WxBaseAPITest.WxXmlConfigStorage; +import chanjarster.weixin.util.xml.XmlTransformer; + +import com.google.inject.Binder; +import com.google.inject.Module; + +public class ApiTestModule implements Module { + + @Override + public void configure(Binder binder) { + try { + InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml"); + WxXmlConfigStorage config = XmlTransformer.fromXml(WxXmlConfigStorage.class, is1); + WxServiceImpl wxService = new WxServiceImpl(); + wxService.setWxConfigStorage(config); + + binder.bind(WxServiceImpl.class).toInstance(wxService); + } catch (JAXBException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/src/test/java/chanjarster/weixin/api/WxBaseAPITest.java b/src/test/java/chanjarster/weixin/api/WxBaseAPITest.java new file mode 100644 index 00000000..6fcb1f7a --- /dev/null +++ b/src/test/java/chanjarster/weixin/api/WxBaseAPITest.java @@ -0,0 +1,113 @@ +package chanjarster.weixin.api; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +import org.apache.commons.lang3.StringUtils; +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + +import chanjarster.weixin.bean.result.WxMediaUploadResult; +import chanjarster.weixin.exception.WxErrorException; + +import com.google.inject.Inject; + +@Test(groups="baseAPI") +@Guice(modules = ApiTestModule.class) +public class WxBaseAPITest { + + @Inject + protected WxServiceImpl wxService; + + private List media_ids = new ArrayList(); + + @Test + public void testRefreshAccessToken() throws WxErrorException { + WxConfigStorage configStorage = wxService.wxConfigStorage; + String before = configStorage.getAccessToken(); + wxService.refreshAccessToken(); + + String after = configStorage.getAccessToken(); + + Assert.assertNotEquals(before, after); + Assert.assertTrue(StringUtils.isNotBlank(after)); + } + + @Test(dependsOnMethods = { "testRefreshAccessToken" }, dataProvider="uploadMedia", enabled = true) + public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException { + InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName); + WxMediaUploadResult res = wxService.uploadMedia(mediaType, fileType, inputStream); + Assert.assertNotNull(res.getType()); + Assert.assertNotNull(res.getCreated_at()); + Assert.assertTrue(res.getMedia_id() != null || res.getThumb_media_id() != null); + + if (res.getMedia_id() != null) { + media_ids.add(res.getMedia_id()); + } + if (res.getThumb_media_id() != null) { + media_ids.add(res.getThumb_media_id()); + } + } + + @DataProvider + public Object[][] uploadMedia() { + return new Object[][] { + 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" } + }; + } + + @Test(dependsOnMethods = { "testUploadMedia" }, dataProvider="downloadMedia") + public void testDownloadMedia(String media_id) throws WxErrorException { + wxService.downloadMedia(media_id); + } + + @DataProvider + public Object[][] downloadMedia() { + Object[][] params = new Object[this.media_ids.size()][]; + for (int i = 0; i < this.media_ids.size(); i++) { + params[i] = new Object[] { this.media_ids.get(i) }; + } + return params; + } + + @Test(enabled = true) + public void testCheckSignature() throws WxErrorException { + String timestamp = "23234235423246"; + String nonce = "y7didfkcmvnbd90sdofjkiefhsd"; + String signature = "77b6651628dfb9a64bfb0d3432ee053ac566a459"; + Assert.assertTrue(wxService.checkSignature(timestamp, nonce, signature)); + } + + @XmlRootElement(name = "xml") + @XmlAccessorType(XmlAccessType.FIELD) + public static class WxXmlConfigStorage extends WxInMemoryConfigStorage { + + protected String openId; + + public String getOpenId() { + return openId; + } + public void setOpenId(String openId) { + this.openId = openId; + } + @Override + public String toString() { + return "SimpleWxConfigProvider [appId=" + appId + ", secret=" + secret + ", accessToken=" + accessToken + + ", expiresIn=" + expiresIn + ", token=" + token + ", openId=" + openId + "]"; + } + + } + + +} diff --git a/src/test/java/chanjarster/weixin/api/WxCustomMessageAPITest.java b/src/test/java/chanjarster/weixin/api/WxCustomMessageAPITest.java new file mode 100644 index 00000000..b2a99d52 --- /dev/null +++ b/src/test/java/chanjarster/weixin/api/WxCustomMessageAPITest.java @@ -0,0 +1,30 @@ +package chanjarster.weixin.api; + +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + +import com.google.inject.Inject; + +import chanjarster.weixin.api.WxBaseAPITest.WxXmlConfigStorage; +import chanjarster.weixin.bean.WxCustomMessage; +import chanjarster.weixin.exception.WxErrorException; + +@Test(dependsOnGroups="baseAPI") +@Guice(modules = ApiTestModule.class) +public class WxCustomMessageAPITest { + + @Inject + protected WxServiceImpl wxService; + + @Test(enabled = true) + public void testSendCustomMessage() throws WxErrorException { + WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigStorage; + WxCustomMessage message = new WxCustomMessage(); + message.setMsgtype(WxConsts.CUSTOM_MSG_TEXT); + message.setTouser(configProvider.getOpenId()); + message.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:Hello World"); + + wxService.sendCustomMessage(message); + } + +} diff --git a/src/test/java/chanjarster/weixin/api/WxMenuAPITest.java b/src/test/java/chanjarster/weixin/api/WxMenuAPITest.java new file mode 100644 index 00000000..66f060a7 --- /dev/null +++ b/src/test/java/chanjarster/weixin/api/WxMenuAPITest.java @@ -0,0 +1,86 @@ +package chanjarster.weixin.api; + +import javax.xml.bind.JAXBException; + +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 chanjarster.weixin.bean.WxMenu; +import chanjarster.weixin.bean.WxMenu.WxMenuButton; +import chanjarster.weixin.exception.WxErrorException; + +@Test(dependsOnGroups="baseAPI") +@Guice(modules = ApiTestModule.class) +public class WxMenuAPITest { + + @Inject + protected WxServiceImpl wxService; + + @Test(dataProvider = "menu", enabled = true) + public void testCreateMenu(WxMenu wxMenu) throws WxErrorException { + wxService.createMenu(wxMenu); + } + + @Test(dependsOnMethods = { "testCreateMenu"}, enabled = true) + public void testGetMenu() throws WxErrorException { + Assert.assertNotNull(wxService.getMenu()); + } + + @Test(dependsOnMethods = { "testGetMenu"}, enabled = true) + public void testDeleteMenu() throws WxErrorException { + wxService.deleteMenu(); + } + + @DataProvider(name="menu") + public Object[][] getMenu() throws JAXBException { + WxMenu menu = new WxMenu(); + WxMenuButton button1 = new WxMenuButton(); + button1.setType("click"); + button1.setName("今日歌曲"); + button1.setKey("V1001_TODAY_MUSIC"); + + WxMenuButton button2 = new WxMenuButton(); + button2.setType("click"); + button2.setName("歌手简介"); + button2.setKey("V1001_TODAY_SINGER"); + + WxMenuButton button3 = new WxMenuButton(); + button3.setName("菜单"); + + menu.getButton().add(button1); + menu.getButton().add(button2); + menu.getButton().add(button3); + + WxMenuButton button31 = new WxMenuButton(); + button31.setType("view"); + button31.setName("搜索"); + button31.setUrl("http://www.soso.com/"); + + WxMenuButton button32 = new WxMenuButton(); + button32.setType("view"); + button32.setName("视频"); + button32.setUrl("http://v.qq.com/"); + + WxMenuButton button33 = new WxMenuButton(); + button33.setType("click"); + button33.setName("赞一下我们"); + button33.setKey("V1001_GOOD"); + + button3.getSub_button().add(button31); + button3.getSub_button().add(button32); + button3.getSub_button().add(button33); + + return new Object[][] { + new Object[] { + menu + } + }; + + } + + +} diff --git a/src/test/java/chanjarster/weixin/api/WxMessageRouterTest.java b/src/test/java/chanjarster/weixin/api/WxMessageRouterTest.java index 65e3f4f2..39d65498 100644 --- a/src/test/java/chanjarster/weixin/api/WxMessageRouterTest.java +++ b/src/test/java/chanjarster/weixin/api/WxMessageRouterTest.java @@ -46,11 +46,6 @@ public class WxMessageRouterTest { Assert.assertEquals(sb.toString(), expected); } - @Test() - public void test() { - - } - @DataProvider(name="messages-1") public Object[][] messages2() { WxXmlMessage message1 = new WxXmlMessage(); diff --git a/src/test/java/chanjarster/weixin/api/WxServiceTest.java b/src/test/java/chanjarster/weixin/api/WxServiceTest.java deleted file mode 100644 index 181e2e6c..00000000 --- a/src/test/java/chanjarster/weixin/api/WxServiceTest.java +++ /dev/null @@ -1,195 +0,0 @@ -package chanjarster.weixin.api; - -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; - -import javax.xml.bind.JAXBException; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlRootElement; - -import org.apache.commons.lang3.StringUtils; -import org.testng.Assert; -import org.testng.annotations.BeforeTest; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; - -import chanjarster.weixin.bean.WxCustomMessage; -import chanjarster.weixin.bean.WxMenu; -import chanjarster.weixin.bean.WxMenu.WxMenuButton; -import chanjarster.weixin.bean.result.WxMediaUploadResult; -import chanjarster.weixin.exception.WxErrorException; -import chanjarster.weixin.util.xml.XmlTransformer; - -public class WxServiceTest { - - private WxServiceImpl wxService; - - private List media_ids = new ArrayList(); - - @BeforeTest - public void prepare() throws JAXBException { - this.wxService = null; - InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml"); - WxXmlConfigStorage config = XmlTransformer.fromXml(WxXmlConfigStorage.class, is1); - this.wxService = new WxServiceImpl(); - this.wxService.setWxConfigStorage(config); - } - - @Test() - public void testRefreshAccessToken() throws WxErrorException { - WxConfigStorage configStorage = wxService.wxConfigStorage; - String before = configStorage.getAccessToken(); - wxService.refreshAccessToken(); - - String after = configStorage.getAccessToken(); - - Assert.assertNotEquals(before, after); - Assert.assertTrue(StringUtils.isNotBlank(after)); - } - - @Test(dependsOnMethods = "testRefreshAccessToken", enabled = true) - public void sendCustomMessage() throws WxErrorException { - WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigStorage; - WxCustomMessage message = new WxCustomMessage(); - message.setMsgtype(WxConsts.CUSTOM_MSG_TEXT); - message.setTouser(configProvider.getOpenId()); - message.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:Hello World"); - - wxService.sendCustomMessage(message); - } - - @Test(dataProvider = "menu", dependsOnMethods = "testRefreshAccessToken", enabled = true) - public void testCreateMenu(WxMenu wxMenu) throws WxErrorException { - wxService.createMenu(wxMenu); - } - - @Test(dependsOnMethods = { "testRefreshAccessToken" , "testCreateMenu"}, enabled = true) - public void testGetMenu() throws WxErrorException { - Assert.assertNotNull(wxService.getMenu()); - } - - @Test(dependsOnMethods = { "testRefreshAccessToken", "testGetMenu"}, enabled = true) - public void testDeleteMenu() throws WxErrorException { - wxService.deleteMenu(); - } - - @Test(dependsOnMethods = { "testRefreshAccessToken" }, dataProvider="uploadMedia", enabled = true) - public void testUploadMedia1(String mediaType, String fileType, String fileName) throws WxErrorException, IOException { - InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName); - WxMediaUploadResult res = wxService.uploadMedia(mediaType, fileType, inputStream); - Assert.assertNotNull(res.getType()); - Assert.assertNotNull(res.getCreated_at()); - Assert.assertTrue(res.getMedia_id() != null || res.getThumb_media_id() != null); - - if (res.getMedia_id() != null) { - media_ids.add(res.getMedia_id()); - } - if (res.getThumb_media_id() != null) { - media_ids.add(res.getThumb_media_id()); - } - } - - @DataProvider - public Object[][] uploadMedia() { - return new Object[][] { - 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" } - }; - } - - @Test(dependsOnMethods = { "testUploadMedia1" }, dataProvider="downloadMedia") - public void testDownloadMedia(String media_id) throws WxErrorException { - wxService.downloadMedia(media_id); - } - - @DataProvider - public Object[][] downloadMedia() { - Object[][] params = new Object[this.media_ids.size()][]; - for (int i = 0; i < this.media_ids.size(); i++) { - params[i] = new Object[] { this.media_ids.get(i) }; - } - return params; - } - - @Test(enabled = true) - public void testCheckSignature() throws WxErrorException { - String timestamp = "23234235423246"; - String nonce = "y7didfkcmvnbd90sdofjkiefhsd"; - String signature = "77b6651628dfb9a64bfb0d3432ee053ac566a459"; - Assert.assertTrue(wxService.checkSignature(timestamp, nonce, signature)); - } - - @DataProvider(name="menu") - public Object[][] getMenu() throws JAXBException { - WxMenu menu = new WxMenu(); - WxMenuButton button1 = new WxMenuButton(); - button1.setType("click"); - button1.setName("今日歌曲"); - button1.setKey("V1001_TODAY_MUSIC"); - - WxMenuButton button2 = new WxMenuButton(); - button2.setType("click"); - button2.setName("歌手简介"); - button2.setKey("V1001_TODAY_SINGER"); - - WxMenuButton button3 = new WxMenuButton(); - button3.setName("菜单"); - - menu.getButton().add(button1); - menu.getButton().add(button2); - menu.getButton().add(button3); - - WxMenuButton button31 = new WxMenuButton(); - button31.setType("view"); - button31.setName("搜索"); - button31.setUrl("http://www.soso.com/"); - - WxMenuButton button32 = new WxMenuButton(); - button32.setType("view"); - button32.setName("视频"); - button32.setUrl("http://v.qq.com/"); - - WxMenuButton button33 = new WxMenuButton(); - button33.setType("click"); - button33.setName("赞一下我们"); - button33.setKey("V1001_GOOD"); - - button3.getSub_button().add(button31); - button3.getSub_button().add(button32); - button3.getSub_button().add(button33); - - return new Object[][] { - new Object[] { - menu - } - }; - - } - - @XmlRootElement(name = "xml") - @XmlAccessorType(XmlAccessType.FIELD) - public static class WxXmlConfigStorage extends WxInMemoryConfigStorage { - - protected String openId; - - public String getOpenId() { - return openId; - } - public void setOpenId(String openId) { - this.openId = openId; - } - @Override - public String toString() { - return "SimpleWxConfigProvider [appId=" + appId + ", secret=" + secret + ", accessToken=" + accessToken - + ", expiresIn=" + expiresIn + ", token=" + token + ", openId=" + openId + "]"; - } - - } - - -} diff --git a/src/test/java/chanjarster/weixin/bean/WxMenuTest.java b/src/test/java/chanjarster/weixin/bean/WxMenuTest.java index 90ad602d..7ac2794f 100644 --- a/src/test/java/chanjarster/weixin/bean/WxMenuTest.java +++ b/src/test/java/chanjarster/weixin/bean/WxMenuTest.java @@ -57,7 +57,7 @@ public class WxMenuTest { Assert.assertEquals(menu.toJson(), json); } - @Test(dataProvider="wxReturnMenu") + @DataProvider public Object[][] wxReturnMenu() { Object[][] res = menuJson(); String json = "{ \"menu\" : " + res[0][0] + " }"; diff --git a/src/test/resources/testng.xml b/src/test/resources/testng.xml new file mode 100644 index 00000000..6691adb1 --- /dev/null +++ b/src/test/resources/testng.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file