/*
 * 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.felix.framework;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;

import junit.framework.TestCase;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;

public class StartStopBundleTest extends TestCase {

    public static final int DELAY = 1000;

    private File cacheDir;

    public void testStartStopBundle() throws Exception
    {
        Map params = new HashMap();
        params.put(Constants.FRAMEWORK_SYSTEMPACKAGES,
            "org.osgi.framework; version=1.4.0,"
            + "org.osgi.service.packageadmin; version=1.2.0,"
            + "org.osgi.service.startlevel; version=1.1.0,"
            + "org.osgi.util.tracker; version=1.3.3,"
            + "org.osgi.service.url; version=1.0.0");
        cacheDir = File.createTempFile("felix-cache", ".dir");
        cacheDir.delete();
        cacheDir.mkdirs();
        String cache = cacheDir.getPath();
        params.put("felix.cache.profiledir", cache);
        params.put("felix.cache.dir", cache);
        params.put(Constants.FRAMEWORK_STORAGE, cache);

        String mf = "Bundle-SymbolicName: boot.test\n"
            + "Bundle-Version: 1.1.0\n"
            + "Bundle-ManifestVersion: 2\n"
            + "Import-Package: org.osgi.framework\n";
        File bundleFile = createBundle(mf);

        Framework f = new Felix(params);
        f.init();
        f.start();

        final Bundle bundle = f.getBundleContext().installBundle(bundleFile.toURI().toString());
        final CountDownLatch latch = new CountDownLatch(1);

        new Thread() {
            public void run() {
                try {
                    bundle.start();
                } catch (BundleException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        Thread.sleep(DELAY / 4);
        long t0 = System.currentTimeMillis();
        bundle.stop();
        long t1 = System.currentTimeMillis();

        assertEquals(Bundle.RESOLVED, bundle.getState());
        assertTrue((t1 -t0) > DELAY / 2);

        bundle.start();

        new Thread() {
            public void run() {
                try {
                    bundle.stop();
                } catch (BundleException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        Thread.sleep(DELAY / 4);
        t0 = System.currentTimeMillis();
        bundle.start();
        t1 = System.currentTimeMillis();

        assertEquals(Bundle.ACTIVE, bundle.getState());
        assertTrue((t1 -t0) > DELAY / 2);
    }

    private static File createBundle(String manifest) throws IOException
    {
        File f = File.createTempFile("felix-bundle", ".jar");
        f.deleteOnExit();

        Manifest mf = new Manifest(new ByteArrayInputStream(manifest.getBytes("utf-8")));
        mf.getMainAttributes().putValue("Manifest-Version", "1.0");
        mf.getMainAttributes().putValue(Constants.BUNDLE_ACTIVATOR, TestBundleActivator.class.getName());
        JarOutputStream os = new JarOutputStream(new FileOutputStream(f), mf);

        String path = TestBundleActivator.class.getName().replace('.', '/') + ".class";
        os.putNextEntry(new ZipEntry(path));

        InputStream is = TestBundleActivator.class.getClassLoader().getResourceAsStream(path);
        byte[] b = new byte[is.available()];
        is.read(b);
        is.close();
        os.write(b);

        os.close();
        return f;
    }

    public static class TestBundleActivator implements BundleActivator {
        public void start(BundleContext context) throws Exception {
            Thread.sleep(DELAY);
        }
        public void stop(BundleContext context) throws Exception {
            Thread.sleep(DELAY);
        }
    }

}

@@ -1503,19 +1503,18 @@
 TestCase
- %7B
 %0A
+%7B
 %0A    pub
@@ -1548,17 +1548,16 @@
 = 1000;%0A
-%0A
     priv
@@ -2810,32 +2810,40 @@
     new Thread()
+%0A       
  %7B%0A            p
@@ -2850,32 +2850,44 @@
 ublic void run()
+%0A           
  %7B%0A             
@@ -2884,32 +2884,48 @@
              try
+%0A               
  %7B%0A             
@@ -2956,32 +2956,48 @@
                %7D
+%0A               
  catch (BundleEx
@@ -2998,32 +2998,48 @@
 ndleException e)
+%0A               
  %7B%0A             
@@ -3340,32 +3340,33 @@
 assertTrue((t1 -
+ 
 t0) %3E DELAY / 2)
@@ -3413,16 +3413,24 @@
 Thread()
+%0A       
  %7B%0A     
@@ -3453,16 +3453,28 @@
 id run()
+%0A           
  %7B%0A     
@@ -3487,16 +3487,32 @@
      try
+%0A               
  %7B%0A     
@@ -3558,16 +3558,32 @@
        %7D
+%0A               
  catch (
@@ -3600,16 +3600,32 @@
 ption e)
+%0A               
  %7B%0A     
@@ -3931,16 +3931,17 @@
 ue((t1 -
+ 
 t0) %3E DE
@@ -4920,16 +4920,20 @@
 ctivator
+%0A   
  %7B%0A     
@@ -4984,32 +4984,40 @@
 throws Exception
+%0A       
  %7B%0A            T
@@ -5038,32 +5038,33 @@
 LAY);%0A        %7D%0A
+%0A
         public v
@@ -5111,16 +5111,24 @@
 xception
+%0A       
  %7B%0A     
@@ -5175,7 +5175,5 @@
   %7D%0A
-%0A%7D%0A
+%7D

Reformat to match Felix style.