package org.ccnx.ccn.test.repo;

import java.io.File;

import junit.framework.Assert;

import org.ccnx.ccn.impl.repo.RepositoryDaemon;
import org.ccnx.ccn.impl.repo.LogStructRepoStore.LogStructRepoStoreProfile;
import org.junit.Test;

public class RepoInitialPolicyTest extends RepoTestBase {

	@Test
	public void testPolicyInitialization() throws Exception {
		RepoDaemonTest rdt = new RepoDaemonTest(new String[]{"-root", _fileTestDir2, 
						"-policy", _topdir + "/org/ccnx/ccn/test/repo/policyTest.xml",
						"-global", "/parc.com/csl/ccn/repositories/TestRepository"}, this);
		Thread th = new Thread(rdt);
		th.start();
		synchronized (this) {
			this.wait();
		}
		rdt.shutdown();
		th.join();
		File testFile = new File(_fileTestDir2 + "/" + LogStructRepoStoreProfile.CONTENT_FILE_PREFIX + "1");
		long testLength = testFile.length();
		th = new Thread(rdt);
		th.start();
		synchronized (this) {
			this.wait();
		}
		rdt.shutdown();
		th.join();
		Assert.assertEquals(testLength, testFile.length());
	}
	
	public class RepoDaemonTest extends RepositoryDaemon implements Runnable {
		protected String[] _args = null;
		protected WorkerThread _wt = null;
		protected Object _waiter;
		public RepoDaemonTest(String[] args, Object waiter) {
			_args = args;
			_waiter = waiter;
		}
		public void run() {
			initialize(_args, this);
			_wt = createWorkerThread();
			_wt.start();
			_wt.waitForStart();
			synchronized (_waiter) {
				_waiter.notifyAll();
			}
			try {
				_wt.join();
			} catch (InterruptedException e) {}
		}
		public void shutdown() {
			_wt.shutDown();
		}
	}
}

@@ -582,123 +582,28 @@
 ;%0A%09%09
-Thread th = new Thread(rdt);%0A%09%09th.start();%0A%09%09synchronized (this) %7B%0A%09%09%09this.wait();%0A%09%09%7D%0A%09%09rdt.shutdown();%0A%09%09th.join(
+startAndStopRepo(rdt
 );%0A%09
@@ -745,16 +745,261 @@
 th();%0A%09%09
+startAndStopRepo(rdt);%0A%09%09Assert.assertEquals(testLength, testFile.length());%0A%09%09RepoDaemonTest rdtNoChange = new RepoDaemonTest(new String%5B%5D%7B%22-root%22, _fileTestDir2,%0A%09%09%09%09%22-global%22, %22/parc.com/csl/ccn/repositories/TestRepository%22%7D, this);%0A%09%09Thread 
 th = new
@@ -1005,24 +1005,32 @@
 w Thread(rdt
+NoChange
 );%0A%09%09th.star
@@ -1084,88 +1084,668 @@
 %7D%0A%09%09
-rdt.shutdown();%0A%09%09th.join();%0A%09%09Assert.assertEquals(testLength, testFile.length()
+checkNameSpace(%22/repoTest/data3%22, false);%0A%09%09checkNameSpace(%22/testNameSpace/data1%22, true);%0A%09%09rdtNoChange.shutdown();%0A%09%09th.join();%0A%09%09rdt = new RepoDaemonTest(new String%5B%5D%7B%22-root%22, _fileTestDir2, %0A%09%09%09%09%22-prefix%22, %22/policyTest/foo%22,%0A%09%09%09%09%22-global%22, %22/parc.com/csl/ccn/repositories/TestRepository%22%7D, this);%0A%09%09startAndStopRepo(rdt);%0A%09%09testLength = testFile.length();%0A%09%09startAndStopRepo(rdt);%0A%09%09Assert.assertEquals(testLength, testFile.length());%0A%09%09th = new Thread(rdtNoChange);%0A%09%09th.start();%0A%09%09synchronized (this) %7B%0A%09%09%09this.wait();%0A%09%09%7D%0A%09%09checkNameSpace(%22/testNameSpace/data1%22, false);%0A%09%09checkNameSpace(%22/policyTest/foo/bar%22, true);%0A%09%09rdtNoChange.shutdown();%0A%09%09th.join(
 );%0A%09
@@ -2321,10 +2321,216 @@
 %0A%09%09%7D%0A%09%7D%0A
+%09%0A%09private void startAndStopRepo(RepoDaemonTest rdt) throws InterruptedException %7B%0A%09%09Thread th = new Thread(rdt);%0A%09%09th.start();%0A%09%09synchronized (this) %7B%0A%09%09%09this.wait();%0A%09%09%7D%0A%09%09rdt.shutdown();%0A%09%09th.join();%0A%09%7D%0A
 %7D%0A

Add more testing to initial policy test refs #100472