package kieker.test.tools.junit.bridge;

import java.io.BufferedReader;
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/";

		// TODO create dir

		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();
		}

		try {

			final FileReader fr = new FileReader("test.txt");
			final BufferedReader br = new BufferedReader(fr);
			String line;
			int rows = 0;

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

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

		}
		// TODO check if the number of send records is equal to TestServiceConnector.SEND_NUMBER_OF_RECORDS,
		// you have to get the information from the Kieker record log
		Assert.assertTrue("The number of send records is not equal to TestServiceConnector.SEND_NUMBER_OF_RECORDS",
				TestServiceConnector.SEND_NUMBER_OF_RECORDS == 20);
		// find file /tmp/kdb/kieker-*/kieker-*.dat
		// count lines in file

		// remove all files and directories in /tmp/kdb/ (including /tmp/kdb/)
	}

	/**
	 * 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;
	}

}

@@ -57,32 +57,53 @@
 BufferedReader;%0A
+import java.io.File;%0A
 import java.io.F
@@ -105,32 +105,32 @@
 .io.FileReader;%0A
-
 import java.io.I
@@ -956,16 +956,277 @@
 p/kdb/%22;
+%0A%09%09final String dirName = %22data%22;%0A%09%09final File dir = new File(path + dirName);%0A%09%09int rows = 0;%0A%0A%09%09if (dir.mkdir()) %7B%0A%09%09%09System.out.println(dir + %22 - konnte nicht erstellt werden.%22);%0A%09%09%7D else %7B%0A%09%09%09System.out.println(dir + %22 - konnte nicht erstellt werden.%22);%0A%09%09%7D
 %0A%0A%09%09// T
@@ -1891,16 +1891,65 @@
 t.txt%22);
+ // instead of test.txt fill the correct filename
 %0A%09%09%09fina
@@ -2013,25 +2013,8 @@
 ine;
-%0A%09%09%09int rows = 0;
 %0A%0A%09%09
@@ -2091,16 +2091,31 @@
 ows - 1;
+%0A%09%09%09br.close();
 %0A%0A%09%09%7D ca
@@ -2518,10 +2518,12 @@
  == 
-20
+rows
 );%0A%09
@@ -2599,78 +2599,77 @@
 %0A%0A%09%09
-// remove all files and directories in /tmp/kdb/ (including /tmp/kdb/)
+dir.delete(); // deletes the whole directory from path /tmp/kdb/data/
 %0A%09%7D%0A

test creates directory and fill it with records