This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.jcr.repoinit-1.0.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-repoinit.git
commit d20da334eadeb0bf6b7e7f20b2e76921bbb240d3 Author: Bertrand Delacretaz <[email protected]> AuthorDate: Mon Dec 21 15:06:28 2015 +0000 SLING-5355 - factor our TestUtil git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/acldef/oak-jcr@1721183 13f79535-47bb-0310-9956-ffa450edef68 --- .../sling/acldef/jcr/CreateServiceUsersTest.java | 71 ++++---------------- .../java/org/apache/sling/acldef/jcr/TestUtil.java | 78 ++++++++++++++++++++++ 2 files changed, 90 insertions(+), 59 deletions(-) diff --git a/src/test/java/org/apache/sling/acldef/jcr/CreateServiceUsersTest.java b/src/test/java/org/apache/sling/acldef/jcr/CreateServiceUsersTest.java index 0d42c47..0f09972 100644 --- a/src/test/java/org/apache/sling/acldef/jcr/CreateServiceUsersTest.java +++ b/src/test/java/org/apache/sling/acldef/jcr/CreateServiceUsersTest.java @@ -16,27 +16,12 @@ */ package org.apache.sling.acldef.jcr; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -import java.io.Reader; -import java.io.StringReader; -import java.util.List; import java.util.Random; import javax.jcr.RepositoryException; -import javax.jcr.Session; -import org.apache.commons.io.IOUtils; -import org.apache.jackrabbit.api.security.user.Authorizable; -import org.apache.jackrabbit.api.security.user.User; -import org.apache.jackrabbit.api.security.user.UserManager; -import org.apache.sling.acldef.parser.ACLDefinitions; -import org.apache.sling.acldef.parser.ParseException; -import org.apache.sling.acldef.parser.operations.Operation; import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.apache.sling.testing.mock.sling.junit.SlingContext; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -47,56 +32,24 @@ public class CreateServiceUsersTest { @Rule public final SlingContext context = new SlingContext(ResourceResolverType.JCR_OAK); - private Session session; - private UserManager userManager; private static final Random random = new Random(42); private String namePrefix; + private TestUtil U; - private List<Operation> parse(String input) throws ParseException { - final Reader r = new StringReader(input); - try { - return new ACLDefinitions(r).parse(); - } finally { - IOUtils.closeQuietly(r); - } - } - @Before public void setup() throws RepositoryException { - // We don't need to logout this session, the Sling Mocks library - // takes care of that - session = context.resourceResolver().adaptTo(Session.class); - - userManager = ServiceUserUtil.getUserManager(session); + U = new TestUtil(context); namePrefix = "user_" + random.nextInt(); } - private void assertServiceUser(String info, String id, boolean expectToExist) throws RepositoryException { - final Authorizable a = userManager.getAuthorizable(id); - if(!expectToExist) { - assertNull(info + ", expecting Principal to be absent:" + id, a); - } else { - assertNotNull(info + ", expecting Principal to exist:" + id, a); - final User u = (User)a; - assertNotNull(info + ", expecting Principal to be a System user:" + id, u.isSystemUser()); - } - } - - private void exec(String input) throws ParseException { - final AclOperationVisitor v = new AclOperationVisitor(session); - for(Operation o : parse(input)) { - o.accept(v); - } - } - @Test public void createDeleteSingleTest() throws Exception { final String userId = namePrefix + "_cdst"; - assertServiceUser("at start of test", userId, false); - exec("create service user " + userId); - assertServiceUser("affter creating user", userId, true); - exec("delete service user " + userId); - assertServiceUser("after deleting user", userId, false); + U.assertServiceUser("at start of test", userId, false); + U.parseAndExecute("create service user " + userId); + U.assertServiceUser("after creating user", userId, true); + U.parseAndExecute("delete service user " + userId); + U.assertServiceUser("after deleting user", userId, false); } private String user(int index) { @@ -110,24 +63,24 @@ public class CreateServiceUsersTest { { final StringBuilder input = new StringBuilder(); for(int i=0; i < n; i++) { - assertServiceUser("at start of test", user(i), false); + U.assertServiceUser("at start of test", user(i), false); input.append("create service user ").append(user(i)).append("\n"); } - exec(input.toString()); + U.parseAndExecute(input.toString()); } { final StringBuilder input = new StringBuilder(); for(int i=0; i < n; i++) { - assertServiceUser("before deleting user", user(i), true); + U.assertServiceUser("before deleting user", user(i), true); input.append("delete service user ").append(user(i)).append("\n"); } - exec(input.toString()); + U.parseAndExecute(input.toString()); } for(int i=0; i < n; i++) { - assertServiceUser("after deleting users", user(i), false); + U.assertServiceUser("after deleting users", user(i), false); } } } diff --git a/src/test/java/org/apache/sling/acldef/jcr/TestUtil.java b/src/test/java/org/apache/sling/acldef/jcr/TestUtil.java new file mode 100644 index 0000000..c07dd21 --- /dev/null +++ b/src/test/java/org/apache/sling/acldef/jcr/TestUtil.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.acldef.jcr; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.io.Reader; +import java.io.StringReader; +import java.util.List; + +import javax.jcr.RepositoryException; +import javax.jcr.Session; +import javax.jcr.SimpleCredentials; + +import org.apache.commons.io.IOUtils; +import org.apache.jackrabbit.api.security.user.Authorizable; +import org.apache.jackrabbit.api.security.user.User; +import org.apache.sling.acldef.parser.ACLDefinitions; +import org.apache.sling.acldef.parser.ParseException; +import org.apache.sling.acldef.parser.operations.Operation; +import org.apache.sling.testing.mock.sling.junit.SlingContext; + +/** Test utilities */ +class TestUtil { + + final Session adminSession; + + TestUtil(SlingContext ctx) { + adminSession = ctx.resourceResolver().adaptTo(Session.class); + } + + List<Operation> parse(String input) throws ParseException { + final Reader r = new StringReader(input); + try { + return new ACLDefinitions(r).parse(); + } finally { + IOUtils.closeQuietly(r); + } + } + + void assertServiceUser(String info, String id, boolean expectToExist) throws RepositoryException { + final Authorizable a = ServiceUserUtil.getUserManager(adminSession).getAuthorizable(id); + if(!expectToExist) { + assertNull(info + ", expecting Principal to be absent:" + id, a); + } else { + assertNotNull(info + ", expecting Principal to exist:" + id, a); + final User u = (User)a; + assertNotNull(info + ", expecting Principal to be a System user:" + id, u.isSystemUser()); + } + } + + void parseAndExecute(String input) throws ParseException { + final AclOperationVisitor v = new AclOperationVisitor(adminSession); + for(Operation o : parse(input)) { + o.accept(v); + } + } + + Session getServiceSession(String serviceUsername) throws RepositoryException { + final SimpleCredentials cred = new SimpleCredentials(serviceUsername, new char[0]); + return adminSession.impersonate(cred); + } +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
