Results 1 to 4 of 4

Thread: Using PropertyPlaceholderConfigurer to test DAO layer

  1. #1
    Join Date
    May 2006
    Posts
    8

    Default Using PropertyPlaceholderConfigurer to test DAO layer

    I would like my applicationcontext to load a different file if I am running my unit tests, versus if I am deploying to a container.

    I would like to use the in memory Hsqldb for quickly running through my tests that hit the database. For actual deployment, I want to use a mysql db.

    Here is what I have so far:
    Code:
    	
    <!--  ======================== Persistence Layer ============================ -->
    	
    	<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
    	<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
    	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="location" value="/WEB-INF/jdbc.properties"/>
    	</bean>
    
    	<!-- 
    		Simple local DataSource that works in any environment.
    		This uses the JDBC DriverManager to obtain connections, and does NOT perform connection
    		pooling. Connection pooling is essential to all real-world applications.
    		This definition is good for getting started, as it introduces no dependencies beyond
    		the JDK, but DriverManagerDataSource is not intended for production usage.
    	 -->
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="${jdbc.driverClassName}"/>
    		<property name="url" value="${jdbc.url}"/>
    		<property name="username" value="${jdbc.username}"/>
    		<property name="password" value="${jdbc.password}"/>
    	</bean>
    	
    	<bean id="baseDao" class="com.infoglide.biscuit.dao.BaseDaoImpl" abstract="true">
    		<constructor-arg>
    			<ref bean="dataSource"/>
    		</constructor-arg>
    		<constructor-arg>
    			<ref bean="biscuitManager"/>
    		</constructor-arg>
    	</bean>
    However, this is using the WEB-INF/jdbc.properties file. Right now I will need to go in and manually comment out the mysql db properties and uncomment the hsqldb properties and keep going back and forth with the same process.

    In the style of dependency injection, how do I make this such that when running my tests, I "inject" in a new hsqljdbc.properties with my test db config and all other times I use the jdbc.properties? The manual commenting/uncommenting seems very crude.

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    The solution I use is to have two PropertyPlaceholderConfigurer definitions in two files - one for production and one for environment (basically only the property files are different). For tests, the test-configuration.xml is used while in production, production-configuration.xml is chosen (for example). This can be done through some sort of basic test class - this way no matter if the tests are fired from the editor or from command line, they still work.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Jun 2005
    Posts
    4,241

    Default Use two properties files?

    Or you could use two jdbc.properties files, one in your web app (/WEB-INF in the war file), and the other in another location (e.g. /WEB-INF in the unit test source directory). You can change the class path for your unit test so that the web app properties file is not on the classpath, but the other jdbc.properties is.

  4. #4
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    25

    Post Use a system property to indicate which properties file to use

    You can define:
    Code:
    	<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
    	<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
    	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="location" value="${jdbc.properties}"/>
    	</bean>
    And you define a system property called "jdbc.properties". In production, the value of this system property is set to "/WEB-INF/jdbc.properties" while in test environment or JUnit, set the value of this system property to "/WEB-INF/hsqljdbc.properties".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •