Injecting JNDI datasources for JUnit Tests outside of a container
来源:互联网 发布:魔术师生涯数据 编辑:程序博客网 时间:2024/06/01 09:05
I was working on some webservices that we're moving into libraries the other day, and needed to run a full set of tests using junit, but outside of a container. I didn't want to create and deploy an entire test harness, I just wanted to run the junit tests in the nb ide. But I couldn't get the test to inject the datasource resource so it was usable. I finally got it working, but I'll spare you all the pain I went through to get here. Here's how I did it in my test class:
@BeforeClass
public static void setUpClass() throws Exception {
// rcarver - setup the jndi context and the datasource
try {
// Create initial context
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.naming.java.javaURLContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES,
"org.apache.naming");
InitialContext ic = new InitialContext();
ic.createSubcontext("java:");
ic.createSubcontext("java:/comp");
ic.createSubcontext("java:/comp/env");
ic.createSubcontext("java:/comp/env/jdbc");
// Construct DataSource
OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();
ds.setURL("jdbc:oracle:thin:@host:port:db");
ds.setUser("MY_USER_NAME");
ds.setPassword("MY_USER_PASSWORD");
ic.bind("java:/comp/env/jdbc/nameofmyjdbcresource", ds);
} catch (NamingException ex) {
Logger.getLogger(MyDAOTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
As you can see, in this case I used the OracleConnectionPoolDataSource,
you should use the datasource for your db connection. Don't forget to
link in the appropriate db jar (in my case it was ojdbc14.jar)
The code that is used to retrieve the datasource (which works for test
and production) looks like this:
Context initContext = new InitialContext();
Context webContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) webContext.lookup("jdbc/nameofmyjdbcresource");
For SQLServer:
SQLServerConnectionPoolDataSource ds = new SQLServerConnectionPoolDataSource();
ds.setURL("jdbc:sqlserver://host:1433;databaseName=db");
Expose the JNDI name to the Web application
(https://glassfish.dev.java.net/javaee5/ejb/compdependencies_xmlforum_nov15.pdf),
by Adding a 'resource-ref' element in web.xml like this:
- <resource-ref>
- <res-ref-name>mail/initurfms</res-ref-name>
- <res-type>javax.mail.Session</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
Adding a 'reference-descriptor' element in weblogic.xml:
<reference-descriptor> <resource-description> <res-ref-name>mail/initurfms</res-ref-name> <jndi-name>mail/initurfms</jndi-name> </resource-description> </reference-descriptor>If you run your application in Weblogic, you don't need to create these two xml files, instead you can
configure the connectionpool, datasource directly in weblogic console. see JUnit关于数据库访问的单元测试 (2)--------------------------------
-------------------------------------------------------------------------------------------------------------
Another approach to unit test the datasource without the context:
using the class oracel.jdbc.xa.client.OracleXADataSourc
A factory for XAConnection objects. An object that implements the
XADataSource interface is typically registered with a JNDI service provider.
For optimization purposes, we implemented 2 versions of OracleXADataSource:
one for client, and one for server when used in Java Stored Procedures and
in EJB. The server-version is only available in 8.1.6 and post-8.1.6 backends.
This one is the implementation of OracleXADataSource for client-side usage
and works for both pre- and post-8.1.6 RMs.
public static DataSource createContractDataSource() throws SQLException {
OracleXADataSource dataSource;
dataSource = new OracleXADataSource();
dataSource.setUser(Configuration.getConfigurationProperty(Configuration.SKEL_DATABASE_USERNAME));
dataSource.setPassword(Configuration.getConfigurationProperty(Configuration.SKEL_DATABASE_PASSWORD));
dataSource.setURL(Configuration.getConfigurationProperty(Configuration.SKEL_DATABASE_URL));
Properties properties = new Properties();
properties.put("someproperty", "value");
dataSource.setConnectionProperties(properties);
return dataSource;
}
public class Configuration {
/** Configuration File Name. */
public static final String CONFIGURATION_FILE_NAME = "configuration.properties";
/** Skeleton Database Username. */
public static final String SKEL_DATABASE_USERNAME = "database.username";
/** Skeleton Database Password. */
public static final String SKEL_DATABASE_PASSWORD = "database.password";
/** Skeleton Database URL. */
public static final String SKEL_DATABASE_URL = "database.url";
private static Properties configuration = null;
private Configuration() {
//private so we can't instantiate the class
}
public static String getConfigurationProperty(String property) {
if (configuration == null) {
try {
loadConfiguration();
} catch (IOException e) {
//TODO error loading file, handle it better than this.
return null;
}
}
return configuration.getProperty(property);
}
private static void loadConfiguration() throws IOException {
configuration = new Properties();
InputStream input = ClassLoader.getSystemClassLoader().getResourceAsStream(
CONFIGURATION_FILE_NAME);
if (input == null) {
throw new FileNotFoundException(CONFIGURATION_FILE_NAME + " is not on the classpath");
}
configuration.load(input);
}
}
- Injecting JNDI datasources for JUnit Tests outside of a container
- initialize a static const value outside of Constructor only for integral types
- Embedding JUnit tests
- Unit tests with JUnit
- no junit tests found
- Multithreaded Tests with JUnit
- junit unrooted tests错误
- Junit Unrooted Tests 分析
- __FUNCTION__ predefined macro cannot appear outside of a function body
- A Presentation of the STL Vector Container
- A Presentation of the STL Vector Container
- tests or sets bit of a u8 type array
- Run Eclipse Junit Tests headless
- Types of tests
- Write Tests for People
- Timeout for tests
- Tests for Variances
- Write Tests for People
- Linux环境进程间通信(一) 管道及有名管道
- c#简介
- Linux 环境进程间通信(六) 套接口
- 求伯君70万年薪正式出任金山CEO
- 枚举进程(2)——利用EPROCESS/PEB地址特征进行内存搜索
- Injecting JNDI datasources for JUnit Tests outside of a container
- 常用软件测试类型分类讲解
- 0605周四
- [Singles]星外星2+1系列:半世纪的爵士情浓
- CheckBoxList详细用法
- 人品不行
- El Perro del Mar - From The Valley To The Stars
- 迷茫
- OleDbConnection 与 SqlConnection