package kieker.test.tools.junit.bridge;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;

import kieker.common.configuration.Configuration;
import kieker.monitoring.core.configuration.ConfigurationFactory;
import kieker.tools.bridge.ServiceContainer;

import kieker.test.common.junit.AbstractKiekerTest;

/**
 * @author Pascale Brandt
 */

public class ConnectorTest extends AbstractKiekerTest {

	private boolean setup = false;
	private boolean close = false;

	public ConnectorTest() {}

	@Test
	public void firstTest() throws IOException {

		/**
		 * 1st parameter: Configuration is in CLIServerMain
		 * next steps stays in CLIServerMain//
		 * First element is a default configuration like in CLIServerMain(),
		 * the second part starts a new record which is written in the TestServiceConnector class.
		 * 
		 */
		final String path = "/tmp/kdb/";
		final File dir = new File(path);
		int rows = 0;

		Assert.assertTrue("Directory could not created", dir.mkdir() == true);

		final Configuration configuration = ConfigurationFactory.createDefaultConfiguration();
		configuration.setProperty("kieker.monitoring.writer.filesystem.AsyncFsWriter.storeInJavaIoTmpdir", "false");
		configuration.setProperty("kieker.monitoring.writer.filesystem.AsyncFsWriter.customStoragePath", path);

		final ServiceContainer serviceContainer = new ServiceContainer(configuration,
				new TestServiceConnector(this));
		try {
			serviceContainer.run();
		} catch (final Exception e) {
			System.out.println("Something went wrong: " + e.getStackTrace());
			e.printStackTrace();
		}

		// check number of lines in Kieker record file
		try {
			final FileReader fr = new FileReader("");
			final BufferedReader br = new BufferedReader(fr);
			String line;

			while ((line = br.readLine()) != null) {
				rows++;
			}
			rows = rows - 1;
			br.close();

		} catch (final IOException e) {
			System.out.println(e.getMessage());

		}

		Assert.assertTrue("The number of send records is not equal to TestServiceConnector.SEND_NUMBER_OF_RECORDS",
				TestServiceConnector.SEND_NUMBER_OF_RECORDS == rows);

		// Delete iterativ every element from the created folder above
		if (dir.exists())
		{
			for (final File f : dir.listFiles())
			{
				f.delete();
			}
			dir.delete();
		}

		Assert.assertTrue("Directory is not cleaned", path.isEmpty() == true);
	}

	/**
	 * The following three methods test if the setup, deserialize and close method is getting called.
	 * If that is not the case the error should throw an exception which has to be catched.
	 */
	public void setupCalled() {
		Assert.assertTrue("Connector's setup() method was called more than once.", this.setup == false);
		this.setup = true;
		this.close = false;
	}

	public void deserializeCalled() {
		Assert.assertTrue("Connector's deserialize() method called before setup() was called.", this.setup == true);
	}

	public void closeCalled() {
		Assert.assertTrue("Connector's close() method was called before setup() was called.", this.setup == true);
		Assert.assertTrue("Connector's close() method was called more than once.", this.close == false);
		this.close = true;
		this.setup = false;
	}

}

@@ -184,20 +184,83 @@
 g.junit.
-Test
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.TemporaryFolder
 ;%0A%0Aimpor
@@ -654,16 +654,89 @@
 t() %7B%7D%0A%0A
+%09@Rule%0A%09public final TemporaryFolder tmpFolder = new TemporaryFolder();%0A%0A
 %09@Test%0A%09
@@ -1058,24 +1058,25 @@
  %0A%09%09 */%0A
+%0A
 %09%09final 
 String p
@@ -1071,22 +1071,20 @@
 nal 
-String
+File
  path = 
 %22/tm
@@ -1083,53 +1083,31 @@
 h = 
-%22/tmp/kdb/%22;%0A%09%09final File dir = new File(path
+this.tmpFolder.getRoot(
 );%0A%09
@@ -1120,24 +1120,27 @@
 ows = 0;%0A%0A%09%09
+// 
 Assert.asser
@@ -1187,27 +1187,28 @@
 .mkdir() == 
-tru
+fals
 e);%0A%0A%09%09final
@@ -1503,16 +1503,34 @@
 h%22, path
+.getAbsolutePath()
 );%0A%0A%09%09fi
@@ -1965,16 +1965,22 @@
 fr);%0A%09%09%09
+final 
 String l
@@ -1982,16 +1982,32 @@
 ing line
+ = br.readLine()
 ;%0A%0A%09%09%09wh
@@ -2014,31 +2014,13 @@
 ile 
-(
 (line
- = br.readLine())
  != 
@@ -2395,16 +2395,24 @@
 above%0A%09%09
+/*%0A%09%09 * 
 if (dir.
@@ -2423,21 +2423,26 @@
 ts())%0A%09%09
+ * 
 %7B%0A%09%09
-%09
+ * 
 for (fin
@@ -2476,15 +2476,18 @@
 )%0A%09%09
-%09
+ * 
 %7B%0A%09%09
-%09%09
+ * 
 f.de
@@ -2496,22 +2496,26 @@
 te();%0A%09%09
-%09
+ * 
 %7D%0A%09%09
-%09
+ * 
 dir.dele
@@ -2518,25 +2518,34 @@
 delete();%0A%09%09
-%7D
+ * %7D%0A%09%09 */
 %0A%0A%09%09Assert.a
@@ -2586,20 +2586,39 @@
 d%22, 
-pa
 th
-.
 is
-Empty
+.tmpFolder.getRoot().exists
 () =

use Rule for creation of tempFolder for records