@@ -1,20 +1,24 @@ | |||||
package me.chanjar.weixin.cp.api; | package me.chanjar.weixin.cp.api; | ||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
import org.slf4j.Logger; | |||||
import org.slf4j.LoggerFactory; | |||||
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 me.chanjar.weixin.common.util.xml.XStreamInitializer; | ||||
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; | import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; | ||||
import me.chanjar.weixin.cp.config.WxCpConfigStorage; | |||||
import me.chanjar.weixin.cp.config.WxCpInMemoryConfigStorage; | import me.chanjar.weixin.cp.config.WxCpInMemoryConfigStorage; | ||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
public class ApiTestModule implements Module { | public class ApiTestModule implements Module { | ||||
private final Logger log = LoggerFactory.getLogger(this.getClass()); | |||||
private static final String TEST_CONFIG_XML = "test-config.xml"; | |||||
public static <T> T fromXml(Class<T> clazz, InputStream is) { | |||||
private static <T> T fromXml(Class<T> clazz, InputStream is) { | |||||
XStream xstream = XStreamInitializer.getInstance(); | XStream xstream = XStreamInitializer.getInstance(); | ||||
xstream.alias("xml", clazz); | xstream.alias("xml", clazz); | ||||
xstream.processAnnotations(clazz); | xstream.processAnnotations(clazz); | ||||
@@ -23,17 +27,19 @@ public class ApiTestModule implements Module { | |||||
@Override | @Override | ||||
public void configure(Binder binder) { | public void configure(Binder binder) { | ||||
try (InputStream is1 = ClassLoader | |||||
.getSystemResourceAsStream("test-config.xml")) { | |||||
WxXmlCpInMemoryConfigStorage config = fromXml( | |||||
WxXmlCpInMemoryConfigStorage.class, is1); | |||||
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(TEST_CONFIG_XML)) { | |||||
if (inputStream == null) { | |||||
throw new RuntimeException("测试配置文件【" + TEST_CONFIG_XML + "】未找到,请参照test-config-sample.xml文件生成"); | |||||
} | |||||
WxXmlCpInMemoryConfigStorage config = fromXml(WxXmlCpInMemoryConfigStorage.class, inputStream); | |||||
WxCpService wxService = new WxCpServiceImpl(); | WxCpService wxService = new WxCpServiceImpl(); | ||||
wxService.setWxCpConfigStorage(config); | wxService.setWxCpConfigStorage(config); | ||||
binder.bind(WxCpService.class).toInstance(wxService); | binder.bind(WxCpService.class).toInstance(wxService); | ||||
binder.bind(WxXmlCpInMemoryConfigStorage.class).toInstance(config); | binder.bind(WxXmlCpInMemoryConfigStorage.class).toInstance(config); | ||||
} catch (IOException e) { | } catch (IOException e) { | ||||
e.printStackTrace(); | |||||
this.log.error(e.getMessage(), e); | |||||
} | } | ||||
} | } | ||||
@@ -1,22 +1,30 @@ | |||||
package cn.binarywang.wx.miniapp.test; | package cn.binarywang.wx.miniapp.test; | ||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
import java.util.concurrent.locks.ReentrantLock; | |||||
import org.slf4j.Logger; | |||||
import org.slf4j.LoggerFactory; | |||||
import cn.binarywang.wx.miniapp.api.WxMaService; | import cn.binarywang.wx.miniapp.api.WxMaService; | ||||
import cn.binarywang.wx.miniapp.config.WxMaConfig; | import cn.binarywang.wx.miniapp.config.WxMaConfig; | ||||
import com.google.inject.Binder; | import com.google.inject.Binder; | ||||
import com.google.inject.Module; | import com.google.inject.Module; | ||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
import java.util.concurrent.locks.ReentrantLock; | |||||
/** | /** | ||||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | * @author <a href="https://github.com/binarywang">Binary Wang</a> | ||||
*/ | */ | ||||
public class ApiTestModule implements Module { | public class ApiTestModule implements Module { | ||||
private final Logger log = LoggerFactory.getLogger(this.getClass()); | |||||
private static final String TEST_CONFIG_XML = "test-config.xml"; | |||||
@Override | @Override | ||||
public void configure(Binder binder) { | public void configure(Binder binder) { | ||||
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream("test-config.xml")) { | |||||
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(TEST_CONFIG_XML)) { | |||||
if (inputStream == null) { | |||||
throw new RuntimeException("测试配置文件【" + TEST_CONFIG_XML + "】未找到,请参照test-config-sample.xml文件生成"); | |||||
} | |||||
TestConfig config = TestConfig.fromXml(inputStream); | TestConfig config = TestConfig.fromXml(inputStream); | ||||
config.setAccessTokenLock(new ReentrantLock()); | config.setAccessTokenLock(new ReentrantLock()); | ||||
@@ -26,7 +34,7 @@ public class ApiTestModule implements Module { | |||||
binder.bind(WxMaService.class).toInstance(wxService); | binder.bind(WxMaService.class).toInstance(wxService); | ||||
binder.bind(WxMaConfig.class).toInstance(config); | binder.bind(WxMaConfig.class).toInstance(config); | ||||
} catch (IOException e) { | } catch (IOException e) { | ||||
e.printStackTrace(); | |||||
this.log.error(e.getMessage(), e); | |||||
} | } | ||||
} | } | ||||
@@ -1,5 +1,12 @@ | |||||
package me.chanjar.weixin.mp.api.test; | package me.chanjar.weixin.mp.api.test; | ||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
import java.util.concurrent.locks.ReentrantLock; | |||||
import org.slf4j.Logger; | |||||
import org.slf4j.LoggerFactory; | |||||
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; | ||||
@@ -8,16 +15,18 @@ import me.chanjar.weixin.mp.api.WxMpConfigStorage; | |||||
import me.chanjar.weixin.mp.api.WxMpService; | import me.chanjar.weixin.mp.api.WxMpService; | ||||
import me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl; | import me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl; | ||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
import java.util.concurrent.locks.ReentrantLock; | |||||
public class ApiTestModule implements Module { | public class ApiTestModule implements Module { | ||||
private final Logger log = LoggerFactory.getLogger(this.getClass()); | |||||
private static final String TEST_CONFIG_XML = "test-config.xml"; | |||||
@Override | @Override | ||||
public void configure(Binder binder) { | public void configure(Binder binder) { | ||||
try (InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml")) { | |||||
TestConfigStorage config = this.fromXml(TestConfigStorage.class, is1); | |||||
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(TEST_CONFIG_XML)) { | |||||
if (inputStream == null) { | |||||
throw new RuntimeException("测试配置文件【" + TEST_CONFIG_XML + "】未找到,请参照test-config-sample.xml文件生成"); | |||||
} | |||||
TestConfigStorage config = this.fromXml(TestConfigStorage.class, inputStream); | |||||
config.setAccessTokenLock(new ReentrantLock()); | config.setAccessTokenLock(new ReentrantLock()); | ||||
WxMpService wxService = new WxMpServiceOkHttpImpl(); | WxMpService wxService = new WxMpServiceOkHttpImpl(); | ||||
wxService.setWxMpConfigStorage(config); | wxService.setWxMpConfigStorage(config); | ||||
@@ -25,7 +34,7 @@ public class ApiTestModule implements Module { | |||||
binder.bind(WxMpService.class).toInstance(wxService); | binder.bind(WxMpService.class).toInstance(wxService); | ||||
binder.bind(WxMpConfigStorage.class).toInstance(config); | binder.bind(WxMpConfigStorage.class).toInstance(config); | ||||
} catch (IOException e) { | } catch (IOException e) { | ||||
e.printStackTrace(); | |||||
this.log.error(e.getMessage(), e); | |||||
} | } | ||||
} | } | ||||
@@ -1,5 +1,11 @@ | |||||
package com.github.binarywang.wxpay.testbase; | package com.github.binarywang.wxpay.testbase; | ||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
import org.slf4j.Logger; | |||||
import org.slf4j.LoggerFactory; | |||||
import com.github.binarywang.wxpay.config.WxPayConfig; | import com.github.binarywang.wxpay.config.WxPayConfig; | ||||
import com.github.binarywang.wxpay.service.WxPayService; | import com.github.binarywang.wxpay.service.WxPayService; | ||||
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; | import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; | ||||
@@ -8,17 +14,15 @@ import com.google.inject.Module; | |||||
import com.thoughtworks.xstream.XStream; | import com.thoughtworks.xstream.XStream; | ||||
import me.chanjar.weixin.common.util.xml.XStreamInitializer; | import me.chanjar.weixin.common.util.xml.XStreamInitializer; | ||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
public class ApiTestModule implements Module { | public class ApiTestModule implements Module { | ||||
private final Logger log = LoggerFactory.getLogger(this.getClass()); | |||||
private static final String TEST_CONFIG_XML = "test-config.xml"; | private static final String TEST_CONFIG_XML = "test-config.xml"; | ||||
@Override | @Override | ||||
public void configure(Binder binder) { | public void configure(Binder binder) { | ||||
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(TEST_CONFIG_XML)) { | try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(TEST_CONFIG_XML)) { | ||||
if (inputStream == null) { | if (inputStream == null) { | ||||
throw new RuntimeException("测试配置文件【" + TEST_CONFIG_XML + "】未找到"); | |||||
throw new RuntimeException("测试配置文件【" + TEST_CONFIG_XML + "】未找到,请参照test-config-sample.xml文件生成"); | |||||
} | } | ||||
XmlWxPayConfig config = this.fromXml(XmlWxPayConfig.class, inputStream); | XmlWxPayConfig config = this.fromXml(XmlWxPayConfig.class, inputStream); | ||||
@@ -28,8 +32,9 @@ public class ApiTestModule implements Module { | |||||
binder.bind(WxPayService.class).toInstance(wxService); | binder.bind(WxPayService.class).toInstance(wxService); | ||||
binder.bind(WxPayConfig.class).toInstance(config); | binder.bind(WxPayConfig.class).toInstance(config); | ||||
} catch (IOException e) { | } catch (IOException e) { | ||||
e.printStackTrace(); | |||||
this.log.error(e.getMessage(), e); | |||||
} | } | ||||
} | } | ||||
@SuppressWarnings("unchecked") | @SuppressWarnings("unchecked") | ||||