Hi Bertrand I just saw that AsyncInstaller is annotated with @RunWith(PaxExam.class). Either I'm missing something or that is wrong.
Regards Julian On Fri, Jul 3, 2015 at 4:56 PM, <[email protected]> wrote: > Author: bdelacretaz > Date: Fri Jul 3 14:56:08 2015 > New Revision: 1689031 > > URL: http://svn.apache.org/r1689031 > Log: > SLING-4851 - test bundles installed via OSGi installer (but not based on > start levels yet) > > Added: > > sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/AsyncInstaller.java > Modified: > sling/whiteboard/bdelacretaz/it-startup/pom.xml > > sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/IncrementalStartupIT.java > > sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/P.java > > Modified: sling/whiteboard/bdelacretaz/it-startup/pom.xml > URL: > http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/it-startup/pom.xml?rev=1689031&r1=1689030&r2=1689031&view=diff > ============================================================================== > --- sling/whiteboard/bdelacretaz/it-startup/pom.xml (original) > +++ sling/whiteboard/bdelacretaz/it-startup/pom.xml Fri Jul 3 14:56:08 2015 > @@ -165,5 +165,23 @@ > <version>2.0.0</version> > <scope>test</scope> > </dependency> > + <dependency> > + <groupId>org.apache.sling</groupId> > + <artifactId>org.apache.sling.commons.testing</artifactId> > + <version>2.0.18</version> > + <scope>test</scope> > + </dependency> > + <dependency> > + <groupId>org.apache.sling</groupId> > + <artifactId>org.apache.sling.installer.core</artifactId> > + <version>3.6.7-SNAPSHOT</version> > + <scope>test</scope> > + </dependency> > + <dependency> > + <groupId>org.apache.sling</groupId> > + <artifactId>org.apache.sling.commons.log</artifactId> > + <version>4.0.2</version> > + <scope>test</scope> > + </dependency> > </dependencies> > </project> > > Added: > sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/AsyncInstaller.java > URL: > http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/AsyncInstaller.java?rev=1689031&view=auto > ============================================================================== > --- > sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/AsyncInstaller.java > (added) > +++ > sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/AsyncInstaller.java > Fri Jul 3 14:56:08 2015 > @@ -0,0 +1,118 @@ > +/* > + * 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.launchpad.it.startup; > + > +import static org.junit.Assert.assertTrue; > + > +import java.io.InputStream; > +import java.util.Collection; > +import java.util.HashSet; > +import java.util.Random; > +import java.util.Set; > +import java.util.UUID; > +import java.util.concurrent.atomic.AtomicInteger; > + > +import org.apache.sling.installer.api.InstallableResource; > +import org.apache.sling.installer.api.OsgiInstaller; > +import org.junit.runner.RunWith; > +import org.ops4j.pax.exam.junit.PaxExam; > +import org.ops4j.pax.tinybundles.core.TinyBundles; > +import org.osgi.framework.Bundle; > +import org.osgi.framework.BundleContext; > +import org.osgi.framework.Constants; > +import org.osgi.framework.FrameworkEvent; > +import org.osgi.framework.FrameworkListener; > +import org.osgi.service.startlevel.StartLevel; > +import org.slf4j.Logger; > +import org.slf4j.LoggerFactory; > + > +/** Asynchronously installs test bundles when start levels change, > + * to verify the Sling incremental startup mechanism. > + */ > +@RunWith(PaxExam.class) > +class AsyncInstaller implements FrameworkListener { > + > + private final Logger log = LoggerFactory.getLogger(getClass()); > + private final BundleContext bundleContext; > + private final StartLevel startLevelService; > + private final OsgiInstaller installer; > + private final Set<String> installedBundles = new HashSet<String>(); > + private final String bundleNamePrefix = "AsyncInstaller-" + > UUID.randomUUID().toString() + "."; > + private final Random random = new Random(42); > + private final AtomicInteger counter = new AtomicInteger(); > + > + public static final int MAX_BUNDLES_PER_LEVEL = 7; > + > + AsyncInstaller(BundleContext bc, OsgiInstaller inst, StartLevel s) { > + bundleContext = bc; > + startLevelService = s; > + installer = inst; > + bundleContext.addFrameworkListener(this); > + } > + > + private InputStream getTestBundleStream(String bundleSymbolicName) > throws Exception { > + return TinyBundles.bundle() > + .set(Constants.BUNDLE_SYMBOLICNAME, bundleSymbolicName) > + .build(TinyBundles.withBnd()); > + } > + > + void installBundles() throws Exception { > + final int n = (int)(random.nextFloat() * MAX_BUNDLES_PER_LEVEL); > + final int startLevel = startLevelService.getStartLevel(); > + log.info("Installing {} test bundles at start level {}", n, > startLevel); > + final InstallableResource [] toInstall = new InstallableResource[n]; > + > + for(int i=0; i < n; i++) { > + final String bsn = bundleNamePrefix + counter.incrementAndGet() > + "." + startLevel; > + final InputStream is = getTestBundleStream(bsn); > + toInstall[i] = new InstallableResource(bsn, is, null, bsn, > "bundle", 100); > + installedBundles.add(bsn); > + } > + > + installer.registerResources(getClass().getSimpleName(), toInstall); > + } > + > + boolean isTestBundle(Bundle b) { > + return b.getSymbolicName().startsWith(bundleNamePrefix); > + } > + > + Collection<String> getBundleIssues(Bundle [] toCheck, boolean > checkActiveState) { > + final Set<String> issues = new HashSet<String>(installedBundles); > + assertTrue("Expecting some installed bundles", issues.size() > 0); > + for(Bundle b : toCheck) { > + if(issues .remove(b.getSymbolicName()) && checkActiveState) { > + if(b.getState() != Bundle.ACTIVE) { > + issues.add(b.getSymbolicName() + " (not active)"); > + } > + } > + } > + return issues ; > + } > + > + @Override > + public void frameworkEvent(FrameworkEvent event) { > + if(event.getType() == FrameworkEvent.STARTLEVEL_CHANGED) { > + try { > + installBundles(); > + } catch(Exception e) { > + log.error("Installing bundles failed", e); > + } > + } > + } > +} > \ No newline at end of file > > Modified: > sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/IncrementalStartupIT.java > URL: > http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/IncrementalStartupIT.java?rev=1689031&r1=1689030&r2=1689031&view=diff > ============================================================================== > --- > sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/IncrementalStartupIT.java > (original) > +++ > sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/IncrementalStartupIT.java > Fri Jul 3 14:56:08 2015 > @@ -19,71 +19,72 @@ > package org.apache.sling.launchpad.it.startup; > > import static org.junit.Assert.assertEquals; > +import static org.junit.Assert.fail; > > -import java.io.InputStream; > -import java.util.ArrayList; > -import java.util.List; > -import java.util.UUID; > +import java.util.Collection; > > import javax.inject.Inject; > > -import org.junit.Before; > +import org.apache.sling.installer.api.OsgiInstaller; > import org.junit.Test; > import org.junit.runner.RunWith; > import org.ops4j.pax.exam.Option; > import org.ops4j.pax.exam.junit.PaxExam; > -import org.ops4j.pax.tinybundles.core.TinyBundles; > -import org.osgi.framework.Bundle; > import org.osgi.framework.BundleContext; > -import org.osgi.framework.Constants; > +import org.osgi.service.startlevel.StartLevel; > import org.slf4j.Logger; > import org.slf4j.LoggerFactory; > > /** Test the Sling Launchpad incremental startup mechanism */ > @RunWith(PaxExam.class) > public class IncrementalStartupIT { > - > + > private final Logger log = LoggerFactory.getLogger(getClass()); > > @Inject > protected BundleContext bundleContext; > > - private List<Bundle> bundles; > - > + @Inject > + private StartLevel startLevel; > + > + @Inject > + private OsgiInstaller installer; > + > @org.ops4j.pax.exam.Configuration > public Option[] config() { > return P.paxConfig(); > } > > - private InputStream getTestBundleStream(String bundleSymbolicName) > throws Exception { > - return TinyBundles.bundle() > - .set(Constants.BUNDLE_SYMBOLICNAME, bundleSymbolicName) > - .build(TinyBundles.withBnd()); > - } > - > - @Before > - public void setup() throws Exception { > - bundles = new ArrayList<Bundle>(); > - final String basename = UUID.randomUUID().toString(); > - for(int i=0; i<25; i++) { > - final InputStream is = getTestBundleStream(basename + "_" + i); > - try { > - final Bundle b = bundleContext.installBundle(basename, is); > - bundles.add(b); > - b.start(); > - log.info("Started test bundle {}", b); > - } finally { > - is.close(); > - } > - } > - } > - > @Test > public void allBundlesActive() throws Exception { > - // TODO setup is not called by JUnit?? > - setup(); > - for(Bundle b : bundles) { > - assertEquals("Expecting bundle to be active:" + b, > Bundle.ACTIVE, b.getState()); > + final AsyncInstaller ai = new AsyncInstaller(bundleContext, > installer, startLevel); > + ai.installBundles(); > + > + final int to = startLevel.getStartLevel(); > + > + /* > + final int from = startLevel.getStartLevel(); > + final int to = from + 10; > + log.info("Changing start level from {} to {}", from, to); > + startLevel.setStartLevel(to); > + */ > + > + final long timeoutMsec = 10000; > + final long endtime = System.currentTimeMillis() + timeoutMsec; > + Collection<String> issues = null; > + while(System.currentTimeMillis() < endtime) { > + if(startLevel.getStartLevel() != to) { > + continue; > + } > + issues = ai.getBundleIssues(bundleContext.getBundles(), true); > + if(issues.isEmpty()) { > + return; > + } > + Thread.sleep(50); > + } > + assertEquals("Expecting start level change to be done", to, > startLevel.getStartLevel()); > + if(!issues.isEmpty()) { > + fail("Missing or inactive bundles:" + issues); > } > } > } > \ No newline at end of file > > Modified: > sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/P.java > URL: > http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/P.java?rev=1689031&r1=1689030&r2=1689031&view=diff > ============================================================================== > --- > sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/P.java > (original) > +++ > sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/P.java > Fri Jul 3 14:56:08 2015 > @@ -36,7 +36,11 @@ public class P { > provision(bundle(thisProjectsBundle.toURI().toString())), > wrappedBundle(mavenBundle("org.ops4j.pax.tinybundles", > "tinybundles").versionAsInProject()), > mavenBundle("biz.aQute.bnd", "bndlib").versionAsInProject(), > - wrappedBundle(mavenBundle("junit", > "junit").versionAsInProject()) > + wrappedBundle(mavenBundle("junit", > "junit").versionAsInProject()), > + > mavenBundle("org.apache.sling","org.apache.sling.installer.core").versionAsInProject(), > + mavenBundle("org.slf4j","slf4j-api").versionAsInProject(), > + > mavenBundle("org.apache.sling","org.apache.sling.commons.log").versionAsInProject(), > + > wrappedBundle(mavenBundle("org.apache.sling","org.apache.sling.commons.testing").versionAsInProject()) > ).getOptions(); > } > } > >
