package org.richfaces.realworld.util;

import junit.framework.TestCase;

import org.richfaces.realworld.manager.FileManager;

//TODO nick - test should be moved to src\main\test
//TODO nick - test doesn't pass
public class FileManagerTest extends TestCase {

	protected void setUp() throws Exception {
		fileManager=new FileManager();
		fileManager.setUploadRoot("C:/Upload/");
	}

	protected void tearDown() throws Exception {
	}

private byte[] image = new byte[40];
	private FileManager fileManager;
	public void testIsDirectoryPresent() {
		fileManager.addDirectory("amarkhel");
		assertEquals(fileManager.isDirectoryPresent("amarkhel"), true);
		assertEquals(fileManager.isDirectoryPresent("amarkhel2"), false);
		fileManager.deleteDirectory("amarkhel");
	}

	public void testDeleteDirectory() {
		fileManager.addDirectory("amarkhel");
		assertEquals(fileManager.isDirectoryPresent("amarkhel"), true);
		fileManager.deleteDirectory("amarkhel");
		assertEquals(fileManager.isDirectoryPresent("amarkhel"), false);
	}

	public void testRenameDirectory() throws Exception {
		fileManager.addDirectory("amarkhel");
		assertEquals(fileManager.isDirectoryPresent("amarkhel"), true);
		fileManager.renameDirectory("amarkhel", "amarkhel2");
		assertEquals(fileManager.isDirectoryPresent("amarkhel"), false);
		assertEquals(fileManager.isDirectoryPresent("amarkhel2"), true);
		fileManager.deleteDirectory("amarkhel2");
	}
	
	public void testRenameDirectory2(){
		fileManager.addDirectory("amarkhel");
		fileManager.addDirectory("amarkhel2");
		assertEquals(fileManager.isDirectoryPresent("amarkhel"), true);
			try {
				fileManager.renameDirectory("amarkhel", "amarkhel2");
			} catch (Exception e) {
				assertTrue(true);
			}
		
		assertEquals(fileManager.isDirectoryPresent("amarkhel"), true);
		assertEquals(fileManager.isDirectoryPresent("amarkhel2"), true);
		fileManager.deleteDirectory("amarkhel");
		fileManager.deleteDirectory("amarkhel2");
	}
	
	public void testRenameDirectory3() throws Exception {
		fileManager.addDirectory("a"+System.getProperty("file.separator")+"amarkhel");
		assertEquals(fileManager.isDirectoryPresent("a"+System.getProperty("file.separator")+"amarkhel"), true);
		fileManager.renameDirectory("a"+System.getProperty("file.separator")+"amarkhel", "b"+System.getProperty("file.separator")+"amarkhel");
		assertEquals(fileManager.isDirectoryPresent("a"+System.getProperty("file.separator")+"amarkhel"), false);
		assertEquals(fileManager.isDirectoryPresent("b"+System.getProperty("file.separator")+"amarkhel"), true);
		fileManager.deleteDirectory("b"+System.getProperty("file.separator")+"amarkhel");
	}
	
	public void testRenameDirectory4() throws Exception {
		fileManager.addDirectory("a"+System.getProperty("file.separator")+"a"+System.getProperty("file.separator")+"amarkhel");
		assertEquals(fileManager.isDirectoryPresent("a"+System.getProperty("file.separator")+"a"+System.getProperty("file.separator")+"amarkhel"), true);
		fileManager.renameDirectory("a"+System.getProperty("file.separator")+"a"+System.getProperty("file.separator")+"amarkhel", "b"+System.getProperty("file.separator")+"b"+System.getProperty("file.separator")+"amarkhel");
		assertEquals(fileManager.isDirectoryPresent("a"+System.getProperty("file.separator")+"a"+System.getProperty("file.separator")+"amarkhel"), false);
		assertEquals(fileManager.isDirectoryPresent("b"+System.getProperty("file.separator")+"b"+System.getProperty("file.separator")+"amarkhel"), true);
		fileManager.deleteDirectory("b"+System.getProperty("file.separator")+"b"+System.getProperty("file.separator")+"amarkhel");
	}

	public void testAddDirectory() {
		assertEquals(fileManager.isDirectoryPresent("amarkhel"), false);
		fileManager.addDirectory("amarkhel");
		assertEquals(fileManager.isDirectoryPresent("amarkhel"), true);
		fileManager.deleteDirectory("amarkhel");
	}
	
	public void testAddDirectory2() {
		assertEquals(fileManager.isDirectoryPresent("a"+System.getProperty("file.separator")+"amarkhel"), false);
		fileManager.addDirectory("a"+System.getProperty("file.separator")+"amarkhel");
		assertEquals(fileManager.isDirectoryPresent("a"+System.getProperty("file.separator")+"amarkhel"), true);
		fileManager.deleteDirectory("a"+System.getProperty("file.separator")+"amarkhel");
	}

	/*public void testAddImage() throws IOException {
		assertEquals(fileManager.isImagePresent("amarkhel.gif"), false);
		fileManager.addImage("amarkhel.gif", image);
		assertEquals(fileManager.isImagePresent("amarkhel.gif"), true);
		fileManager.deleteImage("amarkhel.gif");
	}

	public void testDeleteImage() throws IOException {
		fileManager.addImage("amarkhel.gif", image);
		assertEquals(fileManager.isImagePresent("amarkhel.gif"), true);
		fileManager.deleteImage("amarkhel.gif");
		assertEquals(fileManager.isImagePresent("amarkhel.gif"), false);
	}

	public void testRenameImage() throws IOException {
		fileManager.addImage("amarkhel.gif", image);
		assertEquals(fileManager.isImagePresent("amarkhel.gif"), true);
		fileManager.renameImage("amarkhel.gif", "amarkhel2.gif");
		assertEquals(fileManager.isImagePresent("amarkhel.gif"), false);
		assertEquals(fileManager.isImagePresent("amarkhel2.gif"), true);
		fileManager.deleteImage("amarkhel2.gif");
	}
	
	public void testRenameImage2() throws IOException {
		fileManager.addImage("a"+System.getProperty("file.separator")+"amarkhel.gif", image);
		assertEquals(fileManager.isImagePresent("a"+System.getProperty("file.separator")+"amarkhel.gif"), true);
		fileManager.renameImage("a"+System.getProperty("file.separator")+"amarkhel.gif", "b"+System.getProperty("file.separator")+"amarkhel2.gif");
		assertEquals(fileManager.isImagePresent("a"+System.getProperty("file.separator")+"amarkhel.gif"), false);
		assertEquals(fileManager.isImagePresent("b"+System.getProperty("file.separator")+"amarkhel2.gif"), true);
		fileManager.deleteImage("b"+System.getProperty("file.separator")+"amarkhel2.gif");
		fileManager.deleteDirectory("b");
		fileManager.deleteDirectory("a");
	}

	public void testIsImagePresent() throws IOException {
		fileManager.addImage("amarkhel.gif", image);
		assertEquals(fileManager.isImagePresent("amarkhel.gif"), true);
		assertEquals(fileManager.isImagePresent("amarkhel3.gif"), false);
		fileManager.deleteImage("amarkhel.gif");
		fileManager.addImage("am"+System.getProperty("file.separator")+"amarkhel.gif", image);
		assertEquals(fileManager.isImagePresent("am"+System.getProperty("file.separator")+"amarkhel.gif"), true);
		fileManager.deleteImage("am"+System.getProperty("file.separator")+"amarkhel.gif");
		fileManager.deleteDirectory("am");
	}*/

}
