diff --git a/source/java/org/alfresco/repo/web/scripts/BaseWebScriptTest.java b/source/java/org/alfresco/repo/web/scripts/BaseWebScriptTest.java index 4a734a9603..ccdd0c4e50 100644 --- a/source/java/org/alfresco/repo/web/scripts/BaseWebScriptTest.java +++ b/source/java/org/alfresco/repo/web/scripts/BaseWebScriptTest.java @@ -67,8 +67,7 @@ public abstract class BaseWebScriptTest extends TestCase private boolean traceReqRes = false; // Local Server access - private static String customContext = null; - private static TestWebScriptServer server = null; + private String customContext = null; // Remote Server access private String defaultRunAs = null; @@ -156,9 +155,9 @@ public abstract class BaseWebScriptTest extends TestCase * Sets custom context for Test Web Script Server (in-process only) * @param customContext */ - public static void setCustomContext(String customContext) + protected void setCustomContext(String customContext) { - BaseWebScriptTest.customContext = customContext; + this.customContext = customContext; } /** @@ -237,20 +236,19 @@ public abstract class BaseWebScriptTest extends TestCase } } - protected static TestWebScriptServer getServer() + /** + * Get the server for the previously-supplied {@link #setCustomContext(String) custom context} + */ + protected TestWebScriptServer getServer() { - if (BaseWebScriptTest.server == null) + if (customContext == null) { - if (BaseWebScriptTest.customContext == null) - { - BaseWebScriptTest.server = TestWebScriptRepoServer.getTestServer(); - } - else - { - BaseWebScriptTest.server = TestWebScriptRepoServer.getTestServer(customContext); - } + return TestWebScriptRepoServer.getTestServer(); + } + else + { + return TestWebScriptRepoServer.getTestServer(customContext); } - return BaseWebScriptTest.server; } @@ -348,7 +346,7 @@ public abstract class BaseWebScriptTest extends TestCase asUser = (asUser == null) ? defaultRunAs : asUser; if (asUser == null) { - return BaseWebScriptTest.getServer().submitRequest(req.getMethod(), req.getFullUri(), req.getHeaders(), req.getBody(), req.getEncoding(), req.getType()); + return getServer().submitRequest(req.getMethod(), req.getFullUri(), req.getHeaders(), req.getBody(), req.getEncoding(), req.getType()); } else { @@ -358,7 +356,7 @@ public abstract class BaseWebScriptTest extends TestCase @SuppressWarnings("synthetic-access") public Response doWork() throws Exception { - return BaseWebScriptTest.getServer().submitRequest(req.getMethod(), req.getFullUri(), req.getHeaders(), req.getBody(), req.getEncoding(), req.getType()); + return getServer().submitRequest(req.getMethod(), req.getFullUri(), req.getHeaders(), req.getBody(), req.getEncoding(), req.getType()); } }, asUser); } diff --git a/source/java/org/alfresco/repo/web/scripts/TestWebScriptRepoServer.java b/source/java/org/alfresco/repo/web/scripts/TestWebScriptRepoServer.java index ea707529d2..a34d5e9ccc 100644 --- a/source/java/org/alfresco/repo/web/scripts/TestWebScriptRepoServer.java +++ b/source/java/org/alfresco/repo/web/scripts/TestWebScriptRepoServer.java @@ -34,8 +34,8 @@ import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork; import org.alfresco.repo.transaction.RetryingTransactionHelper; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; import org.alfresco.service.cmr.security.AuthenticationService; +import org.alfresco.util.EqualsHelper; import org.alfresco.web.scripts.TestWebScriptServer; -import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -46,34 +46,6 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; */ public class TestWebScriptRepoServer extends TestWebScriptServer { - private RetryingTransactionHelper retryingTransactionHelper; - private AuthenticationService authenticationService; - - - /** - * Sets helper that provides transaction callbacks - */ - public void setTransactionHelper(RetryingTransactionHelper retryingTransactionHelper) - { - this.retryingTransactionHelper = retryingTransactionHelper; - } - - /** - * @param authenticationService - */ - public void setAuthenticationService(AuthenticationService authenticationService) - { - this.authenticationService = authenticationService; - } - - /** - * Get default user name - */ - protected String getDefaultUserName() - { - return AuthenticationUtil.getAdminUserName(); - } - /** * Main entry point. */ @@ -106,26 +78,102 @@ public class TestWebScriptRepoServer extends TestWebScriptServer "classpath:alfresco/web-scripts-application-context-test.xml" }; + /** A static reference to the application context being used */ + private static ClassPathXmlApplicationContext ctx; + private static String appendedTestConfiguration; + + private RetryingTransactionHelper retryingTransactionHelper; + private AuthenticationService authenticationService; + + /** - * Retrieve an instance of the TestWebScriptServer - * - * @return Test Server + * Sets helper that provides transaction callbacks + */ + public void setTransactionHelper(RetryingTransactionHelper retryingTransactionHelper) + { + this.retryingTransactionHelper = retryingTransactionHelper; + } + + /** + * @param authenticationService + */ + public void setAuthenticationService(AuthenticationService authenticationService) + { + this.authenticationService = authenticationService; + } + + /** + * Get default user name + */ + protected String getDefaultUserName() + { + return AuthenticationUtil.getAdminUserName(); + } + + /** + * {@inheritDoc #getTestServer(String)} */ public static TestWebScriptServer getTestServer() { - ApplicationContext context = new ClassPathXmlApplicationContext(CONFIG_LOCATIONS); - TestWebScriptServer testServer = (TestWebScriptRepoServer)context.getBean("webscripts.test"); - return testServer; + return getTestServer(null); } - public static TestWebScriptServer getTestServer(String appendTestConfigLocation) + /** + * Start up a context and get the server bean. + *
+ * This method will close and restart the application context only if the configuration has + * changed. + * + * @param appendTestConfigLocation additional context file to include in the application context + * @return Test Server + */ + public static synchronized TestWebScriptServer getTestServer(String appendTestConfigLocation) { - String[] config_locations = new String[CONFIG_LOCATIONS.length+1]; - System.arraycopy(CONFIG_LOCATIONS, 0, config_locations, 0, CONFIG_LOCATIONS.length); - config_locations[CONFIG_LOCATIONS.length] = appendTestConfigLocation; + if (TestWebScriptRepoServer.ctx != null) + { + boolean configChanged = !EqualsHelper.nullSafeEquals( + appendTestConfigLocation, + TestWebScriptRepoServer.appendedTestConfiguration); + if (configChanged) + { + // The config changed, so close the context (it'll be restarted later) + try + { + ctx.close(); + ctx = null; + } + catch (Throwable e) + { + throw new RuntimeException("Failed to shut down existing application context", e); + } + } + else + { + // There is already a context with the required configuration + } + } - ApplicationContext context = new ClassPathXmlApplicationContext(config_locations); - TestWebScriptServer testServer = (TestWebScriptRepoServer)context.getBean("webscripts.test"); + // Check if we need to start/restart the context + if (TestWebScriptRepoServer.ctx == null) + { + // Restart it + final String[] configLocations; + if (appendTestConfigLocation == null) + { + configLocations = CONFIG_LOCATIONS; + } + else + { + configLocations = new String[CONFIG_LOCATIONS.length+1]; + System.arraycopy(CONFIG_LOCATIONS, 0, configLocations, 0, CONFIG_LOCATIONS.length); + configLocations[CONFIG_LOCATIONS.length] = appendTestConfigLocation; + } + TestWebScriptRepoServer.ctx = new ClassPathXmlApplicationContext(configLocations); + TestWebScriptRepoServer.appendedTestConfiguration = appendTestConfigLocation; + } + + // Get the bean + TestWebScriptServer testServer = (TestWebScriptRepoServer)TestWebScriptRepoServer.ctx.getBean("webscripts.test"); return testServer; }