You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:49:35 UTC

[sling-org-apache-sling-jcr-repoinit] 08/17: SLING-5917 - add RepositoryInitializer tests

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.2
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-repoinit.git

commit 35302c8c2ea1e782bb7869eeed343a8d4a340f3b
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Fri Jul 29 09:33:32 2016 +0000

    SLING-5917 - add RepositoryInitializer tests
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/jcr/repoinit@1754490 13f79535-47bb-0310-9956-ffa450edef68
---
 .../jcr/repoinit/RepositoryInitializerTest.java    | 129 +++++++++++++++++++++
 1 file changed, 129 insertions(+)

diff --git a/src/test/java/org/apache/sling/jcr/repoinit/RepositoryInitializerTest.java b/src/test/java/org/apache/sling/jcr/repoinit/RepositoryInitializerTest.java
new file mode 100644
index 0000000..8350af6
--- /dev/null
+++ b/src/test/java/org/apache/sling/jcr/repoinit/RepositoryInitializerTest.java
@@ -0,0 +1,129 @@
+/*
+ * 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.jcr.repoinit;
+
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import javax.jcr.Session;
+
+import org.apache.sling.jcr.api.SlingRepository;
+import org.apache.sling.jcr.repoinit.impl.JcrRepoInitOpsProcessorImpl;
+import org.apache.sling.jcr.repoinit.impl.RepositoryInitializer;
+import org.apache.sling.jcr.repoinit.impl.TestUtil;
+import org.apache.sling.repoinit.parser.impl.RepoInitParserService;
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+/** Test the two ways in which our RepositoryInitializer
+ *  can read repoinit statements: either from a provisioning
+ *  model file or directly as raw repoinit statements. 
+ */
+@RunWith(Parameterized.class)
+public class RepositoryInitializerTest {
+    
+    @Rule
+    public final SlingContext context = new SlingContext(ResourceResolverType.JCR_OAK);
+    
+    private RepositoryInitializer initializer;
+    private Map<String, Object> config;
+    private TestUtil U;
+    private final String repoInitText;
+    private final String url;
+    private final String modelSection;
+    private final boolean testLogin;
+    private final String serviceUser;
+    
+    @Parameters(name="{0}")
+    public static Collection<Object[]> data() {
+        final List<Object []> result = new ArrayList<Object[]>();
+        
+        result.add(new Object[] { "All empty, just setup + parsing", "", false });
+        result.add(new Object[] { "Using provisioning model", "SECTION_" + UUID.randomUUID(), true }); 
+        result.add(new Object[] { "Raw repoinit/empty section", "", true}); 
+        result.add(new Object[] { "Raw repoinit/null section", "", true}); 
+        return result;
+    }
+    
+    public RepositoryInitializerTest(String description, String modelSection, boolean testLogin) throws IOException {
+        serviceUser = getClass().getSimpleName() + "-" + UUID.randomUUID();
+        
+        String txt = "create service user " + serviceUser; 
+        if(modelSection != null && modelSection.length() > 0) {
+            txt = "[feature name=foo]\n[:" + modelSection + "]\n" + txt; 
+        }
+        this.repoInitText = txt + "\n";
+        this.url = getTestUrl(repoInitText);
+        this.modelSection = modelSection;
+        this.testLogin = testLogin;
+    }
+    
+    @Before
+    public void setup() throws Exception {
+        U = new TestUtil(context);
+
+        initializer = new RepositoryInitializer();
+        config = new HashMap<String, Object>();
+        config.put(RepositoryInitializer.PROP_TEXT_URL, url);
+        config.put(RepositoryInitializer.PROP_MODEL_SECTION_NAME, modelSection);
+        initializer.activate(config);
+        
+        context.registerInjectActivateService(new RepoInitParserService());
+        context.registerInjectActivateService(new JcrRepoInitOpsProcessorImpl());
+        context.registerInjectActivateService(initializer, config);
+        
+        // Mock environment doesn't cause this to be called
+        initializer.processRepository(context.getService(SlingRepository.class));
+    }
+    
+    @Test
+    public void testLogin() throws Exception {
+        if(testLogin) {
+            try {
+                U.loginService(serviceUser);
+            } catch(Exception e) {
+                fail("Login failed for " + serviceUser + " repoinit statements (" + repoInitText + ") not applied?");
+            }
+        }
+    }
+    
+    /** Return the URL of a temporary file that contains repoInitText */
+    private String getTestUrl(String repoInitText) throws IOException {
+        final File tmpFile = File.createTempFile(getClass().getSimpleName(), "txt");
+        tmpFile.deleteOnExit();
+        final FileWriter w = new FileWriter(tmpFile);
+        w.write(repoInitText);
+        w.flush();
+        w.close();
+        return "file://" + tmpFile.getAbsolutePath();
+    }
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.