Bug 462423 - URLStreamHandlerService registrations
 - Add a testcase

Change-Id: Iee15b54c61b853ce42e987016bb4c13bfb55c3e4
diff --git a/bundles/org.eclipse.osgi.tests/.classpath b/bundles/org.eclipse.osgi.tests/.classpath
index 8e39802..aa1bad3 100644
--- a/bundles/org.eclipse.osgi.tests/.classpath
+++ b/bundles/org.eclipse.osgi.tests/.classpath
@@ -130,5 +130,6 @@
 	<classpathentry kind="src" output="bundle_tests/test.system.nls"  path="bundles_src/test.system.nls"/>
 	<classpathentry kind="src" output="bundle_tests/test.bug449484" path="bundles_src/test.bug449484"/>	
 	<classpathentry kind="src" output="bundle_tests/wrapper.hooks.a" path="bundles_src/wrapper.hooks.a"/>
+	<classpathentry kind="src" output="bundle_tests/test.protocol.handler" path="bundles_src/test.protocol.handler"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>
diff --git a/bundles/org.eclipse.osgi.tests/build.properties b/bundles/org.eclipse.osgi.tests/build.properties
index d2699ba..e654843 100644
--- a/bundles/org.eclipse.osgi.tests/build.properties
+++ b/bundles/org.eclipse.osgi.tests/build.properties
@@ -264,6 +264,8 @@
 manifest.bundle_tests/test.bug449484.jar = META-INF/MANIFEST.MF
 source.bundle_tests/wrapper.hooks.a.jar = bundles_src/wrapper.hooks.a/
 manifest.bundle_tests/wrapper.hooks.a.jar = META-INF/MANIFEST.MF
+source.bundle_tests/test.protocol.handler.jar = bundles_src/test.protocol.handler/
+manifest.bundle_tests/test.protocol.handler.jar = META-INF/MANIFEST.MF
 
 jars.compile.order = bundle_tests/ext.framework.b.jar,\
                      osgitests.jar,\
@@ -391,4 +393,5 @@
                      bundle_tests/test.bug438904.global.jar,\
                      bundle_tests/test.system.nls.jar,\
                      bundle_tests/test.bug449484.jar,\
-                     bundle_tests/wrapper.hooks.a.jar
+                     bundle_tests/wrapper.hooks.a.jar,\
+                     bundle_tests/test.protocol.handler.jar
diff --git a/bundles/org.eclipse.osgi.tests/bundles_src/test.protocol.handler/META-INF/MANIFEST.MF b/bundles/org.eclipse.osgi.tests/bundles_src/test.protocol.handler/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..9c15177
--- /dev/null
+++ b/bundles/org.eclipse.osgi.tests/bundles_src/test.protocol.handler/META-INF/MANIFEST.MF
@@ -0,0 +1,5 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-SymbolicName: test.protocol.handler
+Bundle-Activator: test.protocol.handler.Activator
+Import-Package: org.osgi.framework, org.osgi.service.url
diff --git a/bundles/org.eclipse.osgi.tests/bundles_src/test.protocol.handler/test/protocol/handler/Activator.java b/bundles/org.eclipse.osgi.tests/bundles_src/test.protocol.handler/test/protocol/handler/Activator.java
new file mode 100644
index 0000000..3b317e6
--- /dev/null
+++ b/bundles/org.eclipse.osgi.tests/bundles_src/test.protocol.handler/test/protocol/handler/Activator.java
@@ -0,0 +1,53 @@
+/*******************************************************************************
+ * Copyright (c) 2015 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package test.protocol.handler;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.url.*;
+
+public class Activator extends AbstractURLStreamHandlerService implements BundleActivator {
+	BundleContext bc;
+
+	@Override
+	public void start(BundleContext context) throws Exception {
+		bc = context;
+		Dictionary<String, String> props = new Hashtable<String, String>();
+		props.put(URLConstants.URL_HANDLER_PROTOCOL, "testing1");
+		context.registerService(URLStreamHandlerService.class, this, props);
+	}
+
+	@Override
+	public void stop(BundleContext context) throws Exception {
+		// nothing
+	}
+
+	@Override
+	public URLConnection openConnection(URL u) {
+		return new URLConnection(u) {
+
+			@Override
+			public void connect() throws IOException {
+				try {
+					bc.getBundle();
+				} catch (IllegalStateException e) {
+					throw new IOException(e);
+				}
+				return;
+			}
+		};
+	}
+}
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/BundleTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/BundleTests.java
index 558268b..01fb4ff 100644
--- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/BundleTests.java
+++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/BundleTests.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2006, 2013 IBM Corporation and others.
+ * Copyright (c) 2006, 2015 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -16,6 +16,7 @@
 public class BundleTests {
 	public static Test suite() {
 		TestSuite suite = new TestSuite(BundleTests.class.getName());
+		suite.addTest(URLHandlerTests.suite());
 		suite.addTest(PersistedBundleTests.suite());
 		suite.addTest(CascadeConfigTests.suite());
 		suite.addTest(DiscardBundleTests.suite());
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java
index d367c4f..bc01731 100644
--- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java
+++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2008, 2014 IBM Corporation and others.
+ * Copyright (c) 2008, 2015 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -1323,6 +1323,7 @@
 		}
 		assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox2.getState()); //$NON-NLS-1$
 		handlerReg.unregister();
+		System.getProperties().remove("test.url");
 	}
 
 	public void testUUID() {
@@ -2259,7 +2260,7 @@
 				EquinoxLocations.PROP_CONFIG_AREA, //
 				EquinoxLocations.PROP_INSTALL_AREA, //
 				EclipseStarter.PROP_LOGFILE //
-				);
+		);
 		Properties systemProperties = (Properties) System.getProperties().clone();
 		Map<String, Object> configuration = new HashMap<String, Object>();
 		for (Object key : systemProperties.keySet()) {
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/URLHandlerTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/URLHandlerTests.java
new file mode 100644
index 0000000..3751a22
--- /dev/null
+++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/URLHandlerTests.java
@@ -0,0 +1,34 @@
+/*******************************************************************************
+ * Copyright (c) 2015 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.osgi.tests.bundles;
+
+import java.net.URL;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.osgi.framework.Bundle;
+
+public class URLHandlerTests extends AbstractBundleTests {
+	public static Test suite() {
+		return new TestSuite(URLHandlerTests.class);
+	}
+
+	public void testURLHandlerUnregister() throws Exception {
+		Bundle test = installer.installBundle("test.protocol.handler"); //$NON-NLS-1$
+		test.start();
+		URL testURL = new URL("testing1://test");
+		testURL.openConnection().connect();
+		test.stop();
+		test.start();
+		testURL = new URL("testing1://test");
+		testURL.openConnection().connect();
+	}
+
+}