package hu.elte.txtuml.export.cpp.wizardz;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

import java.io.File;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.junit.Test;

public class CompileTests {

	private static class Config {
		final String project;
		final String model;
		final String deployment;

		Config(String project, String model, String deployment) {
			this.project = project;
			this.model = model;
			this.deployment = deployment;
		}
	}

	private static final String pathToProjects = "../../../examples/demo/";

	private static final Config[] testProjects = {
			new Config("machine", "machine1.j.model", "machine1.j.DefaultConfiguration"),
			new Config("monitoring", "monitoring.x.model", "monitoring.x.DefaultConfiguration"),
			new Config("producer_consumer", "producer_consumer.j.model", "producer_consumer.j.DefaultConfiguration"),
			new Config("train", "train.j.model", "train.j.DefaultConfiguration"), };

	@Test
	public void exportTest() {
		for (Config config : testProjects) {
			TxtUMLToCppGovernor cppgen = new TxtUMLToCppGovernor(true);
			try {
				String canPathToProjects = new File(pathToProjects).getCanonicalPath();
				IProjectDescription desc = ResourcesPlugin.getWorkspace()
						.loadProjectDescription(new Path(canPathToProjects + "/" + config.project + "/.project"));
				desc.setLocation(new Path(new File(canPathToProjects + "/" + config.project).getCanonicalPath()));
				IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(desc.getName());
				if (!project.exists()) {
					project.create(desc, null);
				}
				project.open(null);
				String testProject = "test_" + config.project;
				project.copy(new Path(testProject), true, null);
				project.close(null);
				project = ResourcesPlugin.getWorkspace().getRoot().getProject(testProject);
				project.refreshLocal(IProject.DEPTH_INFINITE, null);

				cppgen.uml2ToCpp(testProject, config.model, config.deployment, false);
			} catch (Exception e) {
				e.printStackTrace();
				assertThat(false, is(true));
			}
		}
	}
}

@@ -1121,82 +1121,115 @@
 ;%0A%0A%09
-@Test%0A%09public void exportTest() %7B%0A%09%09for (Config config : testProjects)
+private static String generateCPP(Config config, String testPrefix, boolean addRuntime) throws Exception
  %7B%0A
-%09
 %09%09Tx
@@ -1290,19 +1290,9 @@
 e);%0A
-%09%09%09try %7B%0A%09%09
+%0A
 %09%09St
@@ -1361,18 +1361,16 @@
 Path();%0A
-%09%09
 %09%09IProje
@@ -1425,18 +1425,16 @@
 e()%0A%09%09%09%09
-%09%09
 .loadPro
@@ -1516,18 +1516,16 @@
 ect%22));%0A
-%09%09
 %09%09desc.s
@@ -1617,18 +1617,16 @@
 th()));%0A
-%09%09
 %09%09IProje
@@ -1707,18 +1707,16 @@
 ame());%0A
-%09%09
 %09%09if (!p
@@ -1737,18 +1737,16 @@
 )) %7B%0A%09%09%09
-%09%09
 project.
@@ -1769,16 +1769,12 @@
 l);%0A
-%09%09
 %09%09%7D%0A
-%09%09
 %09%09pr
@@ -1791,18 +1791,17 @@
 (null);%0A
-%09%09
+%0A
 %09%09String
@@ -1819,15 +1819,18 @@
 t = 
-%22
 test
-_%22
+Prefix
  + c
@@ -1844,18 +1844,16 @@
 roject;%0A
-%09%09
 %09%09projec
@@ -1893,26 +1893,24 @@
 e, null);%0A%09%09
-%09
-%09
 project.clos
@@ -1918,18 +1918,16 @@
 (null);%0A
-%09%09
 %09%09projec
@@ -1998,18 +1998,16 @@
 ect);%0A%09%09
-%09
-%09
 project.
@@ -2052,18 +2052,16 @@
 null);%0A%0A
-%09%09
 %09%09cppgen
@@ -2117,20 +2117,411 @@
 oyment, 
-fals
+addRuntime);%0A%0A%09%09return testProject;%0A%09%7D%0A%0A%09@Test%0A%09public void exportTest() %7B%0A%09%09for (Config config : testProjects) %7B%0A%09%09%09try %7B%0A%09%09%09%09generateCPP(config, %22exportTest_%22, false);%0A%09%09%09%7D catch (Exception e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%09assertThat(false, is(true));%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09@Test%0A%09public void compileTest() %7B%0A%09%09for (Config config : testProjects) %7B%0A%09%09%09try %7B%0A%09%09%09%09generateCPP(config, %22compileTest_%22, tru
 e);%0A%09%09%09%7D

Add export and compile tests