package biz.aQute.remote;

import java.util.*;

import junit.framework.*;

import org.osgi.framework.*;
import org.osgi.framework.dto.*;

import aQute.lib.io.*;
import aQute.remote.api.*;
import aQute.remote.main.*;
import aQute.remote.plugin.*;

/**
 * Start the main program which will wait for requests to create a framework.
 */
public class MainTest extends TestCase {

	private Thread	thread;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		thread = new Thread() {
			@Override
			public void run() {
				try {
					Main.main(new String[] {
							"-s", "generated/storage", "-c", "generated/cache", "-n", "*", "-et"
					});
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
		thread.setDaemon(true);
		thread.start();

	}

	@Override
	protected void tearDown() throws Exception {
		Main.stop();
		IO.delete(IO.getFile("generated/cache"));
		IO.delete(IO.getFile("generated/storage"));
		super.tearDown();
	}

	public void testRemoteMain() throws Exception {

		//
		// Create a framework & start an agent
		//

		LauncherSupervisor supervisor = new LauncherSupervisor();
		supervisor.connect("localhost", Agent.DEFAULT_PORT);

		assertEquals("not talking to an envoy", true, supervisor.getAgent().isEnvoy());

		HashMap<String,Object> configuration = new HashMap<String,Object>();
		configuration.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
		configuration.put(Constants.FRAMEWORK_STORAGE, "generated/storage");
		List<String> emptyList = Collections.emptyList();
		boolean created = supervisor.getAgent().createFramework("test", emptyList, configuration);
		assertTrue("there already was a framework, funny, since we created the main in setUp?", created);

		FrameworkDTO framework = supervisor.getAgent().getFramework();
		assertNotNull("just created it, so we should have a framework", framework);

		//
		// Create a second supervisor and ensure we do not
		// kill the primary
		//

		LauncherSupervisor sv2 = new LauncherSupervisor();
		sv2.connect("localhost", Agent.DEFAULT_PORT);

		assertTrue("no second framework", supervisor.getAgent().ping());

		assertEquals("must be an envoy", true, sv2.getAgent().isEnvoy());
		assertFalse("the framework should already exist",
				sv2.getAgent().createFramework("test", emptyList, configuration));

		assertTrue("first framework is gone", supervisor.getAgent().ping());

		FrameworkDTO fw2 = sv2.getAgent().getFramework();
		assertEquals("we should not have created a new framework", framework.properties.get("org.osgi.framework.uuid"),
				fw2.properties.get("org.osgi.framework.uuid"));

		//
		// Kill the second framework
		//

		supervisor.getAgent().abort();
		Thread.sleep(500);
		assertFalse(supervisor.isOpen());

		assertTrue("should not have killed sv2", sv2.getAgent().ping());

		sv2.abort();
		Thread.sleep(500);
		assertFalse(sv2.isOpen());
	}

}

@@ -573,16 +573,51 @@
 %0A%09%09%09%09%09%09%09
+%22-p%22, Agent.DEFAULT_PORT + 1 + %22%22, 
 %22-s%22, %22g
@@ -659,17 +659,24 @@
 /cache%22,
- 
+%0A%09%09%09%09%09%09%09
 %22-n%22, %22*
@@ -1208,32 +1208,36 @@
 ent.DEFAULT_PORT
+ + 1
 );%0A%0A%09%09assertEqua
@@ -2126,16 +2126,20 @@
 ULT_PORT
+ + 1
 );%0A%0A%09%09as

change port allow junit tests to run