Results 1 to 4 of 4

Thread: Dynamically loading the values for property placeholders in context xml file

  1. #1
    Join Date
    Jan 2007
    Location
    California
    Posts
    31

    Default Dynamically loading the values for property placeholders in context xml file

    Hi,

    I am running some JUnit tests cases, and I trying to dynamically load the values for certain property placeholder in my context xml file during runtime.

    For example, I have a JUnit class that has 5 different test cases (i.e. 5 test methods in one Test class); all of these test cases share the same context xml file, such as the one below:

    Code:
      <bean id="resolver" class="org.springframework.xml.castor.CastorResolverFactoryBean">
        <property name="mappingLocations">
          <list>
            <value>${folder.path}/castor-mapping.xml</value>
          </list>
        </property>
      </bean>
      
      <bean id="unmarshaller" class="org.springframework.xml.castor.CastorUnmarshallerFactoryBean">
        <property name="resolver" ref="resolver"/>
      </bean>

    I have the JUnit Test class which contains the following setup method:

    Code:
        protected void setUp() throws Exception
        {
            /*
             * Load the unmarshaller object through Spring FactoryBean
             */
            ApplicationContextUtil appContextUtil = new ApplicationContextUtil();
            unmarshaller = (Unmarshaller) appContextUtil.getBean("unmarshaller");
        }
    And in my JUnit I have 5 test methods, for example, testMethod1(), testMethod2(), ...etc. And each test method has its own testdata subfolder path, for example, folder paths: testdata/testMethod1, testdata/testMethod2, ....etc.

    Each testdata subfolder contains a different version of castor-mapping.xml file, which is purposely made different for each test method for testing purposes. So there are a total of 5 testdata subfolders, with 5 different castor-mapping.xml files. And basically for each test case I want to be able to dynamically replace the ${folder.path} property placeholder with the correct testdata folder path for that JUnit test method. This way the unmarshaller bean would get loaded from the Spring factory bean with the correct castor-mapping.xml file for the JUnit test method that is being executed.

    How can I set the value of the ${folder.path} property in the context xml file during runtime for each one of my test methods? I already thought about using the PropertyPlaceholderConfigurer, but that wouldn't work, because the ${folder.path} property would get set to the same value in the context xml file for all the test methods within my JUnit.

    Any help is very much appreciated.

    Thanks.

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I'm not sure if there is a nice way to sort this out. The PropertyPlaceholderConfigurer will run and replace anyvalues as required. What you want to do is re-run this later once changing the replacement. I don't think this is possible as once it's been substituted that's it. I guess you could manually set the value within each test by calling the setter.

  3. #3
    Join Date
    Jan 2007
    Location
    California
    Posts
    31

    Default

    Yes, I agree. I don't think there is a clean way for doing this with Spring. For now I was able to handle this situation by the doing the following extra code.

    Code:
    // Attempt to load resource from class path
    Resource resource = new ClassPathResource(contextLocation);
    
    // instantiate factory bean with resource
    XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(resource);
    
    PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
    cfg.setProperties(props);
    
    // now actually do the replacement (replace property place holders
    // in the context xml file with values from the properties object)
    cfg.postProcessBeanFactory(xmlBeanFactory);
    In the above code the contextLocation variable holds the file path for the spring context xml file, and the props is a java.util.Properties object that holds the properties that need to be replaced in the spring context xml file. I have thought about using the ApplicationContext rather than the BeanFactory, but I was not able to achieve the above with ApplicationContext's implementations. So I resorted to using the XmlBeanFactory for solving this problem.

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I've not used it yet, but the parameterized test feature of JUnit4 might have been useful here. I'm not sure it's entirely what you were trying to do.
    http://www.devx.com/Java/Article/31983/1954?pf=true

Posting Permissions

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