diff --git a/source/java/org/alfresco/util/test/junitrules/AlfrescoPeople.java b/source/java/org/alfresco/util/test/junitrules/AlfrescoPeople.java index 85851f918e..b8520ddb28 100644 --- a/source/java/org/alfresco/util/test/junitrules/AlfrescoPeople.java +++ b/source/java/org/alfresco/util/test/junitrules/AlfrescoPeople.java @@ -33,6 +33,24 @@ import org.springframework.context.ApplicationContext; /** * This JUnit rule can be used to setup and teardown a set of Alfresco users for test purposes. + *
+ * Example usage: + *+ * public class YourTestClass + * { + * // Normally we would initialise the spring application context in another rule. + * @ClassRule public static final ApplicationContextInit APP_CONTEXT_RULE = new ApplicationContextInit(); + * + * // This rule will give us 8 GUID-named users. + * @Rule public final AlfrescoPeople testPeople = new AlfrescoPeople(APP_CONTEXT_RULE, 8); + * + * @Test public void aTestMethod() + * { + * Set<String> userNames = testPeople.getUsernames(); + * // etc + * } + * } + ** * @author Neil Mc Erlean * @since Odin diff --git a/source/java/org/alfresco/util/test/junitrules/AlfrescoPerson.java b/source/java/org/alfresco/util/test/junitrules/AlfrescoPerson.java index 1de7af6f20..463aad0bf2 100644 --- a/source/java/org/alfresco/util/test/junitrules/AlfrescoPerson.java +++ b/source/java/org/alfresco/util/test/junitrules/AlfrescoPerson.java @@ -27,8 +27,27 @@ import org.springframework.context.ApplicationContext; /** * This JUnit rule can be used to setup and teardown a single Alfresco user for test purposes. - * - * TODO Sample usage. + * + * Example usage: + *
+ * public class YourTestClass + * { + * // Normally we would initialise the spring application context in another rule. + * @ClassRule public static final ApplicationContextInit APP_CONTEXT_RULE = new ApplicationContextInit(); + * + * // We pass the rule that creates the spring application context. + * // This rule will give us a user with username 'NeilM'. + * @Rule public final AlfrescoPerson namedPerson = new AlfrescoPerson(APP_CONTEXT_RULE, "NeilM"); + * // This rule with give us a user with a GUID-generated name. + * @Rule public final AlfrescoPerson guidPerson = new AlfrescoPerson(APP_CONTEXT_RULE); + * + * @Test public void aTestMethod() + * { + * AuthenticationUtil.setFullyAuthenticatedUser(namedPerson.getUsername()); + * // etc + * } + * } + ** * @author Neil Mc Erlean * @since Odin diff --git a/source/java/org/alfresco/util/test/junitrules/ApplicationContextInit.java b/source/java/org/alfresco/util/test/junitrules/ApplicationContextInit.java index 0454632255..c95179609b 100644 --- a/source/java/org/alfresco/util/test/junitrules/ApplicationContextInit.java +++ b/source/java/org/alfresco/util/test/junitrules/ApplicationContextInit.java @@ -30,8 +30,21 @@ import org.springframework.context.ApplicationContext; /** * This JUnit rule can be used to bring up a {@link ApplicationContext spring application context}. - * - * TODO Sample usage. + * + * Example usage: + *
+ * public class YourTestClass + * { + * // Parameterless construction brings up the default Alfresco spring configuration. + * @ClassRule public static final ApplicationContextInit APP_CONTEXT_RULE = new ApplicationContextInit(); + * private static NodeService NODE_SERVICE; + * + * @BeforeClass public static void initSpringServices() throws Exception + * { + * NODE_SERVICE = (NodeService) APP_CONTEXT_RULE.getApplicationContext().getBean("nodeService"); + * } + * } + ** * @author Neil Mc Erlean * @since Odin diff --git a/source/java/org/alfresco/util/test/junitrules/RunAsFullyAuthenticatedRule.java b/source/java/org/alfresco/util/test/junitrules/RunAsFullyAuthenticatedRule.java index e06087f2cf..f930c4f01a 100644 --- a/source/java/org/alfresco/util/test/junitrules/RunAsFullyAuthenticatedRule.java +++ b/source/java/org/alfresco/util/test/junitrules/RunAsFullyAuthenticatedRule.java @@ -28,6 +28,8 @@ import java.lang.annotation.Target; import java.lang.reflect.InvocationTargetException; import java.util.Collection; +import junit.framework.Test; + import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.util.ParameterCheck; import org.apache.commons.logging.Log; @@ -38,8 +40,30 @@ import org.junit.runners.model.Statement; /** * This JUnit rule can be used to make test methods run as a particular user. - * - * TODO Example usage. + * A username can be provided on construction to the rule and then all
@Test
methods will be
+ * run as that user.
+ *
+ * Furthermore, if an individual test method is annotated like this @RunAsUser(userName="John")
than that
+ * method (and only that method) will be run as "John".
+ *
+ * Example usage:
+ * + * public class YourTestClass + * { + * @ClassRule public static final ApplicationContextInit APP_CONTEXT_RULE = new ApplicationContextInit(); + * @Rule public RunAsFullyAuthenticatedRule runAsGuidPerson = new RunAsFullyAuthenticatedRule("NeilM"); + * + * @Test public void doSomething() throws Exception + * { + * // This will run as NeilM + * } + * + * @Test @RunAsUser(userName="DaveC") public void doSomething() throws Exception + * { + * // This will run as DaveC + * } + * } + ** * @author Neil Mc Erlean * @since Odin