'''
@author: shylent
'''
from tftp.backend import (FilesystemSynchronousBackend, FilesystemReader,
    FilesystemWriter, IReader, IWriter)
from tftp.errors import Unsupported, AccessViolation, FileNotFound, FileExists
from twisted.python.filepath import FilePath
from twisted.internet.defer import inlineCallbacks
from twisted.trial import unittest
import os.path
import shutil
import tempfile


class BackendSelection(unittest.TestCase):
    test_data = """line1
line2
line3
"""


    def setUp(self):
        self.temp_dir = tempfile.mkdtemp()
        self.existing_file_name = os.path.join(self.temp_dir, 'dir', 'foo')
        os.mkdir(os.path.dirname(self.existing_file_name))
        with open(self.existing_file_name, 'w') as f:
            f.write(self.test_data)

    @inlineCallbacks
    def test_read_supported_by_default(self):
        b = FilesystemSynchronousBackend(self.temp_dir)
        reader = yield b.get_reader('dir/foo')
        self.assertTrue(IReader.providedBy(reader))

    @inlineCallbacks
    def test_write_supported_by_default(self):
        b = FilesystemSynchronousBackend(self.temp_dir)
        writer = yield b.get_writer('dir/bar')
        self.assertTrue(IWriter.providedBy(writer))

    def test_read_unsupported(self):
        b = FilesystemSynchronousBackend(self.temp_dir, can_read=False)
        return self.assertFailure(b.get_reader('dir/foo'), Unsupported)

    def test_write_unsupported(self):
        b = FilesystemSynchronousBackend(self.temp_dir, can_write=False)
        return self.assertFailure(b.get_writer('dir/bar'), Unsupported)

    def test_insecure_reader(self):
        b = FilesystemSynchronousBackend(self.temp_dir)
        return self.assertFailure(
            b.get_reader('../foo'), AccessViolation)

    def test_insecure_writer(self):
        b = FilesystemSynchronousBackend(self.temp_dir)
        return self.assertFailure(
            b.get_writer('../foo'), AccessViolation)

    def tearDown(self):
        shutil.rmtree(self.temp_dir)


class Reader(unittest.TestCase):
    test_data = """line1
line2
line3
"""

    def setUp(self):
        self.temp_dir = FilePath(tempfile.mkdtemp())
        self.existing_file_name = self.temp_dir.child('foo')
        with self.existing_file_name.open('w') as f:
            f.write(self.test_data)

    def test_file_not_found(self):
        self.assertRaises(FileNotFound, FilesystemReader, self.temp_dir.child('bar'))

    def test_read_existing_file(self):
        r = FilesystemReader(self.temp_dir.child('foo'))
        data = r.read(3)
        ostring = data
        while data:
            data = r.read(3)
            ostring += data
        self.assertEqual(r.read(3), '')
        self.assertEqual(r.read(5), '')
        self.assertEqual(r.read(7), '')
        self.failUnless(r.file_obj.closed,
                        "The file has been exhausted and should be in the closed state")
        self.assertEqual(ostring, self.test_data)

    def test_size(self):
        r = FilesystemReader(self.temp_dir.child('foo'))
        self.assertEqual(len(self.test_data), r.size)

    def test_size_when_reader_finished(self):
        r = FilesystemReader(self.temp_dir.child('foo'))
        r.finish()
        self.assertIsNone(r.size)

    def test_size_when_file_removed(self):
        # FilesystemReader.size uses fstat() to discover the file's size, so
        # the absence of the file does not matter.
        r = FilesystemReader(self.temp_dir.child('foo'))
        self.existing_file_name.remove()
        self.assertEqual(len(self.test_data), r.size)

    def test_cancel(self):
        r = FilesystemReader(self.temp_dir.child('foo'))
        r.read(3)
        r.finish()
        self.failUnless(r.file_obj.closed,

            "The session has been finished, so the file object should be in the closed state")
        r.finish()

    def tearDown(self):
        self.temp_dir.remove()


class Writer(unittest.TestCase):
    test_data = """line1
line2
line3
"""

    def setUp(self):
        self.temp_dir = FilePath(tempfile.mkdtemp())
        self.existing_file_name = self.temp_dir.child('foo')
        with self.existing_file_name.open('w') as f:
            f.write(self.test_data)

    def test_write_existing_file(self):
        self.assertRaises(FileExists, FilesystemWriter, self.temp_dir.child('foo'))

    def test_finished_write(self):
        w = FilesystemWriter(self.temp_dir.child('bar'))
        w.write(self.test_data)
        w.finish()
        with self.temp_dir.child('bar').open() as f:
            self.assertEqual(f.read(), self.test_data)

    def test_cancelled_write(self):
        w = FilesystemWriter(self.temp_dir.child('bar'))
        w.write(self.test_data)
        w.cancel()
        self.failIf(self.temp_dir.child('bar').exists(),
                    "If a write is cancelled, the file should not be left behind")

    def tearDown(self):
        self.temp_dir.remove()

@@ -346,23 +346,8 @@
 est%0A
-import os.path%0A
 impo
@@ -356,16 +356,16 @@
  shutil%0A
+
 import t
@@ -505,16 +505,25 @@
 p_dir = 
+FilePath(
 tempfile
@@ -532,16 +532,17 @@
 kdtemp()
+)
 %0A       
@@ -572,48 +572,47 @@
 e = 
-os.path.join(self.temp_dir, 'dir', 'foo'
+self.temp_dir.descendant((%22dir%22, %22foo%22)
 )%0A  
@@ -621,132 +621,94 @@
     
-os.mkdir(os.path.dirname(self.existing_file_name))%0A        with open(self.existing_file_name, 'w') as f:%0A            f.write
+self.existing_file_name.parent().makedirs()%0A        self.existing_file_name.setContent
 (sel
@@ -834,32 +834,37 @@
 nd(self.temp_dir
+.path
 )%0A        reader
@@ -1063,32 +1063,37 @@
 nd(self.temp_dir
+.path
 )%0A        writer
@@ -1265,24 +1265,29 @@
 elf.temp_dir
+.path
 , can_read=F
@@ -1457,16 +1457,21 @@
 temp_dir
+.path
 , can_wr
@@ -1636,32 +1636,37 @@
 nd(self.temp_dir
+.path
 )%0A        return
@@ -1830,16 +1830,21 @@
 temp_dir
+.path
 )%0A      
@@ -1942,32 +1942,32 @@
 tearDown(self):%0A
-
         shutil.r
@@ -1985,16 +1985,21 @@
 temp_dir
+.path
 )%0A%0A%0Aclas

Use FilePath in BackendSelection.