def testSendZipFile(self):
   """Tests if the htm is extrated from zipfile"""
   zip_input_url = 'data/test.zip'
   data = open(zip_input_url).read()
   zipdocument = FileSystemDocument(self.tmp_url, data, 'zip')
   mime = magic.Magic(mime=True)
   mimetype = mime.from_buffer(zipdocument.getContent(True))
   self.assertEquals(mimetype, "application/zip")
   mimetype = mime.from_buffer(zipdocument.getContent())
   self.assertEquals(mimetype, "text/html")
   zipfile = ZipFile(StringIO(zipdocument.getContent(True)))
   self.assertEquals(sorted(zipfile.namelist()),
               sorted(['logo.gif', 'test.htm']))
 def testSendZipFile(self):
   """Tests if the htm is extrated from zipfile"""
   zip_input_url = 'data/test.zip'
   data = open(zip_input_url).read()
   zipdocument = FileSystemDocument(self.tmp_url, data, 'zip')
   mime = magic.Magic(mime=True)
   mimetype = mime.from_buffer(zipdocument.getContent(True))
   self.assertEquals(mimetype, "application/zip")
   mimetype = mime.from_buffer(zipdocument.getContent())
   self.assertEquals(mimetype, "text/html")
   zipfile = ZipFile(StringIO(zipdocument.getContent(True)))
   self.assertEquals(sorted(zipfile.namelist()),
               sorted(['logo.gif', 'test.htm']))
Esempio n. 3
0
 def __init__(self, base_folder_url, data, source_format, **kw):
     """Creates document in file system and loads it in OOo."""
     self.document = FileSystemDocument(base_folder_url, data,
                                        source_format)
     self.zip = kw.get('zip', False)
     self.uno_path = kw.get("uno_path", None)
     self.office_binary_path = kw.get("office_binary_path", None)
     self.timeout = kw.get("timeout", 600)
     self.refresh = kw.get('refresh', False)
     self.source_format = source_format
     if not self.uno_path:
         self.uno_path = environ.get("uno_path")
     if not self.office_binary_path:
         self.office_binary_path = environ.get("office_binary_path")
class TestUnoConverter(HandlerTestCase):
  """Test case to test all features of the unoconverter script"""

  file_msg_list = ["Microsoft Office Document",
                  "CDF V2 Document, Little Endian, Os: Windows, Version 1.0,"]

  def afterSetUp(self):
    """ """
    openoffice.acquire()
    self.hostname, self.port = openoffice.getAddress()
    data = open("data/test.odt", 'r').read()
    self.document = FileSystemDocument(self.tmp_url, data, 'odt')

  def tearDown(self):
    """Called to unlock the openoffice"""
    openoffice.release()

  def testUnoConverterOdtToDoc(self):
    """Test script unoconverter"""
    mimemapper = dict(filter_list=[('doc',
                                    'com.sun.star.text.TextDocument',
                                    'MS Word 97')],
                     doc_type_list_by_extension=dict(doc=['com.sun.star.text.TextDocument']))
    mimemapper_pickled = json.dumps(mimemapper)
    python = join(self.office_binary_path, "python")
    command = [exists(python) and python or "python",
          pkg_resources.resource_filename("cloudooo.handler.ooo",
                                          "/helper/unoconverter.py"),
          "--convert",
          "--uno_path=%s" % self.uno_path,
          "--office_binary_path=%s" % self.office_binary_path,
          "--hostname=%s" % self.hostname,
          "--port=%s" % self.port,
          "--document_url=%s" % self.document.getUrl(),
          "--destination_format=%s" % "doc",
          "--source_format=%s" % "odt",
          "--mimemapper=%s" % mimemapper_pickled]
    stdout, stderr = Popen(command,
                           stdout=PIPE,
                           stderr=PIPE).communicate()
    self.assertEquals(stderr, '')
    output_url = stdout.replace('\n', '')
    self.assertTrue(exists(output_url), stdout)
    mime = magic.Magic(mime=True)
    self.assertEquals(mime.from_file(output_url), 'application/msword')
    self.document.trash()
    self.assertEquals(exists(output_url), False)
Esempio n. 5
0
 def _createDocument(self, base_folder_url, data, source_format):
   if source_format == 'csv':
     # Cloudooo expect utf-8 encoded csv, but also tolerate latin9 for
     # backward compatibility.
     # The heuristic is "if it's not utf-8", let's assume it's iso-8859-15.
     try:
       unicode(data, 'utf-8')
     except UnicodeDecodeError:
       data = unicode(data, 'iso-8859-15').encode('utf-8')
       logger.warn("csv data is not utf-8, assuming iso-8859-15")
   self.document = FileSystemDocument(
        base_folder_url,
        data,
        source_format)
Esempio n. 6
0
 def __init__(self, base_folder_url, data, source_format, **kw):
   """Creates document in file system and loads it in OOo."""
   self.document = FileSystemDocument(base_folder_url,
                                     data,
                                     source_format)
   self.zip = kw.get('zip', False)
   self.uno_path = kw.get("uno_path", None)
   self.office_binary_path = kw.get("office_binary_path", None)
   self.timeout = kw.get("timeout", 600)
   self.refresh = kw.get('refresh', False)
   self.source_format = source_format
   if not self.uno_path:
     self.uno_path = environ.get("uno_path")
   if not self.office_binary_path:
     self.office_binary_path = environ.get("office_binary_path")
 def afterSetUp(self):
   """ """
   openoffice.acquire()
   self.hostname, self.port = openoffice.getAddress()
   data = open("data/test.odt", 'r').read()
   self.document = FileSystemDocument(self.tmp_url, data, 'odt')
Esempio n. 8
0
class Handler(object):
  """OOO Handler is used to access the one Document and OpenOffice.
  For each Document inputed is created on instance of this class to manipulate
  the document. This Document must be able to create and remove a temporary
  document at FS, load and export.
  """
  implements(IHandler)

  def __init__(self, base_folder_url, data, source_format, **kw):
    """Creates document in file system and loads it in OOo."""
    self.document = FileSystemDocument(base_folder_url,
                                      data,
                                      source_format)
    self.zip = kw.get('zip', False)
    self.uno_path = kw.get("uno_path", None)
    self.office_binary_path = kw.get("office_binary_path", None)
    self.timeout = kw.get("timeout", 600)
    self.refresh = kw.get('refresh', False)
    self.source_format = source_format
    if not self.uno_path:
      self.uno_path = environ.get("uno_path")
    if not self.office_binary_path:
      self.office_binary_path = environ.get("office_binary_path")

  def _getCommand(self, *args, **kw):
    """Transforms all parameters passed in a command"""
    hostname, port = openoffice.getAddress()
    kw['hostname'] = hostname
    kw['port'] = port
    python = path.join(self.office_binary_path, "python")
    command_list = [path.exists(python) and python or "python",
                    pkg_resources.resource_filename(__name__,
                                 path.join("helper", "unoconverter.py")),
                    "--uno_path=%s" % self.uno_path,
                    "--office_binary_path=%s" % self.office_binary_path,
                    '--document_url=%s' % self.document.getUrl()]
    for arg in args:
      command_list.insert(3, "--%s" % arg)
    for k, v in kw.iteritems():
      command_list.append("--%s=%s" % (k, v))

    return command_list

  def _startTimeout(self):
    """start the Monitor"""
    self.monitor = MonitorTimeout(openoffice, self.timeout)
    self.monitor.start()
    return

  def _stopTimeout(self):
    """stop the Monitor"""
    self.monitor.terminate()
    return

  def _subprocess(self, command_list):
    """Run one procedure"""
    if monitor_sleeping_time is not None:
      monitor_sleeping_time.touch()
    try:
      self._startTimeout()
      process = Popen(command_list, stdout=PIPE, stderr=PIPE, close_fds=True,
                      env=openoffice.environment_dict.copy())
      stdout, stderr = process.communicate()
    finally:
      self._stopTimeout()
      if pid_exists(process.pid):
        process.terminate()
    return stdout, stderr

  def _callUnoConverter(self, *feature_list, **kw):
    """ """
    if not openoffice.status():
      openoffice.start()
    command_list = self._getCommand(*feature_list, **kw)
    stdout, stderr = self._subprocess(command_list)
    if not stdout and len(re.findall("\w*Exception|\w*Error", stderr)) >= 1:
      logger.debug(stderr)
      self.document.restoreOriginal()
      openoffice.restart()
      kw['document_url'] = self.document.getUrl()
      command = self._getCommand(*feature_list, **kw)
      stdout, stderr = self._subprocess(command)
      if stderr != "":
          raise Exception(stderr)

    return stdout, stderr

  def _serializeMimemapper(self,
                           source_extension=None,
                           destination_extension=None):
    """Serialize parts of mimemapper"""
    if destination_extension is None:
      return json.dumps(dict(mimetype_by_filter_type=mimemapper._mimetype_by_filter_type))

    filter_list = []
    service_type_list = mimemapper._doc_type_list_by_extension.get(
      source_extension, mimemapper.document_service_list)
    for service_type in service_type_list:
      filter_list.append((destination_extension,
                          service_type,
                          mimemapper.getFilterName(destination_extension, service_type)))
    logger.debug("Filter List: %r" % filter_list)
    return json.dumps(dict(doc_type_list_by_extension=mimemapper._doc_type_list_by_extension,
                            filter_list=filter_list,
                            mimetype_by_filter_type=mimemapper._mimetype_by_filter_type))

  def convert(self, destination_format=None, **kw):
    """Convert a document to another format supported by the OpenOffice
    Keyword Arguments:
    destination_format -- extension of document as String
    """
    logger.debug("OooConvert: %s > %s" % (self.source_format, destination_format))
    kw['source_format'] = self.source_format
    if destination_format:
      kw['destination_format'] = destination_format
    kw['mimemapper'] = self._serializeMimemapper(self.source_format,
                                                 destination_format)
    kw['refresh'] = json.dumps(self.refresh)
    openoffice.acquire()
    try:
      stdout, stderr = self._callUnoConverter(*['convert'], **kw)
    finally:
      openoffice.release()
    url = stdout.replace('\n', '')
    self.document.reload(url)
    content = self.document.getContent(self.zip)
    self.document.trash()
    return content

  def getMetadata(self, base_document=False):
    """Returns a dictionary with all metadata of document.
    Keywords Arguments:
    base_document -- Boolean variable. if true, the document is also returned
    along with the metadata."""
    logger.debug("getMetadata")
    kw = dict(mimemapper=self._serializeMimemapper())
    if base_document:
      feature_list = ['getmetadata', 'convert']
    else:
      feature_list = ['getmetadata']
    openoffice.acquire()
    try:
      stdout, stderr = self._callUnoConverter(*feature_list, **kw)
    finally:
      openoffice.release()
    metadata = json.loads(decodestring(stdout))
    if 'document_url' in metadata:
      self.document.reload(metadata['document_url'])
      metadata['Data'] = self.document.getContent()
      del metadata['document_url']
    self.document.trash()
    return metadata

  def setMetadata(self, metadata):
    """Returns a document with new metadata.
    Keyword arguments:
    metadata -- expected an dictionary with metadata.
    """
    metadata_pickled = json.dumps(metadata)
    logger.debug("setMetadata")
    kw = dict(metadata=encodestring(metadata_pickled))
    openoffice.acquire()
    try:
      stdout, stderr = self._callUnoConverter(*['setmetadata'], **kw)
    finally:
      openoffice.release()
    doc_loaded = self.document.getContent()
    self.document.trash()
    return doc_loaded
Esempio n. 9
0
class Handler(object):
  """OOO Handler is used to access the one Document and OpenOffice.
  For each Document inputed is created on instance of this class to manipulate
  the document. This Document must be able to create and remove a temporary
  document at FS, load and export.
  """
  implements(IHandler)

  def __init__(self, base_folder_url, data, source_format, **kw):
    """Creates document in file system and loads it in OOo."""
    self.zip = kw.get('zip', False)
    self.uno_path = kw.get("uno_path", None)
    self.office_binary_path = kw.get("office_binary_path", None)
    self.timeout = kw.get("timeout", 600)
    self.refresh = kw.get('refresh', False)
    self.source_format = source_format
    if not self.uno_path:
      self.uno_path = environ.get("uno_path")
    if not self.office_binary_path:
      self.office_binary_path = environ.get("office_binary_path")
    self._createDocument(base_folder_url, data, source_format)

  def _createDocument(self, base_folder_url, data, source_format):
    if source_format == 'csv':
      # Cloudooo expect utf-8 encoded csv, but also tolerate latin9 for
      # backward compatibility.
      # The heuristic is "if it's not utf-8", let's assume it's iso-8859-15.
      try:
        unicode(data, 'utf-8')
      except UnicodeDecodeError:
        data = unicode(data, 'iso-8859-15').encode('utf-8')
        logger.warn("csv data is not utf-8, assuming iso-8859-15")
    self.document = FileSystemDocument(
         base_folder_url,
         data,
         source_format)

  def _getCommand(self, *args, **kw):
    """Transforms all parameters passed in a command"""
    hostname, port = openoffice.getAddress()
    kw['hostname'] = hostname
    kw['port'] = port
    python = path.join(self.office_binary_path, "python")
    command_list = [path.exists(python) and python or "python",
                    pkg_resources.resource_filename(__name__,
                                 path.join("helper", "unoconverter.py")),
                    "--uno_path=%s" % self.uno_path,
                    "--office_binary_path=%s" % self.office_binary_path,
                    '--document_url=%s' % self.document.getUrl()]
    for arg in args:
      command_list.insert(3, "--%s" % arg)
    for k, v in kw.iteritems():
      command_list.append("--%s=%s" % (k, v))

    return command_list

  def _startTimeout(self):
    """start the Monitor"""
    self.monitor = MonitorTimeout(openoffice, self.timeout)
    self.monitor.start()
    return

  def _stopTimeout(self):
    """stop the Monitor"""
    self.monitor.terminate()
    return

  def _subprocess(self, command_list):
    """Run one procedure"""
    if monitor_sleeping_time is not None:
      monitor_sleeping_time.touch()
    try:
      self._startTimeout()
      process = Popen(command_list, stdout=PIPE, stderr=PIPE, close_fds=True,
                      env=openoffice.environment_dict.copy())
      stdout, stderr = process.communicate()
    finally:
      self._stopTimeout()
      if pid_exists(process.pid):
        process.terminate()
    return stdout, stderr

  def _callUnoConverter(self, *feature_list, **kw):
    """ """
    if not openoffice.status():
      openoffice.start()
    command_list = self._getCommand(*feature_list, **kw)
    stdout, stderr = self._subprocess(command_list)
    if not stdout and stderr:
      first_error = stderr
      logger.error(stderr)
      self.document.restoreOriginal()
      openoffice.restart()
      kw['document_url'] = self.document.getUrl()
      command = self._getCommand(*feature_list, **kw)
      stdout, stderr = self._subprocess(command)
      if not stdout and stderr:
        second_error = "\nerror of the second run: " + stderr
        logger.error(second_error)
        raise Exception(first_error + second_error)

    return stdout, stderr

  def _serializeMimemapper(self,
                           source_extension=None,
                           destination_extension=None):
    """Serialize parts of mimemapper"""
    if destination_extension is None:
      return json.dumps(dict(mimetype_by_filter_type=mimemapper._mimetype_by_filter_type))

    filter_list = []
    service_type_list = mimemapper._doc_type_list_by_extension.get(
      source_extension, mimemapper.document_service_list)
    for service_type in service_type_list:
      filter_list.append((destination_extension,
                          service_type,
                          mimemapper.getFilterName(destination_extension, service_type)))
    logger.debug("Filter List: %r" % filter_list)
    return json.dumps(dict(doc_type_list_by_extension=mimemapper._doc_type_list_by_extension,
                            filter_list=filter_list,
                            mimetype_by_filter_type=mimemapper._mimetype_by_filter_type))

  def convert(self, destination_format=None, **kw):
    """Convert a document to another format supported by the OpenOffice
    Keyword Arguments:
    destination_format -- extension of document as String
    """
    logger.debug("OooConvert: %s > %s" % (self.source_format, destination_format))
    kw['source_format'] = self.source_format
    if destination_format:
      kw['destination_format'] = destination_format
    kw['mimemapper'] = self._serializeMimemapper(self.source_format,
                                                 destination_format)
    kw['refresh'] = json.dumps(self.refresh)
    openoffice.acquire()
    try:
      stdout, stderr = self._callUnoConverter(*['convert'], **kw)
    finally:
      openoffice.release()
    url = stdout.replace('\n', '')
    self.document.reload(url)
    content = self.document.getContent(self.zip)
    self.document.trash()
    return content

  def getMetadata(self, base_document=False):
    """Returns a dictionary with all metadata of document.
    Keywords Arguments:
    base_document -- Boolean variable. if true, the document is also returned
    along with the metadata."""
    logger.debug("getMetadata")
    kw = dict(mimemapper=self._serializeMimemapper())
    if base_document:
      feature_list = ['getmetadata', 'convert']
    else:
      feature_list = ['getmetadata']
    openoffice.acquire()
    try:
      stdout, stderr = self._callUnoConverter(*feature_list, **kw)
    finally:
      openoffice.release()
    metadata = json.loads(decodestring(stdout))
    if 'document_url' in metadata:
      self.document.reload(metadata['document_url'])
      metadata['Data'] = self.document.getContent()
      del metadata['document_url']
    self.document.trash()
    return metadata

  def setMetadata(self, metadata):
    """Returns a document with new metadata.
    Keyword arguments:
    metadata -- expected an dictionary with metadata.
    """
    metadata_pickled = json.dumps(metadata)
    logger.debug("setMetadata")
    kw = dict(metadata=encodestring(metadata_pickled))
    openoffice.acquire()
    try:
      stdout, stderr = self._callUnoConverter(*['setmetadata'], **kw)
    finally:
      openoffice.release()
    doc_loaded = self.document.getContent()
    self.document.trash()
    return doc_loaded

  @staticmethod
  def getAllowedConversionFormatList(source_mimetype):
    """Returns a list content_type and their titles which are supported
    by enabled handlers.

    [('application/vnd.oasis.opendocument.text', 'ODF Text Document'),
     ('application/pdf', 'PDF - Portable Document Format'),
     ...
    ]
    """
    # XXX please never guess extension from mimetype
    output_set = set()
    if "/" in source_mimetype:
      parsed_mimetype_type = parseContentType(source_mimetype).gettype()
      # here `guess_all_extensions` never handles mimetype parameters
      #   (even for `text/plain;charset=UTF-8` which is standard)
      extension_list = mimetypes.guess_all_extensions(parsed_mimetype_type)  # XXX never guess
    else:
      extension_list = [source_mimetype]

    for ext in extension_list:
      for ext, title in mimemapper.getAllowedExtensionList(extension=ext.replace(".", "")):
        if ext in ("fodt", ".fodt"):  # BBB
          output_set.add(("application/vnd.oasis.opendocument.text-flat-xml", title))
          continue
        if ext:
          mimetype, _ = mimetypes.guess_type("a." + ext)  # XXX never guess
          if mimetype:
            output_set.add((mimetype, title))
    return list(output_set)
Esempio n. 10
0
 def setUp(self):
   """Create data to tests and instantiated a FileSystemDocument"""
   self.tmp_url = '/tmp'
   self.data = decodestring("cloudooo Test")
   self.fsdocument = FileSystemDocument(self.tmp_url, self.data, 'txt')
Esempio n. 11
0
class TestFileSystemDocument(unittest.TestCase):
  """Test to class FileSystemDocument"""

  def setUp(self):
    """Create data to tests and instantiated a FileSystemDocument"""
    self.tmp_url = '/tmp'
    self.data = decodestring("cloudooo Test")
    self.fsdocument = FileSystemDocument(self.tmp_url, self.data, 'txt')

  def tearDown(self):
    """Remove the file in file system"""
    if self.fsdocument.getUrl() is not None:
      self.fsdocument.trash()

  def testRestoreOriginal(self):
    """Test if changing the document and call remake, the document back to
    original state"""
    old_document_url = self.fsdocument.getUrl()
    document_filename = "document"
    document_test_url = path.join(self.fsdocument.directory_name,
                                  document_filename)
    open(document_test_url, 'wb').write(decodestring("Test Document"))
    self.fsdocument.reload(document_test_url)
    self.assertEquals(path.exists(old_document_url), False)
    self.assertNotEquals(self.fsdocument.original_data,
        self.fsdocument.getContent())
    old_document_url = self.fsdocument.getUrl()
    self.fsdocument.restoreOriginal()
    self.assertEquals(path.exists(old_document_url), False)
    self.assertNotEquals(old_document_url, self.fsdocument.getUrl())
    self.assertTrue(path.exists(self.fsdocument.getUrl()))
    self.assertEquals(self.fsdocument.getContent(), self.data)

  def testgetContent(self):
    """Test if returns the data correctly"""
    self.assertEquals(self.fsdocument.getContent(), self.data)

  def testgetUrl(self):
    """Check if the url is correct"""
    url = self.fsdocument.getUrl()
    self.assertTrue(path.exists(url))

  def testLoadDocumentFile(self):
    """Test if the document is created correctly"""
    url = self.fsdocument.getUrl()
    tmp_document = open(url, 'r').read()
    self.assertEquals(self.data, tmp_document)
    self.fsdocument.trash()
    self.assertEquals(path.exists(url), False)

  def testReload(self):
    """Change url and check if occurs correctly"""
    old_document_url = self.fsdocument.getUrl()
    document_filename = "document"
    document_test_url = path.join(self.fsdocument.directory_name,
                                               document_filename)
    open(document_test_url, 'wb').write(self.data)
    self.fsdocument.reload(document_test_url)
    url = self.fsdocument.getUrl()
    self.assertEquals(path.exists(old_document_url), False)
    self.assertEquals(self.fsdocument.getContent(), self.data)
    self.fsdocument.trash()
    self.assertEquals(path.exists(url), False)

  def testZipDocumentList(self):
    """Tests if the zip file is returned correctly"""
    open(path.join(self.fsdocument.directory_name, 'document2'), 'w').write('test')
    zip_file = self.fsdocument.getContent(True)
    mime = magic.Magic(mime=True)
    mimetype = mime.from_buffer(zip_file)
    self.assertEquals(mimetype, 'application/zip')
    ziptest = ZipFile(StringIO(zip_file), 'r')
    self.assertEquals(len(ziptest.filelist), 2)
    for file in ziptest.filelist:
      if file.filename.endswith("document2"):
        self.assertEquals(file.file_size, 4)
      else:
        self.assertEquals(file.file_size, 9)

  def testSendZipFile(self):
    """Tests if the htm is extrated from zipfile"""
    zip_input_url = 'data/test.zip'
    data = open(zip_input_url).read()
    zipdocument = FileSystemDocument(self.tmp_url, data, 'zip')
    mime = magic.Magic(mime=True)
    mimetype = mime.from_buffer(zipdocument.getContent(True))
    self.assertEquals(mimetype, "application/zip")
    mimetype = mime.from_buffer(zipdocument.getContent())
    self.assertEquals(mimetype, "text/html")
    zipfile = ZipFile(StringIO(zipdocument.getContent(True)))
    self.assertEquals(sorted(zipfile.namelist()),
                sorted(['logo.gif', 'test.htm']))
 def setUp(self):
   """Create data to tests and instantiated a FileSystemDocument"""
   self.tmp_url = '/tmp'
   self.data = decodestring("cloudooo Test")
   self.fsdocument = FileSystemDocument(self.tmp_url, self.data, 'txt')
class TestFileSystemDocument(unittest.TestCase):
  """Test to class FileSystemDocument"""

  def setUp(self):
    """Create data to tests and instantiated a FileSystemDocument"""
    self.tmp_url = '/tmp'
    self.data = decodestring("cloudooo Test")
    self.fsdocument = FileSystemDocument(self.tmp_url, self.data, 'txt')

  def tearDown(self):
    """Remove the file in file system"""
    if self.fsdocument.getUrl() is not None:
      self.fsdocument.trash()

  def testRestoreOriginal(self):
    """Test if changing the document and call remake, the document back to
    original state"""
    old_document_url = self.fsdocument.getUrl()
    document_filename = "document"
    document_test_url = path.join(self.fsdocument.directory_name,
                                  document_filename)
    open(document_test_url, 'wb').write(decodestring("Test Document"))
    self.fsdocument.reload(document_test_url)
    self.assertEquals(path.exists(old_document_url), False)
    self.assertNotEquals(self.fsdocument.original_data,
        self.fsdocument.getContent())
    old_document_url = self.fsdocument.getUrl()
    self.fsdocument.restoreOriginal()
    self.assertEquals(path.exists(old_document_url), False)
    self.assertNotEquals(old_document_url, self.fsdocument.getUrl())
    self.assertTrue(path.exists(self.fsdocument.getUrl()))
    self.assertEquals(self.fsdocument.getContent(), self.data)

  def testgetContent(self):
    """Test if returns the data correctly"""
    self.assertEquals(self.fsdocument.getContent(), self.data)

  def testgetUrl(self):
    """Check if the url is correct"""
    url = self.fsdocument.getUrl()
    self.assertTrue(path.exists(url))

  def testLoadDocumentFile(self):
    """Test if the document is created correctly"""
    url = self.fsdocument.getUrl()
    tmp_document = open(url, 'r').read()
    self.assertEquals(self.data, tmp_document)
    self.fsdocument.trash()
    self.assertEquals(path.exists(url), False)

  def testReload(self):
    """Change url and check if occurs correctly"""
    old_document_url = self.fsdocument.getUrl()
    document_filename = "document"
    document_test_url = path.join(self.fsdocument.directory_name,
                                               document_filename)
    open(document_test_url, 'wb').write(self.data)
    self.fsdocument.reload(document_test_url)
    url = self.fsdocument.getUrl()
    self.assertEquals(path.exists(old_document_url), False)
    self.assertEquals(self.fsdocument.getContent(), self.data)
    self.fsdocument.trash()
    self.assertEquals(path.exists(url), False)

  def testZipDocumentList(self):
    """Tests if the zip file is returned correctly"""
    open(path.join(self.fsdocument.directory_name, 'document2'), 'w').write('test')
    zip_file = self.fsdocument.getContent(True)
    mime = magic.Magic(mime=True)
    mimetype = mime.from_buffer(zip_file)
    self.assertEquals(mimetype, 'application/zip')
    ziptest = ZipFile(StringIO(zip_file), 'r')
    self.assertEquals(len(ziptest.filelist), 2)
    for file in ziptest.filelist:
      if file.filename.endswith("document2"):
        self.assertEquals(file.file_size, 4)
      else:
        self.assertEquals(file.file_size, 9)

  def testSendZipFile(self):
    """Tests if the htm is extrated from zipfile"""
    zip_input_url = 'data/test.zip'
    data = open(zip_input_url).read()
    zipdocument = FileSystemDocument(self.tmp_url, data, 'zip')
    mime = magic.Magic(mime=True)
    mimetype = mime.from_buffer(zipdocument.getContent(True))
    self.assertEquals(mimetype, "application/zip")
    mimetype = mime.from_buffer(zipdocument.getContent())
    self.assertEquals(mimetype, "text/html")
    zipfile = ZipFile(StringIO(zipdocument.getContent(True)))
    self.assertEquals(sorted(zipfile.namelist()),
                sorted(['logo.gif', 'test.htm']))