incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1486095 - in /sling/whiteboard/bdelacretaz/sling-pax-util: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/sling/ src/main/java/org/apache/sling/paxexam/ src/main/java/org/apache/sling...
Date Fri, 24 May 2013 15:35:34 GMT
Author: bdelacretaz
Date: Fri May 24 15:35:33 2013
New Revision: 1486095

URL: http://svn.apache.org/r1486095
Log:
Playing with pax exam bundle provisioning driven by Sling bundle lists - still quite rough

Added:
    sling/whiteboard/bdelacretaz/sling-pax-util/   (with props)
    sling/whiteboard/bdelacretaz/sling-pax-util/pom.xml
    sling/whiteboard/bdelacretaz/sling-pax-util/src/
    sling/whiteboard/bdelacretaz/sling-pax-util/src/main/
    sling/whiteboard/bdelacretaz/sling-pax-util/src/main/java/
    sling/whiteboard/bdelacretaz/sling-pax-util/src/main/java/org/
    sling/whiteboard/bdelacretaz/sling-pax-util/src/main/java/org/apache/
    sling/whiteboard/bdelacretaz/sling-pax-util/src/main/java/org/apache/sling/
    sling/whiteboard/bdelacretaz/sling-pax-util/src/main/java/org/apache/sling/paxexam/
    sling/whiteboard/bdelacretaz/sling-pax-util/src/main/java/org/apache/sling/paxexam/util/
    sling/whiteboard/bdelacretaz/sling-pax-util/src/main/java/org/apache/sling/paxexam/util/SlingPaxOptions.java
    sling/whiteboard/bdelacretaz/sling-pax-util/src/test/
    sling/whiteboard/bdelacretaz/sling-pax-util/src/test/java/
    sling/whiteboard/bdelacretaz/sling-pax-util/src/test/java/org/
    sling/whiteboard/bdelacretaz/sling-pax-util/src/test/java/org/apache/
    sling/whiteboard/bdelacretaz/sling-pax-util/src/test/java/org/apache/sling/
    sling/whiteboard/bdelacretaz/sling-pax-util/src/test/java/org/apache/sling/paxexam/
    sling/whiteboard/bdelacretaz/sling-pax-util/src/test/java/org/apache/sling/paxexam/util/
    sling/whiteboard/bdelacretaz/sling-pax-util/src/test/java/org/apache/sling/paxexam/util/SlingBundlesTest.java

Propchange: sling/whiteboard/bdelacretaz/sling-pax-util/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Fri May 24 15:35:33 2013
@@ -0,0 +1,12 @@
+target
+bin
+*.iml
+*.ipr
+*.iws
+.settings
+.project
+.classpath
+.externalToolBuilders
+maven-eclipse.xml
+derby.log
+jackrabbit

Added: sling/whiteboard/bdelacretaz/sling-pax-util/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/sling-pax-util/pom.xml?rev=1486095&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/sling-pax-util/pom.xml (added)
+++ sling/whiteboard/bdelacretaz/sling-pax-util/pom.xml Fri May 24 15:35:33 2013
@@ -0,0 +1,121 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- 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. -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+    
+    <parent>
+        <groupId>org.apache.sling</groupId>
+        <artifactId>sling</artifactId>
+        <version>16</version>
+        <relativePath>../../parent/pom.xml</relativePath>
+    </parent>
+
+    <groupId>org.apache.sling</groupId>
+    <artifactId>org.apache.sling.paxexam.util</artifactId>
+    <version>1.0-SNAPSHOT</version>
+    <packaging>jar</packaging>
+
+    <name>Sling Pax Exam Utilities</name>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <exam.version>3.0.3</exam.version>
+        <url.version>1.5.2</url.version>
+        <pax.exam.log.level>INFO</pax.exam.log.level>
+    </properties>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <configuration>
+                    <systemProperties>
+                        <property>
+                            <name>pax.exam.log.level</name>
+                            <value>${pax.exam.log.level}</value>
+                        </property>
+                        <property>
+                            <name>java.protocol.handler.pkgs</name>
+                            <value>org.ops4j.pax.url</value>
+                        </property>
+                    </systemProperties>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+            <version>4.2.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam-container-native</artifactId>
+            <version>${exam.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam-junit4</artifactId>
+            <version>${exam.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam-link-mvn</artifactId>
+            <version>${exam.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.url</groupId>
+            <artifactId>pax-url-aether</artifactId>
+            <version>${url.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.framework</artifactId>
+            <version>4.2.1</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>maven-launchpad-plugin</artifactId>
+            <version>2.2.1-SNAPSHOT</version>
+            <type>maven-plugin</type>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.launchpad.api</artifactId>
+            <version>1.1.0</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+</project>

Added: sling/whiteboard/bdelacretaz/sling-pax-util/src/main/java/org/apache/sling/paxexam/util/SlingPaxOptions.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/sling-pax-util/src/main/java/org/apache/sling/paxexam/util/SlingPaxOptions.java?rev=1486095&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/sling-pax-util/src/main/java/org/apache/sling/paxexam/util/SlingPaxOptions.java
(added)
+++ sling/whiteboard/bdelacretaz/sling-pax-util/src/main/java/org/apache/sling/paxexam/util/SlingPaxOptions.java
Fri May 24 15:35:33 2013
@@ -0,0 +1,138 @@
+/*
+ * 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.paxexam.util;
+
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.sling.maven.projectsupport.BundleListUtils;
+import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.Bundle;
+import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.BundleList;
+import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.StartLevel;
+import org.ops4j.pax.exam.options.CompositeOption;
+import org.ops4j.pax.exam.options.DefaultCompositeOption;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Pax exam options utilities for Sling */
+public class SlingPaxOptions {
+    private static final Logger log = LoggerFactory.getLogger(SlingPaxOptions.class);
+    
+    public static CompositeOption slingBundleList(String groupId, String artifactId, String
version, String type, String classifier) {
+        
+        final DefaultCompositeOption result = new DefaultCompositeOption();
+        
+        final String paxUrl = new StringBuilder()
+        .append("mvn:")
+        .append(groupId)
+        .append("/")
+        .append(artifactId)
+        .append("/")
+        .append(version == null ? "" : version)
+        .append("/")
+        .append(type == null ? "" : type)
+        .append("/")
+        .append(classifier == null ? "" : classifier)
+        .toString();
+        
+        // TODO BundleList should take an InputStream - for now copy to a tmp file for parsing
+        log.info("Getting bundle list {}", paxUrl);
+        File tmp = null;
+        try {
+            tmp = dumpMvnUrlToTmpFile(paxUrl);
+            final BundleList list = BundleListUtils.readBundleList(tmp);
+            int counter = 0;
+            for(StartLevel s : list.getStartLevels()) {
+                final int startLevel = s.getStartLevel();
+                for(Bundle b : s.getBundles()) {
+                    counter++;
+                    
+                    // TODO need better fragment detection
+                    // (but pax exam should really detect that by itself?)
+                    final List<String> KNOWN_FRAGMENTS = new ArrayList<String>();
+                    KNOWN_FRAGMENTS.add("org.apache.sling.extensions.webconsolebranding");
+                    final boolean isFragment = b.getArtifactId().contains("fragment") ||
KNOWN_FRAGMENTS.contains(b.getArtifactId());
+                    
+                    if(isFragment) {
+                        result.add(mavenBundle(b.getGroupId(), b.getArtifactId(), b.getVersion()).noStart());
+                    } else if(startLevel == 0){
+                        result.add(mavenBundle(b.getGroupId(), b.getArtifactId(), b.getVersion()));
+                    } else {
+                        result.add(mavenBundle(b.getGroupId(), b.getArtifactId(), b.getVersion()).startLevel(startLevel));
+                    }
+                    
+                    log.info("Bundle added: {}/{}/{}", new Object [] { b.getGroupId(), b.getArtifactId(),
b.getVersion()});
+                }
+            }
+            log.info("Got {} bundles from {}", counter, paxUrl);
+        } catch(Exception e) {
+            throw new RuntimeException("Error getting bundle list " + paxUrl, e);
+        } finally {
+            if(tmp != null) {
+                tmp.delete();
+            }
+        }
+        
+        return result;
+    }
+
+    public static CompositeOption slingBootstrapBundles() {
+        return new DefaultCompositeOption(
+                mavenBundle("org.apache.felix", "org.apache.felix.http.jetty", "2.2.0"),
+                
+                // TODO: why is this needed?
+                mavenBundle("org.apache.sling", "org.apache.sling.launchpad.api", "1.1.0")
+        );
+    }
+    
+    public static CompositeOption slingLaunchpadBundles(String version) {
+        return slingBundleList("org.apache.sling", "org.apache.sling.launchpad", version,
"xml", "bundlelist");
+    }
+    
+    private static File dumpMvnUrlToTmpFile(String mvnUrl) throws IOException {
+        final URL url = new URL(mvnUrl);
+        final InputStream is = new BufferedInputStream(url.openStream());
+        
+        final File tmp = File.createTempFile(SlingPaxOptions.class.getName(), "xml");
+        log.debug("Copying bundle list contents to {}", tmp.getAbsolutePath());
+        tmp.deleteOnExit();
+        final OutputStream os = new BufferedOutputStream(new FileOutputStream(tmp));
+        try {
+            final byte [] buffer = new byte[16384];
+            int len = 0;
+            while( (len = is.read(buffer, 0, buffer.length)) > 0) {
+                os.write(buffer, 0, len);
+            }
+            os.flush();
+        } finally {
+            os.close();
+            is.close();
+        }
+        
+        return tmp;
+    }
+}

Added: sling/whiteboard/bdelacretaz/sling-pax-util/src/test/java/org/apache/sling/paxexam/util/SlingBundlesTest.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/sling-pax-util/src/test/java/org/apache/sling/paxexam/util/SlingBundlesTest.java?rev=1486095&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/sling-pax-util/src/test/java/org/apache/sling/paxexam/util/SlingBundlesTest.java
(added)
+++ sling/whiteboard/bdelacretaz/sling-pax-util/src/test/java/org/apache/sling/paxexam/util/SlingBundlesTest.java
Fri May 24 15:35:33 2013
@@ -0,0 +1,265 @@
+/*
+ * 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.paxexam.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+import static org.ops4j.pax.exam.CoreOptions.junitBundles;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.CoreOptions.systemProperty;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.inject.Inject;
+
+import org.apache.sling.launchpad.api.StartupListener;
+import org.apache.sling.launchpad.api.StartupMode;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@RunWith(PaxExam.class)
+public class SlingBundlesTest {
+    
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    
+    @Inject
+    private BundleContext bundleContext;
+    
+    @org.ops4j.pax.exam.Configuration
+    public Option[] config() {
+        final String paxLogLevel = System.getProperty("pax.exam.log.level", "INFO");
+        
+        return options(
+                junitBundles(),
+                systemProperty( "org.ops4j.pax.logging.DefaultServiceLog.level" ).value(paxLogLevel),
+                SlingPaxOptions.slingBootstrapBundles(),
+                SlingPaxOptions.slingLaunchpadBundles(null)
+        );
+    }
+
+    private boolean isFragment(Bundle b) {
+        return b.getHeaders().get("Fragment-Host") != null;
+    }
+    
+    private void assertBundleActive(String symbolicName) {
+        Bundle b = null;
+        for(Bundle x : bundleContext.getBundles()) {
+            if(symbolicName.equals(x.getSymbolicName())) {
+                b = x;
+                break;
+            }
+        }
+        assertNotNull("Expecting bundle " + symbolicName + " to be present", b);
+        if(!isFragment(b)) {
+            assertEquals("Expecting bundle " + symbolicName + " to be active", Bundle.ACTIVE,
b.getState());
+        }
+    }
+    
+    @Before 
+    public void startAllBundles() {
+        final List<String> notStarted = new LinkedList<String>();
+        int lastNotStarted = Integer.MAX_VALUE;
+        
+        while(true) {
+            notStarted.clear();
+            for(Bundle b : bundleContext.getBundles()) {
+                if(!isFragment(b) && b.getState() != Bundle.ACTIVE) {
+                    notStarted.add(b.getSymbolicName());
+                    try {
+                        b.start();
+                    } catch(Exception e) {
+                        fail("Cannot start Bundle " + b.getSymbolicName() + ": " + e);
+                    } 
+                }
+            }
+            
+            if(notStarted.isEmpty()) {
+                break;
+            }
+            
+            if(!notStarted.isEmpty() && notStarted.size() >= lastNotStarted) {
+                log.error("No bundles started in the last cycle, inactive bundles={}", notStarted);
+                break;
+            }
+            lastNotStarted = notStarted.size();
+        }
+    }
+    
+    @Test
+    public void testBundleContext() {
+        assertNotNull("Expecting BundleContext to be set", bundleContext);
+    }
+    
+    @Test
+    public void testSlingBundles() {
+        final String [] bundles = {
+                "org.apache.sling.adapter",
+                "org.apache.sling.api",
+                "org.apache.sling.auth.core",
+                "org.apache.sling.auth.form",
+                "org.apache.sling.auth.openid",
+                "org.apache.sling.auth.selector",
+                "org.apache.sling.bundleresource.impl",
+                "org.apache.sling.commons.classloader",
+                "org.apache.sling.commons.json",
+                "org.apache.sling.commons.log",
+                "org.apache.sling.commons.logservice",
+                "org.apache.sling.commons.mime",
+                "org.apache.sling.commons.osgi",
+                "org.apache.sling.commons.scheduler",
+                "org.apache.sling.commons.threads",
+                "org.apache.sling.discovery.api",
+                "org.apache.sling.discovery.impl",
+                "org.apache.sling.discovery.support",
+                "org.apache.sling.engine",
+                "org.apache.sling.event",
+                "org.apache.sling.extensions.explorer",
+                "org.apache.sling.extensions.groovy",
+                "org.apache.sling.extensions.threaddump",
+                "org.apache.sling.extensions.webconsolebranding",
+                "org.apache.sling.extensions.webconsolesecurityprovider",
+                "org.apache.sling.fragment.activation",
+                "org.apache.sling.fragment.transaction",
+                "org.apache.sling.fragment.ws",
+                "org.apache.sling.fragment.xml",
+                "org.apache.sling.fsresource",
+                "org.apache.sling.installer.api",
+                "org.apache.sling.installer.console",
+                "org.apache.sling.installer.core",
+                "org.apache.sling.installer.factory.configuration",
+                "org.apache.sling.installer.provider.file",
+                "org.apache.sling.installer.provider.jcr",
+                "org.apache.sling.jcr.jcr-wrapper",
+                "org.apache.sling.jcr.api",
+                "org.apache.sling.jcr.base",
+                "org.apache.sling.jcr.classloader",
+                "org.apache.sling.jcr.contentloader",
+                "org.apache.sling.jcr.davex",
+                "org.apache.sling.jcr.jackrabbit.accessmanager",
+                "org.apache.sling.jcr.jackrabbit.server",
+                "org.apache.sling.jcr.jackrabbit.usermanager",
+                "org.apache.sling.jcr.ocm",
+                "org.apache.sling.jcr.registration",
+                "org.apache.sling.jcr.resource",
+                "org.apache.sling.jcr.webconsole",
+                "org.apache.sling.jcr.webdav",
+                "org.apache.sling.launchpad.content",
+                "org.apache.sling.launchpad.installer",
+                "org.apache.sling.resourceresolver",
+                "org.apache.sling.scripting.api",
+                "org.apache.sling.scripting.core",
+                "org.apache.sling.scripting.javascript",
+                "org.apache.sling.scripting.jsp",
+                "org.apache.sling.scripting.jsp.taglib",
+                "org.apache.sling.servlets.get",
+                "org.apache.sling.servlets.post",
+                "org.apache.sling.servlets.resolver",
+                "org.apache.sling.settings"
+        };
+        
+        for(String bundleName : bundles) {
+            assertBundleActive(bundleName);
+        }
+    }
+    
+    @Test
+    public void testSlingServices() {
+        
+        class LocalStartupListener implements StartupListener {
+            
+            boolean testsHaveRun;
+            
+            public void inform(StartupMode m, boolean finished) {
+                log.info("inform(finished={})", finished);
+                if(finished) {
+                    runTests();
+                }
+            }
+
+            public void startupFinished(StartupMode m) {
+                log.info("Startup finished");
+                runTests();
+            }
+
+            public void startupProgress(float f) {
+                log.info("Startup progress {}", f);
+            }
+            
+            void runTests() {
+                if(!testsHaveRun) {
+                    testsHaveRun = true;
+                }
+                
+                try {
+                    assertBundleActive("org.apache.sling.commons.mime");
+                    assertBundleActive("org.apache.sling.engine");
+                    
+                    final String [] services = {
+                            "org.apache.sling.commons.mime.MimeTypeService",
+                            "org.apache.sling.engine.SlingRequestProcessor"
+                    };
+                    
+                    for(String svc : services) {
+                        final ServiceReference<?> ref = bundleContext.getServiceReference(svc);
+                        assertNotNull("Expecting " + svc + " to be available", ref);
+                        bundleContext.ungetService(ref);
+                    }
+                } finally {
+                    log.info("Done running tests");
+                    synchronized (this) {
+                        notify();
+                    }
+                }
+            }
+        };
+        
+
+        final LocalStartupListener s = new LocalStartupListener();
+        s.runTests();
+        
+        /*
+        // TODO generalize this "wait for Sling startup" - assuming we really need it
+        final ServiceRegistration<?> reg = bundleContext.registerService(StartupListener.class.getName(),
s, null);
+        final long timeout = 5000L;
+        try {
+            synchronized (s) {
+                log.info("Waiting for {} to be done running tests", s);
+                s.wait(timeout);
+            }
+        } catch(InterruptedException ignored) {
+            fail("InterruptedException waiting tests to be executed");
+        } finally {
+            reg.unregister();
+        }
+        
+        if(!s.testsHaveRun) {
+            fail("Timeout waiting for tests to run, after " + timeout + " msec");
+        }
+        */
+    }
+}
\ No newline at end of file



Mime
View raw message