package it.mice.voila.runtime.oawtemplate.test;

import it.mice.voila.runtime.oawtemplate.runner.OAWWorkflowRunner;
import it.mice.voila.runtime.oawtemplate.test.model.Element;
import it.mice.voila.runtime.oawtemplate.test.model.Model;

import java.io.File;
import java.io.FileOutputStream;
import java.util.zip.ZipOutputStream;

import junit.framework.TestCase;

public class TestOAWFinal extends TestCase {
	public void test1() throws Exception {
		String resource = null; //"C:/Lavoro/EclipseWorkspaces/Workspace_SkyIT/skyslcm-docbook/bin/";
		Model model = prepareModel("no thread");
		String temp = System.getProperty("java.io.tmpdir");
		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(temp + File.separator + "output.zip"));
		
		OAWWorkflowRunner workflowRunner = new OAWWorkflowRunner();
		workflowRunner.execute(model, resource, zos);
	}

	public void testThread() throws Exception {
		Thread[] threads = new Thread[50];
		for (int i = 0; i < 1; i++) {
			threads[i] = new TestThread("Thread n. " + i);
		}
		for (int i = 0; i < 1; i++) {
			threads[i].join();
		}
	}

	private Model prepareModel(String threadName) {
		Model model = new Model();
		model.setField1(threadName + " Field1 value");
		model.setField2(threadName + " Field2 value");
		Element el1 = new Element();
		el1.setName("Element1");
		el1.setField1("el1 f1");
		el1.setField2("el1 f2");
		model.addElement(el1);
		el1 = new Element();
		el1.setName("Element2");
		el1.setField1("el2 f1");
		el1.setField2("el2 f2");
		model.addElement(el1);
		return model;
	}
	
	public class TestThread extends Thread {

		public TestThread(String name) {
			super(name);
			start();
		}

		/* (non-Javadoc)
		 * @see java.lang.Thread#run()
		 */
		@Override
		public void run() {
			String resource = null; //"C:/Lavoro/EclipseWorkspaces/Workspace_SkyIT/skyslcm-docbook/bin/";
			Model model = prepareModel(getName());
			ZipOutputStream zos;
			try {
				String temp = System.getProperty("java.io.tmpdir");
				zos = new ZipOutputStream(new FileOutputStream(temp + File.separator + getName() + ".zip"));
				OAWWorkflowRunner workflowRunner = new OAWWorkflowRunner();
				workflowRunner.execute(model, resource, zos);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
	}
}
