/*
 * A CCNx library test.
 *
 * Copyright (C) 2008, 2009, 2011 Palo Alto Research Center, Inc.
 *
 * This work is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the
 * Free Software Foundation. 
 * This work is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details. You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

package org.ccnx.ccn.test.profiles.ccnd;

import java.io.IOException;
import org.ccnx.ccn.CCNFilterListener;
import org.ccnx.ccn.CCNInterestListener;
import org.ccnx.ccn.protocol.ContentName;
import org.ccnx.ccn.protocol.ContentObject;
import org.ccnx.ccn.protocol.Interest;
import org.ccnx.ccn.protocol.MalformedContentNameStringException;
import org.ccnx.ccn.test.LibraryTestBase;
import org.junit.Assert;
import org.junit.Test;



/**
 * Test sending interests across ccnd.
 * Requires a running ccnd
 *
 */
public class InterestEndToEndUsingPrefixTest extends LibraryTestBase implements CCNFilterListener, CCNInterestListener {
	private Interest _interestSent;
	private String _prefix = "/interestEtoETestUsingPrefix/test-" + rand.nextInt(10000);
	private final static int TIMEOUT = 3000;

	
	@Test
	public void testInterestEndToEnd() throws MalformedContentNameStringException, IOException, InterruptedException {
		getHandle.registerFilter(ContentName.fromNative(_prefix), this);
		_interestSent = new Interest(ContentName.fromNative(_prefix + "/simpleTest"));
		doTest();
		_interestSent = new Interest(ContentName.fromNative(_prefix + "/simpleTest2"));
		_interestSent.maxSuffixComponents(4);
		_interestSent.minSuffixComponents(3);
		doTest();
		_interestSent = new Interest(ContentName.fromNative(_prefix + "/simpleTest2"));
		_interestSent.maxSuffixComponents(1);
		doTest();
		getHandle.unregisterFilter(ContentName.fromNative(_prefix), this);
		_interestSent = new Interest(ContentName.fromNative(_prefix + "/simpleTest"));
		doTestFail();
	}

	public boolean handleInterest(Interest interest) {
		Assert.assertTrue(_interestSent.equals(interest));
		synchronized(this) {
			notify();
		}
		return true;
	}
	
	public Interest handleContent(ContentObject data,
			Interest interest) {
		// TODO Auto-generated method stub
		return null;
	}
	
	private void doTest() throws IOException, InterruptedException {
		long startTime = System.currentTimeMillis();
		synchronized (this) {
			putHandle.expressInterest(_interestSent, this);
			wait(TIMEOUT);
		}
		Assert.assertTrue((System.currentTimeMillis() - startTime) < TIMEOUT);
	}

	private void doTestFail() throws IOException, InterruptedException {
		long startTime = System.currentTimeMillis();
		synchronized (this) {
			putHandle.expressInterest(_interestSent, this);
			wait(TIMEOUT);
		}
		Assert.assertFalse((System.currentTimeMillis() - startTime) < TIMEOUT);
	}

}

@@ -1489,16 +1489,56 @@
 10000);%0A
+%09private boolean _interestSeen = false;%0A
 %09private
@@ -1571,17 +1571,16 @@
 = 3000;%0A
-%0A
 %09%0A%09@Test
@@ -2017,16 +2017,17 @@
 ents(3);
+ 
 %0A%09%09doTes
@@ -2480,16 +2480,40 @@
 ();%0A%09%09%7D%0A
+%09%09_interestSeen = true;%0A
 %09%09return
@@ -2728,51 +2728,29 @@
 %7B%0A%09%09
-long startTime = System.currentTimeMillis()
+_interestSeen = false
 ;%0A%09%09
@@ -2868,58 +2868,21 @@
 rue(
-(System.currentTimeMillis() - startTime) %3C TIMEOUT
+_interestSeen
 );%0A%09
@@ -2960,51 +2960,29 @@
 %7B%0A%09%09
-long startTime = System.currentTimeMillis()
+_interestSeen = false
 ;%0A%09%09
@@ -3101,58 +3101,21 @@
 lse(
-(System.currentTimeMillis() - startTime) %3C TIMEOUT
+_interestSeen
 );%0A%09

Test for results in InterestEndToEndUsingPrefix test via whether interests were seen rather than timeouts. refs #100391