/*
 * Copyright (C) 2011 Harald Wellmann
 *
 * Licensed 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.ops4j.pax.exam.multifw;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.util.HashMap;

import org.apache.felix.framework.Felix;
import org.eclipse.osgi.launch.Equinox;
import org.junit.Before;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.launch.Framework;

/**
 * Not a real Exam Test. We just want to make sure that Equinox and
 * Felix can install bundles directly from mvn: URLs, which does
 * not currently work with JBoss OSGi.
 * 
 * @author Harald Wellmann
 *
 */
public class MvnUrlTest {

    private HashMap<String, String> props;

    @Before
    public void setUp() {
        System.setProperty( "java.protocol.handler.pkgs", "org.ops4j.pax.url" );
        props = new HashMap<String, String>();
    }
    
    @Test
    public void equinoxMvnUrl() throws BundleException, IOException {
        Framework framework = new Equinox(props);
        provisionMvnUrl(framework);
    }

    @Test
    public void felixMvnUrl() throws BundleException, IOException {
        Framework framework = new Felix(props);
        provisionMvnUrl(framework);
    }

    private void provisionMvnUrl(Framework framework) throws BundleException, IOException {
        framework.start();
        BundleContext bc = framework.getBundleContext();
        assertNotNull(bc);
        
        String url = "mvn:org.ops4j.base/ops4j-base-lang/1.2.3";
        Bundle bundle = bc.installBundle(url);
        assertNotNull(bundle);
        assertEquals("org.ops4j.base.lang", bundle.getSymbolicName());

        bundle.start();
        bundle.uninstall();
        framework.stop();
    }
}

@@ -784,89 +784,67 @@
 ap;%0A
-%0A
 import 
-org.apache.felix.framework.Felix;%0Aimport org.eclipse.osgi.launch.Equinox;
+java.util.Iterator;%0Aimport java.util.ServiceLoader;%0A
 %0Aimp
@@ -1049,16 +1049,67 @@
 amework;
+%0Aimport org.osgi.framework.launch.FrameworkFactory;
 %0A%0A/**%0A *
@@ -1173,19 +1173,16 @@
 inox and
-%0A *
  Felix c
@@ -1199,16 +1199,19 @@
  bundles
+%0A *
  directl
@@ -1238,19 +1238,16 @@
 ich does
-%0A *
  not cur
@@ -1308,16 +1308,17 @@
 lmann%0A *
+ 
 %0A */%0Apub
@@ -1337,17 +1337,17 @@
 nUrlTest
- 
+%0A
 %7B%0A%0A    p
@@ -1420,16 +1420,20 @@
  setUp()
+%0A   
  %7B%0A     
@@ -1561,20 +1561,16 @@
 ;%0A    %7D%0A
-    
 %0A    @Te
@@ -1592,15 +1592,17 @@
 oid 
-equinox
+provision
 MvnU
@@ -1633,32 +1633,36 @@
 ion, IOException
+%0A   
  %7B%0A        Frame
@@ -1660,216 +1660,325 @@
     
-Framework framework = new Equinox(props);%0A        provisionMvnUrl(framework
+ServiceLoader%3CFrameworkFactory%3E loader = ServiceLoader.load( FrameworkFactory.class );%0A        Iterator%3CFrameworkFactory%3E it = loader.iterator(
 );%0A    
-%7D%0A%0A
     
-@Test%0A    public void felixMvnUrl() throws BundleException, IOException %7B%0A        Framework framework = new Felix(
+while ( it.hasNext() )%0A        %7B%0A            FrameworkFactory frameworkFactory = it.next();%0A            Framework framework = frameworkFactory.newFramework( 
 props
+ 
 );%0A 
@@ -1984,16 +1984,20 @@
         
+   
+ 
 provisio
@@ -1997,23 +1997,29 @@
 rovision
-MvnUrl(
+ToFramework( 
 framewor
@@ -2019,19 +2019,30 @@
 ramework
+ 
 );%0A
+        %7D%0A
     %7D%0A%0A 
@@ -2066,23 +2066,29 @@
 rovision
-MvnUrl(
+ToFramework( 
 Framewor
@@ -2098,16 +2098,17 @@
 ramework
+ 
 ) throws
@@ -2136,16 +2136,20 @@
 xception
+%0A   
  %7B%0A     
@@ -2253,21 +2253,15 @@
 ull(
+ 
 bc
+ 
 );%0A
-        
 %0A   
@@ -2363,19 +2363,21 @@
 lBundle(
+ 
 url
+ 
 );%0A     
@@ -2393,22 +2393,24 @@
 NotNull(
+ 
 bundle
+ 
 );%0A     
@@ -2425,16 +2425,17 @@
 tEquals(
+ 
 %22org.ops
@@ -2473,16 +2473,17 @@
 icName()
+ 
 );%0A%0A    

use factory SPI to create frameworks, in order to enable or disable framework implementations via Maven profiles