/*
 * Copyright (c) OSGi Alliance (2009, 2010). All Rights Reserved.
 * 
 * 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.osgi.test.support;

import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.util.*;

import junit.framework.*;

import org.osgi.framework.*;

public abstract class OSGiTestCase extends TestCase {
	private volatile BundleContext context;

	/**
	 * This method is called by the JUnit runner for OSGi, and gives us a Bundle
	 * Context.
	 */
	public void setBundleContext(BundleContext context) {
		this.context = context;
	}

	/**
	 * Returns the current Bundle Context
	 */
	public BundleContext getContext() {
		BundleContext c = context;
		if (c == null) {
			Bundle b = FrameworkUtil.getBundle(getClass());
			if ( b != null )
				return context = b.getBundleContext();
			
			fail("No Bundle context set, are you running in OSGi Test mode?");
		}
		return c;
	}

	/**
	 * Fail with cause t.
	 * 
	 * @param message
	 *            Failure message.
	 * @param t
	 *            Cause of the failure.
	 */
	public static void fail(String message, Throwable t) {
		AssertionFailedError e = new AssertionFailedError(message + ": "
				+ t.getMessage());
		e.initCause(t);
		throw e;
	}

	/**
	 * Assert a constant from class has a specific value.
	 * 
	 * @param expected
	 *            Expected value.
	 * @param fieldName
	 *            Constant field name.
	 * @param fieldClass
	 *            Class containing constant.
	 */
	public static void assertConstant(Object expected, String fieldName,
			Class<?> fieldClass) {
		try {
			Field f = fieldClass.getField(fieldName);
			assertTrue(Modifier.isPublic(f.getModifiers()));
			assertTrue(Modifier.isStatic(f.getModifiers()));
			assertTrue(Modifier.isFinal(f.getModifiers()));
			assertEquals(fieldName, expected, f.get(null));
		} catch (NoSuchFieldException e) {
			fail("missing field: " + fieldName, e);
		} catch (IllegalAccessException e) {
			fail("bad field: " + fieldName, e);
		}
	}

	public static <T> void assertEquals(String message,
			Comparator<T> comparator, List<T> expected, List<T> actual) {
		if (expected.size() != actual.size()) {
			failNotEquals(message, expected, actual);
		}

		for (int i = 0, l = expected.size(); i < l; i++) {
			assertEquals(message, comparator, expected.get(i), actual.get(i));
		}
	}

	public static <T> void assertEquals(String message,
			Comparator<T> comparator, T expected, T actual) {
		if (comparator.compare(expected, actual) == 0) {
			return;
		}
		failNotEquals(message, expected, actual);
	}

	/**
	 * Installs a resource within the test bundle as a bundle
	 * 
	 * @param bundle
	 *            a path to an entry that contains a bundle to install
	 * @return The installed bundle
	 * @throws BundleException
	 *             if an error occurred while installing the bundle
	 * @throws IOException
	 *             if an error occurred while reading the bundle content
	 */
	public Bundle install(String bundle) throws BundleException, IOException {
		URL entry = getContext().getBundle().getEntry(bundle);
		assertNotNull("Can not find bundle: " + bundle, entry);
		return getContext().installBundle(bundle, entry.openStream());
	}
}

@@ -64,17 +64,16 @@
 rved.%0A *
- 
 %0A * Lice
@@ -673,17 +673,27 @@
 java.io.
-*
+IOException
 ;%0Aimport
@@ -711,17 +711,21 @@
 reflect.
-*
+Field
 ;%0Aimport
@@ -734,90 +734,383 @@
 ava.
-net.*;%0Aimport java.util.*;%0A%0Aimport junit.framework.*;%0A%0Aimport org.osgi.framework.*
+lang.reflect.Modifier;%0Aimport java.net.URL;%0Aimport java.util.Comparator;%0Aimport java.util.List;%0A%0Aimport junit.framework.AssertionFailedError;%0Aimport junit.framework.TestCase;%0A%0Aimport org.osgi.framework.Bundle;%0Aimport org.osgi.framework.BundleContext;%0Aimport org.osgi.framework.BundleException;%0Aimport org.osgi.framework.FrameworkUtil;%0Aimport org.osgi.test.support.sleep.Sleep
 ;%0A%0Ap
@@ -1638,19 +1638,16 @@
 text();%0A
-%09%09%09
 %0A%09%09%09fail
@@ -1756,25 +1756,24 @@
 cause t.%0A%09 *
- 
 %0A%09 * @param 
@@ -2105,25 +2105,24 @@
 c value.%0A%09 *
- 
 %0A%09 * @param 
@@ -3436,17 +3436,16 @@
 ndle%0A%09 *
- 
 %0A%09 * @pa
@@ -4006,11 +4006,298 @@
 m());%0A%09%7D
+%0A%0A%09/**%0A%09 * Sleep for the requested amount of milliseconds.%0A%09 * %0A%09 * @param timeout%0A%09 */%0A%09public static void sleep(long timeout) %7B%0A%09%09try %7B%0A%09%09%09Sleep.sleep(timeout);%0A%09%09%7D%0A%09%09catch (InterruptedException e) %7B%0A%09%09%09Thread.currentThread().interrupt();%0A%09%09%09fail(%22Unexpected interruption.%22, e);%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Test sleep method without InterruptedException