日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

junit rule_Tomcat上下文JUnit @Rule

發(fā)布時間:2023/12/3 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 junit rule_Tomcat上下文JUnit @Rule 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

junit rule

創(chuàng)建測試上下文的JUnit @Rule的初稿。 這可以用Spring上下文規(guī)則可用于 這個帖子 創(chuàng)建集成測試一個完整的Spring上下文。 import org.apache.commons.dbcp.BasicDataSource; import org.apache.log4j.Logger; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList;import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.*; import java.lang.reflect.Method; import java.sql.Driver; import java.sql.DriverManager;/*** Creates an context for tests using an Apache Tomcat server.xml.** https://blogs.oracle.com/randystuph/entry/injecting_jndi_datasources_for_junit** @author alex.collins*/ public class TomcatContextRule implements TestRule {public static final Logger LOGGER = Logger.getLogger(CatalinaContextRule.class);/*** Creates all the sub-contexts for a name.*/public static void createSubContexts(Context ctx, String name) {String subContext = '';for (String x : name.substring(0, name.lastIndexOf('/')).split('/')) {subContext += x;try {ctx.createSubcontext(subContext);} catch (NamingException e) {// nop}subContext += '/';}}private final File serverXml;public TomcatContextRule(File serverXml, Object target) {if (serverXml == null || !serverXml.isFile()) {throw new IllegalArgumentException();}if (target == null) {throw new IllegalArgumentException();}this.serverXml = serverXml;}public Statement apply(final Statement statement, Description description) {return new Statement() {@Overridepublic void evaluate() throws Throwable {createInitialContext();try {statement.evaluate();} finally {destroyInitialContext();}}};}private void createInitialContext() throws Exception {LOGGER.info('creating context');System.setProperty(Context.INITIAL_CONTEXT_FACTORY, org.apache.naming.java.javaURLContextFactory.class.getName());System.setProperty(Context.URL_PKG_PREFIXES, 'org.apache.naming');final InitialContext ic = new InitialContext();createSubContexts(ic, 'java:/comp/env');final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();final DocumentBuilder builder = factory.newDocumentBuilder();final Document document = builder.parse(serverXml);// create Environment{final NodeList envs = document.getElementsByTagName('Environment');for (int i = 0; i < envs.getLength(); i++) {final Element env = (Element)envs.item(i); // must be Elementfinal String name = 'java:comp/env/' + env.getAttribute('name');final Object instance = Class.forName(env.getAttribute('type')).getConstructor(String.class).newInstance(env.getAttribute('value'));LOGGER.info('binding ' + name + ' <' + instance + '>');createSubContexts(ic, name);ic.bind(name, instance);}}// Resource{final NodeList resources = document.getElementsByTagName('Resource');for (int i = 0; i < resources.getLength(); i++) {final Element resource = (Element)resources.item(i); // must be Elementfinal String name = 'java:comp/env/' + resource.getAttribute('name');final Class<?> type = Class.forName(resource.getAttribute('type'));final Object instance;if (type.equals(DataSource.class)) {{@SuppressWarnings('unchecked') // this mus be driver?final Class<? extends Driver> driverClass = (Class<? extends Driver>) Class.forName(resource.getAttribute('driverClassName'));DriverManager.registerDriver(driverClass.newInstance());}final BasicDataSource dataSource = new BasicDataSource();// find all the bean attributes and set them use some reflectionfor (Method method : dataSource.getClass().getMethods()) {if (!method.getName().matches('^set.*')) {continue;}final String x = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4);if (!resource.hasAttribute(x)) {continue;}Class<?> y = method.getParameterTypes()[0]; // might be primitiveif (y.isPrimitive()) {if (y.getName().equals('boolean')) y = Boolean.class;if (y.getName().equals('byte')) y = Byte.class;if (y.getName().equals('char')) y = Character.class;if (y.getName().equals('double')) y = Double.class;if (y.getName().equals('float')) y = Float.class;if (y.getName().equals('int')) y = Integer.class;if (y.getName().equals('long')) y = Long.class;if (y.getName().equals('short')) y = Short.class;if (y.getName().equals('void')) y = Void.class;}method.invoke(dataSource, y.getConstructor(String.class).newInstance(resource.getAttribute(x)));}instance = dataSource;} else {// not supported, yet...throw new AssertionError('type ' + type + ' not supported');}LOGGER.info('binding ' + name + ' <' + instance + '>');createSubContexts(ic, name);ic.bind(name, instance);}}}private void destroyInitialContext() {System.clearProperty(Context.INITIAL_CONTEXT_FACTORY);System.clearProperty(Context.URL_PKG_PREFIXES);LOGGER.info('context destroyed');}}

例如:

@Rulepublic TestRule rules = RuleChain.outerRule(new CatalinaContextRule(new File(getClass().getResource('/server.xml').getFile()), this)).around(new ContextRule(new String[] {'/applicationContext.xml'}, this));

這段代碼在Github上 。

參考:來自我們的JCG合作伙伴 Alex Collins的Tomcat Context JUnit @Rule ,位于Alex Collins的博客博客中。


翻譯自: https://www.javacodegeeks.com/2012/07/tomcat-context-junit-rule.html

junit rule

總結(jié)

以上是生活随笔為你收集整理的junit rule_Tomcat上下文JUnit @Rule的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。