package org.pocketcampus.plugin.freeroom.server.tests;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.commons.io.IOUtils;
import org.junit.Test;


public class TestFindFreeRooms {
	final String DB_USERNAME = "root";
	final String DB_PASSWORD = "root";
	final String DB_URL = "jdbc:mysql://localhost/?allowMultiQueries=true";
	
	public void populateDBTest() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = DriverManager
					.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
			
			PreparedStatement stmt = conn.prepareStatement("CREATE DATABASE IF NOT EXISTS pocketcampustest");
			stmt.execute();
			conn.setCatalog("pocketcampustest");
			
			File dbFile = new File("src/org/pocketcampus/plugin/freeroom/server/tests/testdb.sql");

			String query = IOUtils.toString(new FileReader(dbFile));
			pstmt = conn.prepareStatement(query);
			pstmt.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	public void removeDBTest() {
		Connection conn = null;
		try {
			conn = DriverManager
					.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
			
			PreparedStatement stmt = conn.prepareStatement("DROP DATABASE pocketcampustest");
			stmt.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		} 
	}

	@Test
	public void test() {
		populateDBTest();
		
		removeDBTest();
	}

}

@@ -299,37 +299,117 @@
 ort 
-org.apache.commons.io.IOUtils
+junit.framework.Assert;%0Aimport org.apache.commons.io.IOUtils;%0Aimport org.junit.After;%0Aimport org.junit.Before
 ;%0Aim
@@ -624,21 +624,19 @@
 ic void 
-popul
+cre
 ateDBTes
@@ -1175,170 +1175,365 @@
 ;%0A%09%09
-%7D catch (SQLException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D catch (FileNotFoundException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09e.printStackTrace(
+%09%0A%09%09%09// TODO: check that the database and two tables are successfully created ?%0A%09%09%7D catch (SQLException e) %7B%0A%09%09%09Assert.fail(%22There was an SQL Exception %5Cn %22 + e);%0A%09%09%7D catch (FileNotFoundException e) %7B%0A%09%09%09Assert.fail(%22Thee test file for the database was not found %5Cn %22 + e);%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09Assert.fail(%22There was another IO Exception %5Cn %22 + e
 );%0A%09
@@ -1798,56 +1798,153 @@
 ;%0A%09%09
-%7D catch (SQLException e) %7B%0A%09%09%09e.printStackTrace(
+%09%0A%09%09%09// TODO : check that the database is successfully deleted ?%0A%09%09%7D catch (SQLException e) %7B%0A%09%09%09Assert.fail(%22There was an SQL Exception %5Cn %22 + e
 );%0A%09
@@ -1953,20 +1953,22 @@
  %0A%09%7D%0A%0A%09@
-Test
+Before
 %0A%09public
@@ -1977,24 +1977,23 @@
 oid 
-test
+setUp
 () %7B%0A%09%09
-popul
+cre
 ateD
@@ -2006,30 +2006,213 @@
 );%0A%09
+%7D%0A
 %09%0A%09
-%09removeDBTest();%0A%09%7D%0A
+@After%0A%09public void finalize() %7B%0A%09%09removeDBTest();%0A%09%7D%0A%09%0A%09@Test%0A%09public void testPopulateDB() %7B%0A%09%09// TODO: populate the database with fake values and extract expected results according to the values.%0A%09%7D
 %0A%7D%0A

test of databases: small changes and refactoring