package org.alfresco.util.test.junitrules; import java.lang.annotation.Annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.Collection; import java.util.concurrent.CountDownLatch; import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.rules.ErrorCollector; import org.junit.runner.Description; import org.junit.runners.model.Statement; /** * This JUnit rule can be used to turn existing test code into Load Tests. * It does this in conjunction with the {@link AlfrescoPeople} JUnit rule. * That rule is used to {@link AlfrescoPeople#AlfrescoPeople(ApplicationContextInit, int) create} a * fixed number of Alfresco users. Then {@link LoadTestRule} will do the following for each of your JUnit 4 @Test methods: *
* public class YourTestClass * { * // We need to ensure that JUnit Rules in the same 'group' (in this case the 'static' group) execute in the correct * // order. To do this we do not annotate the JUnit Rule fields themselves, but instead wrap them up in a RuleChain. * * // Initialise the spring application context with a rule. * public static final ApplicationContextInit APP_CONTEXT_RULE = new ApplicationContextInit(); * public static final AlfrescoPeople TEST_USERS = new AlfrescoPeople(APP_CONTEXT_RULE, 32); * * @ClassRule public static RuleChain STATIC_RULE_CHAIN = RuleChain.outerRule(APP_CONTEXT_RULE) * .around(TEST_USERS); * * @Rule public LoadTestRule loadTestRule = new LoadTestRule(TEST_USERS); * * @Test public void aNormalTestMethod() * { * ensureFeatureFooWorks() * } * * @LoadTest @Test public void aLoadTestMethod() * { * ensureFeatureFooWorks() * } * * public void ensureFeatureFooWorks() {} * } ** * @author Neil Mc Erlean */ public class LoadTestRule extends ErrorCollector { private static final Log log = LogFactory.getLog(LoadTestRule.class); private final AlfrescoPeople people; public LoadTestRule(AlfrescoPeople people) { this.people = people; } /** * Gets the number of users/concurrent threads that this Rule has been configured to use. * @return the number of users/threads. */ public int getCount() { return this.people.getUsernames().size(); } @Override public Statement apply(final Statement base, final Description description) { boolean loadTestingRequestedForThisMethod = false; Collection