Index: /main/waeup.sirp/trunk/src/waeup/sirp/tests/test_imagestorage.py
===================================================================
--- /main/waeup.sirp/trunk/src/waeup/sirp/tests/test_imagestorage.py	(revision 6528)
+++ /main/waeup.sirp/trunk/src/waeup/sirp/tests/test_imagestorage.py	(revision 6529)
@@ -1,11 +1,14 @@
 import os
+import sys
 import tempfile
 import shutil
 import unittest
+from StringIO import StringIO
 from hurry.file.interfaces import IFileRetrieval
-from zope.component.hooks import setSite, clearSite
+from zope.component.hooks import setSite
 from zope.interface.verify import verifyClass, verifyObject
 from waeup.sirp.app import University
 from waeup.sirp.testing import FunctionalLayer, FunctionalTestCase
+from waeup.sirp.image import createWAeUPImageFile
 from waeup.sirp.imagestorage import (
     md5digest, Basket, ImageStorage, ImageStorageFileRetrieval,)
@@ -34,9 +37,15 @@
         self.workdir = tempfile.mkdtemp()
         self.samplefile = os.path.join(self.workdir, 'sample')
+        self.otherfile = os.path.join(self.workdir, 'other')
         open(self.samplefile, 'wb').write('Hi there!')
+        open(self.otherfile, 'wb').write('Hi from other!')
         self.basket = Basket()
         self.fd = open(self.samplefile, 'r')
+        self.fd2 = open(self.otherfile, 'r')
+        self.stderr = StringIO()
+        self.old_stderr = sys.stderr
 
     def tearDown(self):
+        sys.stderr = self.old_stderr
         super(BasketTests, self).tearDown()
         self.fd.close()
@@ -60,4 +69,6 @@
         basket_id = self.basket.storeFile(self.fd, 'sample')
         self.assertEqual(basket_id, '1')
+        contents = self.basket['1'].open('r').read()
+        self.assertEqual(contents, 'Hi there!')
 
     def test_storeFile_double(self):
@@ -65,4 +76,16 @@
         basket_id2 = self.basket.storeFile(self.fd, 'sample')
         self.assertTrue(basket_id1 == basket_id2 == '1')
+        contents = self.basket['1'].open('r').read()
+        self.assertEqual(contents, 'Hi there!')
+
+    def test_storeFile_multiple(self):
+        basket_id1 = self.basket.storeFile(self.fd, 'sample')
+        basket_id2 = self.basket.storeFile(self.fd2, 'sample')
+        self.assertEqual(basket_id1, '1')
+        self.assertEqual(basket_id2, '2')
+        contents1 = self.basket['1'].open('r').read()
+        contents2 = self.basket['2'].open('r').read()
+        self.assertEqual(contents1, 'Hi there!')
+        self.assertEqual(contents2, 'Hi from other!')
 
     def test_retrieveFile(self):
@@ -75,4 +98,28 @@
         result = self.basket.retrieveFile('not-a-valid-basket-id')
         self.assertTrue(result is None)
+
+    def test_detect_zero_length_blobs(self):
+        # Ensure we get a warning when an empty Blob is found
+        self.basket.storeFile(self.fd, 'sample')
+        self.basket['1'].open('w').write('')
+        self.fd.seek(0)
+        sys.stderr = self.stderr         # Redirect stderr
+        self.basket.storeFile(self.fd, 'sample')
+        sys.stderr = self.old_stderr     # Restore stderr
+        self.stderr.seek(0)
+        self.assertTrue(
+            "EMPTY BLOB DETECTED" in self.stderr.read())
+
+    def test_refill_zero_length_blobs(self):
+        # When we detect an empty Blob, it will be reused
+        self.basket.storeFile(self.fd, 'sample')
+        self.basket['1'].open('w').write('')
+        self.fd.seek(0)
+        sys.stderr = self.stderr         # Redirect stderr
+        self.basket.storeFile(self.fd, 'sample')
+        sys.stderr = self.old_stderr     # Restore stderr
+        contents = self.basket['1'].open('r').read()
+        self.assertEqual(contents, 'Hi there!')
+
 
 class ImageStorageTests(FunctionalTestCase):
@@ -110,6 +157,11 @@
         full_id1 = self.storage.storeFile(self.fd, 'sample1.txt')
         full_id2 = self.storage.storeFile(self.fd, 'sample2.txt')
+        full_id3 = self.storage.storeFile(self.fd, 'sample1.txt')
         self.assertEqual(full_id1, '396199333edbf40ad43e62a1c1397793-1')
         self.assertEqual(full_id2, '396199333edbf40ad43e62a1c1397793-1')
+        self.assertEqual(full_id3, '396199333edbf40ad43e62a1c1397793-1')
+        contents = self.storage.retrieveFile(
+            '396199333edbf40ad43e62a1c1397793-1').read()
+        self.assertEqual(contents, 'Hi there!')
 
     def test_storeFile_multiple(self):
@@ -227,4 +279,5 @@
 
     def test_createFile(self):
+        # Ensure we can create WAeUPImageFiles when in site
         setSite(self.app)
         retrieval = ImageStorageFileRetrieval()
@@ -233,2 +286,17 @@
         self.assertEqual(full_id, '396199333edbf40ad43e62a1c1397793-1')
         return
+
+    def test_waeupimagefile(self):
+        # Make sure WAeUPImageFile can use our file retrieval
+        setSite(self.app)
+        myfile = createWAeUPImageFile('sample.jpg', self.fd)
+        contents = myfile.file.read()
+        self.assertEqual(contents, 'Hi there!')
+
+    def test_waeupimagefile_raw(self):
+        # Make sure we can retrieve a file also if it was initialized
+        # with no image storage available
+        myfile = createWAeUPImageFile('sample.jpg', self.fd)
+        setSite(self.app)
+        contents = myfile.file.read()
+        self.assertEqual(contents, 'Hi there!')
