/*
* ################################################################
*
* ProActive: The Java(TM) library for Parallel, Distributed,
*            Concurrent computing with Security and Mobility
*
* Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
* Contact: proactive-support@inria.fr
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
* USA
*
*  Initial developer(s):               The ProActive Team
*                        http://www.inria.fr/oasis/ProActive/contacts.html
*  Contributor(s):
*
* ################################################################
*/
package nonregressiontest.activeobject.acontinuation;

import java.util.Vector;

import org.objectweb.proactive.ProActive;

import testsuite.test.FunctionalTest;


/**
 * @author rquilici
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class Test extends FunctionalTest {
    A a;
    A b;
    Id idPrincipal;
    Id idDeleguate;
    boolean futureByResult;

    public Test() {
        super("Automatic Continuations",
            "Test automatic continuations by results and parameteres");
    }

    /**
     * @see testsuite.test.FunctionalTest#action()
     */
    public void action() throws Exception {
       	//System.out.println( "Property "+System.getProperty("proactive.future.ac"));
				System.setProperty("proactive.future.ac","enable");
				ACThread acthread = new ACThread();
				acthread.start();
				acthread.join();
        System.setProperty("proactive.future.ac","disable");
    }

    /**
     * @see testsuite.test.AbstractTest#initTest()
     */
    public void initTest() throws Exception {
    }

    /**
     * @see testsuite.test.AbstractTest#endTest()
     */
    public void endTest() throws Exception {
    	
    }

    public boolean postConditions() throws Exception {
        return (futureByResult && a.isSuccessful() &&
        a.getFinalResult().equals("dummy"));
    }
    
    private class ACThread extends Thread {
    	
    	public void run() {
    		try{
    		a = (A) ProActive.newActive(A.class.getName(),
                new Object[] { "principal" });
        a.initFirstDeleguate();
        idDeleguate = a.getId("deleguate2");
        idPrincipal = a.getId("principal");
        Vector v = new Vector(2);
        v.add(idDeleguate);
        v.add(idPrincipal);
        if (ProActive.waitForAny(v) == 0) {
            futureByResult = false;
        } else {
            futureByResult = true;
        }
        b = (A) ProActive.newActive(A.class.getName(), new Object[] { "dummy" });
        idPrincipal = b.getIdforFuture();
        a.forwardID(idPrincipal);
    		}catch (Exception e){
    			e.printStackTrace();
    		}
    		
    	}
    }
}

@@ -2113,18 +2113,151 @@
 .ac%22));%0A
-%09%09
+        String initial_ca_setting = System.getProperty(%22proactive.future.ac%22);%0A        if (!%22enable%22.equals(initial_ca_setting)) %7B%0A    
 %09%09System
@@ -2302,18 +2302,26 @@
 able%22);%0A
-%09%09
+        %7D%0A
 %09%09ACThre
@@ -2346,26 +2346,24 @@
 ACThread();%0A
-%09%09
 %09%09acthread.s
@@ -2370,18 +2370,16 @@
 tart();%0A
-%09%09
 %09%09acthre
@@ -2442,17 +2442,26 @@
 ac%22,
-%22disable%22
+initial_ca_setting
 );%0A 

revert to initial proactive.future.ac property after the test