2016-08-29 5 views
1

java 7 및 tomcat 7을 사용하고 있습니다. jndi에 tomcat/conf/server.xml을 사용하는 jUnit에서 몇 가지 테스트를 작성하고 있습니다. 다음은 maven suggest 폴더 구조입니다.junit 테스트 케이스에서 tomcat server.xml 읽기

src 
|___test 
    |___java 
    |  |___Testcase.java 
    |___resources 
      |___conf 
        |___server.xml 

내 샘플의 server.xml은

<Resource name="jdbc/junit_db" 
     type="javax.sql.DataSource" 
     factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory" 
     driverClassName="com.mysql.jdbc.Driver" 
     url="jdbc:mysql://localhost:3306/junit_db?zeroDateTimeBehavior=round&amp;autoReconnect=true&amp;dumpQueriesOnException=true" 
     username="root" 
     password="password" 
     maxIdle="0" 
     minIdle="0" 
     initialSize="1" 
     maxWait="5000" 
     maxActive="50" 
     loginTimeout="1000" 
     minEvictableIdleTimeMillis="2000" 
     timeBetweenEvictionRunsMillis="5000" 
     validationQuery="SELECT 1" 
     testOnBorrow="true" 
     testOnReturn="true" 
     testWhileIdle="false" 
     logAbandoned="true" 
     removeAbandoned="true" 
     poolPreparedStatements="true" 
     maxOpenPreparedStatements="10000" 
     accessToUnderlyingConnectionAllowed="false" 
     defaultAutoCommit="false" 
     defaultReadOnly="false" 
     defaultTransactionIsolation="4"/> 

<Resource name="jdbc/junit_hive_db" 
      type="javax.sql.DataSource" 
      factory="com.office.hive.HiveDataSourceFactory" 
      driverClassName="org.apache.hive.jdbc.HiveDriver" 
      url="jdbc:hive2://localhost:10000/default?zeroDateTimeBehavior=round" 
      username="" 
      password="" /> 

내가 JUnit 테스트 케이스를 실행하기 전에 IntialContext에이 server.xml을로드하려면, 다음과 같이한다. 이것을 달성하는 방법?

답변

0

Give TomcatJNDI a try. 그것은 즉시이 조회됩니다 이러한 파일에 선언 된 모든 JNDI 기반의 객체를 제공합니다.

DataSource ds = (DataSource) InitialContext.doLookup("java:comp/env/path/to/datasource") 

More about TomcatJNDI can be found here.

: 당신이 사용되므로이를 달성하기 코드 예를 다음

TomcatJNDI tomcatJNDI = new TomcatJNDI(); 
tomcatJNDI.processServerXml(serverXmlFile) 
tomcatJNDI.processContextXml(contextXmlFile); 
tomcatJNDI.start(); 

당신이 객체를 조회 할 수 있습니다입니다

관련 문제