/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.ode.test;

import java.io.File;
import java.util.Properties;
import java.util.regex.Pattern;

import javax.xml.namespace.QName;

import junit.framework.TestCase;

import org.apache.ode.bpel.engine.BpelServerImpl;
//import org.apache.ode.bpel.iapi.BpelServer;
import org.apache.ode.bpel.iapi.Message;
import org.apache.ode.bpel.iapi.MyRoleMessageExchange;
import org.apache.ode.bpel.memdao.BpelDAOConnectionFactoryImpl;
import org.apache.ode.test.scheduler.TestScheduler;
import org.apache.ode.utils.DOMUtils;
import org.w3c.dom.Element;

public class BPELTest extends TestCase {
	
	private BpelServerImpl server;

	@Override
	protected void setUp() throws Exception {
		server = new BpelServerImpl();
		server.setDaoConnectionFactory(new BpelDAOConnectionFactoryImpl());
		server.setScheduler(new TestScheduler());
		server.setBindingContext(new BindingContextImpl());
		server.setMessageExchangeContext(new MessageExchangeContextImpl());
		server.setDeploymentManager(new DeploymentManagerImpl());
		server.init();
		server.start();
	}

	@Override
	protected void tearDown() throws Exception {
		server.stop();
	}
	
	private void go(String deployDir) throws Exception {
		
		File testPropsFile = new File(deployDir+"/test.properties");
		if ( !testPropsFile.exists()) {
			System.err.println("can't find "+ testPropsFile.toString());
		}
		Properties testProps = new Properties();
		testProps.load(testPropsFile.toURL().openStream());
		
		QName serviceId = new QName(testProps.getProperty("namespace"),
				testProps.getProperty("service"));
		String operation = testProps.getProperty("operation");
		String in = testProps.getProperty("request");
		String responsePattern = testProps.getProperty("response");
		
		server.getDeploymentService().deploy(new File(deployDir));
		
		MyRoleMessageExchange mex = server.getEngine().createMessageExchange("",serviceId,operation);
		
		Message request = mex.createMessage(null);
		
		Element elem = DOMUtils.stringToDOM(in);
		String tmp = elem.toString();
		request.setMessage(elem);
		
		mex.invoke(request);
		
		Message response = mex.getResponse();
		
		String resp = DOMUtils.domToString(response.getMessage());
		System.out.println(resp);
		assertTrue(Pattern.compile(responsePattern,Pattern.DOTALL).matcher(resp).matches());
	}
	
//	public void testHelloWorld2() throws Exception {
//		go("target/test-classes/bpel/2.0/HelloWorld2");
//	}
	public void testFlowActivity() throws Exception {
		go("target/test-classes/bpel/2.0/TestFlowActivity");
	}

}

@@ -2426,118 +2426,8 @@
 %22);%0A
-%09%09String in = testProps.getProperty(%22request%22);%0A%09%09String responsePattern = testProps.getProperty(%22response%22);%0A
 %09%09%0A%09
@@ -2584,22 +2584,217 @@
 ration);
+%09%09
 %0A%09%09%0A%09%09
+%0A%09%09for (int i=1; testProps.getProperty(%22request%22+i) != null; i++) %7B%0A%09%09%0A%09%09%09String in = testProps.getProperty(%22request%22+i);%0A%09%09%09String responsePattern = testProps.getProperty(%22response%22+i);%0A%09%09%0A%09%09%09
 Message 
@@ -2831,16 +2831,17 @@
 ll);%0A%09%09%0A
+%09
 %09%09Elemen
@@ -2873,24 +2873,25 @@
 oDOM(in);%0A%09%09
+%09
 String tmp =
@@ -2908,16 +2908,17 @@
 ring();%0A
+%09
 %09%09reques
@@ -2942,16 +2942,17 @@
 );%0A%09%09%0A%09%09
+%09
 mex.invo
@@ -2967,16 +2967,17 @@
 st);%0A%09%09%0A
+%09
 %09%09Messag
@@ -3011,16 +3011,17 @@
 e();%0A%09%09%0A
+%09
 %09%09String
@@ -3075,16 +3075,17 @@
 e());%0A%09%09
+%09
 System.o
@@ -3102,16 +3102,17 @@
 (resp);%0A
+%09
 %09%09assert
@@ -3194,15 +3194,17 @@
 ));%0A
+%09%09%7D%0A
 %09%7D%0A%09%0A
-//
 %09pub
@@ -3249,18 +3249,16 @@
 ption %7B%0A
-//
 %09%09go(%22ta
@@ -3299,18 +3299,16 @@
 rld2%22);%0A
-//
 %09%7D%0A%09publ

Add support for multiple tests