views:

2144

answers:

2

Summary

We have a central LDAP server that our deployed Java web app should connect to. Our JUnit tests depend on specific data in the LDAP repository, so they need to connect to an embedded ApacheDS LDAP server, primed with a sample data set. How do we make sure that the ApacheDS server doesn't start up when we deploy our webapp?

Details

We are using Spring security, and have the following line in ldap-context.xml to start up the embedded LDAP server:

<security:ldap-server root="dc=test,dc=com" port="33389" ldif="classpath:EmbeddedServerRoot.ldif" />

Currently, our web.xml references both this test context file and our top-level application-context.xml:

    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>
           classpath:ldap-context.xml
           classpath:application-context.xml 
       </param-value>
   </context-param>

We need to make sure that ldap-context.xml is included when we run our JUnit tests, and when we run the webapp directly from eclipse (via WTP), but excluded when we package the war and deploy it to a server.

We're using maven as the build tool. We can fairly easily take care of this situation for our JUnit tests by making sure they include both spring context files in the context configuration:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:ldap-context.xml", "classpath:application-context.xml" })
public class TestStuff {
}

Then, our web.xml would only include application-context.xml, except for one thing - this doesn't work when running from WTP - we need the embedded server to start up in that case as well. Any suggestions?

+1  A: 

If you're using Maven, why not use the Assembly plugin to manage your environment deployments. It seems like your spring file is not that complex, so you can have a common spring file which doesn't have the ldap-context.xml reference, and then a test-specific version which does have the ldap reference. When assembly is configured and run, the environment specific file will overwrite the common version, and then you can deploy your packaged app.

Spencer K
Are you advocating two web.xml files? It sounds ldap-context.xml would need to be included by default so that WTP would work, then maven would have to override it with a "production" web.xml file...? More details on your approach would be helpful.
Julie
A: 

An other possibility is to use some properties in the pom and a filtered spring bean file defining aliases for the beans to switch between environments. But you need to habe both beans in the config, but you will use the one or the other.

Arne Burmeister